Skip to content

Commit ca301ba

Browse files
authored
Merge pull request #226 from linuxserver/persistent
add persistenkeepalive to server conf to survive host IP changes
2 parents 7b2df94 + 1fc1cd5 commit ca301ba

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ This can be run as a server or a client, based on the parameters used.
8080

8181
## Server Mode
8282

83-
If the environment variable `PEERS` is set to a number or a list of strings separated by comma, the container will run in server mode and the necessary server and peer/client confs will be generated. The peer/client config qr codes will be output in the docker log. They will also be saved in text and png format under `/config/peerX` in case `PEERS` is a variable and an integer or `/config/peer_X` in case a list of names was provided instead of an integer.
83+
If the environment variable `PEERS` is set to a number or a list of strings separated by comma, the container will run in server mode and the necessary server and peer/client confs will be generated. The peer/client config qr codes will be output in the docker log if `LOG_CONFS` is set to `true`. They will also be saved in text and png format under `/config/peerX` in case `PEERS` is a variable and an integer or `/config/peer_X` in case a list of names was provided instead of an integer.
8484

85-
Variables `SERVERURL`, `SERVERPORT`, `INTERNAL_SUBNET` and `PEERDNS` are optional variables used for server mode. Any changes to these environment variables will trigger regeneration of server and peer confs. Peer/client confs will be recreated with existing private/public keys. Delete the peer folders for the keys to be recreated along with the confs.
85+
Variables `SERVERURL`, `SERVERPORT`, `INTERNAL_SUBNET`, `PEERDNS`, `INTERFACE`, `ALLOWEDIPS` and `PERSISTENTKEEPALIVE_PEERS` are optional variables used for server mode. Any changes to these environment variables will trigger regeneration of server and peer confs. Peer/client confs will be recreated with existing private/public keys. Delete the peer folders for the keys to be recreated along with the confs.
8686

8787
To add more peers/clients later on, you increment the `PEERS` environment variable or add more elements to the list and recreate the container.
8888

