Skip to content

Commit 284f91a

Browse files
authored
Add HOSTNAME global env (#35)
* Add HOSTNAME global env * Add logs
1 parent 6db2cd0 commit 284f91a

3 files changed

Lines changed: 90 additions & 73 deletions

File tree

dappnode_package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@
1616
"type": "git",
1717
"url": "https://github.com/dappnode/DNP_WIREGUARD.git"
1818
},
19-
"license": "GPL-3.0"
19+
"license": "GPL-3.0",
20+
"globalEnvs": [
21+
{
22+
"envs": ["HOSTNAME"],
23+
"services": ["wireguard"]
24+
}
25+
]
2026
}

root/app/add-peer

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,51 +17,56 @@ fi
1717

1818
if [ -z "$SERVERURL" ] || [ "$SERVERURL" = "auto" ]; then
1919

20-
function fetchDappnodeDomain {
21-
for i in {1..10}; do
20+
# If _DAPPNODE_GLOBAL_HOSTNAME is set, use it as SERVERURL
21+
if [ ! -z "$_DAPPNODE_GLOBAL_HOSTNAME" ]; then
22+
echo "**** _DAPPNODE_GLOBAL_HOSTNAME is set, using it as SERVERURL ****"
23+
SERVERURL="$_DAPPNODE_GLOBAL_HOSTNAME"
24+
else
25+
function fetchDappnodeDomain {
26+
for i in {1..10}; do
2227
for domain in "$@"; do
2328
echo "**** Fetching DAppNode domain from $domain..."
2429
SERVERURL=$(curl -s "$domain/global-envs/HOSTNAME")
25-
if [ ! -z "$SERVERURL" ]; then
26-
break
27-
fi
30+
if [ ! -z "$SERVERURL" ]; then
31+
break
32+
fi
2833
sleep 2
2934
done
30-
done
31-
}
35+
done
36+
}
3237

33-
# TODO: Remove IP when the IP migration is performed
34-
domains=("my.dappnode" "dappmanager.dappnode" "172.33.1.7")
38+
domains=("my.dappnode" "dappmanager.dappnode" "172.33.1.7" "10.20.0.7")
3539

36-
fetchDappnodeDomain "${domains[@]}"
40+
fetchDappnodeDomain "${domains[@]}"
41+
fi
3742

3843
fi
3944

4045
for i in {1..254}; do
41-
if grep -q "AllowedIPs = ${INTERFACE}.$(( $i + 1 ))/32" /config/wg0.conf; then
46+
if grep -q "AllowedIPs = ${INTERFACE}.$(($i + 1))/32" /config/wg0.conf; then
4247
echo "Peer $i exists"
4348
else
4449
echo "Adding new Peer $i"
4550
mkdir -p /config/peer${i}
4651
if [ ! -f /config/peer${i}/privatekey-peer${i} ]; then
4752
umask 077
48-
wg genkey | tee /config/peer${i}/privatekey-peer${i} | wg pubkey > /config/peer${i}/publickey-peer${i}
53+
wg genkey | tee /config/peer${i}/privatekey-peer${i} | wg pubkey >/config/peer${i}/publickey-peer${i}
4954
fi
50-
eval "`printf %s`
55+
eval "$(printf %s)
5156
cat <<DUDE > /config/peer${i}/peer${i}.conf
52-
`cat /config/templates/peer.conf`
57+
$(cat /config/templates/peer.conf)
5358
DUDE"
54-
cat <<DUDE >> /config/wg0.conf
59+
cat <<DUDE >>/config/wg0.conf
5560
[Peer]
5661
PublicKey = $(cat /config/peer${i}/publickey-peer${i})
57-
AllowedIPs = ${INTERFACE}.$(( $i + 1 ))/32
62+
AllowedIPs = ${INTERFACE}.$(($i + 1))/32
5863

5964
DUDE
6065
echo "PEER ${i} QR code:"
61-
qrencode -t ansiutf8 < /config/peer${i}/peer${i}.conf
62-
qrencode -o /config/peer${i}/peer${i}.png < /config/peer${i}/peer${i}.conf
66+
qrencode -t ansiutf8 </config/peer${i}/peer${i}.conf
67+
qrencode -o /config/peer${i}/peer${i}.png </config/peer${i}/peer${i}.conf
6368
chown -R abc:abc /config/peer${i}
6469
s6-svc -t /var/run/s6/services/wireguard
6570
break
6671
fi
67-
done
72+
done

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

