Skip to content

Commit

Permalink
unify quoting style for echo
Browse files Browse the repository at this point in the history
  • Loading branch information
a1346054 authored and g00nix committed Feb 19, 2022
1 parent a534a3c commit a5bd853
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 112 deletions.
74 changes: 37 additions & 37 deletions connect_to_openvpn_with_token.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,25 @@ adapter_check=$( ip a s tun06 2>&1 )
should_read="Device \"tun06\" does not exist"
pid_filepath="/opt/piavpn-manual/pia_pid"
if [[ $adapter_check != *"$should_read"* ]]; then
echo -e ${RED}The tun06 adapter already exists, that interface is required
echo -e for this configuration.${NC}
echo -e "${RED}The tun06 adapter already exists, that interface is required"
echo -e "for this configuration.${NC}"
if [[ -f $pid_filepath ]]; then
old_pid=$( cat "$pid_filepath" )
old_pid_name=$( ps -p "$old_pid" -o comm= )
if [[ $old_pid_name == "openvpn" ]]; then
echo
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 you would not be able to get a new 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 "you would not be able to get a new connection."
echo -ne "Do you want to run ${RED}$ kill $old_pid${NC} (Y/n): "
read -r close_connection
fi
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
fi
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"
echo
for i in {5..1}; do
Expand All @@ -92,44 +92,44 @@ if [[ -f /proc/net/if_inet6 ]] &&
[[ $(sysctl -n net.ipv6.conf.all.disable_ipv6) -ne 1 ||
$(sysctl -n net.ipv6.conf.default.disable_ipv6) -ne 1 ]]
then
echo -e ${RED}'You should consider disabling IPv6 by running:'
echo 'sysctl -w net.ipv6.conf.all.disable_ipv6=1'
echo -e 'sysctl -w net.ipv6.conf.default.disable_ipv6=1'${NC}
echo -e "${RED}You should consider disabling IPv6 by running:"
echo "sysctl -w net.ipv6.conf.all.disable_ipv6=1"
echo -e "sysctl -w net.ipv6.conf.default.disable_ipv6=1${NC}"
fi

# Check if the mandatory environment variables are set.
if [[ -z $OVPN_SERVER_IP ||
-z $OVPN_HOSTNAME ||
-z $PIA_TOKEN ||
-z $CONNECTION_SETTINGS ]]; then
echo -e ${RED}'This script requires 4 env vars:'
echo 'PIA_TOKEN - the token used for authentication'
echo 'OVPN_SERVER_IP - IP that you want to connect to'
echo 'OVPN_HOSTNAME - name of the server, required for ssl'
echo 'CONNECTION_SETTINGS - the protocol and encryption specification'
echo ' - available options for CONNECTION_SETTINGS are:'
echo ' * openvpn_udp_standard'
echo ' * openvpn_udp_strong'
echo ' * openvpn_tcp_standard'
echo ' * openvpn_tcp_strong'
echo -e "${RED}This script requires 4 env vars:"
echo "PIA_TOKEN - the token used for authentication"
echo "OVPN_SERVER_IP - IP that you want to connect to"
echo "OVPN_HOSTNAME - name of the server, required for ssl"
echo "CONNECTION_SETTINGS - the protocol and encryption specification"
echo " - available options for CONNECTION_SETTINGS are:"
echo " * openvpn_udp_standard"
echo " * openvpn_udp_strong"
echo " * openvpn_tcp_standard"
echo " * openvpn_tcp_strong"
echo
echo You can also specify optional env vars:
echo "You can also specify optional env vars:"
echo "PIA_PF - enable port forwarding"
echo "PAYLOAD_AND_SIGNATURE - In case you already have a port."
echo
echo An easy solution is to just run get_region_and_token.sh
echo as it will guide you through getting the best server and
echo also a token. Detailed information can be found here:
echo -e https://github.com/pia-foss/manual-connections${NC}
echo "An easy solution is to just run get_region_and_token.sh"
echo "as it will guide you through getting the best server and"
echo "also a token. Detailed information can be found here:"
echo -e "https://github.com/pia-foss/manual-connections${NC}"
exit 1
fi

# Create a credentials file with the login token
echo -n "Trying to write /opt/piavpn-manual/pia.ovpn..."
mkdir -p /opt/piavpn-manual
rm -f /opt/piavpn-manual/credentials /opt/piavpn-manual/route_info
echo "${PIA_TOKEN:0:62}""
""${PIA_TOKEN:62}" > /opt/piavpn-manual/credentials || exit 1
echo "${PIA_TOKEN:0:62}
${PIA_TOKEN:62}" > /opt/piavpn-manual/credentials || exit 1
chmod 600 /opt/piavpn-manual/credentials
echo -e "${GREEN}OK!${NC}"

