Skip to content

Commit 6d8d36b

Browse files
committed
eval cast, use rc.local, support open wifi, don't wait for connectivity, check netplan hash
1 parent 9eb329e commit 6d8d36b

File tree

11 files changed

+33
-34
lines changed

11 files changed

+33
-34
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ A SaaH-HaaS[-Spoke] topology may be useful when you can't forward the WireGuard
3232
Move everything in `examples/` out to the parent directory. The files to edit are:
3333

3434
- `dhcp/*dhcp*`: DHCP config, if you want to use the node as a DHCP server but not using Pi-hole
35-
- `netplan/{closed,open}.yml`: network config when internet is reachable or not, respectively
35+
- `netplan.yml`: network config
3636
- `env.sh`: environment variables for the scripts
3737
- `compose.yml`: environment variables for the services and bare WireGuard
3838
- `hooks/{pre,post}-{up,down}.sh`: scripts that run from the active user's home directory before and after everything is started or stopped
@@ -58,10 +58,10 @@ To customize iptables, modify the relevant lines in `start.sh` and `stop.sh`.
5858
Set a node up in two or three steps:
5959

6060
1. Move this directory to the target in any way you like. If you install the `deb` package provided in [Releases](https://github.com/ipitio/closure/releases), it will be created as `/opt/closure`.
61-
2. Edit the files above, run `init.sh` if you didn't install the package, and reboot.
61+
2. Edit the files above. If you didn't install the package, change the path in `rc.local` and move it to `/etc`. Now reboot.
6262
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.
6363

64-
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 after completing Step 1 for it.
64+
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.
6565

6666
> [!NOTE]
6767
> 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.

ddns.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ this_dir=$(dirname "$(readlink -f "$0")")
66
pushd "$this_dir" || exit 1
77
source "lib.sh"
88
direct_domain "$(grep -oP '((?<=http:\/\/)|(?<=https:\/\/)).+?[^/?]+' <<<"$CLS_DYN_DNS")" "wget --no-check-certificate -O - $CLS_DYN_DNS >> /tmp/ddns.log 2>&1"
9+
popd || exit 1

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.5
7+
Version: 1.5.1
88
Maintainer: ipitio <21136719+ipitio@users.noreply.github.com>
99
Depends: containerd.io, curl, docker-ce, docker-ce-cli, docker-buildx-plugin, docker-compose-plugin, flatpak, hostapd, isc-dhcp-server, iw, macchanger, network-manager, net-tools, qrencode, rfkill, wireguard, wireless-tools, wget
1010
Recommends: build-essential, byobu, dkms, iperf3, nmap, tmux, traceroute, wmctrl

debian/postinst

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

4-
bash /opt/closure/init.sh
4+
# Reinstall if no installed
5+
[ -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+
chmod +x /etc/rc.local

examples/netplan/open.yml

Lines changed: 0 additions & 8 deletions
This file was deleted.

init.sh

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ source "lib.sh"
8686
[ -f config/wifis.json ] || echo "{}" >config/wifis.json
8787
if [ -n "$WIFI" ]; then
8888
! $PORTAL || jq "(. | select([\"$WIFI\"]) | .[\"$WIFI\"]) = \"$MAC\"" config/wifis.json | sudo tee config/wifis.json
89-
for conf in open closed; do [[ -n "$PASSWD" || "$PASSWD" != "\"\"" ]] && yq -i ".network.wifis.$CLS_WIFACE.access-points.[\"$WIFI\"].password=\"$PASSWD\"" netplan/"$conf".yml || yq -i ".network.wifis.$CLS_WIFACE.access-points.[\"$WIFI\"]={}" netplan/"$conf".yml; done
89+
[[ -n "$PASSWD" && "$PASSWD" != "\"\"" ]] && yq -i ".network.wifis.$CLS_WIFACE.access-points.[\"$WIFI\"].password=\"$PASSWD\"" netplan.yml || yq -i ".network.wifis.$CLS_WIFACE.access-points.[\"$WIFI\"]={}" netplan.yml
9090
fi
9191

9292
# Free port 53 on Ubuntu for Pi-hole
@@ -171,7 +171,7 @@ echo "network: {config: disabled}" | sudo tee /etc/cloud/cloud.cfg.d/99-disable-
171171
sudo rm -f /etc/netplan/50-cloud-init.yaml
172172
sudo rfkill unblock wlan
173173
sudo iw reg set PA
174-
set_netplan closed
174+
set_netplan "$WIFI"
175175
sudo busctl --system set-property org.freedesktop.NetworkManager /org/freedesktop/NetworkManager org.freedesktop.NetworkManager ConnectivityCheckEnabled "b" 0 2>/dev/null
176176

177177
set_mac="$(jq ".$(iw dev | grep -zoE "$CLS_WIFACE.*type" | tr '\0' '\n' | grep -oP '(?<=ssid ).+')" config/wifis.json 2>/dev/null | tr -d '"')"
@@ -266,10 +266,6 @@ sudo systemctl mask hostapd &>/dev/null
266266
# Kodi
267267
[ -f /home/"$CLS_ACTIVE_USER"/.kodi/.cls ] || sudo cp -r kodi /home/"$CLS_ACTIVE_USER"/.kodi
268268

269-
# Reinstall if no installed
270-
[ -f /etc/rc.local ] || echo "#\!/bin/bash" | tr -d '\\' | tee /etc/rc.local >/dev/null
271-
grep -q closure /etc/rc.local || echo "[ -f $this_dir/installed ] || bash $this_dir/kickstart.sh" | tee -a /etc/rc.local >/dev/null
272-
chmod +x /etc/rc.local
273-
touch installed
274-
269+
sudo mkdir -p /opt/closure
270+
sudo touch /opt/closure/installed
275271
popd || exit 1

lib.sh

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ get_local_ip() {
6262
}
6363

6464
CLS_TYPE_NODE=$(echo "$CLS_TYPE_NODE" | tr '[:upper:]' '[:lower:]')
65-
CLS_LOCAL_IP=$(get_local_ip)
6665
CLS_WG_SERVER=$(echo "$INTERNAL_SUBNET" | awk 'BEGIN{FS=OFS="."} NF--').1
6766
CLS_WG_SERVER_IP=""
6867

6968
set_netplan() {
69+
[[ "$(md5sum netplan.yml | cut -d' ' -f1 | sudo tee new.netplan.hash)" != "$(cat netplan.hash 2>/dev/null)" || -n "$1" ]] || return 0
70+
sudo mv -f new.netplan.hash netplan.hash
7071
ps -aux | grep -P "^[^-]+hostapd" | awk '{print $2}' | while read -r pid; do sudo kill -9 "$pid" &>/dev/null; done
7172
IFS='/' read -r -a wifaces <<<"$CLS_AP_WIFACES"
7273

@@ -75,16 +76,14 @@ set_netplan() {
7576
[[ ! "$wiface" =~ @ ]] || sudo iw dev "$wiface" del &>/dev/null
7677
done
7778

78-
sudo cp -f netplan/"${1:-open}".yml /etc/netplan/99_config.yaml
79+
sudo cp -f netplan.yml /etc/netplan/99_config.yaml
7980
sudo chmod 0600 /etc/netplan/99_config.yaml
8081
sudo netplan apply
8182
sudo iw dev "$CLS_WIFACE" set power_save off
8283
sudo cp -f /etc/resolv.conf.bak /etc/resolv.conf
83-
local try=0
84-
until CLS_LOCAL_IP=$(get_local_ip); do ((try++)) && ((try > 60)) && return 1 || sleep 1; done
84+
get_local_ip # set variables
8585
[ -z "$CLS_LOCAL_IFACE" ] || sudo tc qdisc del dev "$CLS_LOCAL_IFACE" root &>/dev/null
8686
[ -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
87-
sed -i "s/#\?- FTLCONF_LOCAL_IPV4=.*$/- FTLCONF_LOCAL_IPV4=$CLS_LOCAL_IP/" compose.yml
8887
}
8988

9089
is_ip() {

rc.local

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
# directory where closure is installed
4+
CLS_PATH="/opt/closure"
5+
6+
# don't change it here
7+
[ -f /opt/closure/installed ] || bash "$CLS_PATH"/kickstart.sh

start.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if $CLS_AP_HOSTAPD; then
1717
config="${wifaces_configs[$wiface]}"
1818
[ "$config" != "." ] || config="$wiface"
1919

20-
if [ -f hostapd/"$config".conf ] && iw dev | grep -zP "Interface $wiface\n" && ! iw dev "$wiface" info | grep -q ssid; then
20+
if [ -f hostapd/"$config".conf ] && iw dev | grep -qzP "Interface $wiface\n" && ! iw dev "$wiface" info | grep -q ssid; then
2121
# https://raw.githubusercontent.com/MkLHX/AP_STA_RPI_SAME_WIFI_CHIP/refs/heads/master/ap_sta_config2.sh
2222
[[ ! "$wiface" =~ @ ]] || until [ -n "$freq" ]; do freq=$(iwconfig "$wiface" | grep -oP '(?<=Frequency:)\S+' | tr -d '.'); done
2323
[[ ! "$wiface" =~ @ ]] || sudo sed -i "s/^\(channel=\).*/\1$(iw list | grep "$freq." | head -n1 | grep -oP '(?<=\[)[^\]]+')/" hostapd/"$config".conf
@@ -49,8 +49,8 @@ for table in nat filter; do
4949
done
5050
done
5151

52-
cast pre-up ${@@Q}
53-
until ping -c1 1.1.1.1 &>/dev/null || ((timer++ == 90)); do set_netplan open; done
52+
eval "cast pre-up ${*@Q}"
53+
local_ip=$(get_local_ip)
5454
sudo cp -f /etc/resolv.conf.bak /etc/resolv.conf
5555

5656
(
@@ -85,7 +85,7 @@ sudo cp -f /etc/resolv.conf.bak /etc/resolv.conf
8585
sleep 5
8686
done
8787

88-
exec sudo bash restart.sh ${@@Q}
88+
exec sudo bash restart.sh "$@"
8989
) &
9090

9191
if $CLS_DOCKER; then
@@ -94,6 +94,7 @@ if $CLS_DOCKER; then
9494
if ! ip a show "$CLS_INTERN_IFACE" | grep -q UP; then
9595
sudo systemctl restart docker
9696
sudo docker network prune -f
97+
sed -i "s/#\?- FTLCONF_LOCAL_IPV4=.*$/- FTLCONF_LOCAL_IPV4=$local_ip/" compose.yml
9798
sudo docker compose --profile prod up -d --force-recreate --remove-orphans
9899
elif ! sudo docker ps | grep -qE "wireguard.*Up"; then
99100
sudo docker compose --profile prod up -d --force-recreate --remove-orphans
@@ -147,9 +148,9 @@ if sudo docker ps | grep -qE "pihole.*Up" && ! sudo docker exec pihole sh -c "if
147148
sudo docker exec pihole sed -i '/^.*_.*=.*$/!d' /etc/pihole/versions # pihole-updatelists seems to break this
148149

149150
# proxy for dhcphelper
150-
sudo docker exec pihole bash -c "echo 'dhcp-option=option:dns-server,$CLS_LOCAL_IP' | tee /etc/dnsmasq.d/99-dns.conf >/dev/null" || :
151+
sudo docker exec pihole bash -c "echo 'dhcp-option=option:dns-server,$local_ip' | tee /etc/dnsmasq.d/99-dns.conf >/dev/null" || :
151152
sudo docker compose restart --no-deps pihole
152153
fi
153154

154-
cast post-up ${@@Q}
155+
eval "cast post-up ${*@Q}"
155156
popd || exit

0 commit comments

Comments
 (0)