-
Notifications
You must be signed in to change notification settings - Fork 16
/
gen_wg_config.sh
executable file
·363 lines (309 loc) · 10.3 KB
/
gen_wg_config.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
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
#!/bin/sh
set -e
parse_arg() {
while getopts 'fhgn:k:ld:z:' opt; do
case "$opt" in
f)
force_register=1
;;
g)
generate_conf=0
;;
l)
list_registered=1
;;
d)
delete_registered="$OPTARG"
;;
n)
key_name="$OPTARG"
;;
k)
wg_prv="$OPTARG"
;;
z)
save_zip=1
zip_file="$OPTARG"
;;
?|h)
echo "Usage: $(basename $0) [-f]"
echo " -f force register ignore checking"
echo " -g ignore generating profile files"
echo " -n <name> create a manual named key"
echo " -k <key> use provided private key"
echo " -l list registered manual keys"
echo " -d <key-id> delete registered manual key"
echo " -z [zip-file] zip archive in which to save the config files"
exit 1
;;
esac
done
shift "$(($OPTIND -1))"
}
read_config() {
config_file="config.json"
conf_json=$(cat $config_file)
config_folder="$(echo $conf_json | jq '.config_folder')"
config_folder=$(eval echo $config_folder)
username="$(echo $conf_json | jq '.username')"
username=$(eval echo $username)
password="$(echo $conf_json | jq '.password')"
password=$(eval echo $password)
token_file="${config_folder}/token.json"
baseurl="https://api.surfshark.com"
list_registered=0
force_register=0
register=1
generate_conf=1
selected_servers_file="${config_folder}/selected_servers.json"
servers_file="${config_folder}/surf_servers.json"
wg_keys="${config_folder}/wg.json"
srv_conf_file_folder="${config_folder}/conf"
unset conf_json
}
login () {
if [ -f "$token_file" ]; then
curl_res=$(cat $token_file)
else
tmpfile=$(mktemp /tmp/wg-curl-res.XXXXXX)
url="$baseurl/v1/auth/login"
data="{\"username\":\"$username\", \"password\":\"$password\"}"
http_status=$(curl -o $tmpfile -s -w "%{http_code}" -d "$data" -H 'Content-Type: application/json' -X POST $url)
if [ $http_status -eq 200 ]; then
cp $tmpfile $token_file
fi
rm $tmpfile
fi
token=$(echo $curl_res | jq '.token')
renewToken=$(echo $curl_res | jq '.renewToken')
}
get_servers() {
if [ -f "$servers_file" ]; then
echo "servers list already exist"
else
tmpfile=$(mktemp /tmp/wg-curl-res.XXXXXX)
url="$baseurl/v4/server/clusters/generic?countryCode="
http_status=$(curl -o $tmpfile -s -w "%{http_code}" -H "Authorization: Bearer $token" -H 'Content-Type: application/json' $url)
if [ $http_status -eq 200 ]; then
cat $tmpfile > $servers_file
fi
rm $tmpfile
fi
}
select_servers () {
cat_res=$(cat $servers_file | jq 'select(any(.[].tags[]; . == "p2p" or . == "physical"))')
echo $cat_res > $selected_servers_file
}
wg_gen_keys() {
if [ -z "$wg_prv" ] && [ -f "$wg_keys" ]; then
echo "wg keys already exist"
wg_pub=$(cat $wg_keys | jq '.pub')
wg_prv=$(cat $wg_keys | jq '.prv')
wg_prv=$(eval echo $wg_prv)
else
if [ -z "$wg_prv" ]; then
wg_prv=$(wg genkey)
fi
wg_pub=$(echo $wg_prv | wg pubkey)
if [ ! -f "$wg_keys" ]; then
echo "{\"pub\":\"$wg_pub\", \"prv\":\"$wg_prv\"}" > $wg_keys
fi
fi
}
wg_reg_pubkey() {
echo "registering pubkey..."
url="$baseurl/v1/account/users/public-keys"
if [ -n "$key_name" ]; then
data='{"pubKey": "'$wg_pub'", "name": "'$key_name'", "manual": true}'
else
data='{"pubKey": "'$wg_pub'"}'
fi
token=$(eval echo $token)
curl_res=$(eval curl -s -H \"Authorization: Bearer "$token"\" \
-H \"Content-Type: application/json\" -d \'$data\' -X POST $url)
wg_key_data_check "$curl_res"
}
wg_key_data_check() {
keyinfo="$1"
is_refresh="$2"
now=$(date -Iseconds --utc)
expire_date=$(echo "$keyinfo" | jq -r '.expiresAt')
name=$(echo "$keyinfo" | jq -r '.name')
error_code=$(echo "$keyinfo" | jq -r '.code')
if [ "$error_code" != "null" ]; then
echo "Error $error_code: $(echo "$keyinfo" | jq -r '.message')"
exit 1
fi
if [ -n "$key_name" ] && [ "$key_name" != "$name" ]; then
echo "Provided name '$key_name' does not match key '$name'"
exit 1
fi
if [ "${now}" '<' "${expire_date}" ]; then
register=0
if [ -n "$is_refresh" ]; then
printf '%b' "\n\tWG AUTHENTICATION KEY REFRESH\n\n RUN DATE: ${now}\n\n"
fi
printf '%b' "$name KEY EXPIRES: ${expire_date}\n\n"
logger -t SSWG "RUN DATE:${now} KEYS EXPIRE ON: ${expire_date}"
return 0
fi
return 1
}
wg_get_registered() {
tmpfile=$(mktemp /tmp/wg-curl-res.XXXX)
url="$baseurl/v1/account/users/public-keys"
token=$(eval echo "$token")
http_status=$(eval curl -o "$tmpfile" -s -w "%{http_code}" \
-H \"Authorization: Bearer "$token"\" \
-H \"Content-Type: application/json\" -X GET "$url")
if [ "$http_status" -eq 200 ]; then
cat "$tmpfile"
elif [ "$http_status" -eq 401 ]; then
rm -f "$token_file"
echo "Unauthorized. Please run again"
rm "$tmpfile"
exit 1
fi
}
wg_list_registered() {
registered="$(wg_get_registered)"
n_keys=$(echo "$registered" | jq '. | length')
if [ "$n_keys" -gt 0 ]; then
echo " Name ¦ Public Key ¦ Expiration date ¦ Creation date ¦ ID"
echo "---------------------------------------------------------"
fi
for i in $(seq 0 "$((n_keys-1))"); do
keyinfo="$(echo "$registered" | jq '.['"$i"']')"
name=$(echo "$keyinfo" | jq -r '.name')
pubkey=$(echo "$keyinfo" | jq -r '.pubKey')
id=$(echo "$keyinfo" | jq -r '.id')
expiration=$(echo "$keyinfo" | jq -r '.expiresAt')
creation=$(echo "$keyinfo" | jq -r '.createdAt')
echo "$name ¦ $pubkey ¦ $expiration ¦ $creation ¦ $id"
done
}
wg_delete_registered() {
url="$baseurl/v1/account/users/public-keys/$delete_registered"
token=$(eval echo "$token")
http_status=$(eval curl -o /dev/null -s -w "%{http_code}" \
-H \"Authorization: Bearer "$token"\" \
-H \"Content-Type: application/json\" -X DELETE "$url")
if [ "$http_status" = 200 ]; then
return 0
elif [ "$http_status" = 404 ]; then
echo "Deletion failed: key '$delete_registered' not found"
else
echo "Deletion failed: $http_status"
fi
exit 1
}
wg_check_registered_pubkeys() {
registered="$(wg_get_registered)"
n_keys=$(echo "$registered" | jq '. | length')
for i in $(seq 0 "$((n_keys-1))"); do
keyinfo="$(echo "$registered" | jq '.['"$i"']')"
pubkey=$(echo "$keyinfo" | jq '.pubKey')
if [ "$pubkey" = "$wg_pub" ]; then
name=$(echo "$keyinfo" | jq -r '.name')
echo "pubkey for key '$name' found"
if wg_key_data_check "$keyinfo"; then
return 0
fi
break
fi
done
return 1
}
wg_check_pubkey() {
tmpfile=$(mktemp /tmp/wg-curl-res.XXXXXX)
url="$baseurl/v1/account/users/public-keys/validate"
data="{\"pubKey\": $wg_pub}"
token=$(eval echo $token)
http_status=$(eval curl -o $tmpfile -s -w "%{http_code}" -H \"Authorization: Bearer $token\" -H \"Content-Type: application/json\" -d \'$data\' -X POST $url)
if [ $http_status -eq 200 ]; then
curl_res=$(cat $tmpfile)
wg_key_data_check "$curl_res" "true"
elif [ $http_status -eq 401 ]; then
rm -f $token_file
echo "Unauthorized. Please run again"
rm $tmpfile
exit 1
fi
rm $tmpfile
}
gen_client_confs() {
postf=".surfshark.com"
mkdir -p $srv_conf_file_folder
server_hosts=$(echo "${cat_res}" | jq -c '.[] | [.connectionName, .pubKey, .tags]')
for row in $server_hosts; do
srv_host="$(echo $row | jq -r '.[0]')"
srv_pub="$(echo $row | jq -r '.[1]')"
srv_tags="$(echo $row | jq -r '.[2]')"
basename=ss-${srv_host%$postf}
basename=${basename%.prod}
taginfo=
if [ "$(echo "$srv_tags" | jq -r 'index("p2p")')" != 'null' ]; then
basename="${basename}-p2p"
taginfo="p2p"
fi
if [ "$(echo "$srv_tags" | jq -r 'index("virtual")')" != 'null' ]; then
basename="${basename}-v"
if [ -z "$taginfo" ]; then
taginfo="virtual"
else
taginfo="${taginfo}, virtual"
fi
fi
if [ -n "$taginfo" ]; then
echo "generating config for $srv_host ($taginfo)"
else
echo "generating config for $srv_host"
fi
srv_conf_file="${srv_conf_file_folder}/${basename}.conf"
srv_conf="[Interface]\nPrivateKey=$wg_prv\nAddress=10.14.0.2/8\nMTU=1350\n\n[Peer]\nPublicKey=o07k/2dsaQkLLSR0dCI/FUd3FLik/F/HBBcOGUkNQGo=\nAllowedIPs=172.16.0.36/32\nEndpoint=wgs.prod.surfshark.com:51820\nPersistentKeepalive=25\n\n[Peer]\nPublicKey=$srv_pub\nAllowedIPs=0.0.0.0/0\nEndpoint=$srv_host:51820\nPersistentKeepalive=25\n"
uci_conf=""
if [ "`echo -e`" = "-e" ]; then
echo "$srv_conf" > $srv_conf_file
else
echo -e "$srv_conf" > $srv_conf_file
fi
done
}
read_config
parse_arg "$@"
echo "Loggin in if needed ..."
login
if [ $list_registered -eq 1 ]; then
wg_list_registered
exit
fi
if [ -n "$delete_registered" ]; then
wg_delete_registered
exit
fi
echo "Getting the list of servers ..."
get_servers
echo "Selecting servers ..."
select_servers
echo "Generating keys ..."
wg_gen_keys
if [ $force_register -eq 0 ]; then
echo "Checking pubkey ..."
if ! wg_check_registered_pubkeys; then
wg_check_pubkey
fi
fi
if [ $register -eq 1 ]; then
echo "Registring pubkey $key_name..."
wg_reg_pubkey
else
echo "No need to register pubkey"
fi
if [ $generate_conf -eq 1 ]; then
echo "Generating profiles..."
gen_client_confs
if [ "$save_zip" ]; then
zip -j -r "${zip_file:-${config_folder}/config.zip}" "$srv_conf_file_folder"
fi
fi
echo "Done!"