Expand Down Expand Up @@ -168,9 +168,9 @@ echo "remote $OVPN_SERVER_IP $port $protocol" >> /opt/piavpn-manual/pia.ovpn
if [[ $PIA_DNS != "true" ]]; then
cp openvpn_config/openvpn_up.sh /opt/piavpn-manual/
cp openvpn_config/openvpn_down.sh /opt/piavpn-manual/
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 with the env var PIA_DNS=true. Example:
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 "with the env var PIA_DNS=true. Example:"
echo $ OVPN_SERVER_IP=\""$OVPN_SERVER_IP"\" OVPN_HOSTNAME=\""$OVPN_HOSTNAME"\" \
PIA_TOKEN=\""$PIA_TOKEN"\" CONNECTION_SETTINGS=\""$CONNECTION_SETTINGS"\" \
PIA_PF=true PIA_DNS=true ./connect_to_openvpn_with_token.sh
Expand Down Expand Up @@ -233,14 +233,14 @@ To disconnect the VPN, run:

# This section will stop the script if PIA_PF is not set to "true".
if [[ $PIA_PF != "true" ]]; then
echo If you want to also enable port forwarding, you can start the script:
echo -e $ ${GREEN}PIA_TOKEN="$PIA_TOKEN" \
PF_GATEWAY="$gateway_ip" \
PF_HOSTNAME="$OVPN_HOSTNAME" \
./port_forwarding.sh${NC}
echo "If you want to also enable port forwarding, you can start the script:"
echo -e "$ ${GREEN}PIA_TOKEN=$PIA_TOKEN" \
"PF_GATEWAY=$gateway_ip" \
"PF_HOSTNAME=$OVPN_HOSTNAME" \
"./port_forwarding.sh${NC}"
echo
echo The location used must be port forwarding enabled, or this will fail.
echo Calling the ./get_region script with PIA_PF=true will provide a filtered list.
echo "The location used must be port forwarding enabled, or this will fail."
echo "Calling the ./get_region script with PIA_PF=true will provide a filtered list."
exit 1
fi

Expand Down
46 changes: 23 additions & 23 deletions connect_to_wireguard_with_token.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,28 @@ if [[ -f /proc/net/if_inet6 ]] &&
[[ $(sysctl -n net.ipv6.conf.all.disable_ipv6) -ne 1 ||
$(sysctl -n net.ipv6.conf.default.disable_ipv6) -ne 1 ]]
then
echo 'You should consider disabling IPv6 by running:'
echo 'sysctl -w net.ipv6.conf.all.disable_ipv6=1'
echo 'sysctl -w net.ipv6.conf.default.disable_ipv6=1'
echo "You should consider disabling IPv6 by running:"
echo "sysctl -w net.ipv6.conf.all.disable_ipv6=1"
echo "sysctl -w net.ipv6.conf.default.disable_ipv6=1"
fi

# Check if the mandatory environment variables are set.
if [[ -z $WG_SERVER_IP ||
-z $WG_HOSTNAME ||
-z $PIA_TOKEN ]]; then
echo -e ${RED}This script requires 3 env vars:
echo WG_SERVER_IP - IP that you want to connect to
echo WG_HOSTNAME - name of the server, required for ssl
echo PIA_TOKEN - your authentication token
echo -e "${RED}This script requires 3 env vars:"
echo "WG_SERVER_IP - IP that you want to connect to"
echo "WG_HOSTNAME - name of the server, required for ssl"
echo "PIA_TOKEN - your authentication token"
echo
echo You can also specify optional env vars:
echo "You can also specify optional env vars:"
echo "PIA_PF - enable port forwarding"
echo "PAYLOAD_AND_SIGNATURE - In case you already have a port."
echo
echo An easy solution is to just run get_region_and_token.sh
echo as it will guide you through getting the best server and
echo also a token. Detailed information can be found here:
echo -e https://github.com/pia-foss/manual-connections${NC}
echo "An easy solution is to just run get_region_and_token.sh"
echo "as it will guide you through getting the best server and"
echo "also a token. Detailed information can be found here:"
echo -e "https://github.com/pia-foss/manual-connections${NC}"
exit 1
fi

Expand All @@ -92,7 +92,7 @@ export pubKey
# 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
# 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 \
--connect-to "$WG_HOSTNAME::$WG_SERVER_IP:" \
--cacert "ca.rsa.4096.crt" \
Expand All @@ -111,7 +111,7 @@ fi
# get multi-hop running with both WireGuard and OpenVPN by playing with
# these scripts. Feel free to fork the project and test it out.
echo
echo Trying to disable a PIA WG connection in case it exists...
echo "Trying to disable a PIA WG connection in case it exists..."
wg-quick down pia && echo -e "${GREEN}\nPIA WG connection disabled!${NC}"
echo

