summaryrefslogtreecommitdiffstats
path: root/net/xkcptun/files/xkcptun.init
blob: 16b9e868c9e952ffd35186f8379457a8db806c7b (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/sh /etc/rc.common
#
# Copyright (C) 2026 Dengfeng Liu <liudf0716@gmail.com>
#
# This is free software, licensed under the GNU General Public License v3.
# See /LICENSE for more information.
#

USE_PROCD=1
START=99

confdir=/var/etc/xkcptun
bindir=/usr/bin

append_toml_global() {
	local cfg="$1"
	local cfgtype="$2"
	local outfile="$3"

	eval "$(validate_global_options "$cfg" validate_mklocal)"
	validate_global_options "$cfg" || return 0

	[ -z "$remote_addr" ] || echo "remote_addr = \"$remote_addr\"" >> "$outfile"
	[ -z "$remote_port" ] || echo "remote_port = $remote_port" >> "$outfile"
	[ -z "$key" ] || echo "key = \"$key\"" >> "$outfile"
	[ -z "$local_interface" ] || echo "local_interface = \"$local_interface\"" >> "$outfile"
	[ -z "$local_port" ] || echo "local_port = $local_port" >> "$outfile"
	[ -z "$mode" ] || echo "mode = \"$mode\"" >> "$outfile"
	[ -z "$mtu" ] || echo "mtu = $mtu" >> "$outfile"
	[ -z "$sndwnd" ] || echo "sndwnd = $sndwnd" >> "$outfile"
	[ -z "$rcvwnd" ] || echo "rcvwnd = $rcvwnd" >> "$outfile"
	[ -z "$datashard" ] || echo "data_shard = $datashard" >> "$outfile"
	[ -z "$parityshard" ] || echo "parity_shard = $parityshard" >> "$outfile"
	[ -z "$dscp" ] || echo "dscp = $dscp" >> "$outfile"
	[ -z "$nodelay" ] || echo "nodelay = $nodelay" >> "$outfile"
	[ -z "$interval" ] || echo "interval = $interval" >> "$outfile"
	[ -z "$resend" ] || echo "resend = $resend" >> "$outfile"
	[ -z "$nc" ] || echo "nc = $nc" >> "$outfile"
	[ -z "$lossctrl" ] || echo "loss_ctrl = $lossctrl" >> "$outfile"
	[ -z "$pacing" ] || echo "pacing = $pacing" >> "$outfile"
	[ -z "$fec" ] || echo "fec = $fec" >> "$outfile"
	[ -z "$sockbuf" ] || echo "sock_buf = $sockbuf" >> "$outfile"
	[ -z "$keepalive" ] || echo "keepalive = $keepalive" >> "$outfile"
	[ -z "$conntimeout" ] || echo "conn_timeout = $conntimeout" >> "$outfile"

	return 0
}

append_toml_tunnel() {
	local cfg="$1"
	local cfgtype="$2"
	local outfile="$3"

	eval "$("validate_${cfgtype}_section" "$cfg" validate_mklocal)"
	"validate_${cfgtype}_section" "$cfg" || return 1
	[ "${enabled:-1}" -eq 1 ] || return 1
	[ -n "$local_port" ] || return 1

	cat << EOF >> "$outfile"

[[tunnel]]
name = "${name:-$cfg}"
local_interface = "${local_interface:-br-lan}"
local_port = $local_port
EOF
	[ -z "$proto" ] || echo "proto = \"$proto\"" >> "$outfile"
	[ -z "$proxy_type" ] || echo "proxy_type = \"$proxy_type\"" >> "$outfile"
	[ -z "$target_port" ] || echo "target_port = $target_port" >> "$outfile"
	[ -z "$target_addr" ] || echo "target_addr = \"$target_addr\"" >> "$outfile"
	[ -z "$remote_addr" ] || echo "remote_addr = \"$remote_addr\"" >> "$outfile"
	[ -z "$remote_port" ] || echo "remote_port = $remote_port" >> "$outfile"
	[ -z "$key" ] || echo "key = \"$key\"" >> "$outfile"
	[ -z "$mode" ] || echo "mode = \"$mode\"" >> "$outfile"
	[ -z "$mtu" ] || echo "mtu = $mtu" >> "$outfile"
	[ -z "$sndwnd" ] || echo "sndwnd = $sndwnd" >> "$outfile"
	[ -z "$rcvwnd" ] || echo "rcvwnd = $rcvwnd" >> "$outfile"
	[ -z "$datashard" ] || echo "data_shard = $datashard" >> "$outfile"
	[ -z "$parityshard" ] || echo "parity_shard = $parityshard" >> "$outfile"
	[ -z "$dscp" ] || echo "dscp = $dscp" >> "$outfile"
	[ -z "$nodelay" ] || echo "nodelay = $nodelay" >> "$outfile"
	[ -z "$interval" ] || echo "interval = $interval" >> "$outfile"
	[ -z "$resend" ] || echo "resend = $resend" >> "$outfile"
	[ -z "$nc" ] || echo "nc = $nc" >> "$outfile"
	[ -z "$lossctrl" ] || echo "loss_ctrl = $lossctrl" >> "$outfile"
	[ -z "$pacing" ] || echo "pacing = $pacing" >> "$outfile"
	[ -z "$fec" ] || echo "fec = $fec" >> "$outfile"
	[ -z "$sockbuf" ] || echo "sock_buf = $sockbuf" >> "$outfile"
	[ -z "$keepalive" ] || echo "keepalive = $keepalive" >> "$outfile"
	[ -z "$conntimeout" ] || echo "conn_timeout = $conntimeout" >> "$outfile"

	return 0
}

start_daemon() {
	local cfgtype="$1"
	local bin="$bindir/xkcp_$cfgtype"
	[ -x "$bin" ] || bin="$bindir/xkcptun-$cfgtype"
	[ -x "$bin" ] || return

	local conftoml="$confdir/$cfgtype.toml"
	rm -f "$conftoml"

	local mon_port=9086
	[ "$cfgtype" = "server" ] && mon_port=9087

	cat << EOF > "$conftoml"
[global]
syslog = true
mon_port = $mon_port
EOF

	# Append global parameters if configured
	append_global_match() {
		local cfg="$1"
		local match="$2"
		[ "$cfg" = "$match" ] && append_toml_global "$cfg" "$match" "$conftoml"
	}
	config_foreach append_global_match global "$cfgtype"

	local count=0
	append_section() {
		local cfg="$1"
		[ "$cfg" = "global" ] && return
		if append_toml_tunnel "$cfg" "$cfgtype" "$conftoml"; then
			count=$((count + 1))
		fi
	}

	config_foreach append_section "$cfgtype"

	if [ "$cfgtype" = "client" ]; then
		local cli_enabled
		config_get_bool cli_enabled client enabled 0
		if [ "$cli_enabled" -ne 1 ] || [ "$count" -eq 0 ]; then
			rm -f "$conftoml"
			return
		fi
	fi

	if [ "$cfgtype" = "server" ]; then
		local srv_enabled
		config_get_bool srv_enabled server enabled 0
		if [ "$srv_enabled" -ne 1 ]; then
			rm -f "$conftoml"
			return
		fi
	fi

	procd_open_instance "$cfgtype"
	procd_set_param command "$bin" -f --syslog -c "$conftoml"
	procd_set_param file "$conftoml"
	procd_set_param respawn
	procd_close_instance
}

start_service() {
	mkdir -p "$confdir"
	# generated TOML files contain the pre-shared key: keep them private
	chmod 0700 "$confdir"
	umask 077
	config_load xkcptun
	start_daemon server
	start_daemon client
}

stop_service() {
	rm -rf "$confdir"
}

service_triggers() {
	procd_add_reload_interface_trigger wan
	procd_add_reload_interface_trigger lan
	procd_add_reload_trigger xkcptun
	procd_open_validate
	validate_global_section
	validate_server_section
	validate_client_section
	procd_close_validate
}

validate_mklocal() {
	local tuple opts

	shift 2
	for tuple in "$@"; do
		opts="${tuple%%:*} $opts"
	done
	[ -z "$opts" ] || echo "local $opts"
}

validate() {
	uci_validate_section xkcptun "$@"
}

validate_common_options() {
	local cfgtype="$1"; shift
	local cfg="$1"; shift
	local func="$1"; shift
	local enabled_def="${1:-1}"; shift
	local mode_profiles='"fast3", "fast2", "fast", "normal", "manual"'

	"${func:-validate}" "$cfgtype" "$cfg" "$@" \
		"enabled:bool:$enabled_def" \
		'name:string' \
		"mode:or($mode_profiles)" \
		'mtu:uinteger' \
		'sndwnd:uinteger' \
		'rcvwnd:uinteger' \
		'datashard:uinteger' \
		'parityshard:uinteger' \
		'dscp:uinteger' \
		'nodelay:uinteger' \
		'interval:uinteger' \
		'resend:uinteger' \
		'nc:uinteger' \
		'lossctrl:uinteger' \
		'pacing:uinteger' \
		'fec:uinteger' \
		'sockbuf:uinteger' \
		'keepalive:uinteger' \
		'conntimeout:uinteger' \
		'key:string' \
		'syslog:bool:1' \
		'user:string'
}

validate_global_options() {
	validate_common_options global "$1" "$2" 0 \
		'remote_addr:host' \
		'remote_port:port' \
		'local_interface:string' \
		'local_port:port'
}

validate_server_options() {
	validate_common_options server "$1" "$2" 1 \
		'local_interface:string:eth0' \
		'local_port:port:9089' \
		'target_addr:host' \
		'target_port:port'
}

validate_client_options() {
	validate_common_options client "$1" "$2" 1 \
		'local_interface:string:br-lan' \
		'local_port:port:9088' \
		'proto:or("tcp", "udp")' \
		'proxy_type:or("forward", "redir", "socks5", "transparent")' \
		'target_addr:host' \
		'target_port:port' \
		'remote_addr:host' \
		'remote_port:port'
}

validate_global_section() {
	validate_global_options "$1" "$2"
}

validate_server_section() {
	validate_server_options "$1" "$2"
}

validate_client_section() {
	validate_client_options "$1" "$2"
}