Check more things (#2)

This commits will be reviewed in the next commit, to align coding style with the entire project.

The merge request from @triffid included the following commits:
* connect_to_wireguard_with_token: Check if "jq" and "curl" are available, allow script to issue multiple errors in a single invocation
* connect_to_wireguard_with_token: ensure /etc/wireguard exists
* connect_to_wireguard_with_token: only complain about ipv6 if it's not already disabled
* get_region_and_token: allow specifying maximum latency via environment variable, retain 50ms default
* connect_to_wireguard_with_token: pack tool checks within a function
* get_region_and_token: rename maximum_allowed_latency -> MAX_LATENCY
This commit is contained in:
Michael Moon 2020-09-23 05:17:46 +08:00 committed by GitHub
parent 53d2da47e3
commit aac22fe847
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 16 deletions

View File

@ -23,18 +23,35 @@
# PIA currently does not support IPv6. In order to be sure your VPN
# connection does not leak, it is best to disabled IPv6 altogether.
echo 'You should consider disabling IPv6 by running:
sysctl -w net.ipv6.conf.all.disable_ipv6=1
sysctl -w net.ipv6.conf.default.disable_ipv6=1
'
if [ $(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'
fi
# so we can print more than one error about missing tools in a single run
EXIT=0
function check_tool() {
COMMAND=$1
PACKAGE=$2
if ! command -v $COMMAND &>/dev/null
then
echo "$COMMAND could not be found"
echo "Please install $PACKAGE"
fi
EXIT=1
}
# check if the wireguard tools have been installed
if ! command -v wg-quick &> /dev/null
then
echo "wg-quick could not be found."
echo "Please install wireguard-tools"
exit 1
fi
check_tool wg-quick wireguard-tools
# check if curl has been installed
check_tool curl curl
# check if jq has been installed
check_tool jq jq
# Check if the mandatory environment variables are set.
if [[ ! $WG_SERVER_IP || ! $WG_HOSTNAME || ! $WG_TOKEN ]]; then
@ -51,7 +68,13 @@ if [[ ! $WG_SERVER_IP || ! $WG_HOSTNAME || ! $WG_TOKEN ]]; then
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
EXIT=1
fi
# exit if any required tools are missing
if [ $EXIT -ne 0 ]
then
exit $EXIT
fi
# Create ephemeral wireguard keys, that we don't need to save to disk.
@ -87,6 +110,7 @@ fi
# have resolvconf, which will result in the script failing.
# We will enforce the DNS after the connection gets established.
echo -n "Trying to write /etc/wireguard/pia.conf... "
mkdir -p /etc/wireguard
echo "
[Interface]
Address = $(echo "$wireguard_json" | jq -r '.peer_ip')

View File

@ -23,9 +23,9 @@
# Set this to the maximum allowed latency in seconds.
# All servers that repond slower than this will be ignore.
# The value is currently set to 50 milliseconds.
maximum_allowed_latency=0.05
export maximum_allowed_latency
# The default value is 50 milliseconds.
MAX_LATENCY=${MAX_LATENCY:-0.05}
export MAX_LATENCY
serverlist_url='https://serverlist.piaservers.net/vpninfo/servers/v4'
@ -38,7 +38,7 @@ printServerLatency() {
regionName="$(echo ${@:3} |
sed 's/ false//' | sed 's/true/(geo)/')"
time=$(curl -o /dev/null -s \
--connect-timeout $maximum_allowed_latency \
--connect-timeout $MAX_LATENCY \
--write-out "%{time_connect}" \
http://$serverIP:443)
if [ $? -eq 0 ]; then
@ -66,7 +66,7 @@ echo "OK!"
summarized_region_data="$( echo $all_region_data |
jq -r '.regions[] | .servers.meta[0].ip+" "+.id+" "+.name+" "+(.geo|tostring)' )"
echo Testing regions that respond \
faster than $maximum_allowed_latency seconds:
faster than $MAX_LATENCY seconds:
bestRegion="$(echo "$summarized_region_data" |
xargs -i bash -c 'printServerLatency {}' |
sort | head -1 | awk '{ print $2 }')"