From 5fff6612bb265aa52055b17cf2fc7758c30de77a Mon Sep 17 00:00:00 2001 From: yarafie <88841013+yarafie@users.noreply.github.com> Date: Wed, 27 Apr 2022 11:22:06 +0300 Subject: [PATCH 1/4] Update README.bash Minor corrections --- README.bash | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.bash b/README.bash index f957274..14dcc26 100644 --- a/README.bash +++ b/README.bash @@ -35,7 +35,7 @@ Use the script at your own risk :). We are not responsable if anything goes wrong ## First time running script -You may need to run script a few timed with no parameters if you have never run +You may need to run script a few times with no parameters if you have never run script before. This is to make sure all needed files are created and your keys are registered. If you keep getting registeration errors, wait a bit (no idea how long) and try @@ -44,7 +44,7 @@ later it eventually will work. ## Your private/public key expires The token will last around 7 days so it needs to be regenerated before then. I run the script with no parameters every 6 days. -It's also recommendef that a cron job is set to run every day during a known +It's also recommended that a cron job is set to run every day during a known slack period with -n flag to keep your keys refreshed ## The server list changes every so often and the load changes fairly often. @@ -56,7 +56,7 @@ You can do this by runnimg with -r option to regenerate server list. If you are not able to use the generated config files, there might be a chance that there is an unhandled corner case in one of the functions. Check that wg.json and token.json files have been generated. Review the output, this should show where the script failed. Output is logged to -sswg.log in the same folder as the script. +sswg.log in the same folder as the config_folder. # TODO - fold in any updates done by kyndair :). From 9b4ae61dfe2f9aa84d2e72baf555ec96b6a89b57 Mon Sep 17 00:00:00 2001 From: yarafie <88841013+yarafie@users.noreply.github.com> Date: Wed, 27 Apr 2022 11:27:07 +0300 Subject: [PATCH 2/4] Update gen_wg_config.bash Minor fix --- gen_wg_config.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gen_wg_config.bash b/gen_wg_config.bash index 4ed9acb..72dd754 100644 --- a/gen_wg_config.bash +++ b/gen_wg_config.bash @@ -187,7 +187,7 @@ wg_check_pubkey() { # validates the public key registration process and confirms logger -t SSWG "[wg_check_pubkey] RUN DATE:${now} TOKEN EXPIRES ON: ${expire_date}" # Log Status Information (logread -e SSWG) fi rm -f $token_expires - echo "${tmpfile}" >> $token_expires + echo $(cat $tmpfile | jq '.') >> $token_expires echo "[wg_check_pubkey] token requires renewing prior to "$(eval echo $(jq '.expiresAt' $token_expires)) fi rm $tmpfile From f5e3aa949933aa20a0a8c5f73f634a853b3926e6 Mon Sep 17 00:00:00 2001 From: yarafie <88841013+yarafie@users.noreply.github.com> Date: Wed, 27 Apr 2022 11:31:31 +0300 Subject: [PATCH 3/4] Update README.bash Tidy up --- README.bash | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.bash b/README.bash index 14dcc26..d1ab288 100644 --- a/README.bash +++ b/README.bash @@ -1,5 +1,4 @@ # Intro - This is a simple script that generates wireguard client config files for surfshark @@ -18,7 +17,6 @@ be generated. 3. run `gen_wg_config.bash` ## usage - ```bash shell Usage: gen_wg_config.bash [-h] -g ignore generating profile files From d3e1bfdae0361bcdd0c4349e2c14dab3e0a72525 Mon Sep 17 00:00:00 2001 From: yarafie <88841013+yarafie@users.noreply.github.com> Date: Thu, 28 Apr 2022 21:58:25 +0300 Subject: [PATCH 4/4] Add files via upload Reworked script: 1- made curls quit 2- changed logic to handle failed api calls 3- storing successfull apiurl in token.json to be used in future runs 4- added generation of config files for obfuscated and double servers 5- cleaned up print outs much cleaner. --- gen_wg_config.bash | 261 +++++++++++++++++++++++++-------------------- 1 file changed, 148 insertions(+), 113 deletions(-) diff --git a/gen_wg_config.bash b/gen_wg_config.bash index 72dd754..3a533d6 100644 --- a/gen_wg_config.bash +++ b/gen_wg_config.bash @@ -1,5 +1,5 @@ #!/bin/bash -set -e +#set -e # #get location of script # @@ -17,12 +17,10 @@ read_config() { sswg_log=${config_folder}/sswg.log baseurl_1="https://api.surfshark.com" - baseurl_2="https://ux.surfshark.com" - baseurl_3="https://api.uymgg1.com" - baseurl_4="https://ux.uymgg1.com" - baseurl_5="https://api.surf-chiny.com" - baseurl_6="https://ux.surf-chiny.com" - urlcount=6 + baseurl_2="https://api.uymgg1.com" + baseurl_3="https://api.surf-chiny.com" + baseurl_4="https://api.shark-china.com" + urlcount=4 generic_servers_file=${config_folder}/generic_servers.json static_servers_file=${config_folder}/static_servers.json @@ -31,7 +29,6 @@ read_config() { generate_conf=1 reset_all=0 - check_status=0 generate_servers=0 renew_token=0 } @@ -55,7 +52,8 @@ parse_arg() { shift "$(($OPTIND -1))" } -wg_login() { #login and recieve jwt token and renewal token +wg_login() { #login and receive jwt token and renewal token + echo "[wg_login]S ========================================" username=$(eval echo $(jq '.username' ${config_file})) password=$(eval echo $(jq '.password' ${config_file})) tmpfile=$(mktemp /tmp/wg-curl-res.XXXXXX) @@ -64,151 +62,167 @@ wg_login() { #login and recieve jwt token and renewal token until [ $http_status -eq 200 ]; do let basen=$basen+1; baseurl=baseurl_$basen if [ $basen -gt $urlcount ]; then - echo "[wg_login] Unable to login, check your credentials." + echo "[wg_login] Unable to login, check your credentials." + echo "[wg_login]E ========================================" rm $tmpfile exit 100 fi url=$(eval echo \${$baseurl})/v1/auth/login data='{"username":"'${username}'","password":"'${password}'"}' - http_status=$(curl -o $tmpfile -w "%{http_code}" -d "$data" -H 'Content-Type: application/json' -X POST $url) + http_status=$(curl -fsS -o $tmpfile -w "%{http_code}" -d "$data" -H 'Content-Type: application/json' -X POST $url) echo "[$(date -Iseconds)] [wg_login] Login "$url $http_status $(cat $tmpfile) >> $sswg_log done rm -f $token_file - echo $(cat $tmpfile | jq '.') >> $token_file + token="$(eval echo $(jq '.token' $tmpfile))" + rtoken="$(eval echo $(jq '.renewToken' $tmpfile))" + echo -e "{\n\t\"apiurl\":\"$(eval echo \${$baseurl})\",\n\t\"token\":\"$token\",\n\t\"renewToken\":\"$rtoken\"\n}" >> $token_file rm $tmpfile + echo "[wg_login] Used \"$(eval echo \${$baseurl})\" for api calls" + echo "[wg_login]E ========================================" } wg_gen_keys() { # generate priavte/public key pair - echo "[wg_gen_keys] generating new keys" + echo "[wg_gen_keys]S ========================================" + echo "[wg_gen_keys] Generating new keys" wg_prv=$(wg genkey) wg_pub=$(echo $wg_prv | wg pubkey) rm -f $wg_keys echo -e "{\n\t\"pub\":\"$wg_pub\",\n\t\"prv\":\"$wg_prv\"\n}" >> $wg_keys + echo "[wg_gen_keys]E ========================================" } wg_register_pub() { # check to see if the public key has been registered and/or there is an unexpired token & run appropriate modules + echo "[wg_register_pub]S ========================================" if [ ! -f ${token_expires} ] && [ -f ${wg_keys} ]; then + echo "[wg_register_pub] token_expires not found but wg_keys available" + echo "[wg_register_pub] will reg then check pubkey" wg_reg_pubkey - wg_check_pubkey - elif [ $(eval echo $(jq '.pubKey' $token_expires)) == $(eval echo $(jq '.pub' $wg_keys)) ] && [ $(eval echo $(jq '.expiresAt' $token_expires)) '<' $(eval echo $(date -Iseconds -u)) ]; then +# wg_check_pubkey + elif [ "$(eval echo $(jq '.pubKey' $token_expires))" = "$(eval echo $(jq '.pub' $wg_keys))" ] && [ "$(eval echo $(jq '.expiresAt' $token_expires))" < "$(eval echo $(date -Iseconds -u))" ]; then + echo "[wg_register_pub] token_expires and wg_key pubkey match " + echo "[wg_register_pub] but token has expired " + echo "[wg_register_pub] will renew keys and check " wg_token_renwal - wg_check_pubkey - elif [ $(eval echo $(jq '.pubKey' $token_expires)) == $(eval echo $(jq '.pub' $wg_keys)) ]; then +# wg_check_pubkey + elif [ "$(eval echo $(jq '.pubKey' $token_expires))" = "$(eval echo $(jq '.pub' $wg_keys))" ]; then + echo "[wg_register_pub] token_expires and wg_key pubkey match " + echo "[wg_register_pub] will check pubkey " wg_check_pubkey else + echo "[wg_register_pub] keys expired will gen new keys reg and check " rm -f ${token_file} ${wg_keys} wg_login wg_gen_keys wg_reg_pubkey wg_check_pubkey fi -} - -wg_user_status() { # get current status of user - url=$baseurl_1/v1/server/user - token="Authorization: Bearer $(eval echo $(jq '.token' $token_file))" - user_status=$(curl -H "${token}" -H "Content-Type: application/json" ${url} | jq '.') - echo "[$(date -Iseconds)] [wg_user_status] User Status "$url $user_status >> $sswg_log - if [ $(echo $user_status | jq '.secured') ]; then - echo "[wg_user_status] surfshark wireguard is currently on and your IP info is "$(echo $user_status | jq '.ip, .city, .country') - else - echo "[wg_user_status] surfshark wireguard is currently off and your IP info is "$(echo $user_status | jq '.ip, city, .country') - fi + echo "[wg_register_pub]E ========================================" } wg_reg_pubkey() { # register the public key using the jwt token - basen=1 + echo "[wg_reg_pubkey]S ========================================" error_count=0 key_reg=start until [ -z "${key_reg##*expiresAt*}" ]; do - baseurl=baseurl_$basen - url=$(eval echo \${$baseurl})/v1/account/users/public-keys + url="$(eval echo $(jq '.apiurl' $token_file))/v1/account/users/public-keys" data='{"pubKey":'$(jq '.pub' $wg_keys)'}' token="Authorization: Bearer $(eval echo $(jq '.token' $token_file))" - key_reg=$(curl -H "${token}" -H "Content-Type: application/json" -d "${data}" -X POST ${url} | jq '.') - echo "[$(date -Iseconds)] [wg_reg_pubkey] Registration "$url $key_reg >> $sswg_log - let basen=$basen+2 - if [ -n "${key_reg##*expiresAt*}" ] && [ $basen -gt $urlcount ]; then + echo "[wg_reg_pubkey] Using \"$(eval echo $(jq '.apiurl' $token_file))\" for api calls" + key_reg=$(curl -fsS -H "${token}" -H "Content-Type: application/json" -d "${data}" -X POST ${url} | jq '.') + echo "[$(date -Iseconds)] [wg_reg_pubkey] Registration "$url $key_reg >> $sswg_log + + if [ -n "${key_reg##*expiresAt*}" ] && [ $error_count -ne '0' ]; then if [ -z "${key_reg##*400*}" ]; then if [ -z "${key_reg##*Bad Request*}" ]; then - echo "[wg_reg_pubkey] Curl post appears to be malformed" + echo "[wg_reg_pubkey] Curl post appears to be malformed" + echo "[wg_reg_pubkey]E ========================================" exit 110 fi elif [ -z "${key_reg##*401*}" ]; then if [ -z "${key_reg##*Expired*}" ] && [ $error_count -eq 0 ]; then wg_token_renwal error_count=1 - basen=1 elif [ -z "${key_reg##*Expired*}" ] && [ $error_count -eq 1 ]; then - echo "[wg_reg_pubkey] Token is expiring immediately." + echo "[wg_reg_pubkey] Token is expiring immediately." + echo "[wg_reg_pubkey]E ========================================" exit 111 elif [ -z "${key_reg##*Token not found*}" ]; then - echo "[wg_reg_pubkey] Token was not recognised as a token." - echo "[wg_reg_pubkey] If it fails repeatedly check your credentials and that a token exists." + echo "[wg_reg_pubkey] Token was not recognised as a token." + echo "[wg_reg_pubkey] If it fails repeatedly check your credentials and that a token exists." + echo "[wg_reg_pubkey]E ========================================" exit 112 fi else - echo "[wg_reg_pubkey] Unknown error" + echo "[wg_reg_pubkey] Unknown error" + echo "[wg_reg_pubkey]E ========================================" exit 113 fi fi done rm -f $token_expires - echo "${key_reg}" >> $token_expires - echo "[wg_reg_pubkey] token requires renewing prior to "$(eval echo $(jq '.expiresAt' $token_expires)) + echo "${key_reg}" | jq '.' >> $token_expires + echo "[wg_reg_pubkey] token requires renewing prior to "$(eval echo $(jq '.expiresAt' $token_expires)) + echo "[wg_reg_pubkey]E ========================================" } wg_check_pubkey() { # validates the public key registration process and confirms token expiry + echo "[wg_check_pubkey]S ========================================" tmpfile=$(mktemp /tmp/wg-curl-val.XXXXXX) http_status=0 - basen=1 + error_count=0 until [ $http_status -eq 200 ]; do - baseurl=baseurl_$basen - if [ $basen -gt $urlcount ]; then - echo "[wg_check_pubkey] Public Key was not validated & authorised, please try again." - echo "[wg_check_pubkey] If it fails repeatedly check your credentials and that key registration has completed." + let error_count=$error_count+1 + if [ $error_count -gt 5 ]; then + echo "[wg_check_pubkey] Public Key was not validated & authorised, after $error_count tried." + echo "[wg_check_pubkey] If it fails repeatedly check your credentials and that key registration has completed." echo $(cat $tmpfile) rm $tmpfile + echo "[wg_check_pubkey]E ========================================" exit 120 fi - url=$(eval echo \${$baseurl})/v1/account/users/public-keys/validate + url="$(eval echo $(jq '.apiurl' $token_file))/v1/account/users/public-keys/validate" data='{"pubKey":'$(jq '.pub' $wg_keys)'}' token="Authorization: Bearer $(eval echo $(jq '.token' $token_file))" - http_status=$(curl -o $tmpfile -w "%{http_code}" -H "${token}" -H "Content-Type: application/json" -d "${data}" -X POST ${url}) + echo "[wg_check_pubkey] Using \"$(eval echo $(jq '.apiurl' $token_file))\" for api calls" + http_status=$(curl -fsS -o $tmpfile -w "%{http_code}" -H "${token}" -H "Content-Type: application/json" -d "${data}" -X POST ${url}) echo "[$(date -Iseconds)] [wg_check_pubkey] Validation "$url $http_status $(cat $tmpfile) >> $sswg_log - let basen=$basen+2 + let error_count=$error_count+1 done - if [ $(eval echo $(jq '.expiresAt' $tmpfile)) != $(eval echo $(jq '.expiresAt' $token_expires)) ]; then + if [ "$(eval echo $(jq '.expiresAt' $tmpfile))" != "$(eval echo $(jq '.expiresAt' $token_expires))" ]; then + echo "[wg_check_pubkey] New token expiry date found updating" expire_date=$(eval echo $(jq '.expiresAt' $tmpfile)) now=$(date -Iseconds -u) if [ "${now}" '<' "${expire_date}" ]; then - echo "Current Date & Time "${now} # Display Run Date - echo "Token will Expire at "${expire_date} # Display Token Expiry + echo "[wg_check_pubkey] Current Date & Time "${now} # Display Run Date + echo "[wg_check_pubkey] Token will Expire at "${expire_date} # Display Token Expiry logger -t SSWG "[wg_check_pubkey] RUN DATE:${now} TOKEN EXPIRES ON: ${expire_date}" # Log Status Information (logread -e SSWG) fi rm -f $token_expires echo $(cat $tmpfile | jq '.') >> $token_expires - echo "[wg_check_pubkey] token requires renewing prior to "$(eval echo $(jq '.expiresAt' $token_expires)) + echo "[wg_check_pubkey] token requires renewing prior to "$(eval echo $(jq '.expiresAt' $token_expires)) fi rm $tmpfile + echo "[wg_check_pubkey]E ========================================" } wg_token_renwal() { # use renewal token to generate new tokens + echo "[wg_token_renwal]S ========================================" basen=1 error_count=0 key_ren=start until [ -z "${key_ren##*renewToken*}" ]; do - baseurl=baseurl_$basen - url=$(eval echo \${$baseurl})/v1/auth/renew + url="$(eval echo $(jq '.apiurl' $token_file))/v1/auth/renew" data='{"pubKey":'$(jq '.pub' $wg_keys)'}' token="Authorization: Bearer $(eval echo $(jq '.renewToken' $token_file))" - key_ren=$(curl -H "${token}" -H "Content-Type: application/json" -d "${data}" -X POST ${url} | jq '.') + echo "[wg_token_tenwal] Using \"$(eval echo $(jq '.apiurl' $token_file))\" for api calls" + key_ren=$(curl -fsS -H "${token}" -H "Content-Type: application/json" -d "${data}" -X POST ${url} | jq '.') echo "[$(date -Iseconds)] [wg_token_renwal] Renewal "$url $key_ren >> $sswg_log - let basen=$basen+2 - if [ -n "${key_ren##*renewToken*}" ] && [ $basen -gt $urlcount ]; then + let basen=$basen+1 + if [ -n "${key_ren##*renewToken*}" ] && [ $basen -lt 5 ]; then if [ -z "${key_ren##*400*}" ]; then if [ -z "${key_ren##*Bad Request*}" ]; then - echo "[wg_token_renwal] Curl post appears to be malformed" + echo "[wg_token_renwal] Curl post appears to be malformed" + echo "[wg_token_renwal]E ========================================" exit 130 fi elif [ -z "${key_ren##*401*}" ]; then @@ -219,39 +233,51 @@ wg_token_renwal() { # use renewal token to generate new tokens error_count=1 basen=1 elif [ -z "${key_ren##*Expired*}" ] && [ $error_count -eq 1 ]; then - echo "Token is expiring immediately." + echo "[wg_token_renwal] Token is expiring immediately." + echo "[wg_token_renwal]E ========================================" exit 131 elif [ -z "${key_ren##*Token not found*}" ]; then - echo "[wg_token_renwal] Token was not recognised as a token." - echo "[wg_token_renwal] If it fails repeatedly check your credentials and that a token exists." + echo "[wg_token_renwal] Token was not recognised as a token." + echo "[wg_token_renwal] If it fails repeatedly check your credentials and that a token exists." + echo "[wg_token_renwal]E ========================================" exit 132 fi else - echo "[wg_token_renwal] Unknown error" + echo "[wg_token_renwal] Unknown error" + echo "[wg_token_renwal]E ========================================" exit 133 fi fi done - echo "${key_ren}" > $token_file - echo "[wg_token_renwal] token renewed" + token="$(eval echo $(echo $key_ren | jq '.token'))" + rtoken="$(eval echo $(echo $key_ren | jq '.renewToken'))" + apiurl="$(eval echo $(jq '.apiurl' $token_file))" + rm -f $token_file + echo -e "{\n\t\"apiurl\":\"$apiurl\",\n\t\"token\":\"$token\",\n\t\"renewToken\":\"$rtoken\"\n}" >> $token_file + echo "[wg_token_renwal] token renewed" + echo "[wg_token_renwal]E ========================================" } get_servers() { + echo "[get_servers]S ========================================" server_type='generic static obfuscated double' for server in $server_type; do + echo "[get_servers] Getting $server servers" tmpfile=$(mktemp /tmp/wg-curl-ser.XXXXXX) http_status=0 basen=0 until [ $http_status -eq 200 ]; do - let basen=$basen+1; baseurl=baseurl_$basen - if [ $basen -gt $urlcount ]; then - echo "[get_servers] Unable to download server information." + let basen=$basen+1 + if [ $basen -gt 5 ]; then + echo "[get_servers] Unable to download server information after 5 tries." + echo "[get_servers]E ========================================" rm $tmpfile exit 140 fi - url=$(eval echo \${$baseurl})/v4/server/clusters/$server?countryCode= + url="$(eval echo $(jq '.apiurl' $token_file))/v4/server/clusters/$server?countryCode=" token="Authorization: Bearer $(eval echo $(jq '.token' $token_file))" - http_status=$(curl -o $tmpfile -w "%{http_code}" -H "${token}" -H "Content-Type: application/json" ${url}) + echo "[get_servers] Using \"$(eval echo $(jq '.apiurl' $token_file))\" for api calls" + http_status=$(curl -fsS -o $tmpfile -w "%{http_code}" -H "${token}" -H "Content-Type: application/json" ${url}) echo "[$(date -Iseconds)] [get_servers]" $server" servers "$url $http_status >> $sswg_log done server_file="$server""_servers_file" @@ -260,18 +286,29 @@ get_servers() { echo $(cat $tmpfile | jq '.') >> $server_file rm $tmpfile done + echo "[get_servers]E ========================================" } gen_client_confs() { + echo "[gen_client_confs]S ========================================" mkdir -p "${config_folder}/configs" rm -f ${config_folder}/configs/*.conf - servers='generic static obfuscated' # still need to work on obfuscated & double, they will need separate conf gens + servers='generic static obfuscated double' # worked out parsing logic for obfuscated and double should work for server in $servers; do + echo "[gen_client_confs] Generating $server configs" postf=".prod.surfshark.com" server_hosts="$server""_servers_file" server_hosts=$(eval echo \${$server_hosts}) server_hosts=$(cat $server_hosts) - server_hosts=$(echo "${server_hosts}" | jq -c '.[] | [.connectionName,.load,.tags,.pubKey]') + + if [ "$server" = "double" ]; then + server_hosts=$(echo "${server_hosts}" | jq -c '.[] | [.transitCluster.connectionName,.load,.tags,.pubKey]') + elif [ "$server" = "obsfuscated" ]; then + server_hosts=$(echo "${server_hosts}" | jq -c '.[] | select( .info != null)| [.connectionName,.load,.tags,.info[].entry.value]') + else + server_hosts=$(echo "${server_hosts}" | jq -c '.[] | [.connectionName,.load,.tags,.pubKey]') + fi + for row in $server_hosts; do srv_host="$(echo $row | jq '.[0]')" srv_host=$(eval echo $srv_host) @@ -295,91 +332,89 @@ gen_client_confs() { srv_conf_file=${config_folder}/configs/${file_name}.conf echo -e "#$srv_host SERVER:[$server] LOAD:[$srv_load] TAGS:[$srv_tags] PUB:[$srv_pub}" > $srv_conf_file - srv_conf="[Interface]\nPrivateKey=$(eval echo $(jq '.prv' $wg_keys))\nAddress=10.14.0.2/8\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" + srv_conf="[Interface]\nPrivateKey=$(eval echo $(jq '.prv' $wg_keys))\nAddress=10.14.0.2/8\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, ::/0\nEndpoint=${srv_host}:51820\nPersistentKeepalive=25\n" echo -e "$srv_conf" >> $srv_conf_file done file_removal="$server""_servers_file" file_removal=$(eval echo \${$file_removal}) rm -f $file_removal done + echo "[gen_client_confs]E ========================================" } reset_surfshark() { - echo "[reset_surfshark] Clearing old settings ..." + echo "[reset_surfshark]S ========================================" + echo "[reset_surfshark] Clearing old settings ..." rm -fr ${config_folder}/configs rm -f ${config_folder}/*servers.json rm -f ${config_folder}/wg.json rm -f ${config_folder}/token.json rm -f ${config_folder}/token_expires.json + rm -f ${config_folder}/sswg.log + echo "[reset_surfshark] All Settings cleared rerun script to regenerate..." + echo "[reset_surfshark]E ========================================" } -echo "=========Start Main===========" +echo "========================Start Main=========================" read_config parse_arg "$@" if [ $reset_all -eq 1 ]; then - echo "--------------" - echo "Reset All ..." reset_surfshark - echo "--------------" exit 0 fi if [ $generate_servers -eq 1 ]; then - echo "----------------------------------" - echo "Generate Servers and Profiles ..." - get_servers - gen_client_confs - echo "server list now:" - echo "$(ls -xA ${config_folder}/configs/)" - echo "----------------------------------" - exit 0 + if [ -f "$token_file" ]; then + get_servers + gen_client_confs + echo "[MAIN]-r server list now:" + echo "$(ls -xA ${config_folder}/configs/)" + exit 0 + else + echo "[MAIN]-r No token file exiting " + exit 500 + fi fi if [ $renew_token -eq 1 ]; then - echo "----------------" - echo "Renew Token ..." - wg_token_renwal - wg_check_pubkey - echo "----------------" - exit 0 + if [ -f "$token_file" ]; then + wg_token_renwal + wg_check_pubkey + exit 0 + else + echo "[MAIN]-n No token file exiting " + exit 500 + fi fi -echo "------------------------" -echo "Logging in if needed ..." +echo "[MAIN] Logging in if needed ..." if [ -f "$token_file" ]; then - echo "login not required ..." + echo "[MAIN] login not required ..." else - echo "login required ..." + echo "[MAIN] login required ..." wg_login fi -echo "------------------------" -echo "-------------------" -echo "Generating keys ..." +echo "[MAIN] Generating keys ..." if [ -f "$wg_keys" ]; then - echo "using existent wg keys" + echo "[MAIN] using existent wg keys" else wg_gen_keys fi -echo "-------------------" -echo "-------------------------" -echo "Registering public key ..." +echo "[MAIN] Registering public key ..." wg_register_pub -echo "-------------------------" if [ $generate_conf -eq 1 ]; then - echo "-------------------------------" - echo "Getting the list of servers ..." + echo "[MAIN]-g Getting the list of servers ..." get_servers - echo "Generating server profiles ..." + echo "[MAIN]-g Generating server profiles ..." gen_client_confs - echo "-------------------------------" fi -echo "Done!" -echo "=========================" +echo "===========================DONE!===========================" + ############################################################################# # -------------------- # TABLE OF ERROR CODES