Lines changed: 59 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ if ip link add dev test type wireguard; then
1111
ip link del dev test
1212
else
1313
echo "**** The wireguard module is not active, will attempt kernel header install and module compilation. If you believe that your kernel should have wireguard support already, make sure that it is activated via modprobe! ****"
14-
fi
14+
fi
1515

1616
# install headers if necessary
1717
if [ "$SKIP_COMPILE" != "true" ] && [ ! -e /lib/modules/$(uname -r)/build ]; then
1818
echo "**** Attempting kernel header install ****"
1919
apt-get update
20-
if apt-cache show linux-headers-$(uname -r) 2&>1 >/dev/null; then
20+
if apt-cache show linux-headers-$(uname -r) 2 &>1 >/dev/null; then
2121
apt-get install -y \
2222
linux-headers-$(uname -r)
2323
elif (uname -r | grep -q 'v7+') || (uname -r | grep -q 'v7l+') || (uname -r | grep -q 'v8+'); then
2424
echo "**** Raspbian kernel naming convention detected, attempting to install raspbian kernel headers ****"
2525
curl -s http://archive.raspberrypi.org/debian/raspberrypi.gpg.key | apt-key add -
2626
echo -e \
2727
"deb http://archive.raspberrypi.org/debian/ buster main\ndeb-src http://archive.raspberrypi.org/debian/ buster main" \
28-
> /etc/apt/sources.list.d/raspbian.list
28+
>/etc/apt/sources.list.d/raspbian.list
2929
apt-get update
3030
apt-get install -y \
3131
raspberrypi-kernel-headers
@@ -35,21 +35,21 @@ if [ "$SKIP_COMPILE" != "true" ] && [ ! -e /lib/modules/$(uname -r)/build ]; the
3535
if uname -m | grep -q 'x86_64'; then
3636
echo -e \
3737
"deb http://archive.ubuntu.com/ubuntu/ bionic main restricted\ndeb-src http://archive.ubuntu.com/ubuntu/ bionic main restricted\n\ndeb http://archive.ubuntu.com/ubuntu/ bionic-updates main restricted\ndeb-src http://archive.ubuntu.com/ubuntu/ bionic-updates main restricted" \
38-
> /etc/apt/sources.list.d/xenial-bionic.list
38+
>/etc/apt/sources.list.d/xenial-bionic.list
3939
else
4040
echo -e \
41-
"deb http://ports.ubuntu.com/ubuntu-ports/ bionic main restricted\ndeb-src http://ports.ubuntu.com/ubuntu-ports/ bionic main restricted\n\ndeb http://ports.ubuntu.com/ubuntu-ports/ bionic-updates main restricted\ndeb-src http://ports.ubuntu.com/ubuntu-ports/ bionic-updates main restricted" \
42-
> /etc/apt/sources.list.d/xenial-bionic.list
41+
"deb http://ports.ubuntu.com/ubuntu-ports/ bionic main restricted\ndeb-src http://ports.ubuntu.com/ubuntu-ports/ bionic main restricted\n\ndeb http://ports.ubuntu.com/ubuntu-ports/ bionic-updates main restricted\ndeb-src http://ports.ubuntu.com/ubuntu-ports/ bionic-updates main restricted" \
42+
>/etc/apt/sources.list.d/xenial-bionic.list
4343
fi
4444
apt-get update
45-
if apt-cache show linux-headers-$(uname -r) 2&>1 >/dev/null; then
45+
if apt-cache show linux-headers-$(uname -r) 2 &>1 >/dev/null; then
4646
apt-get install -y \
4747
linux-headers-$(uname -r)
4848
else
4949
echo "**** No kernel headers found in the Ubuntu Bionic repo!! Trying Ubuntu Xenial. ****"
5050
sed -i 's/bionic/xenial/g' /etc/apt/sources.list.d/xenial-bionic.list
5151
apt-get update
52-
if apt-cache show linux-headers-$(uname -r) 2&>1 >/dev/null; then
52+
if apt-cache show linux-headers-$(uname -r) 2 &>1 >/dev/null; then
5353
apt-get install -y \
5454
linux-headers-$(uname -r)
5555
else
@@ -61,7 +61,7 @@ if [ "$SKIP_COMPILE" != "true" ] && [ ! -e /lib/modules/$(uname -r)/build ]; the
6161
echo "**** Debian host detected, attempting to install kernel headers from Debian Buster repo ****"
6262
curl -s https://ftp-master.debian.org/keys/archive-key-10.asc | apt-key add -
6363
curl -s https://ftp-master.debian.org/keys/archive-key-10-security.asc | apt-key add -
64-
cat <<DUDE > /etc/apt/sources.list.d/debian.list
64+
cat <<DUDE >/etc/apt/sources.list.d/debian.list
6565
deb http://deb.debian.org/debian buster main contrib non-free
6666
deb-src http://deb.debian.org/debian buster main contrib non-free
6767
deb http://deb.debian.org/debian-security/ buster/updates main contrib non-free
@@ -72,7 +72,7 @@ deb http://deb.debian.org/debian buster-backports main contrib non-free
7272
deb-src http://deb.debian.org/debian buster-backports main contrib non-free
7373
DUDE
7474
apt-get update
75-
if apt-cache show linux-headers-$(uname -r) 2&>1 >/dev/null; then
75+
if apt-cache show linux-headers-$(uname -r) 2 &>1 >/dev/null; then
7676
if uname -r | grep -qs "bpo"; then
7777
echo "**** Backported kernel detected ****"
7878
apt-get install -y -t buster-backports \
@@ -87,7 +87,7 @@ DUDE
8787
curl -s https://ftp-master.debian.org/keys/archive-key-9-security.asc | apt-key add -
8888
sed -i 's/buster/stretch/g' /etc/apt/sources.list.d/debian.list
8989
apt-get update
90-
if apt-cache show linux-headers-$(uname -r) 2&>1 >/dev/null; then
90+
if apt-cache show linux-headers-$(uname -r) 2 &>1 >/dev/null; then
9191
if uname -r | grep -qs "bpo"; then
9292
echo "**** Backported kernel detected ****"
9393
apt-get install -y -t stretch-backports \
@@ -107,12 +107,12 @@ DUDE
107107
fi
108108

