When the internet is slow, the open question is where the delay is: past the modem (the provider) or in the modem. A ping from the modem itself answers that. The modem's NAT table also rules a full session table in or out. Both need a login, so this adds an optional modem.access_code.
What
modem.ModemAdmin: a logged-in client, separate from the existing no-login status poll, which is unchanged.
The login posts md5(access code + nonce), as the gateway's own page does, so the code itself is never sent.
The session is kept between checks and renewed when it expires.
A refused code raises ModemLoginFailed, and the client doesn't try again for 30 minutes.
Monitor (_collect_modem_checks, stored in monitor_state.modem_checks):
NAT sessions every 30 minutes, from the NAT Table page.
A ping from the modem to 1.1.1.1 (Troubleshoot page), whenever UniFi flags the current 5 minutes as slow or measures 150 ms or more, at most every 10 minutes. The result is compared with UniFi's latency through the modem at the same time, giving one of three verdicts:
upstream: the modem's own ping is slow or lossy too, so the delay is on the provider's side.
gateway: the modem's ping is fine but ours through it is slow, so the delay is in the modem.
clear: both are fine by the time of the test.
Any failure pauses the checks for 10 minutes, so the gateway is never hammered.
Needs attention:
The Internet line's detail gains the modem ping result, for example "Modem's own ping 4 min ago: 20 ms; ours through it 1043 ms, so the delay is in the modem, or between it and the router."
New lines: Modem NAT table N% full (at 80% or more), and Modem login failed (tidy up).
WAN / Fiber panel: NAT sessions (in use / available) and "Modem's own ping" with its verdict.
README: the config key, how the checks work, and how failures are handled.
Tested on the real gateway (BGW320-505, firmware 6.40.4)
The branch code was run once from prod:
NAT: 2,343 then 2,520 of 32,767 in use. The second read reused the same session.
Modem ping to 1.1.1.1: 28 ms average, 0% loss, verdict clear.
The access code stays only in prod's config.json, which is git-ignored and mode 600. It never appears in logs, tests or this PR.
Screenshots (preview data)
Checks
557 tests pass (new: page parsers, verdicts, a fake gateway covering login, cookie-first nonce, an expired session, refused-code backoff, ping polling and timeout, plus the monitor scheduling and attention lines).
flake8 and bandit are clean (md5 is marked usedforsecurity=False with a nosec note, because the gateway's protocol requires it). eslint is at the 21-warning baseline.
## Why
When the internet is slow, the open question is where the delay is: past the modem (the provider) or in the modem. A ping from the modem itself answers that. The modem's NAT table also rules a full session table in or out. Both need a login, so this adds an optional `modem.access_code`.
## What
- **`modem.ModemAdmin`**: a logged-in client, separate from the existing no-login status poll, which is unchanged.
- The login posts md5(access code + nonce), as the gateway's own page does, so the code itself is never sent.
- The session is kept between checks and renewed when it expires.
- A refused code raises `ModemLoginFailed`, and the client doesn't try again for 30 minutes.
- **Monitor** (`_collect_modem_checks`, stored in `monitor_state.modem_checks`):
- **NAT sessions** every 30 minutes, from the NAT Table page.
- **A ping from the modem** to 1.1.1.1 (Troubleshoot page), whenever UniFi flags the current 5 minutes as slow or measures 150 ms or more, at most every 10 minutes. The result is compared with UniFi's latency through the modem at the same time, giving one of three verdicts:
- *upstream*: the modem's own ping is slow or lossy too, so the delay is on the provider's side.
- *gateway*: the modem's ping is fine but ours through it is slow, so the delay is in the modem.
- *clear*: both are fine by the time of the test.
- Any failure pauses the checks for 10 minutes, so the gateway is never hammered.
- **Needs attention**:
- The Internet line's detail gains the modem ping result, for example *"Modem's own ping 4 min ago: 20 ms; ours through it 1043 ms, so the delay is in the modem, or between it and the router."*
- New lines: *Modem NAT table N% full* (at 80% or more), and *Modem login failed* (tidy up).
- **WAN / Fiber panel**: NAT sessions (in use / available) and "Modem's own ping" with its verdict.
- **README**: the config key, how the checks work, and how failures are handled.
## Tested on the real gateway (BGW320-505, firmware 6.40.4)
The branch code was run once from prod:
- NAT: 2,343 then 2,520 of 32,767 in use. The second read reused the same session.
- Modem ping to 1.1.1.1: 28 ms average, 0% loss, verdict *clear*.
The access code stays only in prod's `config.json`, which is git-ignored and mode 600. It never appears in logs, tests or this PR.
## Screenshots (preview data)


<img src="https://code.lotusguild.org/LotusGuild/gandalf/raw/branch/modem-login-screenshots/wan-panel-phone.png" width="390">
## Checks
- 557 tests pass (new: page parsers, verdicts, a fake gateway covering login, cookie-first nonce, an expired session, refused-code backoff, ping polling and timeout, plus the monitor scheduling and attention lines).
- flake8 and bandit are clean (md5 is marked `usedforsecurity=False` with a nosec note, because the gateway's protocol requires it). eslint is at the 21-warning baseline.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
With modem.access_code in config.json, the monitor logs in to the
BGW320 (md5 of code + nonce, as its own page does) for two read-only
checks, stored in monitor_state.modem_checks:
- NAT sessions in use every 30 minutes; Needs attention warns at 80%.
- While UniFi says the internet is slow, a ping from the modem itself
to 1.1.1.1 (at most every 10 minutes). Compared with UniFi's latency
through the modem, it says whether the delay is past the modem (the
provider), in the modem, or gone. The Internet line in Needs
attention and the WAN / Fiber panel show the result.
A refused code is reported and not retried for 30 minutes; other
failures pause the checks for 10. The unauthenticated status polling
is unchanged. Tested against the real gateway and a fake one.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jared
scheduled this pull request to auto merge when all checks succeed 2026-11-19 18:38:54 -05:00
jared
merged commit 2a9111d8e8 into main2026-11-19 18:38:55 -05:00
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Why
When the internet is slow, the open question is where the delay is: past the modem (the provider) or in the modem. A ping from the modem itself answers that. The modem's NAT table also rules a full session table in or out. Both need a login, so this adds an optional
modem.access_code.What
modem.ModemAdmin: a logged-in client, separate from the existing no-login status poll, which is unchanged.ModemLoginFailed, and the client doesn't try again for 30 minutes._collect_modem_checks, stored inmonitor_state.modem_checks):Tested on the real gateway (BGW320-505, firmware 6.40.4)
The branch code was run once from prod:
The access code stays only in prod's
config.json, which is git-ignored and mode 600. It never appears in logs, tests or this PR.Screenshots (preview data)
Checks
usedforsecurity=Falsewith a nosec note, because the gateway's protocol requires it). eslint is at the 21-warning baseline.🤖 Generated with Claude Code