generated from ztimson/template
Compare commits
15 Commits
ztimson-pa
...
ztimson-pa
| Author | SHA1 | Date | |
|---|---|---|---|
| 8b7702e90c | |||
| ed20d616f5 | |||
| 0180f81420 | |||
| a47fea04fb | |||
| 985c6b4ca7 | |||
| 6fca943d2c | |||
| 5c6862306d | |||
| 96c6b86d40 | |||
| 255385bf82 | |||
| d0e2752cd8 | |||
| 8758a43088 | |||
| 4f79a652e5 | |||
| 6e201723d9 | |||
| 69372f0b94 | |||
| 63a3dec564 |
112
README.md
112
README.md
@@ -1,132 +1,86 @@
|
||||
<!-- Header -->
|
||||
<div id="top" align="center">
|
||||
<br />
|
||||
|
||||
|
||||
<!-- Logo -->
|
||||
<img src="https://git.zakscode.com/repo-avatars/2b4ee6ba1f2e2618bf7694e4a52fb56d1d0ea6abafa2dcbe496ab786b86d5a76" alt="Logo" width="200" height="200">
|
||||
<img src="https://git.zakscode.com/repo-avatars/80775ae9cf0d2c4fb4aec035efb650e631d4d0046ab6fc04c6c041e91ba11d44" alt="Logo" width="200" height="200">
|
||||
|
||||
<!-- Title -->
|
||||
### Template
|
||||
|
||||
### Fallback AP
|
||||
|
||||
<!-- Description -->
|
||||
Simple repository template
|
||||
Automatic fallback Wi-Fi access point for headless Linux devices
|
||||
|
||||
<!-- Repo badges -->
|
||||
[](https://git.zakscode.com/ztimson/template/tags)
|
||||
[](https://git.zakscode.com/ztimson/template/pulls)
|
||||
[](https://git.zakscode.com/ztimson/template/issues)
|
||||
[](https://git.zakscode.com/ztimson/fallback-ap/tags)
|
||||
[](https://git.zakscode.com/ztimson/fallback-ap/pulls)
|
||||
[](https://git.zakscode.com/ztimson/fallback-ap/issues)
|
||||
|
||||
<!-- Links -->
|
||||
|
||||
---
|
||||
<div>
|
||||
<a href="https://git.zakscode.com/ztimson/template/wiki" target="_blank">Documentation</a>
|
||||
• <a href="https://git.zakscode.com/ztimson/template/releases" target="_blank">Release Notes</a>
|
||||
• <a href="https://git.zakscode.com/ztimson/template/issues/new?template=.github%2fissue_template%2fbug.md" target="_blank">Report a Bug</a>
|
||||
• <a href="https://git.zakscode.com/ztimson/template/issues/new?template=.github%2fissue_template%2fenhancement.md" target="_blank">Request a Feature</a>
|
||||
<a href="https://git.zakscode.com/ztimson/fallback-ap/wiki" target="_blank">Documentation</a>
|
||||
• <a href="https://git.zakscode.com/ztimson/fallback-ap/releases" target="_blank">Release Notes</a>
|
||||
• <a href="https://git.zakscode.com/ztimson/fallback-ap/issues/new?template=.github%2fissue_template%2fbug.md" target="_blank">Report a Bug</a>
|
||||
• <a href="https://git.zakscode.com/ztimson/fallback-ap/issues/new?template=.github%2fissue_template%2fenhancement.md" target="_blank">Request a Feature</a>
|
||||
</div>
|
||||
|
||||
---
|
||||
</div>
|
||||
|
||||
## Table of Contents
|
||||
- [Template](#top)
|
||||
- [Fallback AP](#top)
|
||||
- [About](#about)
|
||||
- [Demo](#demo)
|
||||
- [Built With](#built-with)
|
||||
- [Setup](#setup)
|
||||
- [Production](#production)
|
||||
- [Development](#development)
|
||||
- [Installation](#installation)
|
||||
- [Usage](#usage)
|
||||
- [License](#license)
|
||||
|
||||
## About
|
||||
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
|
||||
A NetworkManager-based script for headless Linux devices (e.g. a Raspberry Pi) that keeps them reachable even when their configured Wi-Fi network isn't available. When the device loses its Wi-Fi connection, it automatically stands up a local access point so you can connect directly and reconfigure it. While that access point has no clients connected, it periodically steps aside for a short window to let NetworkManager try reconnecting to a known network in the background, then brings the access point back up if nothing was found.
|
||||
|
||||
### Demo
|
||||
|
||||
Website: https://git.zakscode.com
|
||||
Runs as a `systemd` service and is managed entirely through `nmcli`/`iw` — no extra daemons (`hostapd`, `dnsmasq`, etc.) required.
|
||||
|
||||
### Built With
|
||||
[](https://angular.io/)
|
||||
[](https://www.android.com/)
|
||||
[](https://www.arduino.cc/)
|
||||
[](https://getbootstrap.com)
|
||||
[](https://en.cppreference.com/w/c/language)
|
||||
[](https://cplusplus.com/)
|
||||
[](https://dotnet.microsoft.com/)
|
||||
[](https://www.w3.org/Style/CSS/Overview.en.html)
|
||||
[](https://www.djangoproject.com/)
|
||||
[](https://docker.com/)
|
||||
[](https://www.electronjs.org/)
|
||||
[](https://firebase.google.com/)
|
||||
[](https://go.dev/)
|
||||
[](https://graphql.org/)
|
||||
[](https://developer.mozilla.org/en-US/docs/Glossary/HTML)
|
||||
[](https://java.com/)
|
||||
[](https://javascript.com/)
|
||||
[](https://jquery.com )
|
||||
[](https://laravel.com)
|
||||
[](https://www.linux.org/)
|
||||
[](https://git.zakscode.com/ztimson/momentum)
|
||||
[](https://www.mongodb.com/)
|
||||
[](https://www.mysql.com/)
|
||||
[](https://nestjs.com/)
|
||||
[](https://dotnet.microsoft.com/)
|
||||
[](https://nextjs.org/)
|
||||
[](https://www.nginx.com/)
|
||||
[](https://nodejs.org/)
|
||||
[](https://p5js.org/)
|
||||
[](https://www.php.net/)
|
||||
[](https://www.postgresql.org/)
|
||||
[](https://www.python.org/)
|
||||
[](https://reactjs.org/)
|
||||
[](https://redis.com/)
|
||||
[](https://sass-lang.com/)
|
||||
[](https://en.wikipedia.org/wiki/Shell_script)
|
||||
[](https://www.microsoft.com/en-ca/sql-server)
|
||||
[](https://www.sqlite.org/index.html)
|
||||
[](https://svelte.dev/)
|
||||
[](https://typescriptlang.org/)
|
||||
[](https://microsoft.com/windows)
|
||||
[](https://vitejs.dev/)
|
||||
[](https://vuejs.org/)
|
||||
[](https://www.linux.org/)
|
||||
|
||||
## Setup
|
||||
|
||||
<details>
|
||||
<details open>
|
||||
<summary>
|
||||
<h3 id="production" style="display: inline">
|
||||
Production
|
||||
<h3 id="installation" style="display: inline">
|
||||
Installation
|
||||
</h3>
|
||||
</summary>
|
||||
|
||||
#### Prerequisites
|
||||
- [Docker](https://docs.docker.com/install/)
|
||||
- A Linux device with a Wi-Fi adapter (`wlan0` by default) — a Raspberry Pi or similar SBC
|
||||
- `NetworkManager` and `iw` (installed automatically if missing)
|
||||
- Root access
|
||||
|
||||
#### Instructions
|
||||
1. Run the docker image: `docker run -p 80:80 git.zakscode.com/ztimson/template:latest`
|
||||
2. Open [http://localhost](http://localhost)
|
||||
1. Download & Install: `curl -sL https://git.zakscode.com/ztimson/fallback-ap/raw/branch/master/install.sh | sudo bash`
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>
|
||||
<h3 id="development" style="display: inline">
|
||||
Development
|
||||
<h3 id="usage" style="display: inline">
|
||||
Usage
|
||||
</h3>
|
||||
</summary>
|
||||
|
||||
#### Prerequisites
|
||||
- [Node.js](https://nodejs.org/en/download)
|
||||
Once installed, the service runs automatically and requires no further interaction. A few commands are available for manual control:
|
||||
|
||||
#### Instructions
|
||||
1. Install the dependencies: `npm install`
|
||||
2. Start the Angular server: `npm run start`
|
||||
3. Open [http://localhost:4200](http://localhost:4200)
|
||||
- `sudo ./pi-fallback-ap.sh status` — show service, connection, and access point state
|
||||
- `sudo ./pi-fallback-ap.sh start` / `stop` — manually bring the access point up or down
|
||||
- `sudo ./pi-fallback-ap.sh uninstall` — remove the service and access point profile
|
||||
|
||||
</details>
|
||||
|
||||
## License
|
||||
Copyright © 2023 Zakary Timson | All Rights Reserved | Available under MIT Licensing
|
||||
|
||||
See the [license](./LICENSE) for more information.
|
||||
Copyright © 2026 Zakary Timson | Available under MIT Licensing
|
||||
|
||||
@@ -3,16 +3,34 @@
|
||||
set -e
|
||||
|
||||
# Configuration =======================================================================================================
|
||||
#
|
||||
# Connection settings (SSID, passphrase, subnet, DHCP range, interface) can
|
||||
# be supplied three ways, highest priority first:
|
||||
# 1. Environment variables (FALLBACK_AP_*) - used for headless installs.
|
||||
# 2. The persisted config file below, written by `install`.
|
||||
# 3. The hard-coded defaults in this block.
|
||||
#
|
||||
# Everything else (timers, paths) is not meant to be reconfigured per-install
|
||||
# and stays as plain constants.
|
||||
|
||||
WLAN0_IFACE="wlan0"
|
||||
CONFIG_FILE="/etc/fallback-ap.conf"
|
||||
if [ -f "$CONFIG_FILE" ]; then source "$CONFIG_FILE"; fi
|
||||
|
||||
AP_NAME="${HOSTNAME}"
|
||||
AP_PASSWORD='guesswhatitis'
|
||||
AP_ENCRYPTION="wpa-psk" # none | wpa-psk | sae
|
||||
WLAN0_IFACE="${FALLBACK_AP_IFACE:-${SAVED_IFACE:-wlan0}}"
|
||||
|
||||
AP_CIDR="10.10.10.1/24"
|
||||
DHCP_START="10.10.10.100"
|
||||
DHCP_END="10.10.10.250"
|
||||
AP_NAME="${FALLBACK_AP_SSID:-${SAVED_SSID:-$HOSTNAME}}"
|
||||
AP_PASSWORD="${FALLBACK_AP_PASSWORD:-${SAVED_PASSWORD:-guesswhatitis}}"
|
||||
AP_ENCRYPTION="${FALLBACK_AP_ENCRYPTION:-${SAVED_ENCRYPTION:-wpa-psk}}" # none | wpa-psk | sae
|
||||
|
||||
FALLBACK_AP_CIDR="${FALLBACK_AP_CIDR:-${SAVED_CIDR:-10.10.10.1/24}}"
|
||||
FALLBACK_AP_DHCP_START_OCTET="${FALLBACK_AP_DHCP_START_OCTET:-${SAVED_DHCP_START_OCTET:-100}}"
|
||||
FALLBACK_AP_DHCP_END_OCTET="${FALLBACK_AP_DHCP_END_OCTET:-${SAVED_DHCP_END_OCTET:-250}}"
|
||||
|
||||
AP_CIDR="$FALLBACK_AP_CIDR"
|
||||
AP_NETWORK_BASE="${AP_CIDR%%/*}" # e.g. "10.10.10.1" out of "10.10.10.1/24"
|
||||
AP_NETWORK_BASE="${AP_NETWORK_BASE%.*}" # e.g. "10.10.10"
|
||||
DHCP_START="${AP_NETWORK_BASE}.${FALLBACK_AP_DHCP_START_OCTET}"
|
||||
DHCP_END="${AP_NETWORK_BASE}.${FALLBACK_AP_DHCP_END_OCTET}"
|
||||
|
||||
# How often, while the AP has no clients, to stop the AP and give
|
||||
# NetworkManager a chance to reconnect to a known Wi-Fi network.
|
||||
@@ -27,10 +45,10 @@ RECOVERY_WINDOW=20
|
||||
STATE_SETTLE_DELAY=1
|
||||
|
||||
CONNECTION_NAME="Fallback AP"
|
||||
SERVICE_NAME="pi-fallback-ap"
|
||||
SERVICE_NAME="fallback-ap"
|
||||
SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME.service"
|
||||
|
||||
STATE_DIR="/var/lib/pi-fallback-ap"
|
||||
STATE_DIR="/var/lib/fallback-ap"
|
||||
LAST_WIFI_FILE="$STATE_DIR/last-wifi"
|
||||
LOCK_FILE="$STATE_DIR/lock"
|
||||
STATE_FILE="$STATE_DIR/state"
|
||||
@@ -189,16 +207,6 @@ stop_ap() {
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
# ==========================================================================
|
||||
# One tick of the control loop. Called once per second, always while
|
||||
# holding the wlan0 lock, so everything here runs to completion before
|
||||
# the next tick starts - there is no interleaving to race against.
|
||||
#
|
||||
# `last_attempt` is intentionally not declared `local` here: it lives in
|
||||
# run()'s scope and this function updates it directly (bash resolves it
|
||||
# via the caller's scope as long as nothing else shadows it).
|
||||
# ==========================================================================
|
||||
tick() {
|
||||
local now="$1"
|
||||
|
||||
@@ -227,8 +235,6 @@ tick() {
|
||||
clients="$(get_client_count)"
|
||||
|
||||
if [ "$clients" -gt 0 ]; then
|
||||
# Someone is using the AP right now. Leave it alone and push the
|
||||
# next recovery attempt back so we don't kick them off.
|
||||
last_attempt="$now"
|
||||
return
|
||||
fi
|
||||
@@ -239,9 +245,6 @@ tick() {
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
# Stop the AP, give NetworkManager a window to autoconnect to any known
|
||||
# network it can see, and bring the AP back if that doesn't happen.
|
||||
attempt_recovery() {
|
||||
log "No AP clients, checking for known Wi-Fi (AP down for up to ${RECOVERY_WINDOW}s)"
|
||||
|
||||
@@ -276,6 +279,21 @@ attempt_recovery() {
|
||||
}
|
||||
|
||||
|
||||
save_config() {
|
||||
mkdir -p "$(dirname "$CONFIG_FILE")"
|
||||
cat > "$CONFIG_FILE" <<EOF
|
||||
SAVED_IFACE="$WLAN0_IFACE"
|
||||
SAVED_SSID="$AP_NAME"
|
||||
SAVED_PASSWORD="$AP_PASSWORD"
|
||||
SAVED_ENCRYPTION="$AP_ENCRYPTION"
|
||||
SAVED_CIDR="$FALLBACK_AP_CIDR"
|
||||
SAVED_DHCP_START_OCTET="$FALLBACK_AP_DHCP_START_OCTET"
|
||||
SAVED_DHCP_END_OCTET="$FALLBACK_AP_DHCP_END_OCTET"
|
||||
EOF
|
||||
chmod 600 "$CONFIG_FILE"
|
||||
}
|
||||
|
||||
|
||||
install() {
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
echo "Run this script as root."
|
||||
@@ -297,6 +315,7 @@ install() {
|
||||
fi
|
||||
|
||||
create_connection
|
||||
save_config
|
||||
|
||||
log "Creating recovery service"
|
||||
|
||||
@@ -324,6 +343,7 @@ EOF
|
||||
|
||||
log "Installation complete"
|
||||
log "Script: $SCRIPT_PATH"
|
||||
log "Config file: $CONFIG_FILE"
|
||||
log "SSID: $AP_NAME"
|
||||
log "Interface: $WLAN0_IFACE"
|
||||
log "Network: $AP_CIDR"
|
||||
@@ -411,6 +431,7 @@ uninstall() {
|
||||
nmcli connection delete "$CONNECTION_NAME" >/dev/null 2>&1 || true
|
||||
|
||||
rm -f "$SERVICE_FILE"
|
||||
rm -f "$CONFIG_FILE"
|
||||
rm -rf "$STATE_DIR"
|
||||
|
||||
systemctl daemon-reload
|
||||
@@ -460,6 +481,13 @@ status() {
|
||||
echo "=== Fallback AP ==="
|
||||
nmcli connection show "$CONNECTION_NAME" 2>/dev/null || echo "Not installed"
|
||||
|
||||
echo
|
||||
echo "=== Config ($CONFIG_FILE) ==="
|
||||
echo "SSID: $AP_NAME"
|
||||
echo "Interface: $WLAN0_IFACE"
|
||||
echo "Network: $AP_CIDR"
|
||||
echo "DHCP: $DHCP_START - $DHCP_END"
|
||||
|
||||
echo
|
||||
echo "=== Last Known Wi-Fi ==="
|
||||
get_last_wifi || echo "None"
|
||||
@@ -486,13 +514,6 @@ run() {
|
||||
|
||||
log "Fallback AP service started"
|
||||
log "Script: $SCRIPT_PATH"
|
||||
|
||||
# Open the lock fd once for the life of the process. Using `flock`
|
||||
# directly against this fd (instead of wrapping each tick in its own
|
||||
# `( ... ) 9>"$LOCK_FILE"` subshell) matters: any variable a tick
|
||||
# updates - like last_attempt below - has to survive into the next
|
||||
# loop iteration, and a subshell would silently throw those updates
|
||||
# away when it exits.
|
||||
exec 9>"$LOCK_FILE"
|
||||
|
||||
flock 9
|
||||
|
||||
34
install.sh
Normal file
34
install.sh
Normal file
@@ -0,0 +1,34 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
SCRIPT_URL="https://git.zakscode.com/ztimson/fallback-ap/raw/branch/master/fallback-ap.sh"
|
||||
INSTALL_PATH="/usr/local/bin/fallback-ap"
|
||||
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
echo "This installer needs to run as root, e.g.:"
|
||||
echo " curl -sL https://git.zakscode.com/ztimson/fallback-ap/raw/branch/master/install.sh | sudo bash"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
prompt() {
|
||||
local __var="$1" __question="$2" __default="$3" __answer=""
|
||||
if [ -e /dev/tty ]; then
|
||||
read -r -p "$__question [$__default]: " __answer < /dev/tty || __answer=""
|
||||
fi
|
||||
printf -v "$__var" '%s' "${__answer:-$__default}"
|
||||
}
|
||||
|
||||
[ -z "${FALLBACK_AP_SSID:-}" ] && prompt FALLBACK_AP_SSID "Access point SSID" "$(hostname)"
|
||||
[ -z "${FALLBACK_AP_PASSWORD:-}" ] && prompt FALLBACK_AP_PASSWORD "Access point passphrase" "guesswhatitis"
|
||||
[ -z "${FALLBACK_AP_CIDR:-}" ] && prompt FALLBACK_AP_CIDR "Access point subnet (CIDR)" "10.10.10.1/24"
|
||||
[ -z "${FALLBACK_AP_DHCP_START_OCTET:-}" ] && prompt FALLBACK_AP_DHCP_START_OCTET "DHCP range start (last octet)" "100"
|
||||
[ -z "${FALLBACK_AP_DHCP_END_OCTET:-}" ] && prompt FALLBACK_AP_DHCP_END_OCTET "DHCP range end (last octet)" "250"
|
||||
|
||||
export FALLBACK_AP_SSID FALLBACK_AP_PASSWORD FALLBACK_AP_CIDR FALLBACK_AP_DHCP_START_OCTET FALLBACK_AP_DHCP_END_OCTET
|
||||
|
||||
echo "Downloading fallback-ap..."
|
||||
curl -fsSL "$SCRIPT_URL" -o "$INSTALL_PATH"
|
||||
chmod +x "$INSTALL_PATH"
|
||||
echo "Running installer..."
|
||||
"$INSTALL_PATH" install
|
||||
Reference in New Issue
Block a user