forked from jnsgruk/wireguard-over-wss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wstunnel.sh
executable file
·179 lines (150 loc) · 4.95 KB
/
wstunnel.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
#!/bin/bash
#
# Original script downloaded from: https://github.com/Kirill888/notes/blob/wg-tunnel-update/wireguard/scripts/wstunnel.sh
# Modified by jnsgruk to use `ip route` for modern Linux distros
#
DEFAULT_HOSTS_FILE='/etc/hosts'
read_host_entry () {
local host=$1
local hfile=${2:-${DEFAULT_HOSTS_FILE}}
awk -v host="$host" '{
if( !($0~"^[ ]*#") && $2==host )
print $1, ($0~"## wstunnel end-point")?"auto":"manual"
}' "${hfile}"
}
add_host_entry () {
local host=$1
local ip=$2
local hfile=${3:-${DEFAULT_HOSTS_FILE}}
echo -e "${ip}\t${host}\t## wstunnel end-point" >> "${hfile}"
}
update_host_entry () {
local host=$1
local ip=$2
local hfile=${3:-${DEFAULT_HOSTS_FILE}}
local edited
edited=$(awk -v host="$host" -v ip="$ip" '{
if( !($0~"^[ ]*#") && $2==host && ($0~"## wstunnel end-point") )
print ip "\t" host "\t" "## wstunnel end-point"
else
print $0
}' "${hfile}")
echo "${edited}" > "${hfile}"
}
delete_host_entry () {
local host=$1
local hfile=${2:-${DEFAULT_HOSTS_FILE}}
local edited
edited=$(awk -v host="$host" '{
if( !($0~"^[ ]*#") && $2==host && ($0~"## wstunnel end-point") )
;
else
print $0
}' "${hfile}")
echo "${edited}" > "${hfile}"
}
maybe_update_host () {
local host="$1"
local current_ip="$2"
local hfile=${3:-${DEFAULT_HOSTS_FILE}}
local recorded_ip h_mode
read -r recorded_ip h_mode < <(read_host_entry "${host}" "${hfile}") || true
if [[ -z "${recorded_ip}" ]]; then
echo "[#] Add new entry ${host} => <${current_ip}>"
add_host_entry "${host}" "${current_ip}" "${hfile}"
else
if [[ "${recorded_ip}" == "${current_ip}" ]]; then
echo "[#] Recorded address is already correct"
else
if [[ "${h_mode}" == "auto" ]]; then
echo "[#] Updating ${recorded_ip} -> ${current_ip}"
update_host_entry "${host}" "${current_ip}" "${hfile}"
else
echo "[#] Manual entry doesn't match current ip: ${recorded_ip} -> ${current_ip}"
exit 2
fi
fi
fi
}
launch_wstunnel () {
local host=${REMOTE_HOST}
local rport=${REMOTE_PORT:-51820}
local wssport=${WSS_PORT:-443}
local lport=${LOCAL_PORT:-${rport}}
local prefix=${WS_PREFIX:-"wstunnel"}
local user=${1:-"nobody"}
local timeout=${TIMEOUT:-"-1"}
local cmd
cmd=$(command -v wstunnel)
cmd="sudo -n -u ${user} -- $cmd"
$cmd >/dev/null 2>&1 </dev/null \
--quiet \
--udpTimeoutSec "${timeout}" \
--upgradePathPrefix "${prefix}" \
--udp -L "127.0.0.1:${lport}:127.0.0.1:${rport}" \
"wss://${host}:${wssport}" & disown
echo "$!"
}
pre_up () {
local wg=$1
local cfg="/etc/wireguard/${wg}.wstunnel"
local remote remote_ip gw wstunnel_pid hosts_file _dnsmasq
if [[ -f "${cfg}" ]]; then
# shellcheck disable=SC1090
source "${cfg}"
remote=${REMOTE_HOST}
hosts_file=${UPDATE_HOSTS}
_dnsmasq=${USING_DNSMASQ:-0}
else
echo "[#] Missing config file: ${cfg}"
exit 1
fi
remote_ip=$(dig +short "${remote}")
if [[ -z "${remote_ip}" ]]; then
echo "[#] Can't resolve ${remote}"
exit 1
fi
if [[ -f "${hosts_file}" ]]; then
# Cache DNS in
maybe_update_host "${remote}" "${remote_ip}" "${hosts_file}"
[[ $_dnsmasq -eq 0 ]] || killall -HUP dnsmasq || true
fi
# Find out current route to ${remote_ip} and make it explicit
gw=$(ip route get "${remote_ip}" | cut -d" " -f3)
ip route add "${remote_ip}" via "${gw}" > /dev/null 2>&1 || true
# Start wstunnel in the background
wstunnel_pid=$(launch_wstunnel nobody)
# save state
mkdir -p /var/run/wireguard
echo "${wstunnel_pid} ${remote} ${remote_ip} \"${hosts_file}\" ${_dnsmasq}" > "/var/run/wireguard/${wg}.wstunnel"
}
post_up () {
local tun=$1
ip route add 0.0.0.0/1 dev "${tun}" > /dev/null 2>&1
ip route add ::0/1 dev "${tun}" > /dev/null 2>&1
ip route add 128.0.0.0/1 dev "${tun}" > /dev/null 2>&1
ip route add 8000::/1 dev "${tun}" > /dev/null 2>&1
}
post_down () {
local tun=$1
local state_file="/var/run/wireguard/${tun}.wstunnel"
local wstunnel_pid remote remote_ip hosts_file _dnsmasq
if [[ -f "${state_file}" ]]; then
read -r wstunnel_pid remote remote_ip hosts_file _dnsmasq < "${state_file}"
# unquote
hosts_file=${hosts_file%\"}
hosts_file=${hosts_file#\"}
rm "${state_file}"
else
echo "[#] Missing state file: ${state_file}"
exit 1
fi
kill -TERM "${wstunnel_pid}" > /dev/null 2>&1 || true
if [[ -n "${remote_ip}" ]]; then
ip route delete "${remote_ip}" > /dev/null 2>&1 || true
fi
if [[ -f "${hosts_file}" ]]; then
delete_host_entry "${remote}" "${hosts_file}"
[[ $_dnsmasq -eq 0 ]] || killall -HUP dnsmasq || true
fi
}