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.
Project description
WireGuard Reconnect
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.
Table of contents
Installation
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.
Use case
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.
How to use it
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
With systemd
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
Packaging
In this section we are going to explain how to replicate the packaging process.
Autopackage
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
PyInstaller
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
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
This library is licensed under the GNU Affero General Public License v3 or later (AGPLv3+)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file wireguard_reconnect-1.0.7-py3-none-any.whl
.
File metadata
- Download URL: wireguard_reconnect-1.0.7-py3-none-any.whl
- Upload date:
- Size: 19.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56177a428aefe4a63f8c3f3332764a7b00f3588304b66370184a09cd6e98c289 |
|
MD5 | b959023f61dfd5e264ad2de2c25814f1 |
|
BLAKE2b-256 | f56dccf75a0c14ef3c9087bcc57a6aa274a5065a3975c385638d6fccdfc5199f |