-
Notifications
You must be signed in to change notification settings - Fork 1
/
vpngate
executable file
·410 lines (342 loc) · 11.3 KB
/
vpngate
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/bin/bash
# NAME GNOS vpngate
# DESC VPN Gate client for SoftEther VPN-SSL protocol
# AUTH [email protected]
# DEPS apt install npm jq net-tools isc-dhcp-client yad
# DEPS npm install ip local-ipv4-address raw-socket
# DEPS npm install -g phantomjs-prebuilt casperjs
########
# FUNC #
########
# FUNC: System
Usage ()
{
local self="$( basename "$( readlink -f "$BASH_SOURCE" )" )"
cat <<EOF >&2
USAGE:
$self [ OPTS ... ] [ PROFILE_NAME | PROFILE_PATH [ COMMAND [ ARGS ... ] ] ]
$self -d PROFILE_NAME
$self -l
$self -h
ARGS:
PROFILE_NAME Profile name, stored in $profilePath/
PROFILE_PATH Profile path
COMMAND Command to execute, or "null" to keep connected
ARGS Command arguments
OPTS:
-d Disconnect profile
-l List active profiles
-r Force reconnect
-u Force cache update
-p HTTP_PROXY HTTP proxy: [http://][user[:password]@]host[:port]
-s SOCKS_PROXY SOCKS4 proxy: [socks://][user[:password]@]host[:port]
-g GUI server selector
-v Verbose output
-h Show help
EOF
exit 1
}
Help ()
{
cat <<EOF >&2
NAME: $( basename "$( readlink -f "$BASH_SOURCE" )" )
DESC: VPN Gate client for SoftEther VPN-SSL protocol
AUTH: [email protected]
DEPS: SoftEther vpnclient + vpncmd
DEPS: policykit-1 isc-dhcp-client npm jq
FEAT:
polkit integration
scapping to JSON cache
jq filtering & selection
auto \$http_proxy support
HTTP & SOCKS4 proxy support
netns (Linux network namespaces)
user profiles with command execution
$( Usage 2>&1 )
PROFILE-FORMAT: BASH
cmd Command to execute
proxy HTTP proxy
socks SOCKS4 proxy
mirror [VPN Gate] Alternative scrapping target
filter [VPN Gate] Server-list filter, jq select syntax
select [VPN Gate] Server-list sorter, jq sort_by syntax
connect Private server "IP_ADDR TCP_PORT", args: $connector -h
CACHE-FORMAT: JSON: { data: [ { key: val, ... }, ... ] }
"Country code" str notnull empty!
"DDNS hostname" str null!
"IP address" str notnull notempty
"ISP hostname" str null!
"Cumulative users" int null!
"Cumulative transfers (GB)" int null!
"Operator" str null!
"Ping (ms)" int null!
"Score" int null!
"SSL-VPN TCP port" int notnull
"SYN scan (ms)" int null!
"Throughput (Mbps)" int null!
"Uptime (min)" int null!
"VPN sessions" int null!
EOF
exit 0
}
Die ()
{
echo "ERROR: $*" >&2
exit 1
}
Say ()
{
[[ -n "$verbose" ]] && echo "$@" >&2
return 0
}
Own ()
{
[[ $( id -u ) == "0" ]] && [[ -n "$PKEXEC_UID" || -n "$SUDO_UID" ]] \
&& chown -R ${PKEXEC_UID:-$SUDO_UID} "$@"
}
# FUNC
CntServers () # $1:TEXT $2:JSON
{
cnt=$( jq '.data | length' "$2" )
if [[ -n "$cnt" ]] && [[ $cnt -gt 0 ]] ; then
Say "$1 servers: $cnt"
else
Die "No $1 servers found"
fi
}
CheckNetnsExists () # $1:NAME
{
for i in $( ip netns list ) ; do
[[ "$i" == "$prefix$1" ]] && return 0
done
return 1
}
########
# INIT #
########
# INIT: Constants
prefix="vpn_"
sudoCmd="pkexec"
appPath="$( dirname "$( readlink -f "$BASH_SOURCE" )" )"
cacheSeconds=7200 # 2 hours
connector=vpnssl-connect
proxy="$http_proxy"
if [[ $( id -u ) == "0" ]] ; then
sudoCmd=""
[[ -n "$PKEXEC_UID" || -n "$SUDO_UID" ]] \
&& HOME="$( getent passwd ${PKEXEC_UID:-$SUDO_UID} | cut -f 6 -d ":" )"
fi
profilePath="$HOME/.config/vpngate"
appCachePath="$HOME/.cache/vpngate"
mirror="" # TOCHECK https://bunkerbustervpn.com/vpngate.html
# INIT: Check netns
cnetns=$( ip netns identify $$ )
[[ $cnetns =~ ^$prefix ]] && Die "Cannot run recursively, netns: $cnetns"
# INIT: Parse opts
while getopts hlivdrugp:s: opt ; do
case "$opt" in
h) Help ; exit ;;
l) list='-l' ;;
i) install='-i' ;;
v) verbose='-v' ;;
d) disconnect='-d' ;;
r) reconnect='-r' ;;
u) update='-u' ;;
g) gui='-g' ;;
p) [[ -n "$OPTARG" ]] && proxy="$OPTARG" && socks='' ;;
s) [[ -n "$OPTARG" ]] && socks="$OPTARG" && proxy='' ;;
[?]) Usage ;;
esac
done
shift $((OPTIND-1))
# INIT: Alternative actions: List
if [[ -n "$list" && -z "$disconnect" && $# -eq 0 ]] ; then
$sudoCmd "$appPath/vpngate-priv" $list _dummy_ "$appCachePath/_dummy_" "$appCachePath"
exit $?
elif [[ -z "$list" && -n "$disconnect" && $# -eq 1 ]] ; then
:
elif [[ -n "$list$disconnect" ]] ; then
Usage
fi
# INIT: Sample Profiles
if [[ ! -d "$profilePath" ]] ; then
mkdir -p "$profilePath"
cat <<'EOF' >"$profilePath/default"
EOF
cat <<'EOF' >"$profilePath/fast"
# DESC: High Throughput
filter='( ."VPN sessions" > 0)'
select='-."Throughput (Mbps)" / ."VPN sessions"'
EOF
cat <<'EOF' >"$profilePath/443"
# DESC: High Throughput
filter='."SSL-VPN TCP port" == 443'
EOF
cat <<'EOF' >"$profilePath/other"
# DESC: TCP port 443 & Exclude current country & US
country="$( curl -s ifconfig.io/country_code )|US" # BUG: returns T1 for tor addresses
not="|not"
filter='( ."SSL-VPN TCP port" == 443 ) and ( ."Country code" | test ("'"$country"'")'"$not"' )'
EOF
cat <<'EOF' >"$profilePath/free"
# DESC: Country code include list
country="CN|HK|IR|KZ|RS|RU|VN" # Non-NATO nations
# country="BR|ID|IN|MA|TR|TH" # Sovereign states
# not="|not"
filter='( ."Country code" | test ("'"$country"'")'"$not"' ) and ( ."VPN sessions" > 0)'
select='-."Throughput (Mbps)" / ."VPN sessions"'
EOF
Own "$profilePath/"
fi
# INIT: Profile
profile="$1"
shift
[[ -z "$profile" ]] && profile=default
if [[ -f "$profile" ]] && bash -n "$profile" ; then
profileFile="$( readlink -f "$profile" )"
elif [[ -f "$profilePath/$profile" ]] && bash -n "$profilePath/$profile" ; then
profileFile="$( readlink -f "$profilePath/$profile" )"
else
Die "Invalid profile: $profile"
fi
profileName="$( basename "$profileFile" )"
[[ "$( echo -n "$profileName" | tr -c '_[:alnum:]' '-' )" == "$profileName" ]] \
|| Die "Profile name has invalid characters: $profileName"
source "$profileFile"
# INIT: Profile defaults
[[ -z "$cmd" ]] && cmd='bash'
[[ -z "$filter" ]] && filter='true'
cachePath="${appCachePath}/$profileName"
# INIT: Alternative action: Disconnect
if [[ -z "$list" && -n "$disconnect" && $# -eq 0 ]] ; then
$sudoCmd "$appPath/vpngate-priv" $verbose $disconnect $profileName "$appCachePath/$profileName"
ret=$?
exit $ret
fi
# INIT: Check DEPS
## DEPS: self
for i in $connector vpngate-priv vpngate-scrap.js vpngate-scan.js ; do
if [[ ! -f "$appPath/$i" ]] ; then
Die "Missing internal: $i"
fi
done
## DEPS: policykit-1
if [[ $( id -u ) != "0" ]] ; then
pkexec --version &>/dev/null \
|| Die "Missing dependency: policykit-1"
fi
## DEPS: jq
jq --version &>/dev/null \
|| Die "Missing dependency: jq"
## DEPS: node
if [[ -z "$NVM_DIR" && -r "$HOME/.nvm/nvm.sh" ]] ; then
source "$HOME/.nvm/nvm.sh"
fi
if [[ -d "$NVM_BIN" && -x "$NVM_BIN/node" ]] ; then
nodePath="$NVM_BIN"
# BUG /proc/*/environ IS NOT UPDATED
# elif [[ $( id -u ) == "0" ]] && [[ -n "$PKEXEC_UID" || -n "$SUDO_UID" ]] ; then
# # TIP use strings instead of \0 parsing
# while IFS='' read -r -d $'\0' line || [[ -n "$line" ]]; do
# [[ "${line%=*}" == "NVM_BIN" ]] && nodePath="${line#*=}"
# done <"/proc/$( printf '%s' $( ps -o ppid= $$ ) )/environ"
else
nodePath="$( dirname "$( which node )" )"
fi
[[ -n "$nodePath" ]] || Die "Missing dependency: node"
## DEPS: node modules
for i in raw-socket ip local-ipv4-address ; do
if [[ $( id -u ) != "0" && ! -d "$appPath/node_modules/$i" ]] ; then
which npm &>/dev/null || Die "Missing npm for installing package: $i"
pushd "$appPath/" &>/dev/null
npm install $i
ret=$?
popd &>/dev/null
[[ $ret -eq 0 ]] || Die "Failed installing npm package: $i"
elif [[ ! -d "$appPath/node_modules/$i" ]] ; then
Die "Will not install npm package as root: $i"
fi
done
## DEPS: node globals
for i in phantomjs-prebuilt:phantomjs casperjs ; do
if [[ $i =~ : ]] ; then
j=${i%%:*}
i=${i##*:}
else
j=$i
fi
if [[ $( id -u ) != "0" ]] && ! PATH="$nodePath:$PATH" which $i &>/dev/null ; then
which npm &>/dev/null || Die "Missing npm for installing GLOBAL package: $i"
npm install -g $j || Die "Failed installing npm GLOBAL package: $i"
elif ! PATH="$nodePath:$PATH" which $i &>/dev/null ; then
Die "Will not install npm GLOBAL package as root: $i"
fi
done
# INIT: Alternative action: Install
if [[ -n "$install" ]] ; then
Say "Installation succedeed"
exit 0
fi
# INIT: Check Profile
## Check filter syntax
notnull='{ "Country code": "", "IP address": ".", "SSL-VPN TCP port": 1 }'
jq "$filter" <<<"$notnull" &>/dev/null || Die "Invalid filter: $filter"
## Check select syntax
if [[ -n "$select" ]] ; then
jq "sort_by($select)" <<<'[]' &>/dev/null || Die "Invalid select: $select"
fi
## Merge cmd
if [[ $# -eq 0 && -n "$cmd" && "$cmd" != "null" ]] ; then
eval exec "$( readlink -f "$BASH_SOURCE" )" $gui $verbose $disconnect $reconnect $update "$profileFile" $cmd
fi
## Check cmd
if [[ $# -ne 0 && "$1" != "null" ]] ; then
which "$1" &>/dev/null || Die "Unknown command: $1"
fi
########
# MAIN #
########
if [[ -z "$reconnect" ]] && CheckNetnsExists "$profileName" ; then
if [[ $# -eq 0 ]] ; then
Say "Existing netns, try -r to force reconnect"
exit 0
fi
elif [[ -z "$connect" ]] ; then
# Scrap & Cache
if [[ -f "$appCachePath/cache.json" ]] \
&& [[ -s "$appCachePath/cache.json" ]] \
&& [[ -z "$update" ]] \
&& [[ ! -w "$appCachePath/cache.json" \
|| $(( $( date -d now "+%s" ) - $( stat --format=%Y "$appCachePath/cache.json" ) )) -lt $cacheSeconds \
]] ; then
[[ ! -w "$appCachePath/cache.json" ]] \
&& Say "Frozen READ-ONLY cache: $appCachePath/cache.json" \
|| Say "Using cache"
CntServers "Cached" "$appCachePath/cache.json"
else
mkdir -p "$appCachePath/"
[[ -w "$appCachePath/" ]] \
&& [[ -w "$appCachePath/cache.json" || ! -e "$appCachePath/cache.json" ]] \
|| Die "Cannot update cache file: $appCachePath/cache.json"
# Scrap + Filter null tcp port
Say "Scrapping VPN Gate, please wait ..."
pushd "$appPath/" &>/dev/null
PATH="$nodePath:$PATH" "$appPath/vpngate-scrap.js" "$mirror" \
>"$appCachePath/cache.json"
ret=$?
popd &>/dev/null
[[ "$ret" -ne 0 ]] && Die "Scrapping failed" # DEV: returns 0 on crash
Own "$appCachePath/cache.json"
CntServers "Scrapped" "$appCachePath/cache.json"
fi
# Filter user rules
mkdir -p "$cachePath/"
jq '{data: [ .data[] | select( '"$filter"' ) ] }' \
"$appCachePath/cache.json" \
>"$cachePath/vpngate_filtered.json" \
|| Die "Filtering failed, select query: $filter"
Own "$cachePath/vpngate_filtered.json"
CntServers "Filtered" "$cachePath/vpngate_filtered.json"
fi
# Privileged
$sudoCmd "$appPath/vpngate-priv" $gui $verbose $reconnect -p "$proxy" -s "$socks" "$profileFile" "$cachePath" "$nodePath" "$(pwd)" "$DISPLAY" "$@"