Skip to content

Commit 05dfef0

Browse files
authored
Merge pull request #134 from linuxserver/site-to-site
Add site to site support, update readme
2 parents 373827d + ae7266b commit 05dfef0

File tree

4 files changed

+189
-140
lines changed

4 files changed

+189
-140
lines changed

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ The architectures supported by this image are:
5858

5959
## Application Setup
6060

61-
This image is designed for Ubuntu and Debian based systems mainly (it works on some others, but ymmv). During container start, it will first check if the wireguard module is already installed and loaded. If not, it will then check if the kernel headers are already installed (in `/usr/src`) and if not, attempt to download the necessary kernel headers from the ubuntu/debian/raspbian repos; then will compile and install the kernel module.
61+
During container start, it will first check if the wireguard module is already installed and loaded. Kernels newer than 5.6 generally have the wireguard module built-in (along with some older custom kernels). However, the module may not be enabled. Make sure it is enabled prior to starting the container.
62+
63+
If the kernel is not built-in, or installed on host, the container will check if the kernel headers are present (in `/usr/src`) and if not, it will attempt to download the necessary kernel headers from the `ubuntu xenial/bionic`, `debian/raspbian buster` repos; then will attempt to compile and install the kernel module. If the kernel headers are not found in either `usr/src` or in the repos mentioned, container will sleep indefinitely as wireguard cannot be installed.
6264

6365
If you're on a debian/ubuntu based host with a custom or downstream distro provided kernel (ie. Pop!_OS), the container won't be able to install the kernel headers from the regular ubuntu and debian repos. In those cases, you can try installing the headers on the host via `sudo apt install linux-headers-$(uname -r)` (if distro version) and then add a volume mapping for `/usr/src:/usr/src`, or if custom built, map the location of the existing headers to allow the container to use host installed headers to build the kernel module (tested successful on Pop!_OS, ymmv).
6466

