-
Notifications
You must be signed in to change notification settings - Fork 2
/
remote-170-wireguard.sh
73 lines (66 loc) · 1.89 KB
/
remote-170-wireguard.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
wireguard_reload() {
wireguard_handle_interface() {
local config="$1"
local proto
config_get proto "$config" proto
if [ "$proto" == 'wireguard' ]
then
echo "wireguard_reload $config"
ifup "$config"
fi
}
config_load network
config_foreach wireguard_handle_interface interface
}
wireguard() {
local wireguard_installed
if oc_opkg_installed wireguard-tools
then
wireguard_installed=1
fi
oc_opkg_install wireguard-tools
oc_uci_merge "$config_wireguard"
oc_uci_commit network && wireguard_reload
if [ -z "$wireguard_installed" ]
then
echo 'workaround: kill netifd'
killall netifd
fi
if [ -e /usr/bin/wireguard_watchdog ]
then
oc_add_cron wireguard '*/7 * * * * /usr/bin/wireguard_watchdog'
else
cat >/tmp/wireguard_cron <<'EOF'
#!/bin/sh
. /lib/functions.sh
handle_interface() {
local config="$1"
local proto
config_get proto "$config" proto
if [ "$proto" == 'wireguard' ]
then
config_foreach handle_peer "wireguard_$config" "$config"
fi
}
handle_peer() {
local config="$1"
local iface="$2"
local public_key endpoint_host endpoint_port
config_get public_key "$config" public_key
config_get endpoint_host "$config" endpoint_host
config_get endpoint_port "$config" endpoint_port
if [ -n "$endpoint_host" ] && [ -n "$endpoint_port" ] && echo "$endpoint_host" | grep -qvE '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'
then
wg set "$iface" peer "$public_key" endpoint "$endpoint_host:$endpoint_port"
fi
}
config_load network
config_foreach handle_interface interface
EOF
mkdir -p ~/bin/
oc_move /tmp/wireguard_cron ~/bin/wireguard_cron
chmod 0755 ~/bin/wireguard_cron
oc_add_cron wireguard '*/7 * * * * ~/bin/wireguard_cron'
fi
}
wireguard