Created
February 10, 2022 16:34
-
-
Save darrensapalo/97f3f133e1a44912ad1b33ffcc556c7f to your computer and use it in GitHub Desktop.
Automatically reconnect clunky wireguard on Ubuntu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/local/bin/fish | |
# Returns the HTTP Status Code of the HTTP request, connecting to some public | |
# internet page. | |
function check_online | |
set RESPONSE (curl -m 2 -Is $argv[1] | grep HTTP | cut -d ' ' -f2) | |
if test $RESPONSE = "" | |
echo "200" | |
else | |
echo $RESPONSE | |
end | |
end | |
# Returns the HTTP Status Code of the HTTP request, connecting to some private VPN | |
# Enabled by Wireguard. | |
function check_vpn | |
curl -m 2 -s https://dev.internal.yourapp.com | |
if test $status = "0" | |
echo "200" | |
else | |
echo "400" | |
end | |
end | |
# Main Program | |
while true | |
# Check if already connected; if so, exit. | |
set is_vpn_connected (check_vpn) | |
if test $is_vpn_connected = "200" | |
echo "π Nothing to do; You are already connected to VPN!" | |
exit 0 | |
end | |
# Attempt to start up the WireGuard configuration. | |
echo "β¬οΈ Starting wg0" | |
wg-quick up wg0; | |
sleep 2; | |
# Check if you are online. | |
set is_internet_okay (check_online http://amionline.net) | |
if test $is_internet_okay = "200" | |
echo "π§ Internet OK" | |
else | |
echo "β No internet" | |
# Wait 3 seconds, before resetting and trying again | |
echo "ποΈ Disposing wg0" | |
wg-quick down wg0; | |
sleep 3 | |
continue | |
end | |
sleep 2 | |
# Check if you have access to the VPN | |
set is_vpn_connected (check_vpn) | |
if test $is_vpn_connected = "200" | |
echo "β Successfully connected to VPN!" | |
break | |
else | |
echo "β No VPN connection" | |
# Wait 1 second before resetting and re-trying again | |
sleep 1 | |
echo "ποΈ Disposing wg0" | |
wg-quick down wg0; | |
sleep 1; | |
continue | |
end | |
# End | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Attempting to reconnect on its own
Already connected