Skip to content

Commit 4f6eb60

Browse files
committed
Add templates, add subnet interface var
1 parent 198d427 commit 4f6eb60

File tree

5 files changed

+110
-42
lines changed

5 files changed

+110
-42
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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 \
@@ -101,6 +102,7 @@ services:
101102
- SERVERPORT=51820 #optional
102103
- PEERS=1 #optional
103104
- PEERDNS=8.8.8.8 #optional
105+
- INTERNAL_SUBNET=10.13.13.0 #optional
104106
volumes:
105107
- /path/to/appdata/config:/config
106108
- /lib/modules:/lib/modules
@@ -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,16 +169,18 @@ 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`.
170173

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.
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.
175+
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.
179184

180185

181186

@@ -243,5 +248,6 @@ Once registered you can define the dockerfile to use with `-f Dockerfile.aarch64
243248

244249
## Versions
245250

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

readme-vars.yml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ custom_params:
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: ""
@@ -65,6 +66,7 @@ custom_compose: |
6566
- SERVERPORT=51820 #optional
6667
- PEERS=1 #optional
6768
- PEERDNS=8.8.8.8 #optional
69+
- INTERNAL_SUBNET=10.13.13.0 #optional
6870
volumes:
6971
- /path/to/appdata/config:/config
7072
- /lib/modules:/lib/modules
@@ -84,18 +86,21 @@ app_setup_block: |
8486
This can be run as a server or a client, based on the parameters used.
8587
8688
## 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`.
89+
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`.
8890
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.
91+
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.
92+
93+
To add more peers/clients later on, you can run `docker exec -it wireguard /app/add-peer` while the container is running.
9094
9195
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).
9296
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.
97+
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.
9498
9599
## Client Mode
96-
Drop your client conf into the config folder as `/config/wg0.conf` and start the container.
100+
Do not set the `PEERS` environment variable. Drop your client conf into the config folder as `/config/wg0.conf` and start the container.
97101
98102
# changelog
99103
changelogs:
104+
- { date: "05.04.20:", desc: "Add `INTERNAL_SUBNET` variable to prevent subnet clashes. Add templates for server and peer confs." }
100105
- { date: "01.04.20:", desc: "Add `show-peer` script and include info on host installed headers." }
101106
- { 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/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)