Package: wireguard-reconnect
WireGuard ReconnectThis program performs unattended handling of selected WireGuard interfaces, bringing them up and down depending on whether there is connectivity to the server or not, or simply bringing them up and
keeping them up.Works great combined with systemd.Table of contentsInstallationUse caseHow to use itWith systemdPackagingAutopackagePyInstallerContributingLicenseInstallationUse the package managerpiporpipxto install it:sudopipinstallwireguard-reconnectAlternatively, you can use one of the two portable versions provided on the releases page.The lightest one has been packaged usingautopackageand will require you to have Python 3.7+ installed.The heavier one has been packaged usingPyInstallerand has no external dependencies, so it doesn't matter if you don't have Python installed, or if your version is
lower than 3.7.SeePackagingfor more information.Use caseI have two VPNs installed simultaneously on my computers, one with OpenVPN (in TCP mode, devicetun0) and the other with WireGuard (UDP, devicewg0), which allow me to access the same subnets
behind certain computers.Knowing the great advantage in speed and ease of use of WireGuard over OpenVPN, one might wonder what's the point of having OpenVPN installed as well and maintaining this redundant network scheme,
and the answer is very simple: compatibility. Both at my work and on many public Wi-Fi networks, UDP traffic is blocked and the only way I can access the aforementioned subnets is by using OpenVPN.The configuration that I have made of both programs prioritizes in the routing table, for access to said subnets, the use of WireGuard over OpenVPN, giving it a lower metric, so that the link used
is the fastest possible. Something like this:192.168.1.0/24 dev wg0 metric 1000 
192.168.1.0/24 dev tun0 metric 2000 
10.4.0.0/24 dev wg0 metric 1000 
10.4.0.0/24 dev tun0 metric 2000The problem arises when, with UDP traffic blocked, the routing table insists on using WireGuard instead of OpenVPN. It's in those cases where it's necessary, to allow the traffic flow,
that the WireGuard interface disappears from the system. And, if at any point we switch to a Wi-Fi network where WireGuard can work on, we'll want its interface back up.Of course, I could do all of this by hand, bringing up and down the WireGuard interface depending on whether I detect that the Wi-Fi I'm on allows UDP traffic or not (beyond DNS,
obviously), but instead I preferred to make a small Python program that, by the way, had a good integration with systemd.How to use itIf you want to bring up a WireGuard interface calledwg0and just check every 30 seconds (default is 20 seconds) that it's still up, run the following command. You probably want this behaviour
on your WireGuard server, but not on clients.sudo wireguard-reconnect -i wg0 -p 30If instead you want to set up a WireGuard interface calledwg1and check every minute whether the server is reachable (using its WireGuard local IP10.4.0.1) so that,
in case there is no connectivity, thewg1interface is removed from the system, and then try to bring it up back again if the connection to the server can be reestablished,
then run this command:sudo wireguard-reconnect -i wg1 -g 10.4.0.1 -p 60In any case, if the program is stopped by a SIGINT (2) signal (Control+C) or a SIGTERM (15) signal (such as those sent bysystemctl stoporkillcommands), the WireGuard interface will be
removed from the system.There is also a--help/-hoption in the program. Don't forget to read it if you forget something:sudo wireguard-reconnect -hWith systemdIt's strongly advised to manage this program using the provided systemd service template in this repository.You just need to copy the file calledwireguard-reconnect@.serviceto your/etc/systemd/systemfolder and run the following commands to bring up interfacewg0checking connectivity to server10.4.0.1:sudo systemctl daemon-reload
sudo systemctl start wireguard-reconnect@wg0-10.4.0.1.serviceTo make the service to start at system boot, execute:sudo systemctl enable wireguard-reconnect@wg0-10.4.0.1.serviceTo watch the program log:sudo journalctl -u wireguard-reconnect@wg0-10.4.0.1.service -fPackagingIn this section we are going to explain how to replicate the packaging process.AutopackageTo generate the program lightest portable version, which is available in this GitHub repository, install firstautopackagewithpip:pip install autopackageThen run the following commands:autopackage -s setup.py -pPyInstallerTo generate the program heaviest portable version, which is also available in this GitHub repository, installpyinstallerwithpip:pip install pyinstallerThen run:pyinstaller --onefile --bootloader-ignore-signals __main__.pyContributingPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.LicenseThis library is licensed under theGNU Affero General Public License v3 or later (AGPLv3+)