From 1f250978d7eccb5ef5bc275402887f124840cce3 Mon Sep 17 00:00:00 2001 From: Daniel Kaminski de Souza Date: Sun, 7 Feb 2021 04:56:07 +0000 Subject: [PATCH] :heavy_plus_sign: Add parent folder for increased code modularity. --- connect_to_openvpn_with_token.sh | 24 +++++++++++++----------- connect_to_wireguard_with_token.sh | 11 ++++++----- get_region.sh | 19 ++++++++++--------- get_token.sh | 2 +- port_forwarding.sh | 5 +++-- run_setup.sh | 9 +++++---- 6 files changed, 38 insertions(+), 32 deletions(-) diff --git a/connect_to_openvpn_with_token.sh b/connect_to_openvpn_with_token.sh index f1eb570..f576df7 100755 --- a/connect_to_openvpn_with_token.sh +++ b/connect_to_openvpn_with_token.sh @@ -18,6 +18,7 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +parent_folder=$(dirname $(realpath $0)) # This function allows you to check if the required tools have been installed. function check_tool() { @@ -141,9 +142,10 @@ IFS=' ' protocol="${connection_settings[1]}" encryption="${connection_settings[2]}" -prefix_filepath="openvpn_config/standard.ovpn" +open_vpn_config_path=$parent_folder/openvpn_config +prefix_filepath="$open_vpn_config_path/standard.ovpn" if [[ $encryption == "strong" ]]; then - prefix_filepath="openvpn_config/strong.ovpn" + prefix_filepath="$open_vpn_config_path/strong.ovpn" fi if [[ $protocol == "udp" ]]; then @@ -167,17 +169,17 @@ echo remote $OVPN_SERVER_IP $port $protocol >> /opt/piavpn-manual/pia.ovpn # Copy the up/down scripts to /opt/piavpn-manual/ # based upon use of PIA DNS if [ "$PIA_DNS" != true ]; then - cp openvpn_config/openvpn_up.sh /opt/piavpn-manual/ - cp openvpn_config/openvpn_down.sh /opt/piavpn-manual/ + cp $open_vpn_config_path/openvpn_up.sh /opt/piavpn-manual/ + cp $open_vpn_config_path/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 $ 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 + PIA_PF=true PIA_DNS=true $parent_folder/connect_to_openvpn_with_token.sh else - cp openvpn_config/openvpn_up_dnsoverwrite.sh /opt/piavpn-manual/openvpn_up.sh - cp openvpn_config/openvpn_down_dnsoverwrite.sh /opt/piavpn-manual/openvpn_down.sh + cp $openvpn_config_path/openvpn_up_dnsoverwrite.sh /opt/piavpn-manual/openvpn_up.sh + cp $openvpn_config_path/openvpn_down_dnsoverwrite.sh /opt/piavpn-manual/openvpn_down.sh fi # Start the OpenVPN interface. @@ -239,10 +241,10 @@ if [ "$PIA_PF" != true ]; then echo -e $ ${GREEN}PIA_TOKEN=$PIA_TOKEN \ PF_GATEWAY=$gateway_ip \ PF_HOSTNAME=$OVPN_HOSTNAME \ - ./port_forwarding.sh${NC} + $parent_folder/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 Calling the $parent_folder/get_region script with PIA_PF=true will provide a filtered list. exit 1 fi @@ -260,9 +262,9 @@ echo -e "Starting procedure to enable port forwarding by running the following c $ ${GREEN}PIA_TOKEN=$PIA_TOKEN \\ PF_GATEWAY=$gateway_ip \\ PF_HOSTNAME=$OVPN_HOSTNAME \\ - ./port_forwarding.sh${NC}" + $parent_folder/port_forwarding.sh${NC}" PIA_TOKEN=$PIA_TOKEN \ PF_GATEWAY=$gateway_ip \ PF_HOSTNAME=$OVPN_HOSTNAME \ - ./port_forwarding.sh + $parent_folder/port_forwarding.sh diff --git a/connect_to_wireguard_with_token.sh b/connect_to_wireguard_with_token.sh index bda0ce1..fc42d2b 100755 --- a/connect_to_wireguard_with_token.sh +++ b/connect_to_wireguard_with_token.sh @@ -18,6 +18,7 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +parent_folder=$(dirname $(realpath $0)) # This function allows you to check if the required tools have been installed. function check_tool() { @@ -94,7 +95,7 @@ export pubKey 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" \ + --cacert "$parent_folder/ca.rsa.4096.crt" \ --data-urlencode "pt=${PIA_TOKEN}" \ --data-urlencode "pubkey=$pubKey" \ "https://${WG_HOSTNAME}:1337/addKey" )" @@ -166,10 +167,10 @@ if [ "$PIA_PF" != true ]; then echo -e $ ${GREEN}PIA_TOKEN=$PIA_TOKEN \ PF_GATEWAY=$WG_SERVER_IP \ PF_HOSTNAME=$WG_HOSTNAME \ - ./port_forwarding.sh${NC} + $parent_folder/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 Calling the $parent_folder/get_region script with PIA_PF=true will provide a filtered list. exit 1 fi @@ -187,9 +188,9 @@ echo -e "Starting procedure to enable port forwarding by running the following c $ ${GREEN}PIA_TOKEN=$PIA_TOKEN \\ PF_GATEWAY=$WG_SERVER_IP \\ PF_HOSTNAME=$WG_HOSTNAME \\ - ./port_forwarding.sh${NC}" + $parent_folder/port_forwarding.sh${NC}" PIA_TOKEN=$PIA_TOKEN \ PF_GATEWAY=$WG_SERVER_IP \ PF_HOSTNAME=$WG_HOSTNAME \ - ./port_forwarding.sh + $parent_folder/port_forwarding.sh diff --git a/get_region.sh b/get_region.sh index a59460d..060d68a 100755 --- a/get_region.sh +++ b/get_region.sh @@ -18,6 +18,7 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +parent_folder=$(dirname $(realpath $0)) # This function allows you to check if the required tools have been installed. function check_tool() { @@ -168,7 +169,7 @@ if [[ $selectedRegion == "none" ]]; then 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 -e $ MAX_LATENCY=1 $parent_folder/get_region.sh${NC} exit 1 else echo -e "A list of servers and connection details, ordered by latency can be @@ -219,10 +220,10 @@ if [[ -z "$PIA_TOKEN" ]] || [[ $PIA_TOKEN == "" ]]; then if [[ ! $PIA_USER || ! $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 $ PIA_USER=p0123456 PIA_PASS=xxx $parent_folder/get_region.sh${NC} exit 0 fi - ./get_token.sh + $parent_folder/get_token.sh PIA_TOKEN=$( awk 'NR == 1' /opt/piavpn-manual/token ) export PIA_TOKEN rm -f /opt/piavpn-manual/token @@ -233,15 +234,15 @@ 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 The $parent_folder/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 -e PIA_PF=$PIA_PF $parent_folder/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 + WG_HOSTNAME=$bestServer_WG_hostname $parent_folder/connect_to_wireguard_with_token.sh rm -f /opt/piavpn-manual/latencyList exit 0 fi @@ -254,20 +255,20 @@ if [[ $VPN_PROTOCOL == openvpn* ]]; then serverIP=$bestServer_OT_IP serverHostname=$bestServer_OT_hostname fi - echo The ./get_region.sh script got started with + echo The $parent_folder/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 -e $parent_folder/connect_to_openvpn_with_token.sh${NC} echo PIA_PF=$PIA_PF PIA_TOKEN=$PIA_TOKEN \ OVPN_SERVER_IP=$serverIP \ OVPN_HOSTNAME=$serverHostname \ CONNECTION_SETTINGS=$VPN_PROTOCOL \ - ./connect_to_openvpn_with_token.sh + $parent_folder/connect_to_openvpn_with_token.sh rm -f /opt/piavpn-manual/latencyList exit 0 fi diff --git a/get_token.sh b/get_token.sh index 6a74935..1fdd18e 100755 --- a/get_token.sh +++ b/get_token.sh @@ -64,7 +64,7 @@ mkdir -p /opt/piavpn-manual if [[ ! $PIA_USER || ! $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 $ PIA_USER=p0123456 PIA_PASS=xxx $parent_folder/get_token.sh exit 1 fi diff --git a/port_forwarding.sh b/port_forwarding.sh index 2e48198..a3de3ce 100755 --- a/port_forwarding.sh +++ b/port_forwarding.sh @@ -18,6 +18,7 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +parent_folder=$(dirname $(realpath $0)) # This function allows you to check if the required tools have been installed. function check_tool() { @@ -86,7 +87,7 @@ if [[ ! $PAYLOAD_AND_SIGNATURE ]]; then echo -n "Getting new signature... " payload_and_signature="$(curl -s -m 5 \ --connect-to "$PF_HOSTNAME::$PF_GATEWAY:" \ - --cacert "ca.rsa.4096.crt" \ + --cacert "$parent_folder/ca.rsa.4096.crt" \ -G --data-urlencode "token=${PIA_TOKEN}" \ "https://${PF_HOSTNAME}:19999/getSignature")" else @@ -133,7 +134,7 @@ Trying to bind the port... " while true; do bind_port_response="$(curl -Gs -m 5 \ --connect-to "$PF_HOSTNAME::$PF_GATEWAY:" \ - --cacert "ca.rsa.4096.crt" \ + --cacert "$parent_folder/ca.rsa.4096.crt" \ --data-urlencode "payload=${payload}" \ --data-urlencode "signature=${signature}" \ "https://${PF_HOSTNAME}:19999/bindPort")" diff --git a/run_setup.sh b/run_setup.sh index dd7a4e9..28a1252 100755 --- a/run_setup.sh +++ b/run_setup.sh @@ -18,6 +18,7 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +parent_folder=$(dirname $(realpath $0)) # Check if terminal allows output, if yes, define colors for output if test -t 1; then @@ -98,7 +99,7 @@ while :; do export PIA_PASS # Confirm credentials and generate token - ./get_token.sh + $parent_folder/get_token.sh tokenLocation="/opt/piavpn-manual/token" # If the script failed to generate an authentication token, the script will exit early. @@ -252,7 +253,7 @@ For example, you can try 0.2 for 200ms allowed latency. export PREFERRED_REGION VPN_PROTOCOL="no" export VPN_PROTOCOL - VPN_PROTOCOL=no ./get_region.sh + VPN_PROTOCOL=no $parent_folder/get_region.sh if [ -s /opt/piavpn-manual/latencyList ]; then # Output the ordered list of servers that meet the latency specification $MAX_LATENCY @@ -308,7 +309,7 @@ For example, you can try 0.2 for 200ms allowed latency. # Validate in-line declaration of PREFERRED_REGION; if invalid remove input to initiate prompts echo Region input is : $PREFERRED_REGION export PREFERRED_REGION - VPN_PROTOCOL=no ./get_region.sh + VPN_PROTOCOL=no $parent_folder/get_region.sh if [[ $? != 1 ]]; then break fi @@ -391,4 +392,4 @@ echo -e "${GREEN}PIA_DNS=$PIA_DNS${NC}" CONNECTION_READY="true" export CONNECTION_READY -./get_region.sh +$parent_folder/get_region.sh