Skip to content

Commit 2ad2c0f

Browse files
committed
rotate macs, fix setting env and starting wg
1 parent faece9d commit 2ad2c0f

File tree

10 files changed

+93
-55
lines changed

10 files changed

+93
-55
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ Set a Hub or HaaS up first, so you can generate the necessary peer configuration
9393
9494
### Maintenance
9595

96-
You can (re)configure WireGuard peers (on bare metal as well, thanks to code shared by [LinuxServer.io](https://github.com/linuxserver/docker-wireguard)):
96+
You can (re)configure WireGuard peers (on bare metal as well, thanks to code shared by [LinuxServer.io](https://github.com/linuxserver/docker-wireguard)). Add WireGuard peers or modify the AllowedIPs of existing ones, show peer config QR codes, and delete peers with:
9797

98-
- Add WireGuard peers, or modify the AllowedIPs of existing ones, with `sudo bash wireguard/add.sh <peer_name> [option]`.
99-
- Show peer config QR codes with `sudo bash wireguard/get.sh <peer_name>`.
100-
- Delete peers with `sudo bash wireguard/del.sh <peer_name>`.
101-
102-
To complete adding a SaaH, create an `SERVER_ALLOWEDIPS_PEER_[SaaH]=` environment variable -- using the peer's name sans the brackets -- for the WireGuard service with the difference of `0.0.0.0/1,128.0.0.0/1,::/1,8000::/1` and the peer's IP. This [AllowedIPs Calculator](https://www.procustodibus.com/blog/2021/03/wireguard-allowedips-calculator) is pretty nifty.
98+
```{bash}
99+
sudo bash wireguard/add.sh <peer_name> [option]
100+
sudo bash wireguard/get.sh <peer_name>
101+
sudo bash wireguard/del.sh <peer_name>
102+
```
103103

104-
Complete the above or any other CUD operation by running `sudo bash restart.sh`. By default, `add.sh` sets the peer to route outgoing traffic through the VPN. You can change this default by modifying AllowedIPs in `compose.yml`. The option it takes may be one of:
104+
By default, `add.sh` sets the peer to route outgoing traffic through the VPN. You can change this default by modifying AllowedIPs in `compose.yml`. The option it takes may be one of:
105105

106106
```{bash}
107107
-e, --internet Route all traffic through the VPN
@@ -110,6 +110,8 @@ Complete the above or any other CUD operation by running `sudo bash restart.sh`.
110110
-o, --outgoing Route outgoing traffic through the VPN
111111
```
112112

113+
After running `add.sh` on a HaaS to create its SaaH peer, create an `SERVER_ALLOWEDIPS_PEER_[SaaH]` environment variable -- using the peer's name sans the brackets -- for the WireGuard service with the difference of `0.0.0.0/1,128.0.0.0/1,::/1,8000::/1` and the peer's IP. This [AllowedIPs Calculator](https://www.procustodibus.com/blog/2021/03/wireguard-allowedips-calculator) is pretty nifty.
114+
113115
> [!NOTE]
114116
> While `start.sh` brings everything up, `restart.sh` only restarts WireGuard unless you first export `CLS_WG_ONLY=false`.
115117

debian/control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Homepage: https://github.com/ipitio/closure
44
Standards-Version: 3.9.2
55

66
Package: closure
7-
Version: 1.6.10
7+
Version: 1.6.11
88
Maintainer: ipitio <[email protected]>
99
Depends: curl, flatpak, gpg, hostapd, isc-dhcp-server, iw, macchanger, netplan.io, network-manager, net-tools, qrencode, rfkill, wireguard, wireless-tools, wget
1010
Recommends: containerd.io, docker-ce, docker-ce-cli, docker-buildx-plugin, docker-compose-plugin

debian/postinst

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
#!/bin/bash
22
# shellcheck disable=SC1003
33

4-
# Reinstall if no installed
5-
[ -f /opt/closure/installed ] || rm -rf /opt/closure/installed
6-
[ -f /etc/rc.local ] || echo "#\!/bin/bash" | tr -d '\\' | tee /etc/rc.local >/dev/null
7-
grep -q closure /etc/rc.local || echo "[ -f /opt/closure/installed ] || bash /opt/closure/init.sh" | tee -a /etc/rc.local >/dev/null
4+
pushd /opt/closure || exit 1
5+
6+
if [ -f /etc/rc.local ]; then
7+
grep -q '^# closure' /etc/rc.local || cp /etc/rc.local rc.local.bak
8+
else
9+
echo "#\!/bin/bash" | tr -d '\\' | tee /etc/rc.local >/dev/null
10+
fi
11+
12+
grep -q '^# closure' /etc/rc.local || sed 1,1d rc.local | tee -a /etc/rc.local >/dev/null
813
chmod +x /etc/rc.local
14+
[ -f /opt/closure/installed ] || rm -rf /opt/closure/installed
15+
popd || exit 1

init.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fi
4444

4545
if ! dpkg -l closure >/dev/null 2>&1; then
4646
sudonot mkdir -m 0755 -p /etc/apt/keyrings/
47-
wget -qO- https://ipitio.github.io/closure/gpg.key | gpg --dearmor | sudonot tee /etc/apt/keyrings/closure.gpg > /dev/null
47+
wget -qO- https://ipitio.github.io/closure/gpg.key | gpg --dearmor | sudonot tee /etc/apt/keyrings/closure.gpg >/dev/null
4848
echo "deb [signed-by=/etc/apt/keyrings/closure.gpg] https://ipitio.github.io/closure master main" | sudonot tee /etc/apt/sources.list.d/closure.list &>/dev/null
4949
sudonot chmod 644 /etc/apt/keyrings/closure.gpg
5050
sudonot chmod 644 /etc/apt/sources.list.d/closure.list
@@ -137,8 +137,8 @@ fi
137137
# Autostart on login
138138
sudo sed -i "s,script_path=.*$,script_path=$PWD," kickstart.sh
139139
active_path=/home/"$CLS_ACTIVE_USER"/.closure/kickstart.sh
140-
allow_active="$CLS_ACTIVE_USER$(echo -e '\t')ALL=(ALL) NOPASSWD:$active_path"
141-
allow_script="$CLS_SCRIPT_USER$(echo -e '\t')ALL=(ALL) NOPASSWD:$PWD/*"
140+
allow_active="$CLS_ACTIVE_USER$(echo -e '\t')ALL=(ALL) NOPASSWD:SETENV:$active_path"
141+
allow_script="$CLS_SCRIPT_USER$(echo -e '\t')ALL=(ALL) NOPASSWD:SETENV:$PWD/*"
142142
sudo chown "$CLS_SCRIPT_USER":"$CLS_SCRIPT_USER" kickstart.sh
143143
sudo chmod +x kickstart.sh
144144
sudo mkdir -p "$(dirname "$active_path")"

kickstart.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
script_path="$(dirname "$(readlink -f "$0")")"
55
sudo -i bash <<EOF
66
pushd "$script_path" || exit 1
7-
sudo bash start.sh ${@@Q} > ks.log
7+
CLS_WG_ONLY=${CLS_WG_ONLY:-false} bash start.sh ${@@Q} > ks.log
88
popd || exit 1
99
EOF

lib.sh

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,16 @@ start_hostapd() {
156156
sudo hostapd -i "$wiface" -P /run/hostapd.pid -B hostapd/"$config".conf
157157
sudo iw dev "$wiface" set power_save off
158158
local octet
159-
octet=$((1 + $( (
160-
ifconfig | grep -oP '(?<=10\.42\.)\S+(?=\.1)'
161-
echo 1
162-
) | grep -v 255 | sort -ru | head -n1)))
163-
sudo ifconfig "$wiface" 10.42.$octet.1 netmask 255.255.255.0
159+
octet=$(ifconfig "$wiface" | grep -zoP '(?<=inet 10\.42\.)\S+(?=\.1)')
160+
161+
if [ -z "$octet" ]; then
162+
octet=$((1 + $( (
163+
ifconfig | grep -zoP '(?<=10\.42\.)\S+(?=\.1)'
164+
echo 1
165+
) | grep -v 255 | sort -ru | head -n1)))
166+
sudo ifconfig "$wiface" 10.42."$octet".1 netmask 255.255.255.0
167+
fi
168+
164169
grep -qF "subnet 10.42.$octet.0 netmask 255.255.255.0" dhcp/dhcpd.conf || echo -e "\nsubnet 10.42.$octet.0 netmask 255.255.255.0 {\n option routers 10.42.$octet.1;\n range 10.42.$octet.2 10.42.$octet.254;\n}" | sudo tee -a dhcp/dhcpd.conf
165170
restart_isc
166171
sleep 15

rc.local

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
#!/bin/bash
22

3+
# closure
34
[ -f /opt/closure/installed ] || bash /opt/closure/init.sh
5+
ifconfig | grep -oP '^\S+(?=:)' | xargs -P0 -I{} bash -c '
6+
ifconfig "{}" down &>/dev/null
7+
macchanger -r "{}" &>/dev/null
8+
ifconfig "{}" up &>/dev/null
9+
'

start.sh

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,36 @@ if ! ${CLS_WG_ONLY:-false}; then
6161
sudo chmod 0600 /etc/netplan/99_config.yaml
6262
stop_hostapd
6363
sudo netplan apply
64+
start_hostapd &
6465
[ -z "$CLS_WIFACE" ] || sudo iw dev "$CLS_WIFACE" set power_save off
65-
[ ! -f /etc/resolv.conf ] || sudo rm -f /etc/resolv.conf
6666
(
67-
cat resolv.conf
68-
(
69-
nmcli dev show "$CLS_LOCAL_IFACE" | grep DNS | grep -oP '\S+$'
70-
) | while read -r ip; do echo "nameserver $ip"; done
71-
(
72-
nmcli dev show "$CLS_LOCAL_IFACE" | grep DOMAIN | grep -oP '\S+$' || echo .
73-
) | while read -r name; do echo "search $name"; done
74-
) | sudo tee /etc/resolv.conf >/dev/null
75-
[ -z "$CLS_LOCAL_IFACE" ] || sudo tc qdisc del dev "$CLS_LOCAL_IFACE" root &>/dev/null
76-
[ -z "$CLS_LOCAL_IFACE" ] || sudo tc qdisc replace dev "$CLS_LOCAL_IFACE" root cake "$([ -z "$CLS_BANDWIDTH" ] && echo diffserv8 || echo "bandwidth $CLS_BANDWIDTH diffserv8")" nat docsis ack-filter
77-
sudo busctl --system set-property org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager ConnectivityCheckEnabled "b" 0 2>/dev/null
78-
(crontab -l 2>/dev/null | grep -Fv "/ddns.sh &") | crontab -
79-
80-
if [ -n "$CLS_DYN_DNS" ]; then
67+
until [ -n "$CLS_LOCAL_IFACE" ]; do
68+
get_local_ip
69+
sleep 1
70+
done
71+
[ ! -f /etc/resolv.conf ] || sudo rm -f /etc/resolv.conf
8172
(
82-
crontab -l 2>/dev/null
83-
echo "0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/bin/sleep 10 ; /usr/bin/bash $this_dir/ddns.sh &"
84-
) | crontab -
85-
sudo bash ddns.sh &
86-
fi
87-
88-
start_hostapd
73+
cat resolv.conf
74+
(
75+
nmcli dev show "$CLS_LOCAL_IFACE" | grep DNS | grep -oP '\S+$'
76+
) | while read -r ip; do echo "nameserver $ip"; done
77+
(
78+
nmcli dev show "$CLS_LOCAL_IFACE" | grep DOMAIN | grep -oP '\S+$' || echo .
79+
) | while read -r name; do echo "search $name"; done
80+
) | sudo tee /etc/resolv.conf >/dev/null
81+
sudo tc qdisc del dev "$CLS_LOCAL_IFACE" root &>/dev/null
82+
sudo tc qdisc replace dev "$CLS_LOCAL_IFACE" root cake "$([ -z "$CLS_BANDWIDTH" ] && echo diffserv8 || echo "bandwidth $CLS_BANDWIDTH diffserv8")" nat docsis ack-filter
83+
sudo busctl --system set-property org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager ConnectivityCheckEnabled "b" 0 2>/dev/null
84+
(crontab -l 2>/dev/null | grep -Fv "/ddns.sh &") | crontab -
85+
86+
if [ -n "$CLS_DYN_DNS" ]; then
87+
(
88+
crontab -l 2>/dev/null
89+
echo "0,5,10,15,20,25,30,35,40,45,50,55 * * * * /usr/bin/sleep 10 ; /usr/bin/bash $this_dir/ddns.sh &"
90+
) | crontab -
91+
sudo bash ddns.sh &
92+
fi
93+
) &
8994
fi
9095

9196
if $CLS_DOCKER; then
@@ -100,10 +105,9 @@ if $CLS_DOCKER; then
100105
else
101106
sudo sysctl -w net.ipv4.ip_forward=0
102107
sudo sysctl -w net.ipv6.conf.all.forwarding=0
103-
sudo wg-quick down "$CLS_INTERN_IFACE"
108+
for iface in $(sudo wg | grep -oP '(?<=interface: ).+'); do sudo wg-quick down "$iface"; done
104109
fi
105110

106-
get_local_ip # set variables
107111
eval "cast pre-up ${*@Q}"
108112

109113
(
@@ -159,7 +163,10 @@ if $CLS_DOCKER; then
159163
if ! ip a show "$CLS_INTERN_IFACE" | grep -q UP; then
160164
sudo systemctl restart docker
161165
sudo docker network prune -f
162-
until [ -n "$CLS_LOCAL_IP" ]; do get_local_ip; sleep 1; done
166+
until [ -n "$CLS_LOCAL_IP" ]; do
167+
get_local_ip
168+
sleep 1
169+
done
163170
sed -i "s/#\?- FTLCONF_LOCAL_IPV4=.*$/- FTLCONF_LOCAL_IPV4=$CLS_LOCAL_IP/" compose.yml
164171
sudo docker compose --profile prod up -d --force-recreate --remove-orphans
165172
elif ! sudo docker ps | grep -qE "wireguard.*Up"; then
@@ -169,11 +176,16 @@ if $CLS_DOCKER; then
169176
else
170177
sudo bash wireguard/etc/run
171178
sudo mkdir -p /etc/wireguard
172-
sudo rm -f /etc/wireguard/"$CLS_INTERN_IFACE".conf &>/dev/null
173-
sudo cp -f wireguard/config/wg_confs/"$CLS_INTERN_IFACE".conf /etc/wireguard/"$CLS_INTERN_IFACE".conf
174-
sudo chmod 600 /etc/wireguard/"$CLS_INTERN_IFACE".conf
175-
sudo chown root:root /etc/wireguard/"$CLS_INTERN_IFACE".conf
176-
sudo wg-quick up "$CLS_INTERN_IFACE"
179+
sudo rm -f /etc/wireguard/*.conf &>/dev/null
180+
sudo ls wireguard/config/wg_confs | grep -oP '.+\.conf$' | while read -r conf; do
181+
[ -s "wireguard/config/wg_confs/$conf" ] || continue
182+
config="/etc/wireguard/$conf"
183+
iface="${conf%.conf}"
184+
sudo cp -f "wireguard/config/wg_confs/$conf" "$config"
185+
sudo chmod 600 "$config"
186+
sudo chown root:root "$config"
187+
sudo wg-quick up "$iface"
188+
done
177189
fi
178190

179191
for tables in iptables ip6tables; do

wireguard/add.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ esac
5353
echo "$conf" >"$path.conf"
5454

5555
if $CLS_DOCKER; then
56-
sudo docker exec wireguard bash -c "wg-quick down $CLS_INTERN_IFACE ; wg-quick up $CLS_INTERN_IFACE"
56+
sudo docker compose restart wireguard
57+
sudo docker exec wireguard bash -c "wg-quick down wg0 ; wg-quick up wg0"
5758
sudo docker compose up -d wireguard
5859
else
5960
sudo wg-quick down "$CLS_INTERN_IFACE" ; sudo wg-quick up "$CLS_INTERN_IFACE"

wireguard/del.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ if grep -q "$1" <<<"$peers"; then
1010
sed -i "s/$peers/$(sed "s/,\?$1//" <<<"$peers")/" compose.yml
1111
sudo rm -rf wireguard/config/peer_"$1"
1212
sudo mv -f wireguard/config/wg_confs/wg0.conf wireguard/config/wg_confs/wg0.conf.bak
13-
sudo docker compose restart wireguard
14-
sudo docker exec wireguard bash -c "wg-quick down wg0 ; wg-quick up wg0"
15-
sudo docker compose up -d wireguard
13+
14+
if $CLS_DOCKER; then
15+
sudo docker compose restart wireguard
16+
sudo docker exec wireguard bash -c "wg-quick down wg0 ; wg-quick up wg0"
17+
sudo docker compose up -d wireguard
18+
else
19+
sudo wg-quick down "$CLS_INTERN_IFACE" ; sudo wg-quick up "$CLS_INTERN_IFACE"
20+
fi
1621
fi
1722

1823
popd &>/dev/null || exit

0 commit comments

Comments
 (0)