summaryrefslogtreecommitdiffstats
path: root/net/xfrpc/files/xfrpc.init
blob: c71ce2a33df6b48b5d3fec8813f087f9452fa7f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/sh /etc/rc.common
# Copyright (C) 2022 Dengfeng Liu <liudf0716@gmail.com>
#
# This is free software, licensed under the GNU General Public License v3.
# See /LICENSE for more information.
#

START=99
USE_PROCD=1

NAME=xfrpc
PROG=/usr/bin/$NAME


handle_xfrpc() {
    local section="$1"
    local config="$2"

    case "$section" in
        common)
            uci_validate_section xfrpc xfrpc common \
                'server_addr:host' \
                'server_port:uinteger' \
                'token:string:'
            ;;
    esac

    # Write the validated settings to the config file
    echo "[${section}]" >> "$config"
    [ -z "$server_addr" ] || echo "server_addr = $server_addr" >> "$config"
    [ -z "$server_port" ] || echo "server_port = $server_port" >> "$config"
    [ -z "$token" ] || echo "token = $token" >> "$config"
}

handle_tcp() {
	local section="$1"
	local config="$2"

	uci_validate_section xfrpc tcp $section \
		'enabled:bool:1' \
		'service_type:string' \
		'local_ip:host' \
		'local_port:uinteger' \
		'remote_port:uinteger' \
		'start_time:string' \
		'end_time:string'
	
	# if enabled is 0, then return
	[ $enabled = 0 ] && return

	# Validate service_type if provided
	if [ -n "$service_type" ]; then
		case "$service_type" in
			ssh|mstsc|rdp|vnc|telnet)
				# Valid service type
				;;
			*)
				echo "Error: Invalid service_type '$service_type'. Must be one of: ssh, mstsc, rdp, vnc, telnet"
				return 1
				;;
		esac
	fi

	# Write the validated settings to the config file
	echo "[${section}]" >> "$config"
	echo "type = tcp" >> "$config"
	[ -z "$service_type" ] || echo "service_type = $service_type" >> "$config"
	[ -z "$local_ip" ] || echo "local_ip = $local_ip" >> "$config"
	[ -z "$local_port" ] || echo "local_port = $local_port" >> "$config"
	[ -z "$remote_port" ] || echo "remote_port = $remote_port" >> "$config"
	[ -z "$start_time" ] || echo "start_time = $start_time" >> "$config"
	[ -z "$end_time" ] || echo "end_time = $end_time" >> "$config"
}

handle_http() {
	local section="$1"
	local config="$2"

	uci_validate_section xfrpc http $section \
		'enabled:bool:1' \
		'local_ip:host' \
		'local_port:uinteger' \
		'custom_domains:string' \
		'subdomain:string' \
		'start_time:string' \
		'end_time:string'

	# if enabled is 0, then return
	[ $enabled = 0 ] && return

	# Write the validated settings to the config file
	echo "[${section}]" >> "$config"
	echo "type = http" >> "$config"
	echo "service_type = http" >> "$config"
	[ -z "$local_ip" ] || echo "local_ip = $local_ip" >> "$config"
	[ -z "$local_port" ] || echo "local_port = $local_port" >> "$config"
	[ -z "$custom_domains" ] || echo "custom_domains = $custom_domains" >> "$config"
	[ -z "$subdomain" ] || echo "subdomain = $subdomain" >> "$config"
	[ -z "$start_time" ] || echo "start_time = $start_time" >> "$config"
	[ -z "$end_time" ] || echo "end_time = $end_time" >> "$config"
}

handle_https() {
	local section="$1"
	local config="$2"

	uci_validate_section xfrpc https $section \
		'enabled:bool:1' \
		'local_ip:host' \
		'local_port:uinteger' \
		'custom_domains:string' \
		'subdomain:string' \
		'start_time:string' \
		'end_time:string'
	
	# if enabled is 0, then return
	[ $enabled = 0 ] && return

	# Write the validated settings to the config file
	echo "[${section}]" >> "$config"
	echo "type = https" >> "$config"
	echo "service_type = https" >> "$config"
	[ -z "$local_ip" ] || echo "local_ip = $local_ip" >> "$config"
	[ -z "$local_port" ] || echo "local_port = $local_port" >> "$config"
	[ -z "$custom_domains" ] || echo "custom_domains = $custom_domains" >> "$config"
	[ -z "$subdomain" ] || echo "subdomain = $subdomain" >> "$config"
	[ -z "$start_time" ] || echo "start_time = $start_time" >> "$config"
	[ -z "$end_time" ] || echo "end_time = $end_time" >> "$config"
}

handle_socks5() {
	local section="$1"
	local config="$2"

	uci_validate_section xfrpc socks5 $section \
		'enabled:bool:1' \
		'remote_port:uinteger' \
		'start_time:string' \
		'end_time:string'

	# if enabled is 0, then return
	[ $enabled = 0 ] && return

	# Write the validated settings to the config file
	echo "[${section}]" >> "$config"
	echo "type = socks5" >> "$config"
	[ -z "$remote_port" ] || echo "remote_port = $remote_port" >> "$config"
	[ -z "$start_time" ] || echo "start_time = $start_time" >> "$config"
	[ -z "$end_time" ] || echo "end_time = $end_time" >> "$config"
}

handle_iod() {
	local section="$1"
	local config="$2"

	uci_validate_section xfrpc iod $section \
		'enabled:bool:1' \
		'local_port:uinteger' \
		'remote_port:uinteger' 

	# if enabled is 0, then return
	[ $enabled = 0 ] && return

	# Write the validated settings to the config file
	echo "[${section}]" >> "$config"
	echo "type = iod" >> "$config"
	[ -z "$local_port" ] || echo "local_port = $local_port" >> "$config"
	[ -z "$remote_port" ] || echo "remote_port = $remote_port" >> "$config"
}

service_triggers() {
	procd_add_reload_trigger "$NAME"
	procd_add_interface_trigger "interface.*.up" wan /etc/init.d/xfrpc restart
}

start_service() {
	local conf_file="/var/etc/$NAME.ini"

	> "$conf_file"
	config_load "$NAME"

	uci_validate_section xfrpc xfrpc common \
			'enabled:bool:0' \
			'loglevel:uinteger:0'

	if [ $enabled = 0 ]; then
		echo "xfrpc service disabled"
		return
	fi

	config_foreach handle_xfrpc xfrpc "$conf_file"
	config_foreach handle_tcp tcp "$conf_file"
	config_foreach handle_http http "$conf_file"
	config_foreach handle_https https "$conf_file"
	config_foreach handle_socks5 socks5 "$conf_file"
	config_foreach handle_iod iod "$conf_file"

	procd_open_instance
	procd_set_param command "$PROG" -c "$conf_file" -s -f -d $loglevel
	procd_set_param file "$conf_file"
	procd_set_param respawn
	procd_close_instance
}

reload_service() {
	stop
	start
}