Select physical AWUS for wardriving capture
This commit is contained in:
1 parent
781c02d7e4
commit
2ee478591f
9 files changed
+190
-78
No files matched your search
@@ -175,7 +175,7 @@ Important values:
|
||||
- `WARDRIVING_ENABLED`: enables the opt-in Kismet session controller; the installer sets this to `true` on Raspberry Pi deployments
|
||||
- `WARDRIVING_KISMET_URL`: local Kismet API URL, normally `http://127.0.0.1:2501`
|
||||
- `WARDRIVING_ADAPTER_SELECTION_PATH`: service-owned persistent hardware selection, normally `/var/lib/pi-car-companion/wardriving-adapter-selection.json`
|
||||
- `WARDRIVING_WIFI_INTERFACE`: legacy dedicated monitor-mode WiFi interface override; when empty, the dashboard prefers a detected Alfa AWUS 1900 by USB identity and then another eligible WiFi adapter. The built-in `wlan0` hotspot is never commandeered; selecting an AWUS 1900 currently used as `wlan1` performs a controlled upstream handoff for the session.
|
||||
- `WARDRIVING_WIFI_INTERFACE`: legacy dedicated monitor-mode WiFi interface override; when empty, the dashboard prefers a detected Alfa AWUS 1900 by USB identity and then another eligible WiFi adapter. The built-in `wlan0` hotspot is never selectable. Kismet owns the selected physical adapter only when a wardriving session is started; the session handoff temporarily releases an upstream AWUS, and monitor children such as `wlan1mon` are hidden from the adapter picker.
|
||||
- `WARDRIVING_BLUETOOTH_INTERFACE`: Bluetooth adapter used by Kismet, normally `hci0`
|
||||
- `WARDRIVING_POLL_SECONDS` / `WARDRIVING_MAX_SIGHTINGS`: Kismet polling interval and bounded observation retention
|
||||
|
||||
@@ -183,10 +183,12 @@ Important values:
|
||||
|
||||
The installer installs Kismet and a manually controlled `pi-car-companion-kismet.service`. It does not start capture at boot. Open **Wardriving** in the dashboard (or the Android companion), pair a phone, and start a session when the vehicle is parked or moving in a place where discovery is lawful. The official Kismet repository currently provides the required capture packages for 64-bit Raspberry Pi OS; the installer stops with an explicit architecture message on unsupported 32-bit images.
|
||||
|
||||
Use a dedicated USB WiFi adapter that supports monitor mode. The Wardriving screen lists live WiFi hardware, identifies USB adapters by vendor/model/path or serial, and marks the Alfa AWUS 1900 as the default when present. The built-in `wlan0` hotspot remains protected. If the AWUS 1900 is currently the `wlan1` upstream adapter, starting a session pauses the upstream NetworkManager controller and releases only that adapter for monitor capture; the phone-facing hotspot stays online, and stopping the session reconnects upstream and resumes the controller. The selected hardware identity is persisted across interface renames and revalidated by the privileged Kismet helper before every start. Bluetooth capture uses the Pi's `hci0` adapter by default and Kismet's discoverable-device scan; a second Bluetooth adapter can be selected with `WARDRIVING_BLUETOOTH_INTERFACE` if the companion BLE link needs its own controller. It does not pair with devices or enumerate services. Kismet is configured for live device tracking without retaining raw packet payloads. Pi Car Companion stores bounded sightings and joins them to a phone GPS fix only while that fix is fresh and accurate. Observations without a suitable fix remain visible but are marked unmapped. The Android companion receives the same adapter inventory and can change the saved selection while a session is stopped.
|
||||
Use a dedicated USB WiFi adapter that supports monitor mode. The Wardriving screen lists live physical WiFi hardware, identifies USB adapters by vendor/model/path or serial, and marks the Alfa AWUS 1900 as the default when present. The built-in `wlan0` hotspot remains protected. When Start is clicked, the privileged helper writes the selected physical radio into Kismet's source configuration, starts Kismet, and verifies that Kismet created monitor mode; monitor children such as `wlan1mon` are never treated as separate selectable hardware. The selected hardware identity is persisted across interface renames and revalidated by the privileged Kismet helper before every start. Bluetooth capture uses the Pi's `hci0` adapter by default and Kismet's discoverable-device scan; a second Bluetooth adapter can be selected with `WARDRIVING_BLUETOOTH_INTERFACE` if the companion BLE link needs its own controller. It does not pair with devices or enumerate services. Kismet is configured for live device tracking without retaining raw packet payloads. Pi Car Companion stores bounded sightings and joins them to a phone GPS fix only while that fix is fresh and accurate. Observations without a suitable fix remain visible but are marked unmapped. The Android companion receives the same adapter inventory and can change the saved selection while a session is stopped.
|
||||
|
||||
The UI exposes the active session, Kismet and GPS health, WiFi/Bluetooth counts, recent device metadata, a Leaflet map of mapped sightings, session controls, and data clearing. The Android app shows the same live summary over its existing Bluetooth bridge and can start or stop the session. Revoke a paired phone in **Settings → Mobile** to stop it contributing location data.
|
||||
|
||||
Wardriving control output is appended to `/var/log/pi-car-companion/wardriving-control.log` with a small automatic rollover; Kismet's generated session files remain under `/var/lib/pi-car-companion/kismet`.
|
||||
|
||||
### Dual-radio car network
|
||||
|
||||
The optional network controller prefers a USB Wi-Fi adapter for upstream internet and keeps the built-in adapter on a stable car hotspot. If the USB adapter disappears, the built-in adapter tries its saved upstream profiles for 60 seconds. When none connect, the controller restores the offline hotspot at `http://10.42.0.1:8787`.
|
||||
|
||||
@@ -202,6 +202,7 @@ install -d -m 0750 -o "${SERVICE_USER}" -g "${SERVICE_USER}" "${DATA_DIR}"
|
||||
# atomically replaces its JSON state files there.
|
||||
chown "${SERVICE_USER}:${SERVICE_USER}" "${DATA_DIR}"
|
||||
chmod 0750 "${DATA_DIR}"
|
||||
install -d -m 0750 -o root -g "${SERVICE_USER}" /var/log/${APP_NAME}
|
||||
install -d -m 0750 -o root -g "${SERVICE_USER}" "${CONFIG_DIR}"
|
||||
|
||||
echo "[4/7] Copying and building the application"
|
||||
@@ -297,6 +298,7 @@ WARDRIVING_STATUS_PATH=${DATA_DIR}/wardriving-status.json
|
||||
WARDRIVING_KISMET_URL=http://127.0.0.1:2501
|
||||
WARDRIVING_KISMET_AUTH_PATH=${DATA_DIR}/kismet/kismet-api-credentials.json
|
||||
WARDRIVING_ADAPTER_SELECTION_PATH=${DATA_DIR}/wardriving-adapter-selection.json
|
||||
WARDRIVING_LOG_PATH=/var/log/${APP_NAME}/wardriving-control.log
|
||||
WARDRIVING_POLL_SECONDS=5
|
||||
WARDRIVING_MAX_SIGHTINGS=250000
|
||||
WARDRIVING_WIFI_INTERFACE=
|
||||
@@ -327,6 +329,7 @@ ensure_env_default WARDRIVING_STATUS_PATH "${DATA_DIR}/wardriving-status.json"
|
||||
ensure_env_default WARDRIVING_KISMET_URL http://127.0.0.1:2501
|
||||
ensure_env_default WARDRIVING_KISMET_AUTH_PATH "${DATA_DIR}/kismet/kismet-api-credentials.json"
|
||||
ensure_env_default WARDRIVING_ADAPTER_SELECTION_PATH "${DATA_DIR}/wardriving-adapter-selection.json"
|
||||
ensure_env_default WARDRIVING_LOG_PATH "/var/log/${APP_NAME}/wardriving-control.log"
|
||||
ensure_env_default WARDRIVING_POLL_SECONDS 5
|
||||
ensure_env_default WARDRIVING_MAX_SIGHTINGS 250000
|
||||
ensure_env_default WARDRIVING_WIFI_INTERFACE ""
|
||||
|
||||
@@ -29,6 +29,7 @@ ensure_service_data_directory() {
|
||||
install -d -m 0750 -o "${SERVICE_USER}" -g "${SERVICE_USER}" "${DATA_DIR}"
|
||||
chown "${SERVICE_USER}:${SERVICE_USER}" "${DATA_DIR}"
|
||||
chmod 0750 "${DATA_DIR}"
|
||||
install -d -m 0750 -o root -g "${SERVICE_USER}" "/var/log/${APP_NAME}"
|
||||
}
|
||||
|
||||
if [[ "${1:-}" == "--check-only" ]]; then
|
||||
|
||||
@@ -28,31 +28,45 @@ if [[ -f "${ENV_FILE}" ]]; then
|
||||
source "${ENV_FILE}"
|
||||
fi
|
||||
|
||||
LOG_FILE="${WARDRIVING_LOG_PATH:-/var/log/pi-car-companion/wardriving-control.log}"
|
||||
setup_logging() {
|
||||
local log_dir
|
||||
log_dir="$(dirname -- "${LOG_FILE}")"
|
||||
install -d -m 0750 -o root -g pi-companion "${log_dir}" 2>/dev/null || return 0
|
||||
if [[ -f "${LOG_FILE}" ]] && [[ "$(stat -c '%s' "${LOG_FILE}" 2>/dev/null || printf 0)" -gt 5242880 ]]; then
|
||||
mv -f -- "${LOG_FILE}" "${LOG_FILE}.1" 2>/dev/null || true
|
||||
fi
|
||||
touch "${LOG_FILE}" 2>/dev/null || return 0
|
||||
chmod 0640 "${LOG_FILE}" 2>/dev/null || true
|
||||
chown root:pi-companion "${LOG_FILE}" 2>/dev/null || true
|
||||
exec > >(tee -a "${LOG_FILE}") 2>&1
|
||||
}
|
||||
setup_logging
|
||||
printf '[%s] wardriving-control action=%s\n' "$(date --iso-8601=seconds)" "${ACTION}"
|
||||
|
||||
log_exit() {
|
||||
local status=$?
|
||||
printf '[%s] wardriving-control action=%s exit=%s\n' "$(date --iso-8601=seconds)" "${ACTION}" "${status}"
|
||||
trap - EXIT
|
||||
exit "${status}"
|
||||
}
|
||||
trap log_exit EXIT
|
||||
|
||||
AUTH_PATH="${WARDRIVING_KISMET_AUTH_PATH:-${DATA_DIR}/kismet-api-credentials.json}"
|
||||
SELECTION_PATH="${WARDRIVING_ADAPTER_SELECTION_PATH:-/var/lib/pi-car-companion/wardriving-adapter-selection.json}"
|
||||
|
||||
wifi_interface="${WARDRIVING_WIFI_INTERFACE:-}"
|
||||
bluetooth_interface="${WARDRIVING_BLUETOOTH_INTERFACE-hci0}"
|
||||
hotspot_interface="${NETWORK_HOTSPOT_INTERFACE:-wlan0}"
|
||||
upstream_interface="${NETWORK_UPSTREAM_INTERFACE:-wlan1}"
|
||||
network_handoff=false
|
||||
|
||||
is_hotspot_interface() {
|
||||
local interface_name="$1"
|
||||
[[ "${interface_name}" == "${hotspot_interface}" || "${interface_name}" == "wlan0" ]]
|
||||
}
|
||||
|
||||
is_upstream_interface() {
|
||||
local interface_name="$1"
|
||||
[[ "${interface_name}" == "${upstream_interface}" || "${interface_name}" == "wlan1" ]]
|
||||
}
|
||||
|
||||
is_capture_reserved_interface() {
|
||||
local interface_name="$1"
|
||||
is_hotspot_interface "${interface_name}" && return 0
|
||||
if is_upstream_interface "${interface_name}" && ! is_awus1900 "${interface_name}"; then
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -92,12 +106,24 @@ is_awus1900() {
|
||||
grep -Eiq '(^|=|[^0-9])(AWUS|ALFA|RTL8814AU|8814AU|8813)([^0-9]|$)' <<<"${properties}"
|
||||
}
|
||||
|
||||
is_monitor_interface() {
|
||||
local interface_name="$1"
|
||||
[[ "${interface_name}" =~ ^(wlan|wl)[a-zA-Z0-9._-]*mon[0-9]*$ || "${interface_name}" =~ (^|[._-])mon[0-9]*$ ]] && return 0
|
||||
if command -v iw >/dev/null 2>&1; then
|
||||
iw dev "${interface_name}" info 2>/dev/null | grep -Eiq '^\s*type\s+monitor\s*$' && return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
wifi_candidates() {
|
||||
{
|
||||
find /sys/class/net -mindepth 1 -maxdepth 1 -type l -exec test -e '{}/wireless' \; -printf '%f\n' 2>/dev/null || true
|
||||
nmcli -t --escape no -f DEVICE,TYPE,STATE,CONNECTION device 2>/dev/null \
|
||||
| awk -F: '$2 == "wifi" { print $1 }' || true
|
||||
} | sort -u
|
||||
} | sort -u | while IFS= read -r candidate; do
|
||||
[[ -n "${candidate}" ]] || continue
|
||||
is_monitor_interface "${candidate}" || printf '%s\n' "${candidate}"
|
||||
done
|
||||
}
|
||||
|
||||
monitor_interfaces() {
|
||||
@@ -145,7 +171,7 @@ if [[ "${ACTION}" != "stop" ]]; then
|
||||
fi
|
||||
done < <(wifi_candidates)
|
||||
if [[ -z "${wifi_interface}" ]]; then
|
||||
echo "The saved wardriving adapter is not present or reserved for networking." >&2
|
||||
echo "The saved wardriving adapter is not present or is the protected Pi hotspot." >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
@@ -171,10 +197,6 @@ if [[ "${ACTION}" != "stop" ]]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ "${ACTION}" != "stop" && -n "${wifi_interface}" ]] && is_upstream_interface "${wifi_interface}" && is_awus1900 "${wifi_interface}"; then
|
||||
network_handoff=true
|
||||
fi
|
||||
|
||||
if [[ "${ACTION}" != "stop" && ( ( -n "${wifi_interface}" && ! "${wifi_interface}" =~ ^[a-zA-Z0-9._-]{1,64}$ ) || ( -n "${bluetooth_interface}" && ! "${bluetooth_interface}" =~ ^[a-zA-Z0-9._-]{1,64}$ ) ) ]]; then
|
||||
echo "Wardriving interfaces contain unsupported characters." >&2
|
||||
exit 1
|
||||
@@ -184,7 +206,7 @@ if [[ "${ACTION}" != "stop" && -n "${wifi_interface}" && ! -e "/sys/class/net/${
|
||||
exit 1
|
||||
fi
|
||||
if [[ "${ACTION}" != "stop" && -n "${wifi_interface}" ]] && is_capture_reserved_interface "${wifi_interface}"; then
|
||||
echo "Wardriving cannot use the hotspot or upstream WiFi interface (${wifi_interface})." >&2
|
||||
echo "Wardriving cannot use the Pi hotspot interface (${wifi_interface})." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -239,24 +261,19 @@ EOF
|
||||
chown root:kismet "${CONFIG}"
|
||||
}
|
||||
|
||||
stop_bluetooth_discovery() {
|
||||
if [[ -z "${bluetooth_interface}" || ! "${bluetooth_interface}" =~ ^hci[0-9]+$ ]] || ! command -v btmgmt >/dev/null 2>&1; then
|
||||
return
|
||||
configured_wifi_interface() {
|
||||
local configured
|
||||
configured="$(sed -n 's/^source=\([^:[:space:]]*\):name=car-wifi$/\1/p' "${CONFIG}" 2>/dev/null | head -n 1)"
|
||||
if [[ "${configured}" =~ ^[a-zA-Z0-9._-]{1,64}$ ]]; then
|
||||
printf '%s\n' "${configured}"
|
||||
fi
|
||||
timeout 5 btmgmt --index "${bluetooth_interface#hci}" stop-discovery >/dev/null 2>&1 || true
|
||||
}
|
||||
|
||||
prepare_network_handoff() {
|
||||
[[ "${network_handoff}" == true ]] || return 0
|
||||
if [[ -f "${HANDOFF_MARKER}" ]]; then
|
||||
return
|
||||
fi
|
||||
local network_was_active=false
|
||||
local managed_state connection_name connection_uuid device_state was_connected=false
|
||||
local prior_monitor_interfaces
|
||||
if systemctl is-active --quiet "${NETWORK_SERVICE}"; then
|
||||
network_was_active=true
|
||||
fi
|
||||
prepare_capture_handoff() {
|
||||
[[ -n "${wifi_interface}" ]] || return 0
|
||||
[[ -f "${HANDOFF_MARKER}" ]] && return 0
|
||||
local network_was_active=false managed_state connection_name connection_uuid device_state was_connected=false prior_monitor_interfaces
|
||||
if systemctl is-active --quiet "${NETWORK_SERVICE}"; then network_was_active=true; fi
|
||||
managed_state="$(nmcli --get-values GENERAL.NM-MANAGED device show "${wifi_interface}" 2>/dev/null | sed -n '1p' | tr -d '[:space:]')"
|
||||
connection_name="$(nmcli --get-values GENERAL.CONNECTION device show "${wifi_interface}" 2>/dev/null | sed -n '1p')"
|
||||
[[ "${connection_name}" == "--" ]] && connection_name=""
|
||||
@@ -268,22 +285,23 @@ prepare_network_handoff() {
|
||||
[[ "${device_state}" == 100* ]] && was_connected=true
|
||||
prior_monitor_interfaces="$(monitor_interfaces "${wifi_interface}" | paste -sd, -)"
|
||||
if [[ "${managed_state}" != yes && "${managed_state}" != no ]]; then
|
||||
echo "Could not determine the selected adapter's NetworkManager state." >&2
|
||||
echo "Could not determine the selected WiFi adapter's NetworkManager state." >&2
|
||||
return 1
|
||||
fi
|
||||
install -d -m 0750 -o root -g pi-companion "${STATE_DIR}"
|
||||
install -d -m 0750 -o pi-companion -g pi-companion "${STATE_DIR}"
|
||||
printf 'interface=%s\nnetwork_service_active=%s\nmanaged=%s\nconnected=%s\nconnection_uuid=%s\nmonitor_interfaces=%s\n' \
|
||||
"${wifi_interface}" "${network_was_active}" "${managed_state}" "${was_connected}" "${connection_uuid}" "${prior_monitor_interfaces}" > "${HANDOFF_MARKER}"
|
||||
chmod 0600 "${HANDOFF_MARKER}"
|
||||
chown root:pi-companion "${HANDOFF_MARKER}"
|
||||
if [[ "${network_was_active}" == true ]] && ! timeout 20 systemctl stop "${NETWORK_SERVICE}" >/dev/null 2>&1; then
|
||||
echo "The Pi network controller could not be stopped for the wardriving handoff." >&2
|
||||
echo "The Pi network controller could not be paused for capture." >&2
|
||||
return 1
|
||||
fi
|
||||
if ! timeout 5 nmcli --wait 0 device set "${wifi_interface}" managed no; then
|
||||
return 1
|
||||
fi
|
||||
timeout 5 nmcli --wait 0 device disconnect "${wifi_interface}" >/dev/null 2>&1 || true
|
||||
printf '[%s] capture handoff released interface=%s\n' "$(date --iso-8601=seconds)" "${wifi_interface}"
|
||||
}
|
||||
|
||||
handoff_value() {
|
||||
@@ -291,7 +309,7 @@ handoff_value() {
|
||||
sed -n "s/^${key}=//p" "${HANDOFF_MARKER}" | sed -n '1p'
|
||||
}
|
||||
|
||||
restore_network_handoff() {
|
||||
restore_capture_handoff() {
|
||||
[[ -f "${HANDOFF_MARKER}" ]] || return 0
|
||||
local handoff_interface network_was_active prior_managed prior_connected connection_uuid prior_monitor_interfaces
|
||||
if grep -q '^interface=' "${HANDOFF_MARKER}"; then
|
||||
@@ -302,8 +320,6 @@ restore_network_handoff() {
|
||||
connection_uuid="$(handoff_value connection_uuid | tr -d '\r\n')"
|
||||
prior_monitor_interfaces="$(handoff_value monitor_interfaces | tr -d '\r\n')"
|
||||
else
|
||||
# Compatibility with the two-line marker written by releases before the
|
||||
# exact NetworkManager state snapshot was added.
|
||||
handoff_interface="$(sed -n '1p' "${HANDOFF_MARKER}" | tr -d '\r\n')"
|
||||
network_was_active="$(sed -n '2p' "${HANDOFF_MARKER}" | tr -d '\r\n')"
|
||||
prior_managed=yes
|
||||
@@ -311,40 +327,81 @@ restore_network_handoff() {
|
||||
connection_uuid=""
|
||||
prior_monitor_interfaces=""
|
||||
fi
|
||||
if [[ ! "${handoff_interface}" =~ ^[a-zA-Z0-9._-]{1,64}$ ]]; then
|
||||
rm -f -- "${HANDOFF_MARKER}"
|
||||
return
|
||||
fi
|
||||
if [[ "${prior_managed}" == yes ]]; then
|
||||
timeout 5 nmcli --wait 0 device set "${handoff_interface}" managed yes >/dev/null 2>&1 || true
|
||||
if [[ "${prior_connected}" == true ]]; then
|
||||
if [[ -n "${connection_uuid}" ]]; then
|
||||
timeout 25 nmcli connection up uuid "${connection_uuid}" ifname "${handoff_interface}" >/dev/null 2>&1 || true
|
||||
else
|
||||
timeout 25 nmcli device connect "${handoff_interface}" >/dev/null 2>&1 || true
|
||||
if [[ "${handoff_interface}" =~ ^[a-zA-Z0-9._-]{1,64}$ ]]; then
|
||||
if [[ "${prior_managed}" == yes ]]; then
|
||||
timeout 5 nmcli --wait 0 device set "${handoff_interface}" managed yes >/dev/null 2>&1 || true
|
||||
if [[ "${prior_connected}" == true ]]; then
|
||||
if [[ -n "${connection_uuid}" ]]; then
|
||||
timeout 25 nmcli connection up uuid "${connection_uuid}" ifname "${handoff_interface}" >/dev/null 2>&1 || true
|
||||
else
|
||||
timeout 25 nmcli device connect "${handoff_interface}" >/dev/null 2>&1 || true
|
||||
fi
|
||||
fi
|
||||
else
|
||||
timeout 5 nmcli --wait 0 device disconnect "${handoff_interface}" >/dev/null 2>&1 || true
|
||||
elif [[ "${prior_managed}" == no ]]; then
|
||||
timeout 5 nmcli --wait 0 device set "${handoff_interface}" managed no >/dev/null 2>&1 || true
|
||||
fi
|
||||
elif [[ "${prior_managed}" == no ]]; then
|
||||
timeout 5 nmcli --wait 0 device set "${handoff_interface}" managed no >/dev/null 2>&1 || true
|
||||
remove_monitor_interfaces "${handoff_interface}" "${prior_monitor_interfaces}"
|
||||
fi
|
||||
remove_monitor_interfaces "${handoff_interface}" "${prior_monitor_interfaces}"
|
||||
if [[ "${network_was_active}" == true ]]; then
|
||||
timeout 20 systemctl start "${NETWORK_SERVICE}" >/dev/null 2>&1 || true
|
||||
fi
|
||||
rm -f -- "${HANDOFF_MARKER}"
|
||||
printf '[%s] capture handoff restored interface=%s\n' "$(date --iso-8601=seconds)" "${handoff_interface:-unknown}"
|
||||
}
|
||||
|
||||
capture_is_monitor() {
|
||||
local interface_name="$1"
|
||||
if [[ -z "${interface_name}" ]] || ! command -v iw >/dev/null 2>&1; then
|
||||
return 1
|
||||
fi
|
||||
if iw dev "${interface_name}" info 2>/dev/null | grep -Eiq '^\s*type\s+monitor\s*$'; then
|
||||
return 0
|
||||
fi
|
||||
[[ -n "$(monitor_interfaces "${interface_name}")" ]]
|
||||
}
|
||||
|
||||
log_capture_diagnostics() {
|
||||
printf '[%s] kismet_active=%s selected_interface=%s\n' \
|
||||
"$(date --iso-8601=seconds)" \
|
||||
"$(systemctl is-active "${SERVICE}" 2>/dev/null || true)" \
|
||||
"${wifi_interface:-none}"
|
||||
if command -v iw >/dev/null 2>&1; then
|
||||
iw dev 2>&1 || true
|
||||
fi
|
||||
if command -v journalctl >/dev/null 2>&1; then
|
||||
journalctl -u "${SERVICE}" -n 40 --no-pager 2>&1 || true
|
||||
fi
|
||||
}
|
||||
|
||||
stop_bluetooth_discovery() {
|
||||
if [[ -z "${bluetooth_interface}" || ! "${bluetooth_interface}" =~ ^hci[0-9]+$ ]] || ! command -v btmgmt >/dev/null 2>&1; then
|
||||
return
|
||||
fi
|
||||
timeout 5 btmgmt --index "${bluetooth_interface#hci}" stop-discovery >/dev/null 2>&1 || true
|
||||
}
|
||||
|
||||
case "${ACTION}" in
|
||||
start)
|
||||
write_config
|
||||
if ! prepare_network_handoff; then
|
||||
restore_network_handoff
|
||||
remove_monitor_interfaces "${wifi_interface}"
|
||||
printf '[%s] selected_wifi_interface=%s selected_is_awus1900=%s\n' "$(date --iso-8601=seconds)" "${wifi_interface:-none}" "$(if [[ -n "${wifi_interface}" ]] && is_awus1900 "${wifi_interface}"; then printf true; else printf false; fi)"
|
||||
if ! prepare_capture_handoff; then
|
||||
restore_capture_handoff
|
||||
exit 1
|
||||
fi
|
||||
if ! timeout 30 systemctl start "${SERVICE}"; then
|
||||
restore_network_handoff
|
||||
log_capture_diagnostics
|
||||
restore_capture_handoff
|
||||
remove_monitor_interfaces "${wifi_interface}"
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
if [[ -n "${wifi_interface}" ]] && ! capture_is_monitor "${wifi_interface}"; then
|
||||
echo "Kismet started, but the selected WiFi adapter did not enter monitor mode." >&2
|
||||
log_capture_diagnostics
|
||||
systemctl stop "${SERVICE}" || true
|
||||
restore_capture_handoff
|
||||
remove_monitor_interfaces "${wifi_interface}"
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
@@ -352,17 +409,32 @@ case "${ACTION}" in
|
||||
stop_status=0
|
||||
systemctl stop "${SERVICE}" || stop_status=$?
|
||||
stop_bluetooth_discovery
|
||||
restore_network_handoff
|
||||
restore_capture_handoff
|
||||
remove_monitor_interfaces "$(configured_wifi_interface)"
|
||||
log_capture_diagnostics
|
||||
exit "${stop_status}"
|
||||
;;
|
||||
restart)
|
||||
write_config
|
||||
if ! prepare_network_handoff; then
|
||||
restore_network_handoff
|
||||
remove_monitor_interfaces "${wifi_interface}"
|
||||
printf '[%s] selected_wifi_interface=%s selected_is_awus1900=%s\n' "$(date --iso-8601=seconds)" "${wifi_interface:-none}" "$(if [[ -n "${wifi_interface}" ]] && is_awus1900 "${wifi_interface}"; then printf true; else printf false; fi)"
|
||||
if ! prepare_capture_handoff; then
|
||||
restore_capture_handoff
|
||||
exit 1
|
||||
fi
|
||||
if ! timeout 30 systemctl restart "${SERVICE}"; then
|
||||
restore_network_handoff
|
||||
log_capture_diagnostics
|
||||
restore_capture_handoff
|
||||
remove_monitor_interfaces "${wifi_interface}"
|
||||
exit 1
|
||||
fi
|
||||
sleep 1
|
||||
if [[ -n "${wifi_interface}" ]] && ! capture_is_monitor "${wifi_interface}"; then
|
||||
echo "Kismet restarted, but the selected WiFi adapter did not enter monitor mode." >&2
|
||||
log_capture_diagnostics
|
||||
systemctl stop "${SERVICE}" || true
|
||||
restore_capture_handoff
|
||||
remove_monitor_interfaces "${wifi_interface}"
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
|
||||
+1
-1
@@ -467,7 +467,7 @@ export async function buildApp(config: AppConfig): Promise<FastifyInstance> {
|
||||
? "Wardriving is not enabled in the Pi configuration"
|
||||
: error instanceof Error && error.message === "WARDRIVING_WIFI_ADAPTER_REQUIRED"
|
||||
? "Select an eligible WiFi monitor adapter before starting a session"
|
||||
: "The Pi could not change the Kismet wardriving service"
|
||||
: "Wardriving control failed; check /var/log/pi-car-companion/wardriving-control.log"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -201,7 +201,7 @@ export function registerMobileRoutes(
|
||||
return reply.code(503).send({
|
||||
error: {
|
||||
code: reason === "WARDRIVING_WIFI_ADAPTER_REQUIRED" ? reason : "WARDRIVING_CONTROL_FAILED",
|
||||
message: reason === "WARDRIVING_WIFI_ADAPTER_REQUIRED" ? "Select an eligible WiFi monitor adapter first" : "The Pi could not change the wardriving session"
|
||||
message: reason === "WARDRIVING_WIFI_ADAPTER_REQUIRED" ? "Select an eligible WiFi monitor adapter first" : "Wardriving control failed; check the Pi wardriving log"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -182,6 +182,13 @@ function isAwus1900(properties: UdevProperties, driver: string | null, usbId: st
|
||||
return /alfa|awus|rtl8814au|8814au|(?:^|[^0-9])8813(?:[^0-9]|$)/i.test(text);
|
||||
}
|
||||
|
||||
/** Monitor-mode children (for example wlan1mon) are created by Kismet and are
|
||||
* not separate hardware adapters. Keep the physical parent in the picker so
|
||||
* the saved AWUS identity remains selectable while a session is running. */
|
||||
export function isMonitorWardrivingInterface(interfaceName: string): boolean {
|
||||
return /^(?:wlan|wl)[a-z0-9._-]*mon[0-9]*$/i.test(interfaceName) || /(?:^|[._-])mon[0-9]*$/i.test(interfaceName);
|
||||
}
|
||||
|
||||
function stableAdapterId(properties: UdevProperties, macAddress: string): string {
|
||||
const safe = (value: string): string => value.replace(/[^a-zA-Z0-9._:/-]/g, "_");
|
||||
const vendorId = properties.ID_VENDOR_ID?.trim().toLowerCase() || "unknown-vendor";
|
||||
@@ -210,7 +217,6 @@ function adapterLabel(
|
||||
|
||||
export function isProtectedWardrivingInterface(interfaceName: string, config: WardrivingConfig): string | null {
|
||||
if (interfaceName === config.hotspotInterface || interfaceName === "wlan0") return "Reserved for the Pi hotspot";
|
||||
if (interfaceName === config.upstreamInterface || interfaceName === "wlan1") return "Reserved for the upstream connection";
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -273,11 +279,32 @@ async function networkManagerWifiState(): Promise<Map<string, { connected: boole
|
||||
}
|
||||
}
|
||||
|
||||
async function monitorInterfaceNames(): Promise<Set<string>> {
|
||||
const names = new Set<string>();
|
||||
try {
|
||||
const { stdout } = await execFileAsync("iw", ["dev"], { timeout: 2_500, maxBuffer: 32 * 1024 });
|
||||
let current: string | null = null;
|
||||
for (const line of stdout.split("\n")) {
|
||||
const interfaceMatch = line.match(/^\s*Interface\s+(\S+)/);
|
||||
if (interfaceMatch) {
|
||||
current = interfaceMatch[1] ?? null;
|
||||
continue;
|
||||
}
|
||||
if (current && /^\s*type\s+monitor\s*$/i.test(line)) names.add(current);
|
||||
}
|
||||
} catch {
|
||||
// The name heuristic below still filters the usual wlan1mon form when iw
|
||||
// is unavailable during an adapter transition.
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
export async function discoverWardrivingAdapters(config: WardrivingConfig): Promise<WardrivingAdapter[]> {
|
||||
let interfaces: string[];
|
||||
const monitorNames = await monitorInterfaceNames();
|
||||
try {
|
||||
const entries = await readdir("/sys/class/net");
|
||||
const candidates = await Promise.all(entries.filter((name) => interfaceNamePattern.test(name)).map(async (name) => {
|
||||
const candidates = await Promise.all(entries.filter((name) => interfaceNamePattern.test(name) && !monitorNames.has(name) && !isMonitorWardrivingInterface(name)).map(async (name) => {
|
||||
try {
|
||||
await access(`/sys/class/net/${name}/wireless`);
|
||||
return name;
|
||||
@@ -306,7 +333,10 @@ export async function discoverWardrivingAdapters(config: WardrivingConfig): Prom
|
||||
const hotspotConflict = interfaceName === config.hotspotInterface || interfaceName === "wlan0";
|
||||
const upstreamConflict = interfaceName === config.upstreamInterface || interfaceName === "wlan1";
|
||||
const requiresNetworkHandoff = upstreamConflict && awus;
|
||||
const eligible = macAddress !== null && !hotspotConflict && (protectedReason === null || requiresNetworkHandoff);
|
||||
// The selected radio may be the current upstream adapter. Starting a
|
||||
// session releases only that adapter for monitor capture; wlan0 remains
|
||||
// protected as the phone-facing hotspot.
|
||||
const eligible = macAddress !== null && !hotspotConflict;
|
||||
const state = networkState.get(interfaceName);
|
||||
return {
|
||||
id: macAddress ? stableAdapterId(properties, macAddress) : `interface:${interfaceName}`,
|
||||
@@ -322,9 +352,7 @@ export async function discoverWardrivingAdapters(config: WardrivingConfig): Prom
|
||||
requiresNetworkHandoff,
|
||||
reason: hotspotConflict
|
||||
? "Reserved for the Pi hotspot"
|
||||
: requiresNetworkHandoff
|
||||
? "Temporarily pauses upstream internet while wardriving"
|
||||
: protectedReason ?? (macAddress ? null : "The adapter has no stable MAC address"),
|
||||
: protectedReason ?? (macAddress ? null : "The adapter has no stable MAC address"),
|
||||
recommended: awus,
|
||||
label: adapterLabel(interfaceName, properties, driver, awus)
|
||||
} satisfies WardrivingAdapter;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { openDatabase } from "../src/database.js";
|
||||
import { chooseWardrivingAdapter, isProtectedWardrivingInterface, normalizeKismetDevice, WardrivingStore, type WardrivingAdapter } from "../src/wardriving.js";
|
||||
import { chooseWardrivingAdapter, isMonitorWardrivingInterface, isProtectedWardrivingInterface, normalizeKismetDevice, WardrivingStore, type WardrivingAdapter } from "../src/wardriving.js";
|
||||
|
||||
const adapter = (overrides: Partial<WardrivingAdapter> = {}): WardrivingAdapter => ({
|
||||
id: "mac:00:11:22:33:44:55",
|
||||
@@ -47,11 +47,17 @@ describe("WiFi adapter selection", () => {
|
||||
expect(chooseWardrivingAdapter([selected, adapter()], { ...selection, macAddress: "00:00:00:00:00:00" }, adapterConfig)).toMatchObject({ id: adapter().id });
|
||||
});
|
||||
|
||||
it("protects the built-in hotspot and upstream interfaces", () => {
|
||||
it("protects only the built-in hotspot while allowing upstream capture hardware", () => {
|
||||
expect(isProtectedWardrivingInterface("wlan0", adapterConfig)).toContain("hotspot");
|
||||
expect(isProtectedWardrivingInterface("wlan1", adapterConfig)).toContain("upstream");
|
||||
expect(isProtectedWardrivingInterface("wlan1", adapterConfig)).toBeNull();
|
||||
expect(isProtectedWardrivingInterface("wlan2", adapterConfig)).toBeNull();
|
||||
});
|
||||
|
||||
it("does not expose Kismet's monitor child as a selectable adapter", () => {
|
||||
expect(isMonitorWardrivingInterface("wlan1mon")).toBe(true);
|
||||
expect(isMonitorWardrivingInterface("wlan1mon0")).toBe(true);
|
||||
expect(isMonitorWardrivingInterface("wlan1")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Kismet device normalization", () => {
|
||||
|
||||
@@ -101,7 +101,7 @@ export function WardrivingPage({ csrfToken }: { csrfToken: string }) {
|
||||
<div className="wardriving-hero-copy">
|
||||
<div className="wardriving-kicker"><Broadcast size={17} weight="fill" /> Low-impact radio log</div>
|
||||
<h2>See the radio landscape around the car.</h2>
|
||||
<p>Kismet watches the configured WiFi and Bluetooth radios. The paired phone supplies the moving GPS fix so each observation can be placed on the map.</p>
|
||||
<p>Kismet watches the selected WiFi adapter and Bluetooth radio. The paired phone supplies the moving GPS fix so each observation can be placed on the map.</p>
|
||||
</div>
|
||||
<div className={`wardriving-session-state ${snapshot?.running ? "is-running" : ""}`}>
|
||||
<span className="wardriving-pulse" aria-hidden="true" />
|
||||
@@ -148,12 +148,12 @@ export function WardrivingPage({ csrfToken }: { csrfToken: string }) {
|
||||
<label htmlFor="wardriving-wifi-adapter">WiFi monitor adapter</label>
|
||||
<select id="wardriving-wifi-adapter" value={snapshot?.selectedWifiAdapterId ?? ""} disabled={snapshot?.running || busy !== null || !snapshot?.wifiAdapters.length} onChange={(event) => { if (event.target.value) void selectAdapter(event.target.value); }}>
|
||||
<option value="">{snapshot?.wifiAdapters.length ? "Select a WiFi adapter" : "No WiFi adapters detected"}</option>
|
||||
{(snapshot?.wifiAdapters ?? []).map((adapter) => <option key={adapter.id} value={adapter.id} disabled={!adapter.eligible}>{adapter.label}{adapter.recommended ? " · Recommended" : ""}{adapter.requiresNetworkHandoff ? " · Pauses upstream" : ""}{!adapter.eligible ? ` · ${adapter.reason ?? "Unavailable"}` : ""}</option>)}
|
||||
{(snapshot?.wifiAdapters ?? []).map((adapter) => <option key={adapter.id} value={adapter.id} disabled={!adapter.eligible}>{adapter.label}{adapter.recommended ? " · Recommended" : ""}{adapter.requiresNetworkHandoff ? " · Monitor mode" : ""}{!adapter.eligible ? ` · ${adapter.reason ?? "Unavailable"}` : ""}</option>)}
|
||||
</select>
|
||||
{selectedAdapter ? <p className="wardriving-adapter-detail"><strong>{selectedAdapter.label}</strong><span>{selectedAdapter.interface} · {selectedAdapter.macAddress ?? "MAC unavailable"}{selectedAdapter.connected ? " · connected" : ""}</span>{selectedAdapter.requiresNetworkHandoff && <em>Starting will pause upstream internet while keeping the Pi hotspot available for the phone.</em>}</p> : <p className="wardriving-adapter-detail">{recommendedAdapter ? `${recommendedAdapter.label} is detected, but it is unavailable: ${recommendedAdapter.reason ?? "reserved for networking"}.` : "The Alfa AWUS 1900 is preferred automatically when it is available."} The hotspot radio stays protected.</p>}
|
||||
{selectedAdapter ? <p className="wardriving-adapter-detail"><strong>{selectedAdapter.label}</strong><span>{selectedAdapter.interface} · {selectedAdapter.macAddress ?? "MAC unavailable"}{selectedAdapter.connected ? " · connected" : ""}</span>{selectedAdapter.requiresNetworkHandoff && <em>Start will put this AWUS adapter into monitor mode for the wardriving session.</em>}</p> : <p className="wardriving-adapter-detail">{recommendedAdapter ? (recommendedAdapter.eligible ? `${recommendedAdapter.label} is ready for the next session.` : `${recommendedAdapter.label} is unavailable: ${recommendedAdapter.reason ?? "not ready"}.`) : "The Alfa AWUS 1900 is preferred automatically when it is available."} The hotspot radio stays protected.</p>}
|
||||
</div>
|
||||
<dl className="wardriving-source-list"><div><dt>WiFi adapter</dt><dd><WifiHigh size={18} />{snapshot?.sources.wifiInterface ?? "Auto-select"}</dd></div><div><dt>Bluetooth adapter</dt><dd><Bluetooth size={18} />{snapshot?.sources.bluetoothInterface ?? "Not configured"}</dd></div><div><dt>Capture mode</dt><dd><ShieldCheck size={18} />WiFi passive · BT discoverable</dd></div></dl>
|
||||
<p className="wardriving-note">Kismet uses passive WiFi capture and a discoverable-device Bluetooth scan. It does not pair, enumerate services, or access payloads. The AWUS 1900 can be handed off from upstream for a session; the Pi hotspot remains available for live phone GPS and statistics, and upstream is restored when capture stops.</p>
|
||||
<p className="wardriving-note">Kismet uses passive WiFi capture and a discoverable-device Bluetooth scan. It does not pair, enumerate services, or access payloads. When you start, Kismet puts the selected AWUS 1900 into monitor mode and owns that radio for the session. The Pi hotspot is not commandeered.</p>
|
||||
</div>
|
||||
<div className="wardriving-source-panel">
|
||||
<div className="wardriving-panel-heading"><div><h2>Data controls</h2><p>{snapshot?.statistics.sessions ?? 0} sessions · bounded to 250,000 sightings.</p></div><Trash size={24} /></div>
|
||||
|
||||
Reference in new issue
Block a user