This 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.
Use the package manager pip or pipx to install it:
sudo pip install wireguard-reconnect
Alternatively, you can use one of the two portable versions provided on the releases page.
- The lightest one has been packaged using autopackage and will require you to have Python 3.7+ installed.
- The heavier one has been packaged using PyInstaller and 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.
See Packaging for more information.
I have two VPNs installed simultaneously on my computers, one with OpenVPN (in TCP mode, device tun0
) and the other with WireGuard (UDP, device wg0
), 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 2000
The 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.
If you want to bring up a WireGuard interface called wg0
and 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 30
If instead you want to set up a WireGuard interface called wg1
and check every minute whether the server is reachable (using its WireGuard local IP 10.4.0.1
) so that,
in case there is no connectivity, the wg1
interface 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 60
In any case, if the program is stopped by a SIGINT (2) signal (Control+C) or a SIGTERM (15) signal (such as those sent by systemctl stop
or kill
commands), the WireGuard interface will be
removed from the system.
There is also a --help/-h
option in the program. Don't forget to read it if you forget something:
sudo wireguard-reconnect -h
It's strongly advised to manage this program using the provided systemd service template in this repository.
You just need to copy the file called wireguard-reconnect@.service
to your /etc/systemd/system
folder and run the following commands to bring up interface wg0
checking connectivity to server 10.4.0.1
:
sudo systemctl daemon-reload
sudo systemctl start wireguard-reconnect@wg0-10.4.0.1.service
To make the service to start at system boot, execute:
sudo systemctl enable wireguard-reconnect@wg0-10.4.0.1.service
To watch the program log:
sudo journalctl -u wireguard-reconnect@wg0-10.4.0.1.service -f
In this section we are going to explain how to replicate the packaging process.
To generate the program lightest portable version, which is available in this GitHub repository, install first autopackage
with pip
:
pip install autopackage
Then run the following commands:
autopackage -s setup.py -p
To generate the program heaviest portable version, which is also available in this GitHub repository, install pyinstaller
with pip
:
pip install pyinstaller
Then run:
pyinstaller --onefile --bootloader-ignore-signals __main__.py
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
This library is licensed under the GNU Affero General Public License v3 or later (AGPLv3+)