Skip to content

feat: internet schedule (closes #32) - #47

Open
amr-elgharip2006 wants to merge 1 commit into
UserJoo9:mainfrom
amr-elgharip2006:feat/internet-schedule
Open

amr-elgharip2006 wants to merge 1 commit into
UserJoo9:mainfrom
amr-elgharip2006:feat/internet-schedule

Conversation

@amr-elgharip2006

Copy link
Copy Markdown
Contributor

Closes #32.

Turn the internet off at set times of the week for chosen people (all their devices) or single devices: bedtime on school nights, study time, meal times.

What the admin gets

  • A new Internet schedule page. Start from a ready plan in one click ("School nights" Sunday–Thursday 23:00–07:00, "Every night", "Study time", "Meal times") or set your own times. A schedule can have several times, and a time can run past midnight.
  • Days off: mark dates (holidays, Eid) when the schedule does not run.
  • Bonus time: +15 / +30 / +60 minutes for a running schedule, without editing it. It can be ended early.
  • Always allowed sites (a school platform, study sites) keep working during every schedule. Subdomains are included.
  • A weekly timeline on each schedule shows the off hours and the current time.
  • The people list and the device card show when a device's internet comes back.
  • Arabic and English.

How it is enforced

  • quota/schedules.py is pure time math: windows per weekday, past-midnight windows belong to the day they start, touching windows are merged so the real end is shown, plus days off and bonus time. Schedules are stored as JSON in settings, and a broken row is skipped instead of stopping the others.
  • quota/schedule_nft.py adds its own table, inet quota_schedule. It is never flushed on service start, so a device stays off while the service restarts.
  • Each device is added with an nft timeout equal to the end of its cut, so the kernel gives the internet back on time even if the service is stopped.
  • The forward chain runs at priority -5, before quota_gateway's counters, so dropped packets are not counted as usage.
  • Rules cover IPv4, IPv6 and MAC (for lease-less devices), with the local networks excluded. The printer, shared drives, the dashboard and DNS to the gateway keep working.
  • Every change is one atomic nft -f - batch (flush and re-add together), cache-gated on (address, end time). There is no unblock window and no nft call when nothing changed.
  • Allowed sites use dnsmasq's nftset= to fill allow4/allow6. The fragment goes through the existing DnsRuleManager path, so dnsmasq --test and the rollback still apply.
  • Tick step 4 programs the table, and saving from the dashboard applies it at once. Devices that are off by schedule are also refused on network drives.

API

  • GET/POST /api/schedules, PUT/DELETE /api/schedules/{id}
  • POST /api/schedules/{id}/pause {minutes: 0..1440}
  • PUT /api/schedule-sites {sites: [...]}
  • /api/dashboard adds schedule: {until_ts, names} per device and per person.

Tests

  • tests/test_schedules.py (31 tests) covers:
    • time math: past midnight, merging, days off, bonus time, time zones, overlapping schedules
    • validation
    • the nft batches: v4/v6/MAC rules, LAN excluded, atomic, cache-gated, retry after a failure
    • the nftset fragment
    • the service: create, edit, delete, refuses the gateway itself, cleans up missing people and devices, a corrupt setting does no harm
  • An API test covers create, the dashboard flag, bonus time, 422/404 and allowed sites.
  • Full suite: all pass except test_updater.py::test_auto_install_fires_after_check, which fails on main too.

Tested on a real gateway

I tested this on a real gateway: Debian 12 arm64, kernel 6.1, nftables 1.0.6, dnsmasq 2.90 (built with nftset), PPPoE WAN.

Test Result
Table and rules created on start OK
Scheduled device loses internet; other devices unaffected OK
Scheduled device keeps LAN access (dashboard on the gateway) OK
Always-allowed site opens during the cut; other sites stay blocked OK (dnsmasq added the site's addresses to allow4)
Bonus time opens the internet; ending it cuts it again OK
Service stopped during a cut: the kernel timeout gave the internet back at the end time OK
Service started again, dnsmasq healthy, test schedule removed OK
Dashboard page in Arabic and English, desktop and phone OK

The changes are listed under [Unreleased] in the CHANGELOG, and quota/version.py is not changed.

Turn the internet off at set times of the week for chosen people (all
their devices) or single devices: bedtime, study time, meal times.

- quota/schedules.py: pure time math. Windows per weekday, past-midnight
  windows belong to the day they start, touching windows merge, skip
  dates (holidays), bonus time (pause without editing), stored as JSON
  in settings. A broken row is skipped, never fatal.
- quota/schedule_nft.py: its own table inet quota_schedule, never
  flushed on service start. Devices are added with an nft timeout equal
  to the end of their cut, so the kernel frees them on time even if the
  service stops. Forward chain at priority -5 (dropped packets are not
  counted), IPv4 + IPv6 + MAC, LAN excluded. Each change is one atomic
  nft -f batch, cache-gated.
- Always-allowed sites: dnsmasq nftset= fills allow4/allow6 through the
  existing DnsRuleManager validation and rollback path.
- Tick step 4 programs the table; API edits apply at once. Devices off
  by schedule are also denied on network drives.
- API: /api/schedules (CRUD), /api/schedules/{id}/pause, /api/schedule-sites;
  /api/dashboard reports when each device's internet comes back.
- Dashboard: new "Internet schedule" page (presets, weekly timeline,
  bonus time, days off, allowed sites), status chip on people, banner in
  the device drawer, Arabic + English.
- Tests: tests/test_schedules.py (31) + API test. Docs updated.
@UserJoo9

Copy link
Copy Markdown
Owner

Thanks a lot for these. We're shipping the Netrex rename (0.5.11) first and will review them right after it, starting with the smaller fixes. The repository is now UserJoo9/Netrex; your PRs move with it automatically, no action needed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Daily internet schedule toggle

2 participants