Skip to content

Instantly share code, notes, and snippets.

@darrensapalo
Created February 10, 2022 16:34
Show Gist options
  • Save darrensapalo/97f3f133e1a44912ad1b33ffcc556c7f to your computer and use it in GitHub Desktop.
Save darrensapalo/97f3f133e1a44912ad1b33ffcc556c7f to your computer and use it in GitHub Desktop.
Automatically reconnect clunky wireguard on Ubuntu
#! /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
@darrensapalo
Copy link
Author

Attempting to reconnect on its own

image

Already connected

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment