-
Notifications
You must be signed in to change notification settings - Fork 1
/
vpngate-priv
executable file
·315 lines (247 loc) · 8.23 KB
/
vpngate-priv
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
#!/bin/bash
# ARGS $1: profileFile
# ARGS $2: cachePath
# ARGS $3: nodePath
# ARGS $4: cmdPath
# ARGS $5: cmdDisplay
# ARGS $*: cmd
########
# FUNC #
########
# FUNC: System
Usage ()
{
Die "Usage: INTERNAL"
}
Die ()
{
if [[ -n "$gui" && -n "$guiDeps" ]] ; then
sudo --set-home -u \#${PKEXEC_UID:-$SUDO_UID} DISPLAY=$cmdDisplay yad \
--center --no-buttons --width=400 \
--window-icon="gnome-networktool" --image="gnome-networktool" --image-on-top \
--title="VpnGate: $profileName profile ERROR" \
--text="$*" \
2>/dev/null
else
echo "ERROR: $*" >&2
fi
exit 1
}
Say ()
{
[[ -n "$verbose" ]] && echo "$@" >&2
return 0
}
# 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
appPath="$( dirname "$( readlink -f "$BASH_SOURCE" )" )"
connector=vpnssl-connect
prefix=vpn_
flagsDir="/usr/local/share/$( basename "$appPath" )/flags"
# INIT: Check root
[[ $( id -u ) != "0" ]] && Die "Requires root privs"
# INIT: Parse opts
while getopts lvdrgp:s: opt ; do
case "$opt" in
l) list='-l' ;;
v) verbose='-v' ;;
d) disconnect='-d' ;;
r) reconnect='-r' ;;
g) gui='-g' ;;
p) [[ -n "$OPTARG" ]] && proxy="$OPTARG" && socks='' ;;
s) [[ -n "$OPTARG" ]] && socks="$OPTARG" && proxy='' ;;
[?]) Usage ;;
esac
done
shift $((OPTIND-1))
# INIT: Alternative actions
if [[ -n "$list" ]] ; then
"$appPath/$connector" -t "$2" $list
exit $?
elif [[ -n "$disconnect" && $# -ge 1 ]] ; then
"$appPath/$connector" -t "$2" $verbose $disconnect "$( basename $1 )"
ret=$?
[[ -d "$2/$1" ]] && rm -rf "$2/$1"
exit $ret
fi
# INIT: Check args: connect
[[ $# -lt 5 ]] \
&& Die "Invalid args count"
profileFile=$1
cachePath=$2
nodePath=$3
cmdPath=$4
cmdDisplay=$5
shift 5
# INIT: Profile
source "$profileFile"
profileName="$( basename "$profileFile" )"
# INIT: Defaults
nicName=$prefix$profileName
nsName=$nicName
[[ -z "$cmd" ]] && cmd=bash
# INIT: DEPS: net-tools
# DEV: local-ipv4-address does not know to use `ip route`, `netstat` required
which netstat &>/dev/null \
|| apt-get install --yes --no-install-recommends net-tools \
|| Die "Failed installing apt package: net-tools"
# INIT: DEPS: GUI
if [[ -n "$gui" ]] ; then
which yad &>/dev/null \
|| apt-get install --yes --no-install-recommends yad \
|| Die "Failed installing apt package: yad"
if [[ ! -f "$flagsDir/_unknown.png" ]] ; then
Say "Installing flags"
tmpFlags=$( mktemp -d )
curl -s --location --fail --connect-timeout 10 --retry 3 \
"https://github.com/gosquared/flags/archive/master.zip" \
>"$tmpFlags/master.zip" \
&& unzip "$tmpFlags/master.zip" 'flags-master/flags/flags-iso/flat/16/*' \
-d "$tmpFlags" &>/dev/null \
&& mkdir -p "$flagsDir" \
&& mv -T "$tmpFlags/flags-master/flags/flags-iso/flat/16/" "$flagsDir" \
|| Die "Failed installing gosquared/flags"
rm -rf "$tmpFlags"
fi
guiDeps=OK
fi
########
# MAIN #
########
if [[ -z "$reconnect" ]] && CheckNetnsExists "$profileName" ; then
# Existing
nodisconnect=1
elif [[ -n "$connect" ]] ; then
# Private connect
"$appPath/$connector" -t "$cachePath" $verbose $reconnect -p "$proxy" -s "$socks" $profileName $connect
ret=$?
[[ $ret -eq 0 || $ret -eq 42 ]] || Die "Connector failed: $connector"
[[ $ret -eq 42 ]] && nodisconnect=1
else
# VPN Gate selector
# SYN-Scan & Filter & Sort by scan time
# DEV: requires root privs
pushd "$appPath/" &>/dev/null
PATH="$nodePath:$PATH" "$nodePath/node" "$appPath/vpngate-scan.js" \
"$cachePath/vpngate_filtered.json" \
>"$cachePath/vpngate_scanned.json" \
2>/dev/null
ret=$?
popd "$appPath/" &>/dev/null
[[ "$ret" -ne 0 ]] && Die "TCP SYN-Scan failed"
chown --reference="$cachePath/vpngate_filtered.json" "$cachePath/vpngate_scanned.json"
CntServers "Scanned" "$cachePath/vpngate_scanned.json"
# Sort
[[ -z "$select" ]] && select='."SYN scan (ms)"'
jq '{data: .data | sort_by( '"$select"' ) }' \
"$cachePath/vpngate_scanned.json" \
>"$cachePath/vpngate_sorted.json" \
|| Die "Sorting failed, select query: $select"
chown --reference="$cachePath/vpngate_scanned.json" "$cachePath/vpngate_sorted.json"
CntServers "Sorted" "$cachePath/vpngate_sorted.json"
# GUI
if [[ -n "$gui" ]] ; then
# Flags support
if [[ -f "$flagsDir/_unknown.png" ]] ; then
flagsYad="--column=:IMG"
flagsJq="
$flagsDir/\\(if (.\"Country code\" | length) > 0 then .\"Country code\" else \"_unknown\" end).png"
fi
# GUI server selector
sudo --set-home -u \#${PKEXEC_UID:-$SUDO_UID} DISPLAY=$cmdDisplay yad \
--width="900" --height="600" --center --no-buttons \
--window-icon="gnome-networktool" --image="gnome-networktool" --image-on-top \
--title="VpnGate: $profileName profile ($( jq '.data | length' "$cachePath/vpngate_sorted.json" ) servers)" \
--text="Please select VPN server:" \
--list --listen \
--hide-column=1 \
--print-column=1 --separator="" \
--column="return" \
$flagsYad \
--column="CC" \
--column="Name" \
--column="Ping":NUM \
--column="Mbps":NUM \
--column="Total":NUM \
--column="Users":NUM \
--column="Total":NUM \
--column="Hours":NUM \
--column="Endpoint" \
>"$cachePath/vpngate_selected.json" 2>/dev/null \
< <( jq -r '.data[] | "{ \"data\": [ \(.) ] }'"$flagsJq"'
\(."Country code")
\(."Operator")
\(."SYN scan (ms)")
\(."Throughput (Mbps)"|floor)M
\(."Cumulative transfers (GB)"|floor)G
\(."VPN sessions")
\(."Cumulative users")
\(."Uptime (min)" / 60 | floor)
\(."IP address"):\(."SSL-VPN TCP port")"' \
"$cachePath/vpngate_sorted.json" \
)
# TOCHECK ."DDNS hostname" ."ISP hostname"
[[ $? -ne 0 || ! -s "$cachePath/vpngate_selected.json" ]] && exit 1
else
# Select first
jq '{data: [.data[0]]}' \
"$cachePath/vpngate_sorted.json" \
>"$cachePath/vpngate_selected.json"
fi
chown --reference="$cachePath/vpngate_sorted.json" "$cachePath/vpngate_selected.json"
Say "Selected target server:"
[[ -n "$verbose" ]] && jq '.data[0]' "$cachePath/vpngate_selected.json" >&2
# Extract
srvIp=$( jq -r '.data[0] | ."IP address"' "$cachePath/vpngate_selected.json" )
srvTcp=$( jq -r '.data[0] | (."SSL-VPN TCP port"//""|tostring)' "$cachePath/vpngate_selected.json" )
# Connector
"$appPath/$connector" -t "$cachePath" $verbose $reconnect -p "$proxy" -s "$socks" $profileName $srvIp $srvTcp
ret=$?
[[ $ret -eq 0 || $ret -eq 42 ]] || Die "Connector failed: $connector"
[[ $ret -eq 42 ]] && nodisconnect=1
fi
# No Command
if [[ $# -eq 0 ]] ; then
# echo "TIP: firejail --netns=$nsName firefox-esr-user-vpn" >&2 # --protocol=unix,inet,inet6,netlink
# echo "TIP: sudo ip -4 netns exec $nsName sudo --set-home -u \#$PKEXEC_UID DISPLAY=$cmdDisplay bash" >&2
exit 0
elif [[ "$1" == "null" ]] ; then
Say "Nulled command"
exit 0
fi
# Command
[[ $# -ne 0 ]] && Say "Running command: $*" || exit 0
if [[ -n "$PKEXEC_UID" || -n "$SUDO_UID" ]] ; then
pushd "$cmdPath" &>/dev/null
ip -4 netns exec $nsName sudo --set-home -u \#${PKEXEC_UID:-$SUDO_UID} DISPLAY=$cmdDisplay "$@"
ret=$?
popd &>/dev/null
else
ip -4 netns exec $nsName env DISPLAY=$cmdDisplay "$@"
ret=$?
fi
[[ $ret -ne 0 ]] && Say "Command exited with signal $ret"
if [[ -z "$nodisconnect" ]] ; then
Say "Disconnecting profile: $profileName"
"$appPath/$connector" -t "$cachePath" $verbose -d $profileName &>/dev/null
fi
exit $ret