forked from yazdan/openwrt-surfshark-wireguard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_wg_config.sh
executable file
·192 lines (162 loc) · 5.5 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
#!/bin/sh
set -e
parse_arg() {
while getopts 'fhg' opt; do
case "$opt" in
f)
force_register=1
;;
g)
generate_conf=0
;;
?|h)
echo "Usage: $(basename $0) [-f]"
echo " -f force register ignore checking"
echo " -g ignore generating profile 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"
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 [ -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
wg_prv=$(wg genkey)
wg_pub=$(echo $wg_prv | wg pubkey)
echo "{\"pub\":\"$wg_pub\", \"prv\":\"$wg_prv\"}" > $wg_keys
fi
}
wg_reg_pubkey() {
url="$baseurl/v1/account/users/public-keys"
data="{\"pubKey\": $wg_pub}"
token=$(eval echo $token)
curl_res=$(eval curl -H \"Authorization: Bearer $token\" -H \"Content-Type: application/json\" -d \'$data\' -X POST $url)
}
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)
expire_date=$(echo $curl_res | jq '.expiresAt')
expire_date=$(eval echo $expire_date)
now=$(date -Iseconds --utc)
if [ "${now}" '<' "${expire_date}" ];then
register=0
printf '%b' "\n\n\tWG AUTHENTICATION KEY REFRESH\n\n RUN DATE: "${now}"\n\n"
printf '%b' " KEY EXPIRES: "${expire_date}"\n\n"
logger -t SSWG "RUN DATE:${now} KEYS EXPIRE ON: ${expire_date}"
fi
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]')
for row in $server_hosts; do
srv_host="$(echo $row | jq '.[0]')"
srv_host=$(eval echo $srv_host)
srv_pub="$(echo $row | jq '.[1]')"
srv_pub=$(eval echo $srv_pub)
echo "generating config for $srv_host"
srv_conf_file="${srv_conf_file_folder}/${srv_host%$postf}.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
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 ..."
wg_check_pubkey
fi
if [ $register -eq 1 ]; then
echo "Registring pubkey ..."
wg_reg_pubkey
else
echo "No need to register pubkey"
fi
if [ $generate_conf -eq 1 ]; then
echo "Generating profiles..."
gen_client_confs
fi
echo "Done!"