Skip to content

Commit

Permalink
support executing script from any path instead of requiring a cd first
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasrangit committed Mar 2, 2022
1 parent 71ff88e commit cc8f34b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
12 changes: 7 additions & 5 deletions connect_to_openvpn_with_token.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# This function allows you to check if the required tools have been installed.
check_tool() {
cmd=$1
Expand Down Expand Up @@ -173,7 +175,7 @@ if [[ $PIA_DNS != "true" ]]; then
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 $DIR/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
Expand Down Expand Up @@ -237,10 +239,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}"
"$DIR/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 $DIR/get_region script with PIA_PF=true will provide a filtered list."
exit 1
fi

Expand All @@ -258,9 +260,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}"
$DIR/port_forwarding.sh${nc}"

PIA_TOKEN=$PIA_TOKEN \
PF_GATEWAY=$gateway_ip \
PF_HOSTNAME=$OVPN_HOSTNAME \
./port_forwarding.sh
$DIR/port_forwarding.sh
10 changes: 6 additions & 4 deletions connect_to_wireguard_with_token.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# This function allows you to check if the required tools have been installed.
check_tool() {
cmd=$1
Expand Down Expand Up @@ -172,10 +174,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}"
"$DIR/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 $DIR/get_region script with PIA_PF=true will provide a filtered list."
exit 1
fi

Expand All @@ -193,9 +195,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}"
$DIR/port_forwarding.sh${nc}"

PIA_TOKEN=$PIA_TOKEN \
PF_GATEWAY=$WG_SERVER_IP \
PF_HOSTNAME=$WG_HOSTNAME \
./port_forwarding.sh
$DIR/port_forwarding.sh
20 changes: 11 additions & 9 deletions get_region.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# This function allows you to check if the required tools have been installed.
check_tool() {
cmd=$1
Expand Down Expand Up @@ -167,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 $DIR/get_region.sh${nc}"
exit 1
else
echo -e "A list of servers and connection details, ordered by latency can be
Expand Down Expand Up @@ -218,10 +220,10 @@ 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 "$ PIA_USER=p0123456 PIA_PASS=xxx $DIR/get_region.sh${nc}"
exit 0
fi
./get_token.sh
$DIR/get_token.sh
PIA_TOKEN=$( awk 'NR == 1' /opt/piavpn-manual/token )
export PIA_TOKEN
rm -f /opt/piavpn-manual/token
Expand All @@ -232,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 $DIR/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 $DIR/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 $DIR/connect_to_wireguard_with_token.sh
rm -f /opt/piavpn-manual/latencyList
exit 0
fi
Expand All @@ -253,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 $DIR/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 " $DIR/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
$DIR/connect_to_openvpn_with_token.sh
rm -f /opt/piavpn-manual/latencyList
exit 0
fi
4 changes: 3 additions & 1 deletion get_token.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# This function allows you to check if the required tools have been installed.
check_tool() {
cmd=$1
Expand Down Expand Up @@ -62,7 +64,7 @@ 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 "$ PIA_USER=p0123456 PIA_PASS=xxx $DIR/get_token.sh"
exit 1
fi

Expand Down
11 changes: 7 additions & 4 deletions run_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
# 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.
set -o xtrace

DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# Check if terminal allows output, if yes, define colors for output
if [[ -t 1 ]]; then
Expand Down Expand Up @@ -98,7 +101,7 @@ while :; do
export PIA_PASS

# Confirm credentials and generate token
./get_token.sh
$DIR/get_token.sh

tokenLocation="/opt/piavpn-manual/token"
# If the script failed to generate an authentication token, the script will exit early.
Expand Down Expand Up @@ -252,7 +255,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 $DIR/get_region.sh

if [[ -s /opt/piavpn-manual/latencyList ]]; then
# Output the ordered list of servers that meet the latency specification $MAX_LATENCY
Expand Down Expand Up @@ -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
echo "Region input is : $PREFERRED_REGION"
export PREFERRED_REGION
VPN_PROTOCOL=no ./get_region.sh
VPN_PROTOCOL=no $DIR/get_region.sh
if [[ $? != 1 ]]; then
break
fi
Expand Down Expand Up @@ -391,4 +394,4 @@ echo -e "${green}PIA_DNS=$PIA_DNS${nc}"
CONNECTION_READY="true"
export CONNECTION_READY

./get_region.sh
$DIR/get_region.sh

0 comments on commit cc8f34b

Please sign in to comment.