Address some shellcheck reported issues

This commit is contained in:
Valery Kalesnik 2021-08-03 17:26:29 -07:00 committed by goonix
parent 6d06b31af1
commit b04be889f0
8 changed files with 96 additions and 96 deletions

View File

@ -22,7 +22,7 @@
# This function allows you to check if the required tools have been installed. # This function allows you to check if the required tools have been installed.
function check_tool() { function check_tool() {
cmd=$1 cmd=$1
if ! command -v $cmd &>/dev/null if ! command -v "$cmd" &>/dev/null
then then
echo "$cmd could not be found" echo "$cmd could not be found"
echo "Please install $cmd" echo "Please install $cmd"
@ -37,7 +37,7 @@ check_tool openvpn
# Check if terminal allows output, if yes, define colors for output # Check if terminal allows output, if yes, define colors for output
if test -t 1; then if test -t 1; then
ncolors=$(tput colors) ncolors=$(tput colors)
if test -n "$ncolors" && test $ncolors -ge 8; then if test -n "$ncolors" && test "$ncolors" -ge 8; then
GREEN='\033[0;32m' GREEN='\033[0;32m'
RED='\033[0;31m' RED='\033[0;31m'
NC='\033[0m' # No Color NC='\033[0m' # No Color
@ -62,19 +62,19 @@ if [[ "$adapter_check" != *"$should_read"* ]]; then
old_pid_name="$( ps -p "$old_pid" -o comm= )" old_pid_name="$( ps -p "$old_pid" -o comm= )"
if [[ $old_pid_name == 'openvpn' ]]; then if [[ $old_pid_name == 'openvpn' ]]; then
echo echo
echo -e It seems likely that process ${RED}$old_pid${NC} is an OpenVPN connection echo -e It seems likely that process ${RED}"$old_pid"${NC} is an OpenVPN connection
echo that was established by using this script. Unless it is closed echo that was established by using this script. Unless it is closed
echo you would not be able to get a new connection. echo you would not be able to get a new connection.
echo -ne "Do you want to run ${RED}$ kill $old_pid${NC} (Y/n): " echo -ne "Do you want to run ${RED}$ kill $old_pid${NC} (Y/n): "
read close_connection read -r close_connection
fi fi
if echo ${close_connection:0:1} | grep -iq n ; then if echo "${close_connection:0:1}" | grep -iq n ; then
echo -e ${RED}Closing script. Resolve tun06 adapter conflict and run the script again. echo -e ${RED}Closing script. Resolve tun06 adapter conflict and run the script again.
exit 1 exit 1
fi fi
echo echo
echo -e ${GREEN}Killing the existing OpenVPN process and waiting 5 seconds...${NC} echo -e ${GREEN}Killing the existing OpenVPN process and waiting 5 seconds...${NC}
kill $old_pid kill "$old_pid"
echo echo
for i in {5..1}; do for i in {5..1}; do
echo -n "$i..." echo -n "$i..."
@ -129,8 +129,8 @@ fi
echo -n "Trying to write /opt/piavpn-manual/pia.ovpn..." echo -n "Trying to write /opt/piavpn-manual/pia.ovpn..."
mkdir -p /opt/piavpn-manual mkdir -p /opt/piavpn-manual
rm -f /opt/piavpn-manual/credentials /opt/piavpn-manual/route_info rm -f /opt/piavpn-manual/credentials /opt/piavpn-manual/route_info
echo ${PIA_TOKEN:0:62}" echo "${PIA_TOKEN:0:62}""
"${PIA_TOKEN:62} > /opt/piavpn-manual/credentials || exit 1 ""${PIA_TOKEN:62}" > /opt/piavpn-manual/credentials || exit 1
chmod 600 /opt/piavpn-manual/credentials chmod 600 /opt/piavpn-manual/credentials
echo -e "${GREEN}OK!${NC}" echo -e "${GREEN}OK!${NC}"
@ -162,7 +162,7 @@ fi
# Create the OpenVPN config based on the settings specified # Create the OpenVPN config based on the settings specified
cat $prefix_filepath > /opt/piavpn-manual/pia.ovpn || exit 1 cat $prefix_filepath > /opt/piavpn-manual/pia.ovpn || exit 1
echo remote $OVPN_SERVER_IP $port $protocol >> /opt/piavpn-manual/pia.ovpn echo remote "$OVPN_SERVER_IP" $port "$protocol" >> /opt/piavpn-manual/pia.ovpn
# Copy the up/down scripts to /opt/piavpn-manual/ # Copy the up/down scripts to /opt/piavpn-manual/
# based upon use of PIA DNS # based upon use of PIA DNS
@ -172,8 +172,8 @@ if [ "$PIA_DNS" != true ]; then
echo -e ${RED}This configuration will not use PIA DNS.${NC} echo -e ${RED}This configuration will not use PIA DNS.${NC}
echo If you want to also enable PIA DNS, please start the script echo If you want to also enable PIA DNS, please start the script
echo with the env var PIA_DNS=true. Example: echo with the env var PIA_DNS=true. Example:
echo $ OVPN_SERVER_IP=\"$OVPN_SERVER_IP\" OVPN_HOSTNAME=\"$OVPN_HOSTNAME\" \ echo $ OVPN_SERVER_IP=\""$OVPN_SERVER_IP"\" OVPN_HOSTNAME=\""$OVPN_HOSTNAME"\" \
PIA_TOKEN=\"$PIA_TOKEN\" CONNECTION_SETTINGS=\"$CONNECTION_SETTINGS\" \ PIA_TOKEN=\""$PIA_TOKEN"\" CONNECTION_SETTINGS=\""$CONNECTION_SETTINGS"\" \
PIA_PF=true PIA_DNS=true ./connect_to_openvpn_with_token.sh PIA_PF=true PIA_DNS=true ./connect_to_openvpn_with_token.sh
else else
cp openvpn_config/openvpn_up_dnsoverwrite.sh /opt/piavpn-manual/openvpn_up.sh cp openvpn_config/openvpn_up_dnsoverwrite.sh /opt/piavpn-manual/openvpn_up.sh
@ -201,7 +201,7 @@ Confirming OpenVPN connection state..."
# Manually adjust the connection_wait_time if needed # Manually adjust the connection_wait_time if needed
connection_wait_time=10 connection_wait_time=10
confirmation="Initialization Sequence Complete" confirmation="Initialization Sequence Complete"
for (( timeout=0; timeout <=$connection_wait_time; timeout++ )) for (( timeout=0; timeout <= connection_wait_time; timeout++ ))
do do
sleep 1 sleep 1
if grep -q "$confirmation" /opt/piavpn-manual/debug_info; then if grep -q "$confirmation" /opt/piavpn-manual/debug_info; then
@ -216,7 +216,7 @@ gateway_ip="$( cat /opt/piavpn-manual/route_info )"
# Report and exit if connection was not initialized within 10 seconds. # Report and exit if connection was not initialized within 10 seconds.
if [ "$connected" != true ]; then if [ "$connected" != true ]; then
echo -e "${RED}The VPN connection was not established within 10 seconds.${NC}" echo -e "${RED}The VPN connection was not established within 10 seconds.${NC}"
kill $ovpn_pid kill "$ovpn_pid"
exit 1 exit 1
fi fi
@ -236,9 +236,9 @@ To disconnect the VPN, run:
# This section will stop the script if PIA_PF is not set to "true". # This section will stop the script if PIA_PF is not set to "true".
if [ "$PIA_PF" != true ]; then if [ "$PIA_PF" != true ]; then
echo If you want to also enable port forwarding, you can start the script: echo If you want to also enable port forwarding, you can start the script:
echo -e $ ${GREEN}PIA_TOKEN=$PIA_TOKEN \ echo -e $ ${GREEN}PIA_TOKEN="$PIA_TOKEN" \
PF_GATEWAY=$gateway_ip \ PF_GATEWAY="$gateway_ip" \
PF_HOSTNAME=$OVPN_HOSTNAME \ PF_HOSTNAME="$OVPN_HOSTNAME" \
./port_forwarding.sh${NC} ./port_forwarding.sh${NC}
echo echo
echo The location used must be port forwarding enabled, or this will fail. echo The location used must be port forwarding enabled, or this will fail.

View File

@ -22,7 +22,7 @@
# This function allows you to check if the required tools have been installed. # This function allows you to check if the required tools have been installed.
function check_tool() { function check_tool() {
cmd=$1 cmd=$1
if ! command -v $cmd &>/dev/null if ! command -v "$cmd" &>/dev/null
then then
echo "$cmd could not be found" echo "$cmd could not be found"
echo "Please install $cmd" echo "Please install $cmd"
@ -37,7 +37,7 @@ check_tool jq
# Check if terminal allows output, if yes, define colors for output # Check if terminal allows output, if yes, define colors for output
if test -t 1; then if test -t 1; then
ncolors=$(tput colors) ncolors=$(tput colors)
if test -n "$ncolors" && test $ncolors -ge 8; then if test -n "$ncolors" && test "$ncolors" -ge 8; then
GREEN='\033[0;32m' GREEN='\033[0;32m'
RED='\033[0;31m' RED='\033[0;31m'
NC='\033[0m' # No Color NC='\033[0m' # No Color
@ -91,7 +91,7 @@ export pubKey
# In case you didn't clone the entire repo, get the certificate from: # In case you didn't clone the entire repo, get the certificate from:
# https://github.com/pia-foss/manual-connections/blob/master/ca.rsa.4096.crt # https://github.com/pia-foss/manual-connections/blob/master/ca.rsa.4096.crt
# In case you want to troubleshoot the script, replace -s with -v. # In case you want to troubleshoot the script, replace -s with -v.
echo Trying to connect to the PIA WireGuard API on $WG_SERVER_IP... echo Trying to connect to the PIA WireGuard API on "$WG_SERVER_IP"...
wireguard_json="$(curl -s -G \ wireguard_json="$(curl -s -G \
--connect-to "$WG_HOSTNAME::$WG_SERVER_IP:" \ --connect-to "$WG_HOSTNAME::$WG_SERVER_IP:" \
--cacert "ca.rsa.4096.crt" \ --cacert "ca.rsa.4096.crt" \
@ -122,9 +122,9 @@ echo
# require it. # require it.
if [ "$PIA_DNS" == true ]; then if [ "$PIA_DNS" == true ]; then
dnsServer="$(echo "$wireguard_json" | jq -r '.dns_servers[0]')" dnsServer="$(echo "$wireguard_json" | jq -r '.dns_servers[0]')"
echo Trying to set up DNS to $dnsServer. In case you do not have resolvconf, echo "Trying to set up DNS to $dnsServer. In case you do not have resolvconf,"
echo this operation will fail and you will not get a VPN. If you have issues, echo "this operation will fail and you will not get a VPN. If you have issues,"
echo start this script without PIA_DNS. echo "start this script without PIA_DNS."
echo echo
dnsSettingForVPN="DNS = $dnsServer" dnsSettingForVPN="DNS = $dnsServer"
fi fi
@ -163,9 +163,9 @@ To disconnect the VPN, run:
# This section will stop the script if PIA_PF is not set to "true". # This section will stop the script if PIA_PF is not set to "true".
if [ "$PIA_PF" != true ]; then if [ "$PIA_PF" != true ]; then
echo If you want to also enable port forwarding, you can start the script: echo If you want to also enable port forwarding, you can start the script:
echo -e $ ${GREEN}PIA_TOKEN=$PIA_TOKEN \ echo -e $ ${GREEN}PIA_TOKEN="$PIA_TOKEN" \
PF_GATEWAY=$WG_SERVER_IP \ PF_GATEWAY="$WG_SERVER_IP" \
PF_HOSTNAME=$WG_HOSTNAME \ PF_HOSTNAME="$WG_HOSTNAME" \
./port_forwarding.sh${NC} ./port_forwarding.sh${NC}
echo echo
echo The location used must be port forwarding enabled, or this will fail. echo The location used must be port forwarding enabled, or this will fail.

View File

@ -22,7 +22,7 @@
# This function allows you to check if the required tools have been installed. # This function allows you to check if the required tools have been installed.
function check_tool() { function check_tool() {
cmd=$1 cmd=$1
if ! command -v $cmd &>/dev/null if ! command -v "$cmd" &>/dev/null
then then
echo "$cmd could not be found" echo "$cmd could not be found"
echo "Please install $cmd" echo "Please install $cmd"
@ -53,7 +53,7 @@ function check_all_region_data() {
# Get all data for the selected region # Get all data for the selected region
# Exit with code 1 if the REGION_ID provided is invalid # Exit with code 1 if the REGION_ID provided is invalid
function get_selected_region_data() { function get_selected_region_data() {
regionData="$( echo $all_region_data | regionData="$( echo "$all_region_data" |
jq --arg REGION_ID "$selectedRegion" -r \ jq --arg REGION_ID "$selectedRegion" -r \
'.regions[] | select(.id==$REGION_ID)')" '.regions[] | select(.id==$REGION_ID)')"
if [[ ! $regionData ]]; then if [[ ! $regionData ]]; then
@ -66,7 +66,7 @@ function get_selected_region_data() {
# Check if terminal allows output, if yes, define colors for output # Check if terminal allows output, if yes, define colors for output
if test -t 1; then if test -t 1; then
ncolors=$(tput colors) ncolors=$(tput colors)
if test -n "$ncolors" && test $ncolors -ge 8; then if test -n "$ncolors" && test "$ncolors" -ge 8; then
GREEN='\033[0;32m' GREEN='\033[0;32m'
RED='\033[0;31m' RED='\033[0;31m'
NC='\033[0m' # No Color NC='\033[0m' # No Color
@ -103,18 +103,18 @@ serverlist_url='https://serverlist.piaservers.net/vpninfo/servers/v6'
printServerLatency() { printServerLatency() {
serverIP="$1" serverIP="$1"
regionID="$2" regionID="$2"
regionName="$(echo ${@:3} | regionName="$(echo "${@:3}" |
sed 's/ false//' | sed 's/true/(geo)/')" sed 's/ false//' | sed 's/true/(geo)/')"
time=$(LC_NUMERIC=en_US.utf8 curl -o /dev/null -s \ time=$(LC_NUMERIC=en_US.utf8 curl -o /dev/null -s \
--connect-timeout $MAX_LATENCY \ --connect-timeout "$MAX_LATENCY" \
--write-out "%{time_connect}" \ --write-out "%{time_connect}" \
http://$serverIP:443) http://"$serverIP":443)
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
>&2 echo Got latency ${time}s for region: $regionName >&2 echo Got latency "${time}"s for region: "$regionName"
echo $time $regionID $serverIP echo "$time $regionID $serverIP"
# Write a list of servers with acceptable latancy # Write a list of servers with acceptable latancy
# to /opt/piavpn-manual/latencyList # to /opt/piavpn-manual/latencyList
echo -e $time $regionID'\t'$serverIP'\t'$regionName >> /opt/piavpn-manual/latencyList echo -e "$time" "$regionID"'\t'"$serverIP"'\t'"$regionName" >> /opt/piavpn-manual/latencyList
fi fi
# Sort the latencyList, ordered by latency # Sort the latencyList, ordered by latency
sort -no /opt/piavpn-manual/latencyList /opt/piavpn-manual/latencyList sort -no /opt/piavpn-manual/latencyList /opt/piavpn-manual/latencyList
@ -150,23 +150,23 @@ if [[ $selectedRegion == "none" ]]; then
if [[ $PIA_PF == "true" ]]; then if [[ $PIA_PF == "true" ]]; then
echo Port Forwarding is enabled, non-PF servers excluded. echo Port Forwarding is enabled, non-PF servers excluded.
echo echo
summarized_region_data="$( echo $all_region_data | summarized_region_data="$( echo "$all_region_data" |
jq -r '.regions[] | select(.port_forward==true) | jq -r '.regions[] | select(.port_forward==true) |
.servers.meta[0].ip+" "+.id+" "+.name+" "+(.geo|tostring)' )" .servers.meta[0].ip+" "+.id+" "+.name+" "+(.geo|tostring)' )"
else else
summarized_region_data="$( echo $all_region_data | summarized_region_data="$( echo "$all_region_data" |
jq -r '.regions[] | jq -r '.regions[] |
.servers.meta[0].ip+" "+.id+" "+.name+" "+(.geo|tostring)' )" .servers.meta[0].ip+" "+.id+" "+.name+" "+(.geo|tostring)' )"
fi fi
echo -e Testing regions that respond \ echo -e Testing regions that respond \
faster than ${GREEN}$MAX_LATENCY${NC} seconds: faster than ${GREEN}"$MAX_LATENCY"${NC} seconds:
selectedRegion="$(echo "$summarized_region_data" | selectedRegion="$(echo "$summarized_region_data" |
xargs -I{} bash -c 'printServerLatency {}' | xargs -I{} bash -c 'printServerLatency {}' |
sort | head -1 | awk '{ print $2 }')" sort | head -1 | awk '{ print $2 }')"
echo echo
if [ -z "$selectedRegion" ]; then if [ -z "$selectedRegion" ]; then
echo -e ${RED}No region responded within ${MAX_LATENCY}s, consider using a higher timeout. echo -e ${RED}No region responded within "${MAX_LATENCY}"s, consider using a higher timeout.
echo For example, to wait 1 second for each region, inject MAX_LATENCY=1 like this: echo For example, to wait 1 second for each region, inject MAX_LATENCY=1 like this:
echo -e $ MAX_LATENCY=1 ./get_region.sh${NC} echo -e $ MAX_LATENCY=1 ./get_region.sh${NC}
exit 1 exit 1
@ -182,19 +182,19 @@ fi
get_selected_region_data get_selected_region_data
bestServer_meta_IP="$(echo $regionData | jq -r '.servers.meta[0].ip')" bestServer_meta_IP="$(echo "$regionData" | jq -r '.servers.meta[0].ip')"
bestServer_meta_hostname="$(echo $regionData | jq -r '.servers.meta[0].cn')" bestServer_meta_hostname="$(echo "$regionData" | jq -r '.servers.meta[0].cn')"
bestServer_WG_IP="$(echo $regionData | jq -r '.servers.wg[0].ip')" bestServer_WG_IP="$(echo "$regionData" | jq -r '.servers.wg[0].ip')"
bestServer_WG_hostname="$(echo $regionData | jq -r '.servers.wg[0].cn')" bestServer_WG_hostname="$(echo "$regionData" | jq -r '.servers.wg[0].cn')"
bestServer_OT_IP="$(echo $regionData | jq -r '.servers.ovpntcp[0].ip')" bestServer_OT_IP="$(echo "$regionData" | jq -r '.servers.ovpntcp[0].ip')"
bestServer_OT_hostname="$(echo $regionData | jq -r '.servers.ovpntcp[0].cn')" bestServer_OT_hostname="$(echo "$regionData" | jq -r '.servers.ovpntcp[0].cn')"
bestServer_OU_IP="$(echo $regionData | jq -r '.servers.ovpnudp[0].ip')" bestServer_OU_IP="$(echo "$regionData" | jq -r '.servers.ovpnudp[0].ip')"
bestServer_OU_hostname="$(echo $regionData | jq -r '.servers.ovpnudp[0].cn')" bestServer_OU_hostname="$(echo "$regionData" | jq -r '.servers.ovpnudp[0].cn')"
if [[ $VPN_PROTOCOL == "no" ]]; then if [[ $VPN_PROTOCOL == "no" ]]; then
echo -ne The $selectedOrLowestLatency region is ${GREEN}"$(echo $regionData | jq -r '.name')"${NC} echo -ne The $selectedOrLowestLatency region is ${GREEN}"$(echo "$regionData" | jq -r '.name')"${NC}
if echo $regionData | jq -r '.geo' | grep true > /dev/null; then if echo "$regionData" | jq -r '.geo' | grep true > /dev/null; then
echo " (geolocated region)." echo " (geolocated region)."
else else
echo "." echo "."
@ -236,8 +236,8 @@ if [[ $VPN_PROTOCOL == wireguard ]]; then
echo The ./get_region.sh script got started with echo The ./get_region.sh script got started with
echo -e ${GREEN}VPN_PROTOCOL=wireguard${NC}, so we will automatically connect to WireGuard, echo -e ${GREEN}VPN_PROTOCOL=wireguard${NC}, so we will automatically connect to WireGuard,
echo by running this command: echo by running this command:
echo -e $ ${GREEN}PIA_TOKEN=$PIA_TOKEN \\ echo -e $ ${GREEN}PIA_TOKEN="$PIA_TOKEN" \\
echo WG_SERVER_IP=$bestServer_WG_IP WG_HOSTNAME=$bestServer_WG_hostname \\ echo WG_SERVER_IP="$bestServer_WG_IP" WG_HOSTNAME="$bestServer_WG_hostname" \\
echo -e PIA_PF=$PIA_PF ./connect_to_wireguard_with_token.sh${NC} echo -e PIA_PF=$PIA_PF ./connect_to_wireguard_with_token.sh${NC}
echo echo
PIA_PF=$PIA_PF PIA_TOKEN=$PIA_TOKEN WG_SERVER_IP=$bestServer_WG_IP \ PIA_PF=$PIA_PF PIA_TOKEN=$PIA_TOKEN WG_SERVER_IP=$bestServer_WG_IP \
@ -257,9 +257,9 @@ if [[ $VPN_PROTOCOL == openvpn* ]]; then
echo The ./get_region.sh script got started with echo The ./get_region.sh script got started with
echo -e ${GREEN}VPN_PROTOCOL=$VPN_PROTOCOL${NC}, so we will automatically echo -e ${GREEN}VPN_PROTOCOL=$VPN_PROTOCOL${NC}, so we will automatically
echo connect to OpenVPN, by running this command: echo connect to OpenVPN, by running this command:
echo -e $ ${GREEN}PIA_PF=$PIA_PF PIA_TOKEN=$PIA_TOKEN \\ echo -e $ ${GREEN}PIA_PF=$PIA_PF PIA_TOKEN="$PIA_TOKEN" \\
echo OVPN_SERVER_IP=$serverIP \\ echo OVPN_SERVER_IP="$serverIP" \\
echo OVPN_HOSTNAME=$serverHostname \\ echo OVPN_HOSTNAME="$serverHostname" \\
echo CONNECTION_SETTINGS=$VPN_PROTOCOL \\ echo CONNECTION_SETTINGS=$VPN_PROTOCOL \\
echo -e ./connect_to_openvpn_with_token.sh${NC} echo -e ./connect_to_openvpn_with_token.sh${NC}
echo echo

View File

@ -22,7 +22,7 @@
# This function allows you to check if the required tools have been installed. # This function allows you to check if the required tools have been installed.
function check_tool() { function check_tool() {
cmd=$1 cmd=$1
if ! command -v $cmd &>/dev/null if ! command -v "$cmd" &>/dev/null
then then
echo "$cmd could not be found" echo "$cmd could not be found"
echo "Please install $cmd" echo "Please install $cmd"
@ -42,7 +42,7 @@ check_tool jq
# Check if terminal allows output, if yes, define colors for output # Check if terminal allows output, if yes, define colors for output
if test -t 1; then if test -t 1; then
ncolors=$(tput colors) ncolors=$(tput colors)
if test -n "$ncolors" && test $ncolors -ge 8; then if test -n "$ncolors" && test "$ncolors" -ge 8; then
GREEN='\033[0;32m' GREEN='\033[0;32m'
RED='\033[0;31m' RED='\033[0;31m'
NC='\033[0m' # No Color NC='\033[0m' # No Color
@ -87,9 +87,9 @@ echo -e ${GREEN}OK!
echo echo
token=$(echo "$generateTokenResponse" | jq -r '.token') token=$(echo "$generateTokenResponse" | jq -r '.token')
tokenExpiration=$(timeout_timestamp) tokenExpiration=$(timeout_timestamp)
echo -e PIA_TOKEN=$token${NC} echo -e PIA_TOKEN="$token"${NC}
echo $token > /opt/piavpn-manual/token || exit 1 echo "$token" > /opt/piavpn-manual/token || exit 1
echo $tokenExpiration >> /opt/piavpn-manual/token echo "$tokenExpiration" >> /opt/piavpn-manual/token
echo echo
echo This token will expire in 24 hours, on $tokenExpiration. echo This token will expire in 24 hours, on "$tokenExpiration".
echo echo

View File

@ -1,4 +1,4 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Write gateway IP for reference # Write gateway IP for reference
echo $route_vpn_gateway > /opt/piavpn-manual/route_info echo "$route_vpn_gateway" > /opt/piavpn-manual/route_info

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Write gateway IP for reference # Write gateway IP for reference
echo $route_vpn_gateway > /opt/piavpn-manual/route_info echo "$route_vpn_gateway" > /opt/piavpn-manual/route_info
# Back up resolv.conf and create new on with PIA DNS # Back up resolv.conf and create new on with PIA DNS
cat /etc/resolv.conf > /opt/piavpn-manual/resolv_conf_backup cat /etc/resolv.conf > /opt/piavpn-manual/resolv_conf_backup

View File

@ -22,7 +22,7 @@
# This function allows you to check if the required tools have been installed. # This function allows you to check if the required tools have been installed.
function check_tool() { function check_tool() {
cmd=$1 cmd=$1
if ! command -v $cmd &>/dev/null if ! command -v "$cmd" &>/dev/null
then then
echo "$cmd could not be found" echo "$cmd could not be found"
echo "Please install $cmd" echo "Please install $cmd"
@ -50,7 +50,7 @@ fi
# Check if terminal allows output, if yes, define colors for output # Check if terminal allows output, if yes, define colors for output
if test -t 1; then if test -t 1; then
ncolors=$(tput colors) ncolors=$(tput colors)
if test -n "$ncolors" && test $ncolors -ge 8; then if test -n "$ncolors" && test "$ncolors" -ge 8; then
GREEN='\033[0;32m' GREEN='\033[0;32m'
RED='\033[0;31m' RED='\033[0;31m'
NC='\033[0m' # No Color NC='\033[0m' # No Color
@ -146,9 +146,9 @@ while true; do
echo -e "${RED}The API did not return OK when trying to bind port... Exiting." echo -e "${RED}The API did not return OK when trying to bind port... Exiting."
exit 1 exit 1
fi fi
echo -e Forwarded port'\t'${GREEN}$port${NC} echo -e Forwarded port'\t'${GREEN}"$port"${NC}
echo -e Refreshed on'\t'${GREEN}$(date)${NC} echo -e Refreshed on'\t'${GREEN}"$(date)"${NC}
echo -e Expires on'\t'${RED}$(date --date="$expires_at")${NC} echo -e Expires on'\t'${RED}"$(date --date="$expires_at")"${NC}
echo -e "\n${GREEN}This script will need to remain active to use port forwarding, and will refresh every 15 minutes.${NC}\n" echo -e "\n${GREEN}This script will need to remain active to use port forwarding, and will refresh every 15 minutes.${NC}\n"
# sleep 15 minutes # sleep 15 minutes

View File

@ -22,7 +22,7 @@
# Check if terminal allows output, if yes, define colors for output # Check if terminal allows output, if yes, define colors for output
if test -t 1; then if test -t 1; then
ncolors=$(tput colors) ncolors=$(tput colors)
if test -n "$ncolors" && test $ncolors -ge 8; then if test -n "$ncolors" && test "$ncolors" -ge 8; then
GREEN='\033[0;32m' GREEN='\033[0;32m'
RED='\033[0;31m' RED='\033[0;31m'
NC='\033[0m' # No Color NC='\033[0m' # No Color
@ -52,12 +52,12 @@ while :; do
# Check for in-line definition of $PIA_USER # Check for in-line definition of $PIA_USER
if [[ ! $PIA_USER || $PIA_USER = "" ]]; then if [[ ! $PIA_USER || $PIA_USER = "" ]]; then
echo echo
read -p "PIA username (p#######): " PIA_USER read -rp "PIA username (p#######): " PIA_USER
fi fi
# Confirm format of PIA_USER input # Confirm format of PIA_USER input
unPrefix=$( echo ${PIA_USER:0:1} ) unPrefix="${PIA_USER:0:1}"
unSuffix=$( echo ${PIA_USER:1} ) unSuffix="${PIA_USER:1}"
if [[ -z "$PIA_USER" ]]; then if [[ -z "$PIA_USER" ]]; then
echo -e "\n${RED}You must provide input.${NC}" echo -e "\n${RED}You must provide input.${NC}"
elif [[ ${#PIA_USER} != 8 ]]; then elif [[ ${#PIA_USER} != 8 ]]; then
@ -103,8 +103,8 @@ while :; do
tokenLocation="/opt/piavpn-manual/token" tokenLocation="/opt/piavpn-manual/token"
# If the script failed to generate an authentication token, the script will exit early. # If the script failed to generate an authentication token, the script will exit early.
if [ ! -f "$tokenLocation" ]; then if [ ! -f "$tokenLocation" ]; then
read -p "Do you want to try again ([N]o/[y]es): " tryAgain read -pr "Do you want to try again ([N]o/[y]es): " tryAgain
if ! echo ${tryAgain:0:1} | grep -iq y; then if ! echo "${tryAgain:0:1}" | grep -iq y; then
exit 1 exit 1
fi fi
PIA_USER="" PIA_USER=""
@ -120,9 +120,9 @@ done
# Check for in-line definition of PIA_PF and prompt for input # Check for in-line definition of PIA_PF and prompt for input
if [[ ! $PIA_PF || $PIA_PF = "" ]]; then if [[ ! $PIA_PF || $PIA_PF = "" ]]; then
echo -n "Do you want a forwarding port assigned ([N]o/[y]es): " echo -n "Do you want a forwarding port assigned ([N]o/[y]es): "
read portForwarding read -r portForwarding
echo echo
if echo ${portForwarding:0:1} | grep -iq y; then if echo "${portForwarding:0:1}" | grep -iq y; then
PIA_PF="true" PIA_PF="true"
fi fi
fi fi
@ -138,11 +138,11 @@ if [[ ! $DISABLE_IPV6 || $DISABLE_IPV6 = "" ]]; then
echo "Having active IPv6 connections might compromise security by allowing" echo "Having active IPv6 connections might compromise security by allowing"
echo "split tunnel connections that run outside the VPN tunnel." echo "split tunnel connections that run outside the VPN tunnel."
echo -n "Do you want to disable IPv6? (Y/n): " echo -n "Do you want to disable IPv6? (Y/n): "
read DISABLE_IPV6 read -r DISABLE_IPV6
echo echo
fi fi
if echo ${DISABLE_IPV6:0:1} | grep -iq n; then if echo "${DISABLE_IPV6:0:1}" | grep -iq n; then
echo -e ${RED}"IPv6 settings have not been altered. echo -e ${RED}"IPv6 settings have not been altered.
"${NC} "${NC}
else else
@ -164,7 +164,7 @@ if [[ ! $AUTOCONNECT ]]; then
echo AUTOCONNECT was not declared. echo AUTOCONNECT was not declared.
echo echo
selectServer="ask" selectServer="ask"
elif echo ${AUTOCONNECT:0:1} | grep -iq f; then elif echo "${AUTOCONNECT:0:1}" | grep -iq f; then
if [[ $AUTOCONNECT != "false" ]]; then if [[ $AUTOCONNECT != "false" ]]; then
echo -e "The variable ${GREEN}AUTOCONNECT=$AUTOCONNECT${NC}, starts with 'f' for 'false'." echo -e "The variable ${GREEN}AUTOCONNECT=$AUTOCONNECT${NC}, starts with 'f' for 'false'."
AUTOCONNECT="false" AUTOCONNECT="false"
@ -199,7 +199,7 @@ while :; do
if [[ $selectServer = "ask" ]]; then if [[ $selectServer = "ask" ]]; then
echo -n "Do you want to manually select a server, instead of auto-connecting to the echo -n "Do you want to manually select a server, instead of auto-connecting to the
server with the lowest latency ([N]o/[y]es): " server with the lowest latency ([N]o/[y]es): "
read selectServer read -r selectServer
echo echo
fi fi
@ -208,7 +208,7 @@ server with the lowest latency ([N]o/[y]es): "
# that meet the latency requirements speciied by $MAX_LATENCY. # that meet the latency requirements speciied by $MAX_LATENCY.
# When $VPN_PROTOCOL is set to no, get_region.sh will sort that list of servers # When $VPN_PROTOCOL is set to no, get_region.sh will sort that list of servers
# to allow for numeric selection, or an easy manual review of options. # to allow for numeric selection, or an easy manual review of options.
if echo ${selectServer:0:1} | grep -iq y; then if echo "${selectServer:0:1}" | grep -iq y; then
# This sets the maximum allowed latency in seconds. # This sets the maximum allowed latency in seconds.
# All servers that respond slower than this will be ignored. # All servers that respond slower than this will be ignored.
if [[ ! $MAX_LATENCY || $MAX_LATENCY = "" ]]; then if [[ ! $MAX_LATENCY || $MAX_LATENCY = "" ]]; then
@ -224,7 +224,7 @@ For example, you can try 0.2 for 200ms allowed latency.
MAX_LATENCY=0.05 # default MAX_LATENCY=0.05 # default
while :; do while :; do
if [[ ! $latencyInput || $latencyInput = "" ]]; then if [[ ! $latencyInput || $latencyInput = "" ]]; then
read -p "Custom latency (no input required for 50ms): " latencyInput read -pr "Custom latency (no input required for 50ms): " latencyInput
echo echo
fi fi
customLatency=0 customLatency=0
@ -258,7 +258,7 @@ For example, you can try 0.2 for 200ms allowed latency.
# Output the ordered list of servers that meet the latency specification $MAX_LATENCY # Output the ordered list of servers that meet the latency specification $MAX_LATENCY
echo -e "Orderd list of servers with latency less than ${GREEN}$MAX_LATENCY${NC} seconds:" echo -e "Orderd list of servers with latency less than ${GREEN}$MAX_LATENCY${NC} seconds:"
i=0 i=0
while read line; do while read -r line; do
i=$((i+1)) i=$((i+1))
time=$( awk 'NR == '$i' {print $1}' /opt/piavpn-manual/latencyList ) time=$( awk 'NR == '$i' {print $1}' /opt/piavpn-manual/latencyList )
id=$( awk 'NR == '$i' {print $2}' /opt/piavpn-manual/latencyList ) id=$( awk 'NR == '$i' {print $2}' /opt/piavpn-manual/latencyList )
@ -267,15 +267,15 @@ For example, you can try 0.2 for 200ms allowed latency.
location2=$( awk 'NR == '$i' {print $5}' /opt/piavpn-manual/latencyList ) location2=$( awk 'NR == '$i' {print $5}' /opt/piavpn-manual/latencyList )
location3=$( awk 'NR == '$i' {print $6}' /opt/piavpn-manual/latencyList ) location3=$( awk 'NR == '$i' {print $6}' /opt/piavpn-manual/latencyList )
location4=$( awk 'NR == '$i' {print $7}' /opt/piavpn-manual/latencyList ) location4=$( awk 'NR == '$i' {print $7}' /opt/piavpn-manual/latencyList )
location=$location1" "$location2" "$location3" "$location4 location="$location1 $location2 $location3 $location4"
printf "%3s : %-8s %-15s %17s" $i $time $ip $id printf "%3s : %-8s %-15s %17s" $i "$time" "$ip" "$id"
echo " - "$location echo " - $location"
done < /opt/piavpn-manual/latencyList done < /opt/piavpn-manual/latencyList
echo echo
# Receive input to specify the server to connect to manually # Receive input to specify the server to connect to manually
while :; do while :; do
read -p "Input the number of the server you want to connect to ([1]-[$i]) : " serverSelection read -pr "Input the number of the server you want to connect to ([1]-[$i]) : " serverSelection
if [[ -z "$serverSelection" ]]; then if [[ -z "$serverSelection" ]]; then
echo -e "\n${RED}You must provide input.${NC}\n" echo -e "\n${RED}You must provide input.${NC}\n"
elif ! [[ $serverSelection =~ $intCheck ]]; then elif ! [[ $serverSelection =~ $intCheck ]]; then
@ -285,9 +285,9 @@ For example, you can try 0.2 for 200ms allowed latency.
elif [[ $serverSelection -gt $i ]]; then elif [[ $serverSelection -gt $i ]]; then
echo -e "\n${RED}You must enter a number between 1 and $i.${NC}\n" echo -e "\n${RED}You must enter a number between 1 and $i.${NC}\n"
else else
PREFERRED_REGION=$( awk 'NR == '$serverSelection' {print $2}' /opt/piavpn-manual/latencyList ) PREFERRED_REGION=$( awk 'NR == '"$serverSelection"' {print $2}' /opt/piavpn-manual/latencyList )
echo echo
echo -e ${GREEN}PREFERRED_REGION=$PREFERRED_REGION${NC} echo -e ${GREEN}PREFERRED_REGION="$PREFERRED_REGION"${NC}
break break
fi fi
done done
@ -306,7 +306,7 @@ For example, you can try 0.2 for 200ms allowed latency.
fi fi
else else
# Validate in-line declaration of PREFERRED_REGION; if invalid remove input to initiate prompts # Validate in-line declaration of PREFERRED_REGION; if invalid remove input to initiate prompts
echo Region input is : $PREFERRED_REGION echo Region input is : "$PREFERRED_REGION"
export PREFERRED_REGION export PREFERRED_REGION
VPN_PROTOCOL=no ./get_region.sh VPN_PROTOCOL=no ./get_region.sh
if [[ $? != 1 ]]; then if [[ $? != 1 ]]; then
@ -328,27 +328,27 @@ case $VPN_PROTOCOL in
;; ;;
none | *) none | *)
echo -n "Connection method ([W]ireguard/[o]penvpn): " echo -n "Connection method ([W]ireguard/[o]penvpn): "
read connection_method read -r connection_method
echo echo
VPN_PROTOCOL="wireguard" VPN_PROTOCOL="wireguard"
if echo ${connection_method:0:1} | grep -iq o; then if echo "${connection_method:0:1}" | grep -iq o; then
echo -n "Connection method ([U]dp/[t]cp): " echo -n "Connection method ([U]dp/[t]cp): "
read protocolInput read -r protocolInput
echo echo
protocol="udp" protocol="udp"
if echo ${protocolInput:0:1} | grep -iq t; then if echo "${protocolInput:0:1}" | grep -iq t; then
protocol="tcp" protocol="tcp"
fi fi
echo "Higher levels of encryption trade performance for security. " echo "Higher levels of encryption trade performance for security. "
echo -n "Do you want to use strong encryption ([N]o/[y]es): " echo -n "Do you want to use strong encryption ([N]o/[y]es): "
read strongEncryption read -r strongEncryption
echo echo
encryption="standard" encryption="standard"
if echo ${strongEncryption:0:1} | grep -iq y; then if echo "${strongEncryption:0:1}" | grep -iq y; then
encryption="strong" encryption="strong"
fi fi
@ -375,10 +375,10 @@ if [[ $setDNS = "yes" ]]; then
if [[ ! $PIA_DNS || $PIA_DNS = "" ]]; then if [[ ! $PIA_DNS || $PIA_DNS = "" ]]; then
echo Using third party DNS could allow DNS monitoring. echo Using third party DNS could allow DNS monitoring.
echo -n "Do you want to force PIA DNS ([Y]es/[n]o): " echo -n "Do you want to force PIA DNS ([Y]es/[n]o): "
read setDNS read -r setDNS
echo echo
PIA_DNS="true" PIA_DNS="true"
if echo ${setDNS:0:1} | grep -iq n; then if echo "${setDNS:0:1}" | grep -iq n; then
PIA_DNS="false" PIA_DNS="false"
fi fi
fi fi