Skip to content

Commit 34158b0

Browse files
committed
install self, boot even faster, gen subnets
1 parent 11eede1 commit 34158b0

File tree

10 files changed

+52
-28
lines changed

10 files changed

+52
-28
lines changed

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
---
88

9-
[![build](https://github.com/ipitio/closure/actions/workflows/release.yml/badge.svg)](https://github.com/ipitio/closure/pkgs/container/closure)
9+
[![build](https://github.com/ipitio/closure/actions/workflows/release.yml/badge.svg)](https://github.com/ipitio/closure/releases/latest)
1010

1111
</div>
1212

@@ -16,7 +16,7 @@ You can run WireGuard with Docker or on the host. If you run it with Docker (ava
1616

1717
## Getting Started
1818

19-
Just edit some variables and go!
19+
Install. Configure. Reboot.
2020

2121
### Definitions
2222

@@ -47,39 +47,39 @@ Keep in mind that:
4747
- To configure Pi-hole more extensively, such as by enabling DHCP, see the [Pi-hole documentation](https://github.com/pi-hole/docker-pi-hole/tree/2024.07.0?tab=readme-ov-file#environment-variables).
4848
- The hooks may be useful, for example, if you'd like to coordinate with an external, outbound VPN on a Hub or SaaH. All arguments given to `start.sh`and `stop.sh` are passed to their respective hooks.
4949

50-
To customize iptables, modify the relevant lines in `start.sh` and `stop.sh`.
51-
52-
> [!WARNING]
50+
> [!NOTE]
5351
> The WireGuard service in the Compose file must be configured whether or not you'll use Docker ([docs](https://docs.linuxserver.io/images/docker-wireguard)).
5452
55-
> [!CAUTION]
53+
> [!WARNING]
5654
> If a user you specify in `env.sh` doesn't exist, it will be created. By default, the password will be the same as the username; change it!
5755
5856
### Deployment
5957

6058
Create or update a node in two or three steps:
6159

62-
1. Move this repo to the target or install the [package](https://github.com/ipitio/closure/releases):
60+
1. Either install the [package](https://github.com/ipitio/closure/releases) directly...
6361

6462
```{bash}
6563
sudo apt-get update
6664
sudo DEBIAN_FRONTEND=noninteractive apt-get install -qq gpg wget
6765
sudo mkdir -m 0755 -p /etc/apt/keyrings/
6866
wget -qO- https://ipitio.github.io/closure/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/closure.gpg > /dev/null
6967
sudo chmod 644 /etc/apt/keyrings/closure.gpg
70-
echo "deb [signed-by=/etc/apt/keyrings/closure.gpg] https://ipitio.github.io/closure master main" | sudo tee /etc/apt/sources.list.d/closure.list
68+
echo "deb [signed-by=/etc/apt/keyrings/closure.gpg] https://ipitio.github.io/closure master main" | sudo tee /etc/apt/sources.list.d/closure.list &>/dev/null
7169
sudo chmod 644 /etc/apt/sources.list.d/closure.list
7270
sudo apt-get update
7371
sudo DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -qq closure
7472
```
7573

76-
2. Edit the files above (in `/opt/closure` if you installed the package). If you didn't install the package, change the path in `rc.local` and move it to `/etc`. Ensure the target is connected to the internet and reboot.
77-
3. On a Hub or HaaS, add a Spoke or SaaH peer by running `add.sh` (as described below). Then, for a SaaH, add 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, and run `sudo kickstart.sh`. This [AllowedIPs Calculator](https://www.procustodibus.com/blog/2021/03/wireguard-allowedips-calculator) is pretty nifty. Follow a similar process for a Spoke, if needed.
74+
...Or copy this repo to `/opt/closure` on the target. Then verify the path inside `rc.local`, make it executable, and move it to `/etc`. The package will be installed after the next step.
75+
76+
2. Edit the files above and reboot. This boot, as well as those after upgrading, may take a while as everything is set up, but the subsequent ones will be much faster.
77+
3. On a Hub or HaaS, add a Spoke or SaaH peer by running `add.sh` (as described below). Then, for a SaaH, add 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, and run `sudo bash restart.sh`. This [AllowedIPs Calculator](https://www.procustodibus.com/blog/2021/03/wireguard-allowedips-calculator) is pretty nifty. Follow a similar process for a Spoke, if needed.
7878

7979
Set a Hub or HaaS up first, so you can generate the necessary peer configuration for a Spoke or SaaH, then drop it in the Spoke's or SaaH's `wireguard/config/wg_confs` directory before their reboot.
8080

8181
> [!NOTE]
82-
> Any arguments passed to `kickstart.sh` are passed to `init.sh` and `start.sh`, and `init.sh` can add or edit wifi networks -- useful on a Raspberry Pi Zero (2) W! See the top of `init.sh` for the arguments it takes.
82+
> Any arguments passed to `kickstart.sh` are passed to `start.sh`, which can add or edit wifi networks -- useful on a Raspberry Pi Zero (2) W! See the top of `start.sh` for the arguments it takes.
8383
8484
> [!IMPORTANT]
8585
> Remember to forward a port to your Hub or HaaS, which listens on 51820 by default. Use 443 on your router to bypass some basic firewall filters.
@@ -101,7 +101,8 @@ By default, `add.sh` sets the peer to route outgoing traffic through the VPN. Yo
101101
-o, --outgoing Route outgoing traffic through the VPN
102102
```
103103

104-
While `start.sh` brings everything up, `stop.sh` only stops WireGuard. `restart.sh` simply calls these two scripts, passing all of its arguments to them. Therefore, when stopping, if you're using Docker, you must also run `sudo docker compose down` to bring the other services down. Happy stargazing!
104+
> [!NOTE]
105+
> While `start.sh` brings everything up, `restart.sh` only restarts WireGuard.
105106
106107
> [!TIP]
107108
> Don't forget to share an updated config with its peer.

debian/control

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ Homepage: https://github.com/ipitio/closure
44
Standards-Version: 3.9.2
55

66
Package: closure
7-
Version: 1.6.5
7+
Version: 1.6.6
88
Maintainer: ipitio <[email protected]>
9-
Depends: curl, flatpak, hostapd, isc-dhcp-server, iw, macchanger, netplan.io, network-manager, net-tools, qrencode, rfkill, wireguard, wireless-tools, wget
9+
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
1111
Suggests: build-essential, byobu, dkms, iperf3, nmap, tmux, traceroute, wmctrl
1212
Copyright: debian/copyright

debian/postinst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# shellcheck disable=SC1003
33

44
# Reinstall if no installed
5+
[ -f /opt/closure/installed ] || rm -rf /opt/closure/installed
56
[ -f /etc/rc.local ] || echo "#\!/bin/bash" | tr -d '\\' | tee /etc/rc.local >/dev/null
6-
grep -q closure /etc/rc.local || echo "[ -f /opt/closure/installed ] || bash /opt/closure/kickstart.sh" | tee -a /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
78
chmod +x /etc/rc.local

examples/dhcp/dhcpd.conf

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ max-lease-time 7200;
66
ddns-update-style none;
77
authoritative;
88

9-
# as many routers as APs
9+
# subnets for netplan ethernets, just keep incrementing the third octet
10+
1011
subnet 10.42.1.0 netmask 255.255.255.0 {
11-
range 10.42.1.4 10.42.1.254;
12-
option routers 10.42.1.1, 10.42.1.2, 10.42.1.3;
12+
option routers 10.42.1.1;
13+
range 10.42.1.2 10.42.1.254;
1314
}
15+
16+
# subnets for hostapd are generated automatically

init.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,20 @@ if ! dpkg -l docker-ce >/dev/null 2>&1; then
5555
echo "deb [trusted=yes arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
5656
fi
5757

58+
if ! dpkg -l closure >/dev/null 2>&1; then
59+
sudo mkdir -m 0755 -p /etc/apt/keyrings/
60+
wget -qO- https://ipitio.github.io/closure/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/closure.gpg > /dev/null
61+
echo "deb [signed-by=/etc/apt/keyrings/closure.gpg] https://ipitio.github.io/closure master main" | sudo tee /etc/apt/sources.list.d/closure.list &>/dev/null
62+
sudo chmod 644 /etc/apt/keyrings/closure.gpg
63+
sudo chmod 644 /etc/apt/sources.list.d/closure.list
64+
fi
65+
5866
[ ! -f /etc/apt/preferences.d/nosnap.pref ] || sudo mv /etc/apt/preferences.d/nosnap.pref /etc/apt/preferences.d/nosnap.pref.bak
5967
sudo systemctl disable --now whoopsie.path &>/dev/null
6068
sudo systemctl mask whoopsie.path &>/dev/null
6169
sudo apt-get purge -y ubuntu-report popularity-contest apport whoopsie
6270
# shellcheck disable=SC2046
63-
apt_install $(grep -oP '((?<=^Depends: )|(?<=^Recommends: )|(?<=^Suggests: )).*' debian/control | tr -d ',' | tr '\n' ' ')
71+
apt_install closure $(grep -oP '((?<=^Depends: )|(?<=^Recommends: )|(?<=^Suggests: )).*' debian/control | tr -d ',' | tr '\n' ' ')
6472
sudo apt autoremove -y
6573
yq -V | grep -q mikefarah &>/dev/null || {
6674
[ ! -f /usr/bin/yq ] || sudo mv -f /usr/bin/yq /usr/bin/yq.bak

kickstart.sh

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

lib.sh

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ restart_isc() {
8383

8484
stop_hostapd() {
8585
local aps
86-
aps=$(sudo find /var/run/hostapd -type s | grep -oP '(?<=/var/run/hostapd/).+')
86+
aps=$(sudo find /var/run/hostapd -type s 2>&1 | grep -oP '(?<=/var/run/hostapd/).+')
8787
ps -aux | grep -P "^[^-]+hostapd$2" | awk '{print $2}' | while read -r pid; do sudo kill -9 "$pid" &>/dev/null; done
8888

89-
for wiface in $aps; do
90-
[[ -z "$1" || "$1" == "${wiface//*@/}" ]] || continue
89+
for wiface in $1 $aps; do
90+
[[ -z "$1" || "$1" == "$wiface" ]] || continue
9191
sudo rm -rf /var/run/hostapd/"$wiface" &>/dev/null
9292
[[ ! "$wiface" =~ @ ]] || sudo iw dev "$wiface" del &>/dev/null
9393
done
@@ -118,6 +118,7 @@ start_hostapd() {
118118
for wiface in "${!wifaces_configs[@]}"; do
119119
config="${wifaces_configs[$wiface]}"
120120
[ "$config" != "." ] || config="$wiface"
121+
echo "Starting hostapd on $wiface with config $config"
121122
[ -f hostapd/"$config".conf ] && ! yq '(.network.wifis | keys)[]' netplan.yml | grep -qFx "$wiface" && iw dev | grep -qzP "Interface ${wiface//*@/}\n" && ! iw dev "$wiface" info | grep -q ssid || continue
122123
# https://raw.githubusercontent.com/MkLHX/AP_STA_RPI_SAME_WIFI_CHIP/refs/heads/master/ap_sta_config2.sh
123124
[[ ! "$wiface" =~ @ ]] || until [ -n "$freq" ]; do freq=$(iwconfig "${wiface//*@/}" | grep -oP '(?<=Frequency:)\S+' | tr -d '.'); done
@@ -134,7 +135,13 @@ start_hostapd() {
134135
[[ ! "$wiface" =~ @ ]] || sudo iw dev "${wiface//*@/}" interface add "$wiface" type __ap
135136
sudo hostapd -i "$wiface" -P /run/hostapd.pid -B hostapd/"$config".conf
136137
sudo iw dev "$wiface" set power_save off
137-
sudo ifconfig "$wiface" 10.42.1.$((1 + $( (ifconfig | grep -oP '(?<=10\.42\.1\.)\S+'; echo 1) | grep -v 255 | sort -ru | head -n1))) netmask 255.255.255.0
138+
local octet
139+
octet=$((1 + $( (
140+
ifconfig | grep -oP '(?<=10\.42\.)\S+(?=\.1)'
141+
echo 1
142+
) | grep -v 255 | sort -ru | head -n1)))
143+
sudo ifconfig "$wiface" 10.42.$octet.1 netmask 255.255.255.0
144+
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
138145
restart_isc
139146
sleep 15
140147
done
@@ -171,7 +178,7 @@ direct_domain() {
171178
local ichi
172179
ichi=$(dig +short "$1")
173180
for ip in $ichi; do ip r | grep -q "$ip" || sudo ip route add "$ip" via "$CLS_GATEWAY" dev "$CLS_LOCAL_IFACE" &>/dev/null; done
174-
$2
181+
$2 &>/dev/null
175182
for ip in $ichi; do ! ip r | grep -q "$ip" || sudo ip route del "$ip" &>/dev/null; done
176183
}
177184

rc.local

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
CLS_PATH="/opt/closure"
55

66
# don't change it here
7-
[ -f /opt/closure/installed ] || bash "$CLS_PATH"/kickstart.sh
7+
[ -f /opt/closure/installed ] || bash "$CLS_PATH"/init.sh

stop.sh renamed to restart.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ for tables in iptables ip6tables; do
3131
done
3232

3333
eval "cast post-down ${*@Q}"
34+
bash start.sh ${@@Q}
3435
popd || exit

start.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ fi
3333
sudo cp -f netplan.yml /etc/netplan/99_config.yaml
3434
sudo chmod 0600 /etc/netplan/99_config.yaml
3535
stop_hostapd
36+
sudo sed -i '/# subnets for hostapd/,$d' dhcp/dhcpd.conf
37+
echo -e "# subnets for hostapd are generated automatically" | sudo tee -a dhcp/dhcpd.conf >/dev/null
3638
sudo netplan apply
3739
start_hostapd
3840
sudo iw dev "$CLS_WIFACE" set power_save off
@@ -60,6 +62,8 @@ if $CLS_DOCKER; then
6062
sudo ip6tables -L -t "$table" | grep -q "$chain" || sudo ip6tables -N "$chain" -t "$table"
6163
done
6264
done
65+
else
66+
sudo wg-quick down "$CLS_INTERN_IFACE"
6367
fi
6468

6569
eval "cast pre-up ${*@Q}"
@@ -101,7 +105,7 @@ sudo cp -f /etc/resolv.conf.bak /etc/resolv.conf
101105
sleep 5
102106
done
103107

104-
exec sudo bash restart.sh "$@"
108+
exec sudo bash restart.sh ${@@Q}
105109
) &
106110

107111
if $CLS_DOCKER; then

0 commit comments

Comments
 (0)