Skip to content

Commit

Permalink
Add WG_INTERFACE environment variable that defaults to "pia"
Browse files Browse the repository at this point in the history
Allows overriding the default Wireguard interface "pia" with "wg0" for example.

Useful when you want to match the Wireguard default `/etc/wireguard/wg0.conf` (see https://www.wireguard.com/quickstart/).
  • Loading branch information
lucasrangit committed Feb 19, 2022
1 parent 9ee75c4 commit 627409c
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions connect_to_wireguard_with_token.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ if [[ -z $WG_SERVER_IP ||
exit 1
fi

# Check if an Wireguard interface name has been specified (e.g. /etc/wireguard/INTERFACE.conf)
if [[ -z "$WG_INTERFACE" ]]; then
WG_INTERFACE=pia
fi

# Create ephemeral wireguard keys, that we don't need to save to disk.
privKey=$(wg genkey)
export privKey
Expand Down Expand Up @@ -112,7 +117,7 @@ fi
# these scripts. Feel free to fork the project and test it out.
echo
echo "Trying to disable a PIA WG connection in case it exists..."
wg-quick down pia && echo -e "${green}\nPIA WG connection disabled!${nc}"
wg-quick down "$WG_INTERFACE" && echo -e "${green}\nPIA WG connection disabled!${nc}"
echo

# Create the WireGuard config based on the JSON received from the API
Expand All @@ -129,7 +134,7 @@ if [[ $PIA_DNS == "true" ]]; then
echo
dnsSettingForVPN="DNS = $dnsServer"
fi
echo -n "Trying to write /etc/wireguard/pia.conf..."
echo -n "Trying to write /etc/wireguard/$WG_INTERFACE.conf..."
mkdir -p /etc/wireguard
echo "
[Interface]
Expand All @@ -141,24 +146,24 @@ PersistentKeepalive = 25
PublicKey = $(echo "$wireguard_json" | jq -r '.server_key')
AllowedIPs = 0.0.0.0/1, 128.0.0.0/1
Endpoint = ${WG_SERVER_IP}:$(echo "$wireguard_json" | jq -r '.server_port')
" > /etc/wireguard/pia.conf || exit 1
" > "/etc/wireguard/$WG_INTERFACE.conf" || exit 1
echo -e "${green}OK!${nc}"

# Start the WireGuard interface.
# If something failed, stop this script.
# If you get DNS errors because you miss some packages,
# just hardcode /etc/resolv.conf to "nameserver 10.0.0.242".
echo
echo "Trying to create the wireguard interface..."
wg-quick up pia || exit 1
echo Trying to create the wireguard interface...
wg-quick up "$WG_INTERFACE" || exit 1
echo
echo -e "${green}The WireGuard interface got created.${nc}
At this point, internet should work via VPN.
To disconnect the VPN, run:
--> ${green}wg-quick down pia${nc} <--
--> ${green}wg-quick down ${WG_INTERFACE}${nc} <--
"

# This section will stop the script if PIA_PF is not set to "true".
Expand Down

0 comments on commit 627409c

Please sign in to comment.