Expand Down Expand Up @@ -142,14 +142,14 @@ PublicKey = $(echo "$wireguard_json" | jq -r '.server_key')
AllowedIPs = 0.0.0.0/0
Endpoint = ${WG_SERVER_IP}:$(echo "$wireguard_json" | jq -r '.server_port')
" > /etc/wireguard/pia.conf || exit 1
echo -e ${GREEN}OK!${NC}
echo -e "${GREEN}OK!${NC}"

# Start the WireGuard interface.
# If something failed, stop this script.
# If you get DNS errors because you miss some packages,
# just hardcode /etc/resolv.conf to "nameserver 10.0.0.242".
echo
echo Trying to create the wireguard interface...
echo "Trying to create the wireguard interface..."
wg-quick up pia || exit 1
echo
echo -e "${GREEN}The WireGuard interface got created.${NC}
Expand All @@ -163,14 +163,14 @@ To disconnect the VPN, run:

# This section will stop the script if PIA_PF is not set to "true".
if [[ $PIA_PF != "true" ]]; then
echo If you want to also enable port forwarding, you can start the script:
echo -e $ ${GREEN}PIA_TOKEN="$PIA_TOKEN" \
PF_GATEWAY="$WG_SERVER_IP" \
PF_HOSTNAME="$WG_HOSTNAME" \
./port_forwarding.sh${NC}
echo "If you want to also enable port forwarding, you can start the script:"
echo -e "$ ${GREEN}PIA_TOKEN=$PIA_TOKEN" \
"PF_GATEWAY=$WG_SERVER_IP" \
"PF_HOSTNAME=$WG_HOSTNAME" \
"./port_forwarding.sh${NC}"
echo
echo The location used must be port forwarding enabled, or this will fail.
echo Calling the ./get_region script with PIA_PF=true will provide a filtered list.
echo "The location used must be port forwarding enabled, or this will fail."
echo "Calling the ./get_region script with PIA_PF=true will provide a filtered list."
exit 1
fi

Expand Down
44 changes: 22 additions & 22 deletions get_region.sh
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ if [[ $selectedRegion == "none" ]]; then
# Test one server from each region to get the closest region.
# If port forwarding is enabled, filter out regions that don't support it.
if [[ $PIA_PF == "true" ]]; then
echo Port Forwarding is enabled, non-PF servers excluded.
echo "Port Forwarding is enabled, non-PF servers excluded."
echo
summarized_region_data="$( echo "$all_region_data" |
jq -r '.regions[] | select(.port_forward==true) |
Expand All @@ -158,16 +158,16 @@ if [[ $selectedRegion == "none" ]]; then
.servers.meta[0].ip+" "+.id+" "+.name+" "+(.geo|tostring)' )"
fi
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" |
xargs -I{} bash -c 'printServerLatency {}' |
sort | head -1 | awk '{ print $2 }')"
echo

if [[ -z $selectedRegion ]]; then
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 -e $ MAX_LATENCY=1 ./get_region.sh${NC}
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}"
exit 1
else
echo -e "A list of servers and connection details, ordered by latency can be
Expand All @@ -192,7 +192,7 @@ bestServer_OU_hostname=$(echo "$regionData" | jq -r '.servers.ovpnudp[0].cn')


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
echo " (geolocated region)."
else
Expand All @@ -216,9 +216,9 @@ fi
# If no token exists, the script will check for login credentials to generate one
if [[ -z $PIA_TOKEN ]]; then
if [[ -z $PIA_USER || -z $PIA_PASS ]]; then
echo -e ${RED}If you want this script to automatically get an authentication
echo token, please add the variables PIA_USER and PIA_PASS. Example:
echo -e $ PIA_USER=p0123456 PIA_PASS=xxx ./get_region.sh${NC}
echo -e "${RED}If you want this script to automatically get an authentication"
echo "token, please add the variables PIA_USER and PIA_PASS. Example:"
echo -e "$ PIA_USER=p0123456 PIA_PASS=xxx ./get_region.sh${NC}"
exit 0
fi
./get_token.sh
Expand All @@ -232,12 +232,12 @@ fi

