├── wg-keepalive.service ├── wg-keepalive.timer ├── wg-keepalive.sh ├── install.sh ├── README.md └── LICENSE /wg-keepalive.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Service for restarting wireguard if necessary 3 | 4 | [Service] 5 | Type=oneshot 6 | ExecStart=/opt/wg-keepalive.sh 7 | User=root 8 | -------------------------------------------------------------------------------- /wg-keepalive.timer: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Timer for wireguard keepalive 3 | 4 | [Timer] 5 | OnCalendar=*:0/5 6 | Persistent=true 7 | 8 | [Install] 9 | WantedBy=timers.target 10 | -------------------------------------------------------------------------------- /wg-keepalive.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | WIREGUARD_ENDPOINT=192.168.33.1 4 | WIREGUARD_INTERFACE=wg0 5 | 6 | set -e 7 | 8 | echo "Checking connection.." 9 | if ! ping -c 1 "$WIREGUARD_ENDPOINT" >/dev/null; then 10 | echo "Connection failed, restarting VPN.." 11 | systemctl restart wg-quick@"$WIREGUARD_INTERFACE".service 12 | echo "VPN restarted" 13 | else 14 | echo "Connection successful, no action needed" 15 | fi 16 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | 5 | echo "Installing keepalive script to /opt/wg-keepalive.sh.." 6 | install -m 700 wg-keepalive.sh /opt/ 7 | echo "..done" 8 | 9 | echo "Installing wg-keepalive.service and wg-keepalive.timer.." 10 | install -m 644 wg-keepalive.service /etc/systemd/system/ 11 | install -m 644 wg-keepalive.timer /etc/systemd/system/ 12 | echo "..done" 13 | 14 | systemctl daemon-reload 15 | 16 | echo "Start and enable wg-keepalive.timer.." 17 | systemctl enable --now wg-keepalive.timer 18 | echo "..done" 19 | 20 | echo 21 | echo "Installation finished, make sure to adjust WIREGUARD_ENDPOINT and WIREGUARD_INTERFACE in /opt/wg-keepalive.sh" 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Wireguard Keepalive 2 | 3 | When using Wireguard with dynDNS endpoints, Wireguard does not automatically 4 | re-resolve the hostname when its IP changes, which causes the connection to 5 | fail when it does. 6 | 7 | This project provides a simple script along with a systemd timer that 8 | periodically checks if the connection is currently working (by sending a ping to 9 | a host in the network) and restarts the wireguard connection if it is not. 10 | 11 | ## Installation 12 | 13 | - Run `./install.sh` as root 14 | - Edit the interface name and ping endpoint in `/opt/wg-keepalive.sh` 15 | - Optional: Adjust check frequency in `/etc/systemd/system/wg-keepalive.timer` 16 | (default: every 5 minutes) 17 | 18 | ## TODOs 19 | 20 | - Provide option to restart VPN based on IP changes of the DNS, not based on 21 | ping 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | --------------------------------------------------------------------------------