@@ -159,6 +159,7 @@ services:
159159
- PEERDNS=auto #optional
160160
- INTERNAL_SUBNET=10.13.13.0 #optional
161161
- ALLOWEDIPS=0.0.0.0/0 #optional
162+
- PERSISTENTKEEPALIVE_PEERS= #optional
162163
- LOG_CONFS=true #optional
163164
volumes:
164165
- /path/to/appdata/config:/config
@@ -186,6 +187,7 @@ docker run -d \
186187
-e PEERDNS=auto `#optional` \
187188
-e INTERNAL_SUBNET=10.13.13.0 `#optional` \
188189
-e ALLOWEDIPS=0.0.0.0/0 `#optional` \
190+
-e PERSISTENTKEEPALIVE_PEERS= `#optional` \
189191
-e LOG_CONFS=true `#optional` \
190192
-p 51820:51820/udp \
191193
-v /path/to/appdata/config:/config \
@@ -211,6 +213,7 @@ Container images are configured using parameters passed at runtime (such as thos
211213
| `-e PEERDNS=auto` | DNS server set in peer/client configs (can be set as `8.8.8.8`). Used in server mode. Defaults to `auto`, which uses wireguard docker host's DNS via included CoreDNS forward. |
212214
| `-e INTERNAL_SUBNET=10.13.13.0` | Internal subnet for the wireguard and server and peers (only change if it clashes). Used in server mode. |
213215
| `-e ALLOWEDIPS=0.0.0.0/0` | The IPs/Ranges that the peers will be able to reach using the VPN connection. If not specified the default value is: '0.0.0.0/0, ::0/0' This will cause ALL traffic to route through the VPN, if you want split tunneling, set this to only the IPs you would like to use the tunnel AND the ip of the server's WG ip, such as 10.13.13.1. |
216+
| `-e PERSISTENTKEEPALIVE_PEERS=` | Set to `all` or a list of comma separated peers (ie. `1,4,laptop`) for the wireguard server to send keepalive packets to listed peers every 25 seconds. Useful if server is accessed via domain name and has dynamic IP. Used only in server mode. |
214217
| `-e LOG_CONFS=true` | Generated QR codes will be displayed in the docker log. Set to `false` to skip log output. |
215218
| `-v /config` | Contains all relevant configuration files. |
216219
| `-v /lib/modules` | Maps host's modules folder. Only required if compiling wireguard modules. |
@@ -329,6 +332,7 @@ Once registered you can define the dockerfile to use with `-f Dockerfile.aarch64
329332

330333
## Versions
331334

335+
* **10.01.23:** - Add new var to add `PersistentKeepalive` to server config for select peers to survive server IP changes when domain name is used.
332336
* **26.10.22:** - Better handle unsupported peer names. Improve logging.
333337
* **12.10.22:** - Add Alpine branch. Optimize wg and coredns services.
334338
* **09.10.22:** - Switch back to iptables-legacy due to issues on some hosts.

readme-vars.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ opt_param_env_vars:
5050
- { env_var: "PEERDNS", env_value: "auto", desc: "DNS server set in peer/client configs (can be set as `8.8.8.8`). Used in server mode. Defaults to `auto`, which uses wireguard docker host's DNS via included CoreDNS forward."}
5151
- { env_var: "INTERNAL_SUBNET", env_value: "10.13.13.0", desc: "Internal subnet for the wireguard and server and peers (only change if it clashes). Used in server mode."}
5252
- { env_var: "ALLOWEDIPS", env_value: "0.0.0.0/0", desc: "The IPs/Ranges that the peers will be able to reach using the VPN connection. If not specified the default value is: '0.0.0.0/0, ::0/0' This will cause ALL traffic to route through the VPN, if you want split tunneling, set this to only the IPs you would like to use the tunnel AND the ip of the server's WG ip, such as 10.13.13.1."}
53+
- { env_var: "PERSISTENTKEEPALIVE_PEERS", env_value: "", desc: "Set to `all` or a list of comma separated peers (ie. `1,4,laptop`) for the wireguard server to send keepalive packets to listed peers every 25 seconds. Useful if server is accessed via domain name and has dynamic IP. Used only in server mode."}
5354
- { env_var: "LOG_CONFS", env_value: "true", desc: "Generated QR codes will be displayed in the docker log. Set to `false` to skip log output."}
5455

5556
optional_block_1: false
@@ -70,9 +71,9 @@ app_setup_block: |
7071
7172
## Server Mode
7273
73-
If the environment variable `PEERS` is set to a number or a list of strings separated by comma, the container will run in server mode and the necessary server and peer/client confs will be generated. The peer/client config qr codes will be output in the docker log. They will also be saved in text and png format under `/config/peerX` in case `PEERS` is a variable and an integer or `/config/peer_X` in case a list of names was provided instead of an integer.
74+
If the environment variable `PEERS` is set to a number or a list of strings separated by comma, the container will run in server mode and the necessary server and peer/client confs will be generated. The peer/client config qr codes will be output in the docker log if `LOG_CONFS` is set to `true`. They will also be saved in text and png format under `/config/peerX` in case `PEERS` is a variable and an integer or `/config/peer_X` in case a list of names was provided instead of an integer.
7475
75-
Variables `SERVERURL`, `SERVERPORT`, `INTERNAL_SUBNET` and `PEERDNS` are optional variables used for server mode. Any changes to these environment variables will trigger regeneration of server and peer confs. Peer/client confs will be recreated with existing private/public keys. Delete the peer folders for the keys to be recreated along with the confs.
76+
Variables `SERVERURL`, `SERVERPORT`, `INTERNAL_SUBNET`, `PEERDNS`, `INTERFACE`, `ALLOWEDIPS` and `PERSISTENTKEEPALIVE_PEERS` are optional variables used for server mode. Any changes to these environment variables will trigger regeneration of server and peer confs. Peer/client confs will be recreated with existing private/public keys. Delete the peer folders for the keys to be recreated along with the confs.
7677
7778
To add more peers/clients later on, you increment the `PEERS` environment variable or add more elements to the list and recreate the container.
7879
@@ -126,6 +127,7 @@ app_setup_block: |
126127
127128
# changelog
128129
changelogs:
130+
- { date: "10.01.23:", desc: "Add new var to add `PersistentKeepalive` to server config for select peers to survive server IP changes when domain name is used." }
129131
- { date: "26.10.22:", desc: "Better handle unsupported peer names. Improve logging." }
130132
- { date: "12.10.22:", desc: "Add Alpine branch. Optimize wg and coredns services." }
131133
- { date: "09.10.22:", desc: "Switch back to iptables-legacy due to issues on some hosts." }

root/etc/s6-overlay/s6-rc.d/init-wireguard-confs/run

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,25 @@ DUDE
8787
echo "Adding ${!SERVER_ALLOWEDIPS} to wg0.conf's AllowedIPs for peer ${i}"
8888
cat <<DUDE >> /config/wg0.conf
8989
AllowedIPs = ${CLIENT_IP}/32,${!SERVER_ALLOWEDIPS}
90-
9190
DUDE
92-
else
93-
cat <<DUDE >> /config/wg0.conf
91+
else
92+
cat <<DUDE >> /config/wg0.conf
9493
AllowedIPs = ${CLIENT_IP}/32
94+
DUDE
95+
fi
96+
# add PersistentKeepalive if the peer is specified
97+
if [ -n "${PERSISTENTKEEPALIVE_PEERS_ARRAY}" ] && ([ "${PERSISTENTKEEPALIVE_PEERS_ARRAY[0]}" = "all" ] || printf '%s\0' "${PERSISTENTKEEPALIVE_PEERS_ARRAY[@]}" | grep -Fxqz -- "${i}"); then
98+
cat <<DUDE >> /config/wg0.conf
99+
PersistentKeepalive = 25
100+
101+
DUDE
102+
else
103+
cat <<DUDE >> /config/wg0.conf
95104

96105
DUDE
97106
fi
98107
if [ -z "${LOG_CONFS}" ] || [ "${LOG_CONFS}" = "true" ]; then
99-
echo "PEER ${i} QR code:"
108+
echo "PEER ${i} QR code (conf file is saved under /config/${PEER_ID}):"
100109
qrencode -t ansiutf8 < /config/${PEER_ID}/${PEER_ID}.conf
101110
else
102111
echo "PEER ${i} conf and QR code png saved in /config/${PEER_ID}"
@@ -114,6 +123,7 @@ ORIG_PEERDNS="$PEERDNS"
114123
ORIG_PEERS="$PEERS"
115124
ORIG_INTERFACE="$INTERFACE"
116125
ORIG_ALLOWEDIPS="$ALLOWEDIPS"
126+
ORIG_PERSISTENTKEEPALIVE_PEERS="$PERSISTENTKEEPALIVE_PEERS"
117127
DUDE
118128
}
119129

@@ -125,6 +135,10 @@ if [ -n "$PEERS" ]; then
125135
PEERS_ARRAY=($(echo "$PEERS" | tr ',' ' '))
126136
fi
127137
PEERS_COUNT=$(echo "${#PEERS_ARRAY[@]}")
138+
if [ -n "${PERSISTENTKEEPALIVE_PEERS}" ]; then
139+
echo "**** PersistentKeepalive will be set for: ${PERSISTENTKEEPALIVE_PEERS/,/ } ****"
140+
PERSISTENTKEEPALIVE_PEERS_ARRAY=($(echo "$PERSISTENTKEEPALIVE_PEERS" | tr ',' ' '))
141+
fi
128142
if [ -z "$SERVERURL" ] || [ "$SERVERURL" = "auto" ]; then
129143
SERVERURL=$(curl -s icanhazip.com)
130144
echo "**** SERVERURL var is either not set or is set to \"auto\", setting external IP to auto detected value of $SERVERURL ****"
@@ -152,7 +166,7 @@ if [ -n "$PEERS" ]; then
152166
echo "**** Server mode is selected ****"
153167
[[ -f /config/.donoteditthisfile ]] && \
154168
. /config/.donoteditthisfile
155-
if [ "$SERVERURL" != "$ORIG_SERVERURL" ] || [ "$SERVERPORT" != "$ORIG_SERVERPORT" ] || [ "$PEERDNS" != "$ORIG_PEERDNS" ] || [ "$PEERS" != "$ORIG_PEERS" ] || [ "$INTERFACE" != "$ORIG_INTERFACE" ] || [ "$ALLOWEDIPS" != "$ORIG_ALLOWEDIPS" ]; then
169+
if [ "$SERVERURL" != "$ORIG_SERVERURL" ] || [ "$SERVERPORT" != "$ORIG_SERVERPORT" ] || [ "$PEERDNS" != "$ORIG_PEERDNS" ] || [ "$PEERS" != "$ORIG_PEERS" ] || [ "$INTERFACE" != "$ORIG_INTERFACE" ] || [ "$ALLOWEDIPS" != "$ORIG_ALLOWEDIPS" ] || [ "$PERSISTENTKEEPALIVE_PEERS" != "$ORIG_PERSISTENTKEEPALIVE_PEERS" ]; then
156170
echo "**** Server related environment variables changed, regenerating 1 server and ${PEERS} peer/client confs ****"
157171
generate_confs
158172
save_vars

0 commit comments

Comments
 (0)