Skip to content

Commit 0c18b6e

Browse files
authored
Merge pull request #3 from linuxserver/templates
Add templates, add subnet interface var
2 parents 198d427 + 4f34f0f commit 0c18b6e

File tree

8 files changed

+121
-71
lines changed

8 files changed

+121
-71
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
open_collective: linuxserver
2+
custom: ["https://www.wireguard.com/donations/",]

README.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![GitHub](https://img.shields.io/static/v1.svg?style=flat-square&color=E68523&label=linuxserver.io&message=GitHub&logo=github&logoColor=FFFFFF)](https://github.com/linuxserver "view the source for all of our repositories.")
88
[![Open Collective](https://img.shields.io/opencollective/all/linuxserver.svg?style=flat-square&color=E68523&label=Supporters&logo=open%20collective&logoColor=FFFFFF)](https://opencollective.com/linuxserver "please consider helping us by either donating or contributing to our budget")
99

10-
The [LinuxServer.io](https://linuxserver.io) team brings you another container release featuring :-
10+
The [LinuxServer.io](https://linuxserver.io) team brings you another container release featuring:
1111

1212
* regular and timely application updates
1313
* easy user mappings (PGID, PUID)
@@ -71,6 +71,7 @@ docker create \
7171
-e SERVERPORT=51820 `#optional` \
7272
-e PEERS=1 `#optional` \
7373
-e PEERDNS=8.8.8.8 `#optional` \
74+
-e INTERNAL_SUBNET=10.13.13.0 `#optional` \
7475
-p 51820:51820/udp \
7576
-v /path/to/appdata/config:/config \
7677
-v /lib/modules:/lib/modules \
@@ -85,6 +86,7 @@ docker create \
8586
Compatible with docker-compose v2 schemas.
8687

8788
```
89+
---
8890
version: "2.1"
8991
services:
9092
wireguard:
@@ -101,6 +103,7 @@ services:
101103
- SERVERPORT=51820 #optional
102104
- PEERS=1 #optional
103105
- PEERDNS=8.8.8.8 #optional
106+
- INTERNAL_SUBNET=10.13.13.0 #optional
104107
volumes:
105108
- /path/to/appdata/config:/config
106109
- /lib/modules:/lib/modules
@@ -109,7 +112,6 @@ services:
109112
sysctls:
110113
- net.ipv4.conf.all.src_valid_mark=1
111114
restart: unless-stopped
112-
113115
```
114116

115117
## Parameters
@@ -122,10 +124,11 @@ Container images are configured using parameters passed at runtime (such as thos
122124
| `-e PUID=1000` | for UserID - see below for explanation |
123125
| `-e PGID=1000` | for GroupID - see below for explanation |
124126
| `-e TZ=Europe/London` | Specify a timezone to use EG Europe/London |
125-
| `-e SERVERURL=wireguard.domain.com` | External IP or domain name for docker host. Required for server mode. |
126-
| `-e SERVERPORT=51820` | External port for docker host. Required for server mode. |
127+
| `-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 |
128+
| `-e SERVERPORT=51820` | External port for docker host. Used in server mode. |
127129
| `-e PEERS=1` | Number of peers to create confs for. Required for server mode. |
128-
| `-e PEERDNS=8.8.8.8` | DNS server set in peer/client configs. |
130+
| `-e PEERDNS=8.8.8.8` | DNS server set in peer/client configs. Used in server mode. |
131+
| `-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. |
129132
| `-v /config` | Contains all relevant configuration files. |
130133
| `-v /lib/modules` | Maps host's modules folder. |
131134
| `--sysctl=` | Required for client mode. |
@@ -166,17 +169,24 @@ If you're on a debian/ubuntu based host with a custom or downstream distro provi
166169
This can be run as a server or a client, based on the parameters used.
167170

168171
## Server Mode
169-
Pass the environment variables `SERVERURL`, `SERVERPORT`, `PEERS` and `PEERDNS`, and the container will generate all necessary confs for both the server and the clients. The client config qr codes will be output in the docker log. They will also be saved in text and png format under `/config/peerX`.
172+
If the environment variable `PEERS` is set to a number, 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`.
173+
174+
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.
170175

171-
If there is an existing `/config/wg0.conf`, the above environment variables won't have any affect. To add more peers/clients later on, you can run `docker exec -it wireguard /app/add-peer` while the container is running.
176+
To add more peers/clients later on, you can run `docker exec -it wireguard /app/add-peer` while the container is running.
172177

173178
To display the QR codes of active peers again, you can use the following command and list the peer numbers as arguments: `docker exec -it wireguard /app/show-peer 1 4 5` (Keep in mind that the QR codes are also stored as PNGs in the config folder).
174179

175-
To recreate all server and client confs, set the above env vars, delete `/config/wg0.conf` and restart the container. Client confs will be recreated with existing private/public keys. Delete the peer folders for the keys to be recreated along with the confs.
180+
The templates used for server and peer confs are saved under `/config/templates`. Advanced users can modify these templates and force conf generation by deleting `/config/wg0.conf` and restarting the container.
176181

177182
## Client Mode
178-
Drop your client conf into the config folder as `/config/wg0.conf` and start the container.
183+
Do not set the `PEERS` environment variable. Drop your client conf into the config folder as `/config/wg0.conf` and start the container.
184+
185+
186+
## Docker Mods
187+
[![Docker Mods](https://img.shields.io/badge/dynamic/yaml?style=for-the-badge&color=E68523&label=mods&query=%24.mods%5B%27wireguard%27%5D.mod_count&url=https%3A%2F%2Fraw.githubusercontent.com%2Flinuxserver%2Fdocker-mods%2Fmaster%2Fmod-list.yml)](https://mods.linuxserver.io/?mod=wireguard "view available mods for this container.")
179188

189+
We publish various [Docker Mods](https://github.com/linuxserver/docker-mods) to enable additional functionality within the containers. The list of Mods available for this image (if any) can be accessed via the dynamic badge above.
180190

181191

182192
## Support Info
@@ -243,5 +253,6 @@ Once registered you can define the dockerfile to use with `-f Dockerfile.aarch64
243253

244254
## Versions
245255

256+
* **05.04.20:** - Add `INTERNAL_SUBNET` variable to prevent subnet clashes. Add templates for server and peer confs.
246257
* **01.04.20:** - Add `show-peer` script and include info on host installed headers.
247258
* **31.03.20:** - Initial Release.

jenkins-vars.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ repo_vars:
2525
- CI_DOCKERENV='TZ=US/Pacific'
2626
- CI_AUTH='user:password'
2727
- CI_WEBPATH=''
28+
sponsor_links:
29+
- { name: "WireGuard", url: "https://www.wireguard.com/donations/" }

readme-vars.yml

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,45 +35,20 @@ cap_add_param_vars:
3535
- { cap_add_var: "NET_ADMIN" }
3636
- { cap_add_var: "SYS_MODULE" }
3737
custom_params:
38-
- { name: "sysctl", name_compose: "sysctls", value: "net.ipv4.conf.all.src_valid_mark=1", desc: "Required for client mode." }
38+
- { name: "sysctl", name_compose: "sysctls", value: ["net.ipv4.conf.all.src_valid_mark=1"], desc: "Required for client mode.", array: "true" }
3939

4040
# optional container parameters
4141
opt_param_usage_include_env: true
4242
opt_param_env_vars:
43-
- { env_var: "SERVERURL", env_value: "wireguard.domain.com", desc: "External IP or domain name for docker host. Required for server mode."}
44-
- { env_var: "SERVERPORT", env_value: "51820", desc: "External port for docker host. Required for server mode."}
43+
- { 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"}
44+
- { env_var: "SERVERPORT", env_value: "51820", desc: "External port for docker host. Used in server mode."}
4545
- { env_var: "PEERS", env_value: "1", desc: "Number of peers to create confs for. Required for server mode."}
46-
- { env_var: "PEERDNS", env_value: "8.8.8.8", desc: "DNS server set in peer/client configs."}
46+
- { env_var: "PEERDNS", env_value: "8.8.8.8", desc: "DNS server set in peer/client configs. Used in server mode."}
47+
- { 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."}
4748

4849
optional_block_1: false
4950
optional_block_1_items: ""
5051

51-
custom_compose: |
52-
version: "2.1"
53-
services:
54-
wireguard:
55-
image: linuxserver/wireguard
56-
container_name: wireguard
57-
cap_add:
58-
- NET_ADMIN
59-
- SYS_MODULE
60-
environment:
61-
- PUID=1000
62-
- PGID=1000
63-
- TZ=Europe/London
64-
- SERVERURL=wireguard.domain.com #optional
65-
- SERVERPORT=51820 #optional
66-
- PEERS=1 #optional
67-
- PEERDNS=8.8.8.8 #optional
68-
volumes:
69-
- /path/to/appdata/config:/config
70-
- /lib/modules:/lib/modules
71-
ports:
72-
- 51820:51820/udp
73-
sysctls:
74-
- net.ipv4.conf.all.src_valid_mark=1
75-
restart: unless-stopped
76-
7752
# application setup block
7853
app_setup_block_enabled: true
7954
app_setup_block: |
@@ -84,18 +59,21 @@ app_setup_block: |
8459
This can be run as a server or a client, based on the parameters used.
8560
8661
## Server Mode
87-
Pass the environment variables `SERVERURL`, `SERVERPORT`, `PEERS` and `PEERDNS`, and the container will generate all necessary confs for both the server and the clients. The client config qr codes will be output in the docker log. They will also be saved in text and png format under `/config/peerX`.
62+
If the environment variable `PEERS` is set to a number, 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`.
63+
64+
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.
8865
89-
If there is an existing `/config/wg0.conf`, the above environment variables won't have any affect. To add more peers/clients later on, you can run `docker exec -it wireguard /app/add-peer` while the container is running.
66+
To add more peers/clients later on, you can run `docker exec -it wireguard /app/add-peer` while the container is running.
9067
9168
To display the QR codes of active peers again, you can use the following command and list the peer numbers as arguments: `docker exec -it wireguard /app/show-peer 1 4 5` (Keep in mind that the QR codes are also stored as PNGs in the config folder).
9269
93-
To recreate all server and client confs, set the above env vars, delete `/config/wg0.conf` and restart the container. Client confs will be recreated with existing private/public keys. Delete the peer folders for the keys to be recreated along with the confs.
70+
The templates used for server and peer confs are saved under `/config/templates`. Advanced users can modify these templates and force conf generation by deleting `/config/wg0.conf` and restarting the container.
9471
9572
## Client Mode
96-
Drop your client conf into the config folder as `/config/wg0.conf` and start the container.
73+
Do not set the `PEERS` environment variable. Drop your client conf into the config folder as `/config/wg0.conf` and start the container.
9774
9875
# changelog
9976
changelogs:
77+
- { date: "05.04.20:", desc: "Add `INTERNAL_SUBNET` variable to prevent subnet clashes. Add templates for server and peer confs." }
10078
- { date: "01.04.20:", desc: "Add `show-peer` script and include info on host installed headers." }
10179
- { date: "31.03.20:", desc: "Initial Release." }

root/defaults/peer.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Interface]
2+
Address = ${INTERFACE}.$(( $i + 1 ))
3+
PrivateKey = $(cat /config/peer${i}/privatekey-peer${i})
4+
ListenPort = 51820
5+
DNS = ${PEERDNS}
6+
7+
[Peer]
8+
PublicKey = $(cat /config/server/publickey-server)
9+
Endpoint = ${SERVERURL}:${SERVERPORT}
10+
AllowedIPs = 0.0.0.0/0, ::/0

root/defaults/server.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[Interface]
2+
Address = ${INTERFACE}.1
3+
ListenPort = 51820
4+
PrivateKey = $(cat /config/server/privatekey-server)
5+
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
6+
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE

root/donate.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
WireGuard: https://www.wireguard.com/donations/

root/etc/cont-init.d/30-config

Lines changed: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/with-contenv bash
22

3-
mkdir -p /config
3+
mkdir -p /config/templates
44

55
# install headers and wireguard
66
apt-get update
@@ -53,59 +53,100 @@ else
5353
wireguard
5454
fi
5555

56+
# prepare symlinks
5657
rm -rf /etc/wireguard
5758
mkdir -p /etc/wireguard
5859
ln -s /config/wg0.conf /etc/wireguard/wg0.conf
60+
# prepare templates
61+
[[ ! -f /config/templates/server.conf ]] && \
62+
cp /defaults/server.conf /config/templates/server.conf
63+
[[ ! -f /config/templates/peer.conf ]] && \
64+
cp /defaults/peer.conf /config/templates/peer.conf
5965

60-
if [ ! -f /config/wg0.conf ] && [ -n "$PEERS" ] && [ -n "$SERVERURL" ]; then
61-
SERVERPORT=${SERVERPORT:-51820}
62-
PEERDNS=${PEERDNS:-8.8.8.8}
66+
generate_confs () {
6367
mkdir -p /config/server
6468
if [ ! -f /config/server/privatekey-server ]; then
6569
umask 077
6670
wg genkey | tee /config/server/privatekey-server | wg pubkey > /config/server/publickey-server
6771
fi
72+
eval "`printf %s`
6873
cat <<DUDE > /config/wg0.conf
69-
[Interface]
70-
Address = 10.13.13.1
71-
ListenPort = 51820
72-
PrivateKey = $(cat /config/server/privatekey-server)
73-
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
74-
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
74+
`cat /config/templates/server.conf`
7575

76-
DUDE
77-
if ! [[ $PEERS =~ ^[0-9]+$ ]]; then
78-
echo "PEERS is not set to an integer, setting it to 1"
79-
PEERS="1"
80-
fi
76+
DUDE"
8177
for i in $(seq 1 $PEERS); do
8278
mkdir -p /config/peer${i}
83-
if [ ! -f /config/peer${i}/privatekey-peer${i} ]; then
79+
if [ ! -f "/config/peer${i}/privatekey-peer${i}" ]; then
8480
umask 077
8581
wg genkey | tee /config/peer${i}/privatekey-peer${i} | wg pubkey > /config/peer${i}/publickey-peer${i}
8682
fi
83+
eval "`printf %s`
8784
cat <<DUDE > /config/peer${i}/peer${i}.conf
88-
[Interface]
89-
Address = 10.13.13.$(( $i + 1 ))
90-
PrivateKey = $(cat /config/peer${i}/privatekey-peer${i})
91-
ListenPort = 51820
92-
DNS = ${PEERDNS}
93-
94-
[Peer]
95-
PublicKey = $(cat /config/server/publickey-server)
96-
Endpoint = ${SERVERURL}:${SERVERPORT}
97-
AllowedIPs = 0.0.0.0/0, ::/0
98-
DUDE
85+
`cat /config/templates/peer.conf`
86+
DUDE"
9987
cat <<DUDE >> /config/wg0.conf
10088
[Peer]
10189
PublicKey = $(cat /config/peer${i}/publickey-peer${i})
102-
AllowedIPs = 10.13.13.$(( $i + 1 ))/32
90+
AllowedIPs = ${INTERFACE}.$(( $i + 1 ))/32
10391

10492
DUDE
10593
echo "PEER ${i} QR code:"
10694
qrencode -t ansiutf8 < /config/peer${i}/peer${i}.conf
10795
qrencode -o /config/peer${i}/peer${i}.png < /config/peer${i}/peer${i}.conf
10896
done
97+
}
98+
99+
save_vars () {
100+
cat <<DUDE > /config/.donoteditthisfile
101+
ORIG_SERVERURL=$SERVERURL
102+
ORIG_SERVERPORT=#SERVER_PORT
103+
ORIG_PEERDNS=$PEERDNS
104+
ORIG_PEERS=$PEERS
105+
ORIG_INTERFACE=$INTERFACE
106+
DUDE
107+
}
108+
109+
if [ -n "$PEERS" ]; then
110+
echo "Server mode is selected"
111+
if ! [[ "$PEERS" =~ ^[0-9]+$ ]]; then
112+
echo "PEERS is not set to an integer, setting it to 1"
113+
PEERS="1"
114+
fi
115+
if [ -z "$SERVERURL" ] || [ "$SERVERURL" = "auto" ]; then
116+
SERVERURL=$(curl icanhazip.com)
117+
echo "SERVERURL var is either not set or is set to \"auto\", setting external IP to auto detected value of $SERVERURL"
118+
else
119+
echo "External server address is set to $SERVERURL"
120+
fi
121+
SERVERPORT=${SERVERPORT:-51820}
122+
echo "External server port is set to ${SERVERPORT}. Make sure that port is properly forwarded to port 51820 inside this container"
123+
PEERDNS=${PEERDNS:-8.8.8.8}
124+
echo "DNS server is set to $PEERDNS"
125+
INTERNAL_SUBNET=${INTERNAL_SUBNET:-10.13.13.0}
126+
echo "Internal subnet is set to $INTERNAL_SUBNET"
127+
INTERFACE=$(echo "$INTERNAL_SUBNET" | awk 'BEGIN{FS=OFS="."} NF--')
128+
if [ ! -f /config/wg0.conf ]; then
129+
echo "No found wg0.conf found (maybe an initial install), generating 1 server and $PEERS peer/client confs"
130+
generate_confs
131+
save_vars
132+
else
133+
echo "Server mode is selected"
134+
[[ -f /config/.donoteditthisfile ]] && \
135+
. /config/.donoteditthisfile
136+
if [ "$SERVERURL" != "$ORIG_SERVERURL" ] || [ "$SERVERPORT" != "$ORIG_SERVERPORT" ] || [ "$PEERDNS" != "$ORIG_PEERDNS" ] || [ "$PEERS" != "$ORIG_PEERS" ] || [ "$INTERFACE" != "$ORIG_INTERFACE" ]; then
137+
echo "Server related environment variables changed, regenerating 1 server and $PEERS peer/client confs"
138+
generate_confs
139+
save_vars
140+
else
141+
echo "No changes to parameters. Existing configs are used."
142+
fi
143+
fi
144+
else
145+
echo "Client mode selected."
146+
if [ !-f /config/wg0.conf ]; then
147+
"No client conf found. Provide your own client conf as \"/config/wg0.conf\" and restart the container."
148+
sleep infinity
149+
fi
109150
fi
110151

111152
# permissions

0 commit comments

Comments
 (0)