Add parent folder for increased code modularity.

This commit is contained in:
Daniel Kaminski de Souza 2021-02-07 04:56:07 +00:00
parent 2e29ad2d8d
commit 1f250978d7
6 changed files with 38 additions and 32 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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")"

View File

@ -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