109109
if [ "$SKIP_COMPILE" != "true" ]; then
110-
if [ -e /lib/modules/$(uname -r)/build ]; then
110+
if [ -e /lib/modules/$(uname -r)/build ]; then
111111
echo "**** Kernel headers seem to be present, attempting to build the wireguard module. . . ****"
112112
if [ ! -f /lib/modules/$(uname -r)/build/certs/signing_key.pem ]; then
113113
mkdir -p /lib/modules/$(uname -r)/build/certs
114114
cd /lib/modules/$(uname -r)/build/certs
115-
cat <<DUDE >> x509.genkey
115+
cat <<DUDE >>x509.genkey
116116
[ req ]
117117
default_bits = 4096
118118
distinguished_name = req_distinguished_name
@@ -133,18 +133,18 @@ DUDE
133133
openssl req -new -nodes -utf8 -sha512 -days 36500 -batch -x509 -config x509.genkey -outform DER -out signing_key.x509 -keyout signing_key.pem
134134
fi
135135
cd /app
136-
for release in "" "v1.0.20200520" "v1.0.20200330" "v0.0.20200105"; do
136+
for release in "" "v1.0.20200520" "v1.0.20200330" "v0.0.20200105"; do
137137
echo "**** Building the module ****"
138-
if ! [ -z ${release} ] ; then
139-
echo "Wireguard release ${release} set, checking out correct version"
140-
cd wireguard-linux-compat
141-
git checkout $release
142-
cd ..
138+
if ! [ -z ${release} ]; then
139+
echo "Wireguard release ${release} set, checking out correct version"
140+
cd wireguard-linux-compat
141+
git checkout $release
142+
cd ..
143143
fi
144144
make -C wireguard-linux-compat/src -j$(nproc) && returncode=$? || returncode=$?
145145
if [ $returncode -ne 0 ]; then continue; fi
146-
make -C wireguard-linux-compat/src install && returncode=$? || returncode=$?
147-
if [ $returncode -eq 0 ] ; then break; fi
146+
make -C wireguard-linux-compat/src install && returncode=$? || returncode=$?
147+
if [ $returncode -eq 0 ]; then break; fi
148148
done
149149
echo "**** Let's test our new module. ****"
150150
ip link del dev test 2>/dev/null
@@ -166,20 +166,20 @@ rm -rf /etc/wireguard
166166
mkdir -p /etc/wireguard
167167
ln -s /config/wg0.conf /etc/wireguard/wg0.conf
168168
# prepare templates
169-
[[ ! -f /config/templates/server.conf ]] && \
169+
[[ ! -f /config/templates/server.conf ]] &&
170170
cp /defaults/server.conf /config/templates/server.conf
171-
[[ ! -f /config/templates/peer.conf ]] && \
171+
[[ ! -f /config/templates/peer.conf ]] &&
172172
cp /defaults/peer.conf /config/templates/peer.conf
173173