# Connect with WireGuard and clear authentication token file and latencyList
if [[ $VPN_PROTOCOL == "wireguard" ]]; then
echo The ./get_region.sh script got started with
echo -e ${GREEN}VPN_PROTOCOL=wireguard${NC}, so we will automatically connect to WireGuard,
echo by running this command:
echo -e $ ${GREEN}PIA_TOKEN="$PIA_TOKEN" \\
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 "The ./get_region.sh script got started with"
echo -e "${GREEN}VPN_PROTOCOL=wireguard${NC}, so we will automatically connect to WireGuard,"
echo "by running this command:"
echo -e "$ ${GREEN}PIA_TOKEN=$PIA_TOKEN \\"
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
PIA_PF=$PIA_PF PIA_TOKEN=$PIA_TOKEN WG_SERVER_IP=$bestServer_WG_IP \
WG_HOSTNAME=$bestServer_WG_hostname ./connect_to_wireguard_with_token.sh
Expand All @@ -253,14 +253,14 @@ if [[ $VPN_PROTOCOL == openvpn* ]]; then
serverIP=$bestServer_OT_IP
serverHostname=$bestServer_OT_hostname
fi
echo The ./get_region.sh script got started with
echo -e ${GREEN}VPN_PROTOCOL=$VPN_PROTOCOL${NC}, so we will automatically
echo connect to OpenVPN, by running this command:
echo -e $ ${GREEN}PIA_PF=$PIA_PF PIA_TOKEN="$PIA_TOKEN" \\
echo OVPN_SERVER_IP="$serverIP" \\
echo OVPN_HOSTNAME="$serverHostname" \\
echo CONNECTION_SETTINGS=$VPN_PROTOCOL \\
echo -e ./connect_to_openvpn_with_token.sh${NC}
echo "The ./get_region.sh script got started with"
echo -e "${GREEN}VPN_PROTOCOL=$VPN_PROTOCOL${NC}, so we will automatically"
echo "connect to OpenVPN, by running this command:"
echo -e "$ ${GREEN}PIA_PF=$PIA_PF PIA_TOKEN=$PIA_TOKEN \\"
echo " OVPN_SERVER_IP=$serverIP \\"
echo " OVPN_HOSTNAME=$serverHostname \\"
echo " CONNECTION_SETTINGS=$VPN_PROTOCOL \\"
echo -e " ./connect_to_openvpn_with_token.sh${NC}"
echo
PIA_PF=$PIA_PF PIA_TOKEN=$PIA_TOKEN \
OVPN_SERVER_IP=$serverIP \
Expand Down
8 changes: 4 additions & 4 deletions get_token.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ fi
mkdir -p /opt/piavpn-manual

if [[ -z $PIA_USER || -z $PIA_PASS ]]; then
echo If you want this script to automatically get a token from the Meta
echo service, please add the variables PIA_USER and PIA_PASS. Example:
echo $ PIA_USER=p0123456 PIA_PASS=xxx ./get_token.sh
echo "If you want this script to automatically get a token from the Meta"
echo "service, please add the variables PIA_USER and PIA_PASS. Example:"
echo "$ PIA_USER=p0123456 PIA_PASS=xxx ./get_token.sh"
exit 1
fi

Expand All @@ -81,7 +81,7 @@ if [[ $(echo "$generateTokenResponse" | jq -r '.status') != "OK" ]]; then
exit
fi

echo -e ${GREEN}OK!
echo -e "${GREEN}OK!"
echo
token=$(echo "$generateTokenResponse" | jq -r '.token')
tokenExpiration=$(timeout_timestamp)
Expand Down
24 changes: 12 additions & 12 deletions port_forwarding.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ check_tool jq

# Check if the mandatory environment variables are set.
if [[ -z $PF_GATEWAY || -z $PIA_TOKEN || -z $PF_HOSTNAME ]]; then
echo This script requires 3 env vars:
echo PF_GATEWAY - the IP of your gateway
echo PF_HOSTNAME - name of the host used for SSL/TLS certificate verification
echo PIA_TOKEN - the token you use to connect to the vpn services
echo "This script requires 3 env vars:"
echo "PF_GATEWAY - the IP of your gateway"
echo "PF_HOSTNAME - name of the host used for SSL/TLS certificate verification"
echo "PIA_TOKEN - the token you use to connect to the vpn services"
echo
echo An easy solution is to just run get_region_and_token.sh
echo as it will guide you through getting the best server and
echo also a token. Detailed information can be found here:
echo https://github.com/pia-foss/manual-connections
echo "An easy solution is to just run get_region_and_token.sh"
echo "as it will guide you through getting the best server and"
echo "also a token. Detailed information can be found here:"
echo "https://github.com/pia-foss/manual-connections"
exit 1
fi

Expand Down Expand Up @@ -142,12 +142,12 @@ while true; do
# This script will exit in 2 months, since the port will expire.
export bind_port_response
if [[ $(echo "$bind_port_response" | jq -r '.status') != "OK" ]]; then
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.${NC}"
exit 1
fi
echo -e Forwarded port'\t'${GREEN}"$port"${NC}
echo -e Refreshed on'\t'${GREEN}"$(date)"${NC}
echo -e Expires on'\t'${RED}"$(date --date="$expires_at")"${NC}
echo -e Forwarded port'\t'"${GREEN}$port${NC}"
echo -e Refreshed on'\t'"${GREEN}$(date)${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"

# sleep 15 minutes
Expand Down
Loading

0 comments on commit a5bd853

Please sign in to comment.