65-
With regards to arm32/64 devices, Raspberry Pi 2-4 running the [official ubuntu images prior to focal](https://ubuntu.com/download/raspberry-pi) or Raspbian Buster are supported out of the box. For all other devices and OSes, you can try installing the kernel headers on the host, and mapping `/usr/src:/usr/src` and it may just work (no guarantees).
67+
With regards to arm32/64 devices, Raspberry Pi 2-4 running the [official ubuntu images](https://ubuntu.com/download/raspberry-pi) or Raspbian Buster are supported out of the box. For all other devices and OSes, you can try installing the kernel headers on the host, and mapping `/usr/src:/usr/src` and it may just work (no guarantees).
6668

6769
This can be run as a server or a client, based on the parameters used.
6870

@@ -104,6 +106,20 @@ When routing via Wireguard from another container using the `service` option in
104106
PreDown = HOMENET=192.168.0.0/16; HOMENET2=10.0.0.0/8; HOMENET3=172.16.0.0/12; ip route del $HOMENET3 via $DROUTE;ip route del $HOMENET2 via $DROUTE; ip route del $HOMENET via $DROUTE; iptables -D OUTPUT ! -o %i -m mark ! --mark $(wg show %i fwmark) -m addrtype ! --dst-type LOCAL -j REJECT; iptables -D OUTPUT -d $HOMENET -j ACCEPT; iptables -D OUTPUT -d $HOMENET2 -j ACCEPT; iptables -D OUTPUT -d $HOMENET3 -j ACCEPT
105107
```
106108

109+
## Site-to-site VPN
110+
111+
** Note: This is not a supported configuration by Linuxserver.io - use at your own risk.
112+
113+
Site-to-site VPN in server mode requires customizing the `AllowedIPs` statement for a specific peer in `wg0.conf`. Since `wg0.conf` is autogenerated when server vars are changed, it is not recommended to edit it manually.
114+
115+
In order to customize the `AllowedIPs` statement for a specific peer in `wg0.conf`, you can set an env var `SERVER_ALLOWEDIPS_PEER_<peer name or number>` to the additional subnets you'd like to add, comma separated and excluding the peer IP (ie. `"192.168.1.0/24,192.168.2.0/24"`). Replace `<peer name or number>` with either the name or number of a peer (whichever is used in the `PEERS` var).
116+
117+
For instance `SERVER_ALLOWEDIPS_PEER_laptop="192.168.1.0/24,192.168.2.0/24"` will result in the wg0.conf entry `AllowedIPs = 10.13.13.2,192.168.1.0/24,192.168.2.0/24` for the peer named `laptop`.
118+
119+
Keep in mind that this var will only be considered when the confs are regenerated. Adding this var for an existing peer won't force a regeneration. You can delete wg0.conf and restart the container to force regeneration if necessary.
120+
121+
Don't forget to set the necessary POSTUP and POSTDOWN rules in your client's peer conf for lan access.
122+
107123
## Usage
108124

109125
Here are some example snippets to help you get started creating a container.
@@ -176,7 +192,7 @@ Container images are configured using parameters passed at runtime (such as thos
176192
| `-e TZ=Europe/London` | Specify a timezone to use EG Europe/London |
177193
| `-e SERVERURL=wireguard.domain.com` | External IP or domain name for docker host. Used in server mode. If set to `auto`, the container will try to determine and set the external IP automatically |
178194
| `-e SERVERPORT=51820` | External port for docker host. Used in server mode. |
179-
| `-e PEERS=1` | Number of peers to create confs for. Required for server mode. Can be a list of names too: myPC,myPhone,myTablet... |
195+
| `-e PEERS=1` | Number of peers to create confs for. Required for server mode. Can also be a list of names: `myPC,myPhone,myTablet` (alphanumeric only) |
180196
| `-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. |
181197
| `-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. |
182198
| `-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. |
@@ -293,6 +309,7 @@ Once registered you can define the dockerfile to use with `-f Dockerfile.aarch64
293309

294310
## Versions
295311

312+
* **28.10.21:** - Add site-to-site vpn support.
296313
* **11.02.21:** - Fix bug related to changing internal subnet and named peer confs not updating.
297314
* **06.10.20:** - Disable CoreDNS in client mode, or if port 53 is already in use in server mode.
298315
* **04.10.20:** - Allow to specify a list of names as PEERS and add ALLOWEDIPS environment variable. Also, add peer name/id to each one of the peer sections in wg0.conf. Important: Existing users need to delete `/config/templates/peer.conf` and restart

readme-vars.yml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ opt_param_usage_include_env: true
4444
opt_param_env_vars:
4545
- { env_var: "SERVERURL", env_value: "wireguard.domain.com", desc: "External IP or domain name for docker host. Used in server mode. If set to `auto`, the container will try to determine and set the external IP automatically"}
4646
- { env_var: "SERVERPORT", env_value: "51820", desc: "External port for docker host. Used in server mode."}
47-
- { env_var: "PEERS", env_value: "1", desc: "Number of peers to create confs for. Required for server mode. Can be a list of names too: myPC,myPhone,myTablet..."}
47+
- { env_var: "PEERS", env_value: "1", desc: "Number of peers to create confs for. Required for server mode. Can also be a list of names: `myPC,myPhone,myTablet` (alphanumeric only)"}
4848
- { 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."}
4949
- { 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."}
5050
- { 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."}
@@ -55,11 +55,13 @@ optional_block_1_items: ""
5555
# application setup block
5656
app_setup_block_enabled: true
5757
app_setup_block: |
58-
This image is designed for Ubuntu and Debian based systems mainly (it works on some others, but ymmv). During container start, it will first check if the wireguard module is already installed and loaded. If not, it will then check if the kernel headers are already installed (in `/usr/src`) and if not, attempt to download the necessary kernel headers from the ubuntu/debian/raspbian repos; then will compile and install the kernel module.
58+
During container start, it will first check if the wireguard module is already installed and loaded. Kernels newer than 5.6 generally have the wireguard module built-in (along with some older custom kernels). However, the module may not be enabled. Make sure it is enabled prior to starting the container.
59+
60+
If the kernel is not built-in, or installed on host, the container will check if the kernel headers are present (in `/usr/src`) and if not, it will attempt to download the necessary kernel headers from the `ubuntu xenial/bionic`, `debian/raspbian buster` repos; then will attempt to compile and install the kernel module. If the kernel headers are not found in either `usr/src` or in the repos mentioned, container will sleep indefinitely as wireguard cannot be installed.
5961
6062
If you're on a debian/ubuntu based host with a custom or downstream distro provided kernel (ie. Pop!_OS), the container won't be able to install the kernel headers from the regular ubuntu and debian repos. In those cases, you can try installing the headers on the host via `sudo apt install linux-headers-$(uname -r)` (if distro version) and then add a volume mapping for `/usr/src:/usr/src`, or if custom built, map the location of the existing headers to allow the container to use host installed headers to build the kernel module (tested successful on Pop!_OS, ymmv).
6163
62-
With regards to arm32/64 devices, Raspberry Pi 2-4 running the [official ubuntu images prior to focal](https://ubuntu.com/download/raspberry-pi) or Raspbian Buster are supported out of the box. For all other devices and OSes, you can try installing the kernel headers on the host, and mapping `/usr/src:/usr/src` and it may just work (no guarantees).
64+
With regards to arm32/64 devices, Raspberry Pi 2-4 running the [official ubuntu images](https://ubuntu.com/download/raspberry-pi) or Raspbian Buster are supported out of the box. For all other devices and OSes, you can try installing the kernel headers on the host, and mapping `/usr/src:/usr/src` and it may just work (no guarantees).
6365
6466
This can be run as a server or a client, based on the parameters used.
6567
@@ -100,9 +102,25 @@ app_setup_block: |
100102
PostUp = DROUTE=$(ip route | grep default | awk '{print $3}'); HOMENET=192.168.0.0/16; HOMENET2=10.0.0.0/8; HOMENET3=172.16.0.0/12; ip route add $HOMENET3 via $DROUTE;ip route add $HOMENET2 via $DROUTE; ip route add $HOMENET via $DROUTE;iptables -I OUTPUT -d $HOMENET -j ACCEPT;iptables -A OUTPUT -d $HOMENET2 -j ACCEPT; iptables -A OUTPUT -d $HOMENET3 -j ACCEPT; iptables -A OUTPUT ! -o %i -m mark ! --mark $(wg show %i fwmark) -m addrtype ! --dst-type LOCAL -j REJECT
101103
PreDown = HOMENET=192.168.0.0/16; HOMENET2=10.0.0.0/8; HOMENET3=172.16.0.0/12; ip route del $HOMENET3 via $DROUTE;ip route del $HOMENET2 via $DROUTE; ip route del $HOMENET via $DROUTE; iptables -D OUTPUT ! -o %i -m mark ! --mark $(wg show %i fwmark) -m addrtype ! --dst-type LOCAL -j REJECT; iptables -D OUTPUT -d $HOMENET -j ACCEPT; iptables -D OUTPUT -d $HOMENET2 -j ACCEPT; iptables -D OUTPUT -d $HOMENET3 -j ACCEPT
102104
```
105+
106+
## Site-to-site VPN
107+
108+
** Note: This is not a supported configuration by Linuxserver.io - use at your own risk.
109+
110+
Site-to-site VPN in server mode requires customizing the `AllowedIPs` statement for a specific peer in `wg0.conf`. Since `wg0.conf` is autogenerated when server vars are changed, it is not recommended to edit it manually.
111+
112+
In order to customize the `AllowedIPs` statement for a specific peer in `wg0.conf`, you can set an env var `SERVER_ALLOWEDIPS_PEER_<peer name or number>` to the additional subnets you'd like to add, comma separated and excluding the peer IP (ie. `"192.168.1.0/24,192.168.2.0/24"`). Replace `<peer name or number>` with either the name or number of a peer (whichever is used in the `PEERS` var).
113+
114+
For instance `SERVER_ALLOWEDIPS_PEER_laptop="192.168.1.0/24,192.168.2.0/24"` will result in the wg0.conf entry `AllowedIPs = 10.13.13.2,192.168.1.0/24,192.168.2.0/24` for the peer named `laptop`.
103115
116+
Keep in mind that this var will only be considered when the confs are regenerated. Adding this var for an existing peer won't force a regeneration. You can delete wg0.conf and restart the container to force regeneration if necessary.
117+
118+
Don't forget to set the necessary POSTUP and POSTDOWN rules in your client's peer conf for lan access.
119+
120+
104121
# changelog
105122
changelogs:
123+
- { date: "28.10.21:", desc: "Add site-to-site vpn support." }
106124
- { date: "11.02.21:", desc: "Fix bug related to changing internal subnet and named peer confs not updating." }
107125
- { date: "06.10.20:", desc: "Disable CoreDNS in client mode, or if port 53 is already in use in server mode." }
108126
- { date: "04.10.20:", desc: "Allow to specify a list of names as PEERS and add ALLOWEDIPS environment variable. Also, add peer name/id to each one of the peer sections in wg0.conf. Important: Existing users need to delete `/config/templates/peer.conf` and restart" }

root/etc/cont-init.d/30-config renamed to root/etc/cont-init.d/30-module

Lines changed: 0 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -151,137 +151,3 @@ DUDE
151151
fi
152152
fi
153153

154-
# prepare symlinks
155-
rm -rf /etc/wireguard
156-
mkdir -p /etc/wireguard
157-
ln -s /config/wg0.conf /etc/wireguard/wg0.conf
158-
# prepare templates
159-
[[ ! -f /config/templates/server.conf ]] && \
160-
cp /defaults/server.conf /config/templates/server.conf
161-
[[ ! -f /config/templates/peer.conf ]] && \
162-
cp /defaults/peer.conf /config/templates/peer.conf
163-
164-
generate_confs () {
165-
mkdir -p /config/server
166-
if [ ! -f /config/server/privatekey-server ]; then
167-
umask 077
168-
wg genkey | tee /config/server/privatekey-server | wg pubkey > /config/server/publickey-server
169-
fi
170-
eval "`printf %s`
171-
cat <<DUDE > /config/wg0.conf
172-
`cat /config/templates/server.conf`
173-
174-
DUDE"
175-
for i in ${PEERS_ARRAY[@]}; do
176-
if [[ "${i}" =~ ^[0-9]+$ ]]; then
177-
PEER_ID="peer${i}"
178-
else
179-
PEER_ID="peer_${i//[^[:alnum:]_-]/}"
180-
fi
181-
mkdir -p /config/${PEER_ID}
182-
if [ ! -f "/config/${PEER_ID}/privatekey-${PEER_ID}" ]; then
183-
umask 077
184-
wg genkey | tee /config/${PEER_ID}/privatekey-${PEER_ID} | wg pubkey > /config/${PEER_ID}/publickey-${PEER_ID}
185-
fi
186-
if [ -f "/config/${PEER_ID}/${PEER_ID}.conf" ]; then
187-
CLIENT_IP=$(cat /config/${PEER_ID}/${PEER_ID}.conf | grep "Address" | awk '{print $NF}')
188-
if [ -n "${ORIG_INTERFACE}" ] && [ "${INTERFACE}" != "${ORIG_INTERFACE}" ]; then
189-
CLIENT_IP=$(echo "${CLIENT_IP}" | sed "s|${ORIG_INTERFACE}|${INTERFACE}|")
190-
fi
191-
else
192-
for idx in {2..254}; do
193-
PROPOSED_IP="${INTERFACE}.${idx}"
194-
if ! grep -q -R "${PROPOSED_IP}" /config/peer*/*.conf && ([ -z "${ORIG_INTERFACE}" ] || ! grep -q -R "${ORIG_INTERFACE}.${idx}" /config/peer*/*.conf); then
195-
CLIENT_IP="${PROPOSED_IP}"
196-
break
197-
fi
198-
done
199-
fi
200-
eval "`printf %s`
201-
cat <<DUDE > /config/${PEER_ID}/${PEER_ID}.conf
202-
`cat /config/templates/peer.conf`
203-
DUDE"
204-
cat <<DUDE >> /config/wg0.conf
205-
[Peer]
206-
# ${PEER_ID}
207-
PublicKey = $(cat /config/${PEER_ID}/publickey-${PEER_ID})
208-
AllowedIPs = ${CLIENT_IP}/32
209-
210-
DUDE
211-
echo "PEER ${i} QR code:"
212-
qrencode -t ansiutf8 < /config/${PEER_ID}/${PEER_ID}.conf
213-
qrencode -o /config/${PEER_ID}/${PEER_ID}.png < /config/${PEER_ID}/${PEER_ID}.conf
214-
done
215-
}
216-
217-
save_vars () {
218-
cat <<DUDE > /config/.donoteditthisfile
219-
ORIG_SERVERURL="$SERVERURL"
220-
ORIG_SERVERPORT="$SERVERPORT"
221-
ORIG_PEERDNS="$PEERDNS"
222-
ORIG_PEERS="$PEERS"
223-
ORIG_INTERFACE="$INTERFACE"
224-
ORIG_ALLOWEDIPS="$ALLOWEDIPS"
225-
DUDE
226-
}
227-
228-
if [ -n "$PEERS" ]; then
229-
echo "**** Server mode is selected ****"
230-
if [[ "$PEERS" =~ ^[0-9]+$ ]] && ! [[ "$PEERS" =~ *,* ]]; then
231-
PEERS_ARRAY=($(seq 1 $PEERS))
232-
else
233-
PEERS_ARRAY=($(echo "$PEERS" | tr ',' ' '))
234-
fi
235-
PEERS_COUNT=$(echo "${#PEERS_ARRAY[@]}")
236-
if [ -z "$SERVERURL" ] || [ "$SERVERURL" = "auto" ]; then
237-
SERVERURL=$(curl -s icanhazip.com)
238-
echo "**** SERVERURL var is either not set or is set to \"auto\", setting external IP to auto detected value of $SERVERURL ****"
239-
else
240-
echo "**** External server address is set to $SERVERURL ****"
241-
fi
242-
SERVERPORT=${SERVERPORT:-51820}
243-
echo "**** External server port is set to ${SERVERPORT}. Make sure that port is properly forwarded to port 51820 inside this container ****"
244-
INTERNAL_SUBNET=${INTERNAL_SUBNET:-10.13.13.0}
245-
echo "**** Internal subnet is set to $INTERNAL_SUBNET ****"
246-
INTERFACE=$(echo "$INTERNAL_SUBNET" | awk 'BEGIN{FS=OFS="."} NF--')
247-
ALLOWEDIPS=${ALLOWEDIPS:-0.0.0.0/0, ::/0}
248-
echo "**** AllowedIPs for peers $ALLOWEDIPS ****"
249-
if [ -z "$PEERDNS" ] || [ "$PEERDNS" = "auto" ]; then
250-
PEERDNS="${INTERFACE}.1"
251-
echo "**** PEERDNS var is either not set or is set to \"auto\", setting peer DNS to ${INTERFACE}.1 to use wireguard docker host's DNS. ****"
252-
else
253-
echo "**** Peer DNS servers will be set to $PEERDNS ****"
254-
fi
255-
if [ ! -f /config/wg0.conf ]; then
256-
echo "**** No wg0.conf found (maybe an initial install), generating 1 server and ${PEERS} peer/client confs ****"
257-
generate_confs
258-
save_vars
259-
else
260-
echo "**** Server mode is selected ****"
261-
[[ -f /config/.donoteditthisfile ]] && \
262-
. /config/.donoteditthisfile
263-
if [ "$SERVERURL" != "$ORIG_SERVERURL" ] || [ "$SERVERPORT" != "$ORIG_SERVERPORT" ] || [ "$PEERDNS" != "$ORIG_PEERDNS" ] || [ "$PEERS" != "$ORIG_PEERS" ] || [ "$INTERFACE" != "$ORIG_INTERFACE" ] || [ "$ALLOWEDIPS" != "$ORIG_ALLOWEDIPS" ]; then
264-
echo "**** Server related environment variables changed, regenerating 1 server and ${PEERS} peer/client confs ****"
265-
generate_confs
266-
save_vars
267-
else
268-
echo "**** No changes to parameters. Existing configs are used. ****"
269-
fi
270-
fi
271-
else
272-
echo "**** Client mode selected. ****"
273-
if [ ! -f /config/wg0.conf ]; then
274-
echo "**** No client conf found. Provide your own client conf as \"/config/wg0.conf\" and restart the container. ****"
275-
sleep infinity
276-
fi
277-
echo "**** Disabling CoreDNS ****"
278-
rm -rf /etc/services.d/coredns
279-
fi
280-
281-
# set up CoreDNS
282-
[[ ! -f /config/coredns/Corefile ]] && \
283-
cp /defaults/Corefile /config/coredns/Corefile
284-
285-
# permissions
286-
chown -R abc:abc \
287-
/config

0 commit comments

Comments
 (0)