174-
generate_confs () {
174+
generate_confs() {
175175
mkdir -p /config/server
176176
if [ ! -f /config/server/privatekey-server ]; then
177177
umask 077
178-
wg genkey | tee /config/server/privatekey-server | wg pubkey > /config/server/publickey-server
178+
wg genkey | tee /config/server/privatekey-server | wg pubkey >/config/server/publickey-server
179179
fi
180-
eval "`printf %s`
180+
eval "$(printf %s)
181181
cat <<DUDE > /config/wg0.conf
182-
`cat /config/templates/server.conf`
182+
$(cat /config/templates/server.conf)
183183

184184
DUDE"
185185
for i in ${PEERS_ARRAY[@]}; do
@@ -191,7 +191,7 @@ DUDE"
191191
mkdir -p /config/${PEER_ID}
192192
if [ ! -f "/config/${PEER_ID}/privatekey-${PEER_ID}" ]; then
193193
umask 077
194-
wg genkey | tee /config/${PEER_ID}/privatekey-${PEER_ID} | wg pubkey > /config/${PEER_ID}/publickey-${PEER_ID}
194+
wg genkey | tee /config/${PEER_ID}/privatekey-${PEER_ID} | wg pubkey >/config/${PEER_ID}/publickey-${PEER_ID}
195195
fi
196196
if [ -f "/config/${PEER_ID}/${PEER_ID}.conf" ]; then
197197
CLIENT_IP=$(cat /config/${PEER_ID}/${PEER_ID}.conf | grep "Address" | awk '{print $NF}')
@@ -207,25 +207,25 @@ DUDE"
207207
fi
208208
done
209209
fi
210-
eval "`printf %s`
210+
eval "$(printf %s)
211211
cat <<DUDE > /config/${PEER_ID}/${PEER_ID}.conf
212-
`cat /config/templates/peer.conf`
212+
$(cat /config/templates/peer.conf)
213213
DUDE"
214-
cat <<DUDE >> /config/wg0.conf
214+
cat <<DUDE >>/config/wg0.conf
215215
[Peer]
216216
# ${PEER_ID}
217217
PublicKey = $(cat /config/${PEER_ID}/publickey-${PEER_ID})
218218
AllowedIPs = ${CLIENT_IP}/32
219219

220220
DUDE
221221
echo "PEER ${i} QR code:"
222-
qrencode -t ansiutf8 < /config/${PEER_ID}/${PEER_ID}.conf
223-
qrencode -o /config/${PEER_ID}/${PEER_ID}.png < /config/${PEER_ID}/${PEER_ID}.conf
222+
qrencode -t ansiutf8 </config/${PEER_ID}/${PEER_ID}.conf
223+
qrencode -o /config/${PEER_ID}/${PEER_ID}.png </config/${PEER_ID}/${PEER_ID}.conf
224224
done
225225
}
226226

227-
save_vars () {
228-
cat <<DUDE > /config/.donoteditthisfile
227+
save_vars() {
228+
cat <<DUDE >/config/.donoteditthisfile
229229
ORIG_SERVERURL="$SERVERURL"
230230
ORIG_SERVERPORT="$SERVERPORT"
231231
ORIG_PEERDNS="$PEERDNS"
@@ -245,28 +245,34 @@ if [ -n "$PEERS" ]; then
245245
PEERS_COUNT=$(echo "${#PEERS_ARRAY[@]}")
246246
if [ -z "$SERVERURL" ] || [ "$SERVERURL" = "auto" ]; then
247247

248-
function fetchDappnodeDomain {
249-
for i in {1..30}; do
248+
# If _DAPPNODE_GLOBAL_HOSTNAME is set, use it as SERVERURL
249+
if [ ! -z "$_DAPPNODE_GLOBAL_HOSTNAME" ]; then
250+
echo "**** _DAPPNODE_GLOBAL_HOSTNAME is set, using it as SERVERURL ****"
251+
SERVERURL="$_DAPPNODE_GLOBAL_HOSTNAME"
252+
else
253+
254+
function fetchDappnodeDomain {
255+
for i in {1..30}; do
250256
for domain in "$@"; do
251-
echo "**** Fetching DAppNode domain from $domain..."
252-
SERVERURL=$(curl -s "$domain/global-envs/HOSTNAME")
253-
if [ ! -z "$SERVERURL" ]; then
254-
break 2
255-
fi
256-
sleep 2
257+
echo "**** Fetching DAppNode domain from $domain..."
258+
SERVERURL=$(curl -s "$domain/global-envs/HOSTNAME")
259+
if [ ! -z "$SERVERURL" ]; then
260+
break 2
261+
fi
262+
sleep 2
257263
done
258-
done
259-
}
264+
done
265+
}
260266

261-
# TODO: Remove IP when the IP migration is performed
262-
domains=("my.dappnode" "dappmanager.dappnode" "172.33.1.7")
267+
domains=("my.dappnode" "dappmanager.dappnode" "172.33.1.7" "10.20.0.7")
263268

264-
fetchDappnodeDomain "${domains[@]}"
269+
fetchDappnodeDomain "${domains[@]}"
270+
fi
265271

266272
if [ -z "$SERVERURL" ]; then
267-
echo "**** SERVERURL var is either not set or is set to \"auto\", setting external IP to auto detected value of $SERVERURL ****"
273+
echo "**** SERVERURL var is either not set or is set to \"auto\", setting external IP to auto detected value of $SERVERURL ****"
268274
else
269-
echo "**** SERVERURL var is set to $SERVERURL ****"
275+
echo "**** SERVERURL var is set to $SERVERURL ****"
270276
fi
271277
else
272278
echo "**** External server address is set to $SERVERURL ****"
@@ -291,7 +297,7 @@ if [ -n "$PEERS" ]; then
291297
save_vars
292298
else
293299
echo "**** Server mode is selected ****"
294-
[[ -f /config/.donoteditthisfile ]] && \
300+
[[ -f /config/.donoteditthisfile ]] &&
295301
. /config/.donoteditthisfile
296302
if [ "$SERVERURL" != "$ORIG_SERVERURL" ] || [ "$SERVERPORT" != "$ORIG_SERVERPORT" ] || [ "$PEERDNS" != "$ORIG_PEERDNS" ] || [ "$PEERS" != "$ORIG_PEERS" ] || [ "$INTERFACE" != "$ORIG_INTERFACE" ] || [ "$ALLOWEDIPS" != "$ORIG_ALLOWEDIPS" ]; then
297303
echo "**** Server related environment variables changed, regenerating 1 server and ${PEERS} peer/client confs ****"
@@ -312,9 +318,9 @@ else
312318
fi
313319

314320
# set up CoreDNS
315-
[[ ! -f /config/coredns/Corefile ]] && \
321+
[[ ! -f /config/coredns/Corefile ]] &&
316322
cp /defaults/Corefile /config/coredns/Corefile
317323

318324
# permissions
319325
chown -R abc:abc \
320-
/config
326+
/config

0 commit comments

Comments
 (0)