support executing script from any path instead of requiring a cd first

This commit is contained in:
Lucas Rangit MAGASWERAN 2022-03-02 16:13:38 +01:00
parent 71ff88e500
commit cc8f34b15e
5 changed files with 34 additions and 23 deletions

View File

@ -19,6 +19,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# 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.
check_tool() { check_tool() {
cmd=$1 cmd=$1
@ -173,7 +175,7 @@ if [[ $PIA_DNS != "true" ]]; then
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 $DIR/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
cp openvpn_config/openvpn_down_dnsoverwrite.sh /opt/piavpn-manual/openvpn_down.sh cp openvpn_config/openvpn_down_dnsoverwrite.sh /opt/piavpn-manual/openvpn_down.sh
@ -237,10 +239,10 @@ if [[ $PIA_PF != "true" ]]; then
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}" "$DIR/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."
echo "Calling the ./get_region script with PIA_PF=true will provide a filtered list." echo "Calling the $DIR/get_region script with PIA_PF=true will provide a filtered list."
exit 1 exit 1
fi fi
@ -258,9 +260,9 @@ echo -e "Starting procedure to enable port forwarding by running the following c
$ ${green}PIA_TOKEN=$PIA_TOKEN \\ $ ${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}" $DIR/port_forwarding.sh${nc}"
PIA_TOKEN=$PIA_TOKEN \ PIA_TOKEN=$PIA_TOKEN \
PF_GATEWAY=$gateway_ip \ PF_GATEWAY=$gateway_ip \
PF_HOSTNAME=$OVPN_HOSTNAME \ PF_HOSTNAME=$OVPN_HOSTNAME \
./port_forwarding.sh $DIR/port_forwarding.sh

View File

@ -19,6 +19,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# 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.
check_tool() { check_tool() {
cmd=$1 cmd=$1
@ -172,10 +174,10 @@ if [[ $PIA_PF != "true" ]]; then
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}" "$DIR/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."
echo "Calling the ./get_region script with PIA_PF=true will provide a filtered list." echo "Calling the $DIR/get_region script with PIA_PF=true will provide a filtered list."
exit 1 exit 1
fi fi
@ -193,9 +195,9 @@ echo -e "Starting procedure to enable port forwarding by running the following c
$ ${green}PIA_TOKEN=$PIA_TOKEN \\ $ ${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}" $DIR/port_forwarding.sh${nc}"
PIA_TOKEN=$PIA_TOKEN \ 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 $DIR/port_forwarding.sh

View File

@ -19,6 +19,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# 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.
check_tool() { check_tool() {
cmd=$1 cmd=$1
@ -167,7 +169,7 @@ if [[ $selectedRegion == "none" ]]; then
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 $DIR/get_region.sh${nc}"
exit 1 exit 1
else else
echo -e "A list of servers and connection details, ordered by latency can be echo -e "A list of servers and connection details, ordered by latency can be
@ -218,10 +220,10 @@ if [[ -z $PIA_TOKEN ]]; then
if [[ -z $PIA_USER || -z $PIA_PASS ]]; then if [[ -z $PIA_USER || -z $PIA_PASS ]]; then
echo -e "${red}If you want this script to automatically get an authentication" 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 "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 $DIR/get_region.sh${nc}"
exit 0 exit 0
fi fi
./get_token.sh $DIR/get_token.sh
PIA_TOKEN=$( awk 'NR == 1' /opt/piavpn-manual/token ) PIA_TOKEN=$( awk 'NR == 1' /opt/piavpn-manual/token )
export PIA_TOKEN export PIA_TOKEN
rm -f /opt/piavpn-manual/token rm -f /opt/piavpn-manual/token
@ -232,15 +234,15 @@ fi
# Connect with WireGuard and clear authentication token file and latencyList # Connect with WireGuard and clear authentication token file and latencyList
if [[ $VPN_PROTOCOL == "wireguard" ]]; then if [[ $VPN_PROTOCOL == "wireguard" ]]; then
echo "The ./get_region.sh script got started with" echo "The $DIR/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 $DIR/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 \
WG_HOSTNAME=$bestServer_WG_hostname ./connect_to_wireguard_with_token.sh WG_HOSTNAME=$bestServer_WG_hostname $DIR/connect_to_wireguard_with_token.sh
rm -f /opt/piavpn-manual/latencyList rm -f /opt/piavpn-manual/latencyList
exit 0 exit 0
fi fi
@ -253,20 +255,20 @@ if [[ $VPN_PROTOCOL == openvpn* ]]; then
serverIP=$bestServer_OT_IP serverIP=$bestServer_OT_IP
serverHostname=$bestServer_OT_hostname serverHostname=$bestServer_OT_hostname
fi fi
echo "The ./get_region.sh script got started with" echo "The $DIR/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 " $DIR/connect_to_openvpn_with_token.sh${nc}"
echo echo
PIA_PF=$PIA_PF PIA_TOKEN=$PIA_TOKEN \ PIA_PF=$PIA_PF PIA_TOKEN=$PIA_TOKEN \
OVPN_SERVER_IP=$serverIP \ OVPN_SERVER_IP=$serverIP \
OVPN_HOSTNAME=$serverHostname \ OVPN_HOSTNAME=$serverHostname \
CONNECTION_SETTINGS=$VPN_PROTOCOL \ CONNECTION_SETTINGS=$VPN_PROTOCOL \
./connect_to_openvpn_with_token.sh $DIR/connect_to_openvpn_with_token.sh
rm -f /opt/piavpn-manual/latencyList rm -f /opt/piavpn-manual/latencyList
exit 0 exit 0
fi fi

View File

@ -19,6 +19,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# 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.
check_tool() { check_tool() {
cmd=$1 cmd=$1
@ -62,7 +64,7 @@ mkdir -p /opt/piavpn-manual
if [[ -z $PIA_USER || -z $PIA_PASS ]]; then if [[ -z $PIA_USER || -z $PIA_PASS ]]; then
echo "If you want this script to automatically get a token from the Meta" 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 "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 $DIR/get_token.sh"
exit 1 exit 1
fi fi

View File

@ -18,6 +18,9 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # 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 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE. # SOFTWARE.
set -o xtrace
DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Check if terminal allows output, if yes, define colors for output # Check if terminal allows output, if yes, define colors for output
if [[ -t 1 ]]; then if [[ -t 1 ]]; then
@ -98,7 +101,7 @@ while :; do
export PIA_PASS export PIA_PASS
# Confirm credentials and generate token # Confirm credentials and generate token
./get_token.sh $DIR/get_token.sh
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.
@ -252,7 +255,7 @@ For example, you can try 0.2 for 200ms allowed latency.
export PREFERRED_REGION export PREFERRED_REGION
VPN_PROTOCOL="no" VPN_PROTOCOL="no"
export VPN_PROTOCOL export VPN_PROTOCOL
VPN_PROTOCOL=no ./get_region.sh VPN_PROTOCOL=no $DIR/get_region.sh
if [[ -s /opt/piavpn-manual/latencyList ]]; then if [[ -s /opt/piavpn-manual/latencyList ]]; then
# 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
@ -308,7 +311,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 # 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 $DIR/get_region.sh
if [[ $? != 1 ]]; then if [[ $? != 1 ]]; then
break break
fi fi
@ -391,4 +394,4 @@ echo -e "${green}PIA_DNS=$PIA_DNS${nc}"
CONNECTION_READY="true" CONNECTION_READY="true"
export CONNECTION_READY export CONNECTION_READY
./get_region.sh $DIR/get_region.sh