-
Notifications
You must be signed in to change notification settings - Fork 20
/
client.sh
executable file
·293 lines (236 loc) · 7.18 KB
/
client.sh
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/bin/sh
set -euo pipefail
log="logger -t wireguard"
dir="$(cd -- "$(dirname "$0")" &> /dev/null && pwd)"
config_file="$(ls -v "${dir}"/*conf | head -1)"
iface="$(basename "$config_file" .conf)"
wan="$(ip route | grep 'default via' | head -1 | awk '{print $5}')"
wan_mtu="$(cat /sys/class/net/${wan}/mtu)"
mtu=$(( wan_mtu > 0 ? wan_mtu - 80 : 1500 - 80 ))
fwmark=51820
routes_table=$fwmark
filtered_config=""
filtered_config_file="/tmp/wireguard.${iface}.filtered.conf"
client_addr=""
client_mask=""
server_addr=""
server_port=""
allowed_ips=""
traffic_rules_def_pref=5000
traffic_rules_suppressor_pref=32751
die() {
$log "${1}. Exit."
exit 1
}
wait_online() {
local i=0
until ping -c 1 -W 1 $1 &> /dev/null; do
i=$(( i < 300 ? i + 1 : i ))
$log "No ping response from $1, waiting $i sec..."
sleep $i
done
}
validate_iface_name() {
[ -z "$(echo "$1" | sed -E 's/^[a-zA-Z0-9_=+.-]{1,15}//')" ] || return 1
}
# Dumb filter for IPv4 or FQDN addresses (has dot),
# since we don't support IPv6 yet
get_valid_addrs() {
addrs="$(echo "$1" | sed 's/,/ /g')"
for addr in $addrs; do
if echo "$addr" | grep -q '\.'; then
echo -n "$addr "
fi
done
}
# Collapse spaces, trim leading and trailing spaces
trim_spaces() {
echo "$1" | sed -E 's/ +/ /g;s/^ //;s/ $//'
}
trim_eof_newlines() {
sed -i -e :a -e '/^\n*$/{$d;N;};/\n$/ba' "$1"
}
add_to_filtered_config() {
filtered_config="${filtered_config}${1}"$'\n'
}
parse_config() {
$log "Parsing config"
dos2unix -u "$1"
local line key val addr cidr err
while read -r line || [ -n "$line" ]; do
[ -z "$line" ] ||
[ "${line:0:1}" = "#" ] && continue
line="$(echo "$line" | sed 's/ //g')"
key="$(echo "$line" | cut -d '=' -f 1)"
val="$(echo "$line" | cut -d '=' -f 2-)"
case "$key" in
Address)
[ -n "$client_addr" ] && continue
cidr="$(get_valid_addrs "$val" | cut -d ' ' -f 1)"
client_addr="$(echo "$cidr" | cut -d '/' -f 1)"
client_mask="$(echo "$cidr" | cut -d '/' -f 2)"
[ "$client_addr" = "$client_mask" ] && client_mask="32"
;;
Endpoint)
[ -n "$server_addr" ] && continue
addr="$(get_valid_addrs "$val" | cut -d ' ' -f 1)"
server_addr="$(echo "$addr" | cut -d ':' -f 1)"
server_port="$(echo "$addr" | cut -d ':' -f 2)"
add_to_filtered_config "${key}=${server_addr}:${server_port}"
;;
AllowedIPs)
allowed_ips="$allowed_ips $(get_valid_addrs "$val")"
;;
[*|PrivateKey|PublicKey|PresharedKey|PersistentKeepalive)
add_to_filtered_config "$line"
;;
*)
$log "Ignoring config entry: $key"
continue
;;
esac
done < "$1"
allowed_ips="$(trim_spaces "$allowed_ips")"
add_to_filtered_config "AllowedIPs=$(echo "$allowed_ips" | sed 's/ /,/g')"
err=""
#[ -z "$client_addr" ] && err="No valid client address in config file"
[ -z "$server_addr" ] && err="No valid server address in config file"
[ -z "$allowed_ips" ] && err="No valid allowed IPs in config file"
[ -n "$err" ] && die "$err"
if [ "$1" = "$config_file" ]; then
echo "$filtered_config" > "$filtered_config_file"
fi
}
configure_traffic_rules() {
local client_cidr action def_route
if [ -z "$client_addr" ]; then
parse_config "$filtered_config_file"
client_cidr="$(ip -o -4 addr list $iface | awk '{print $4}')"
client_addr="$(echo $client_cidr | cut -d '/' -f 1)"
client_mask="$(echo $client_cidr | cut -d '/' -f 2)"
fi
def_route=0
echo "$allowed_ips" | grep -q '/0' && def_route=1
case "$1" in
enable)
ip link show $iface &> /dev/null || { $log "Nonexistent interface $iface"; return 1; }
configure_traffic_rules disable
action="-I"
$log "Setting up traffic rules..."
ip route add default dev $iface table $routes_table
if [ $def_route = 1 ]; then
wg set $iface fwmark $fwmark
ip rule add not fwmark $fwmark table $routes_table
ip rule add table main suppress_prefixlength 0 pref $traffic_rules_suppressor_pref
sysctl -q net.ipv4.conf.all.src_valid_mark=1
else
for i in $allowed_ips; do
ip rule add to $i table $routes_table pref $traffic_rules_def_pref
done
fi
;;
disable)
action="-D"
$log "Removing traffic rules... "
ip route del default table $routes_table || :
ip rule del table $routes_table || :
ip rule del pref $traffic_rules_suppressor_pref || :
;;
*)
$log "Wrong argument: 'enable' or 'disable' expected. Doing nothing." >&2
return
;;
esac
if [ $def_route = 1 ]; then
#iptables $action PREROUTING ! -i $iface -d $client_addr -m addrtype ! --src-type LOCAL -j DROP || :
iptables -t mangle $action POSTROUTING -m mark --mark $fwmark -p udp -j CONNMARK --save-mark || :
iptables -t mangle $action PREROUTING -p udp -j CONNMARK --restore-mark || :
fi
iptables $action INPUT -i $iface -j ACCEPT || :
iptables -t nat $action POSTROUTING -o $iface -j SNAT --to $client_addr || :
iptables -t mangle $action FORWARD ! -o br0 -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu || :
}
start() {
$log "Starting"
ip link show dev "$iface" &> /dev/null && die "'$iface' already exists"
validate_iface_name "$iface" || die "Invalid interface name"
parse_config "$config_file"
$log "Setting up interface"
modprobe wireguard
ip link add $iface type wireguard
ip addr add $client_addr/$client_mask dev $iface
wg setconf $iface "$filtered_config_file"
ip link set $iface up mtu $mtu
configure_traffic_rules enable
}
stop() {
$log "Stopping"
configure_traffic_rules disable
ip link del $iface
rm "$filtered_config_file"
}
autostart() {
local script_path mark_start mark_end
script_path="${dir}/$(basename "$0")"
mark_start="### $script_path autostart: begin"
mark_end="### $script_path autostart: end"
autostart_target="/etc/storage/post_wan_script.sh"
traffic_rules_target="/etc/storage/post_iptables_script.sh"
case "$1" in
enable)
$log "Enabling autostart"
trim_eof_newlines "$autostart_target"
printf "%s\n" \
"" \
"$mark_start" \
"case \"\$1\" in" \
" up) \"$script_path\" start ;;" \
" down) \"$script_path\" stop ;;" \
"esac" \
"$mark_end" \
>> "$autostart_target"
trim_eof_newlines "$traffic_rules_target"
printf "%s\n" \
"" \
"$mark_start" \
"\"$script_path\" traffic-rules enable" \
"$mark_end" \
>> "$traffic_rules_target"
;;
disable)
$log "Disabling autostart"
sed -i "\|^$mark_start|,\|^$mark_end|d" "$autostart_target"
sed -i "\|^$mark_start|,\|^$mark_end|d" "$traffic_rules_target"
trim_eof_newlines "$autostart_target"
trim_eof_newlines "$traffic_rules_target"
;;
*)
$log "Wrong argument: 'enable' or 'disable' expected. Doing nothing." >&2
return
;;
esac
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
traffic-rules)
configure_traffic_rules "$2"
;;
autostart)
autostart "$2"
;;
*)
echo "Usage: $0 {start|stop}" >&2
exit 1
;;
esac
$log "Done"
exit 0