Compare commits

19 Commits
Author SHA1 Message Date
Aemeath 83962d61a9 Dawnline 0.20.1
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / Backend (Go: vet, tests, a real ubusd and uhttpd) (push) Blocked by required conditions
CI / Package files (push) Blocked by required conditions
- Fix the grey panel that 0.20.0 put behind the cards of the People list.
2026-11-13 10:02:04 +03:00
Aemeath ac90575b0b Dawnline 0.20.0
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / Backend (Go: vet, tests, a real ubusd and uhttpd) (push) Blocked by required conditions
CI / Package files (push) Blocked by required conditions
- **Backend rewritten in Go.** One daemon, `dawnlined`, replaces the whole ucode backend: the rpcd plugin, the agent, the CLI and the updater. The package no longer depends on ucode.
- **Per-architecture builds.** The release carries the backend for arm64, ARMv7, mips, mipsle and amd64; the router fetches its own after the package is installed and checks its sha256. Dawnline does not run on other architectures (older ARM, 32-bit x86, RISC-V).
- **Faster on the router.** A firewall sync takes 0.06 s instead of 0.9 s; a full apply takes about 300 ms. The portal is served by the daemon itself, without a CGI process per request.
- **Live pages.** The admin and the portal follow a live stream from the router instead of re-fetching the whole state every 30 seconds.
- **Admin redesigned in the portal's style.** Three sections (Overview · Log · Settings); a server is a page of its own with People, Places and Settings; a device opens as a dialog over its person.
- **Changes wait for «Apply».** Every setting, and a person's access, is staged in one dock before it reaches the router.
- **Configuration rollout.** After a change that makes devices fetch their configuration again, the admin shows how many have fetched it and who has not. Every setting shows beforehand (⟳N) how many devices it will touch.
- **Waiting for a decision.** Overview and People put first the people who need one (access ended, limit reached, a device that does not connect), each with its one action a tap away.
- **Bulk actions on people.** «Select» (or a long press on a card) picks several people to extend by a month, pause or resume at once.
- **Undo.** An action done from a card or a request is undone from its toast.
- **Update dialogs redesigned.** An update is one view from its question to its result; Dawnline's own update runs in it like every other, and the page reloads itself once the new version answers. Other versions are a list, each with its notes and Install on a page of its own.
- **Site to site: networks behind a router.** A router connected as a device can have networks behind it (up to eight, /16 to /30): the server routes them and applies the router's access to them. A network that shares addresses with a tunnel, the router's own networks or another site is refused by name.
- **Routers from the portal.** People can add a router and name its networks on their own page; the admin accepts or declines with a reason, or lets them through without asking.
- **Check access and Health know the networks behind routers.**
- **Usage limits reworked.** A monthly limit no longer follows the calendar. A person's month starts with their first traffic and ends a month later; the next one starts only when they connect again, so time away uses up none of it. Before, everyone's limit reset on the 1st.
- Extra data lasts until that person's month ends; «Reset usage» starts the month anew.
- A limit is for a month or for the whole term; the admin and the portal show when it resets.
- Charts that said «this month» show the last 30 days.
- After the update nobody loses what was counted: the month that was running becomes the person's first and ends on the 1st.
- **Access by time of day removed.** The Hours limit is gone from the admin, the portal and the backend; saved hours are dropped.
- **Expiry simplified.** An end date always stops access, with a heads-up three days before; the per-person choice is gone.
- **No false disconnects from sleeping phones.** A phone or a tablet stays online through ten minutes of silence.
- **Mobile Safari fixes.** Pressing a control no longer sends the page to its top; tab tracks that do not fit scroll; switches animate after a press.
2026-11-13 09:43:50 +03:00
Aemeath f8130e39a8 Dawnline 0.18.0
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- Add V2RayTun to a device's set-up on Android: its awg:// link as a code and to copy, in the portal and in the admin (phones, tablets and other devices), with the server's IP, which its Android app needs; not with the Maximum disguise. Its Windows app is ready but stays off until it gives the tunnel the server's DNS.
- Portal: show V2RayTun's Telegram channel beside Google Play.
- Make WireGuard part of AmneziaWG's choice when the disguise is off: the same file and code, both apps named in the steps, WireGuard's store a tap away; a router still picks its package.
- Portal: Reconnect asks again what the device is and about LAN Bypass, and LAN Bypass switches from a device's ⋯ menu, in a group of its own between the copy actions and Rename, Reconnect, Remove (the keys stay; the device takes its updated configuration).
- Turn LAN Bypass on by default for new devices, in the portal and the admin.
- Portal: an app's name too long for the picker fades at its edge and glides to its end on hover or tap; two store buttons stack where they do not fit side by side; the month chart's "Today" stays inside its axis.
- Write to the router's system log by level (Settings → Logs & history → System log: errors, warnings, normal, verbose): the service starting and stopping, every apply with its time, network, firewall, nftables and peer changes, changed settings and portal errors, in English whatever the admin's language, with keys masked.
- Activity: say what a device's change on the portal was (LAN Bypass on or off, its type).
- Demo: build again after a force-pushed main instead of failing on the commit that is gone.
2026-11-05 13:56:20 +03:00
Aemeath 18e3013047 Dawnline 0.17.0
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- Redraw the admin's charts in the connection map's style: the live speed as lines, the last 30 days and a device's week as stacked download and upload bars, a device's days as a strip, data use as meters.
- Read any chart point by point with the pointer, a tap or the arrow keys; a day's tooltip compares it with the average and shows its download and upload.
- Show where a data limit leads at the recent pace: the week ahead on the meter, and when the limit fills or what a monthly one reaches by the reset.
- Make Speed now live, one point a second, on a person's page, in the People list and in an online person's card.
- Name every place an online person's devices connect from, and the city each device connects from now.
- Portal: read the month and the week by tap or arrow keys, see how a day compares with the others, the pace toward the data limit and how long the allowed hours still run.
- Send VPN clients' DNS to the LAN's DNS after its interceptors, so transparent proxies such as forkop also handle their lookups.
2026-11-02 09:48:31 +03:00
Aemeath b0d9982796 Dawnline 0.16.3
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- Move Dawnline to Services in LuCI and update links and documentation.
- Make activity messages clearer, including device renames and person-specific history.
- Prune redundant frontend tests.
2026-10-31 12:21:53 +03:00
Aemeath f2b5ba101b Dawnline 0.16.2
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- The personal page switches to a renewed HTTPS certificate on its own:
  after ACME issues or renews one, the page restarts with it instead of
  serving the old certificate until Dawnline restarts
2026-10-26 07:39:32 +03:00
Aemeath 0fd22b917a Dawnline 0.16.1
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- Checking one component for updates in Components no longer fails
  with "XHR request timed out": the check starts at once instead of
  keeping the page waiting twenty seconds
- On a router without curl, updates and the installer download release
  files through GitHub's API: GitHub's own download links can make
  OpenWrt's built-in downloader hang
- Adding a device in the admin offers the same four types as the
  personal page: PC, Mobile, Router and Other (phones and tablets are
  one choice)
2026-10-26 06:41:54 +03:00
Aemeath 2388603322 Dawnline 0.16.0
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- The personal page loads faster and costs the router less: its script
  and styles come compressed, stay in the browser until the next update,
  and the icon comes with the page instead of a request of its own
- The page uses the system's own font (SF Pro on Apple devices, Segoe UI
  on Windows, Roboto on Android) instead of downloading one
- The top of the page shows the server's name alone, so it fits on a
  phone
- Text can be selected only where it is there to be copied (addresses,
  file names, the configuration), with the name field's own selection:
  a soft highlight with handles that glides after the pointer
- Sheets scroll without a scrollbar
- Red buttons (Remove) light up under the pointer, as the others do
- Demo: "Demo" and GitHub are one button; on a phone it floats at the
  bottom of the screen, and switching scenarios no longer makes the
  edge fades jump
2026-10-24 21:33:48 +03:00
Aemeath 750564ab7e Dawnline 0.15.1
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- Dropdowns (the ⋯ menu, the language picker, the page's QR card) open
  on a softer spring: they grow in without the wobble at the end
- Renaming a device on its card: the hint under the name and a refusal
  take the status's place as whole words, and after a save the status
  comes back the same way
- Cancelling a rename started while the status was still changing puts
  back what it said, not a mix of its old and new letters
2026-10-22 18:01:01 +03:00
dependabot[bot] 3fadee87d3 Bump vitest from 3.2.4 to 5.0.5 in /web
Closes the open Dependabot alerts for vitest, @vitest/mocker and tinypool (dev dependencies only; tinypool is no longer pulled in).
2026-10-22 17:58:11 +03:00
Aemeath ba231e7187 Dawnline 0.15.0
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- The device menu (⋯) is an iOS-style menu on frosted glass: the copy
  actions show the address they copy, Rename, Reconnect and Remove sit
  under them, and one highlight glides from row to row
- The language picker shows flags; pick a language with a click, or
  press the pill and slide onto one. The flag and the code change in
  place
- The page's QR code opens as a small card under its button, with Copy
  link and Share
- All dropdowns look and behave the same: frosted glass, one open at a
  time, and a press outside, Escape or the button again closes them
- Setting a device up: when it can't finish connecting or connects but
  passes no data, the sheet keeps just the status and what it means.
  No data now says the router drops its traffic because the address
  doesn't match, and the device's card says "Connected, but no data"
  right away instead of "Connected"
- Setting a phone up from another device: Download and Copy for the
  connection sit on its row, next to the arrow
- On iPhone and iPad the caret shows as soon as a field is focused, the
  page no longer zooms on a pinch or a double tap, and sheets and their
  animations run smoother
- A press near the edge of a button or a card always counts, even as it
  shrinks under the pointer
- Animations settle without a jump at the end
- New green, orange and red
- The focus ring shows only while moving around with the keyboard
2026-10-22 04:02:54 +03:00
Aemeath a1915f070b Dawnline 0.14.1
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- The updater started without a command, with one it doesn't know, or
  with an install of nothing prints its usage and exits. It no longer
  counts that as a failed update: nothing goes into the service log,
  Activity or the update history, and Components shows no failed job.
  `updater --help` prints the same usage
2026-10-22 00:20:51 +03:00
Aemeath dc0f159447 Dawnline 0.14.0
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- A device is renamed where its name stands on its card: a click on the
  name, Return or Space on it, or Rename in the ⋯ menu turns it into a
  field with the name selected. Return saves, and the words shimmer until
  the router answers. Esc or a click elsewhere puts the old name back. The
  rename window is gone, and the page no longer redraws under a name being
  edited
- Device names take up to 20 characters: letters of any language, digits,
  spaces and - _ . The router refuses anything else when a device is added
  or renamed, so a name can't carry symbols into configurations, file
  names or the AmneziaVPN key. The portal and the admin turn a character
  like that away as it is typed: the field shakes, its outline goes red
  and a short line says why. Suggested names (a phone's model, "Phone 2")
  are stripped of such characters and cut to fit. Names set before this
  stay as they are
- LAN Bypass: Add a device, in the portal and in the admin, has a switch
  that keeps the device's own local network (printer, TV, NAS) reachable
  while it is connected. It is off by default and stays as chosen; the
  admin's device drawer shows it. With home and internet access the switch
  says which addresses still lead to the home network
- Health warns when a server's home network is on one of the subnets home
  routers come with (192.168.0.x, 192.168.1.x and others). People with
  home access who connect from a network on the same addresses reach your
  home instead of their own, LAN Bypass or not
- The router tells when a device can't connect properly. After the problem
  lasts three minutes, the portal card and the admin drawer name it:
  can't finish connecting (the network may block it), connected but no
  data (an outdated configuration, and the card offers Reconnect), on two
  devices at once, or a connection that keeps dropping. Activity lists
  these under Problems, one line per device per hour, and says when the
  device connects normally again
- The set-up sheet's connection line follows what the device does:
  waiting, can't finish, connected, or connected but no data. The sheet
  closes by itself only once data comes through. On the device itself and
  for a router, the steps regroup to what fixes the problem: "Set it up
  again" when the handshake never finishes, "Add the connection again"
  ("Import the configuration again" for a router) when no data comes
- Set-up in the portal: a QR code too long for a camera to read gives its
  place to a code that opens the set-up link (a LAN Bypass configuration
  or a long AmneziaVPN key can be that long). A TV, box or NAS added from
  a phone gets the steps for setting up from another device. Back from
  "Open on the device" keeps the kind, LAN Bypass and the name typed. The
  packet-size note under the kinds is gone
- The portal on phones keeps its place: nothing jumps when an access row
  opens or folds, a notice comes or goes, or Safari redraws the cards. A
  reload, or coming back from the VPN app, opens where the page was. Cards
  glide to their new height, and an animation interrupted half-way goes on
  from where it is instead of snapping. A double click no longer closes
  the sheet it just opened
- In the admin, a device's drawer has its own MTU and keep-alive (empty
  means the server's), each with a Change dialog. A change touches only
  that device's configuration and restarts nothing on the router. Opening
  the drawer no longer counts as handing out the configuration: "Update
  config" stays until the new configuration is shown as a QR code,
  downloaded or copied ("Show the new configuration"). New keys set the
  device up again, as the portal's Reconnect does
- "Speed now" on a server's Overview is live: one point a second over the
  last two minutes, all there as soon as the Overview opens. The agent
  keeps those two minutes in memory and writes them out only while someone
  is looking
- Disguise values that AmneziaWG refuses or crashes on are stopped before
  they reach the kernel. That covers overlapping H1-H4, Jmin above Jmax,
  broken I1-I5 tags, padding under 12 with header protection, and more.
  Import and restore refuse them. After a hand edit in uci, the server's
  tunnel stays down and Health and the service log say why and what to do.
  Errors are full sentences in the reader's language
- The header protection key no longer shows in the status that read-only
  users can see: the Server page says only that the key is set, and each
  device's configuration still has it. The server no longer turns off
  WireGuard's protection against handshake floods (devices keep
  DisableCookies). A device can't be added with a server's own key
- An AmneziaWG update whose kernel module can't be swapped (another
  AmneziaWG interface holds the old one) no longer fails. It is installed,
  the tunnels come back on the old module, and Health and Components say
  the update starts after a reboot. On apk firmware, an install that apk
  left at the old version counts as failed. A module installed by hand is
  recorded, so after a firmware upgrade Health can tell that it is gone
- Dawnline reads only its own tunnels, each one separately. Another
  AmneziaWG interface on the router no longer slows the agent's
  once-a-second check, has its keys read or breaks Dawnline's reads. A
  portal request reads only that person's server: one process however many
  servers there are
- Fewer false disconnects: a tunnel that netifd sets up again no longer
  logs everyone as leaving and coming back, and a device with a long
  keep-alive stays online between handshakes. One configuration on two
  devices isn't logged as roaming every few seconds, and new keys end the
  old session at once. A tunnel that is missing devices is synced within a
  minute, and traffic across a tunnel restart is no longer undercounted
- Renaming a server no longer restarts its tunnel. If a tunnel's interface
  is edited outside Dawnline (LuCI, uci), the watchdog sets it up again
  with Dawnline's settings and names only the changed options. The routing
  rules for "Internet like a home device" change one at a time, new ones
  first, so traffic never falls between them. Health and the Server page
  warn when the address's AAAA record isn't this router's, or the router
  has no public IPv6: devices that prefer IPv6 fail to connect
- The README screenshots come in English and Russian; the portal's are
  taken from its demo. The demo has a switch beside × in the set-up sheet
  to show each state of the connection line. In Russian, long labels in
  the admin's wide option buttons wrap under their icon. DESIGN.md
  describes the switch, renaming on the card, the shimmer, the shake and
  how the portal scrolls. New tests cover link states, the meter, access,
  Health, firewall rules, network settings and packages
2026-10-22 00:11:50 +03:00
Aemeath 58492be021 Dawnline 0.13.0
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- "Internet like a home device" starts off on new servers: sending the
  tunnel through the home network costs speed. Both server wizards (the
  first one and "Add server") have the switch, and on it says what it
  costs. Servers that have it on keep it
- With it off, Server → Access says what that means: DNS still goes
  through the router, its proxies and filters see the tunnel only once its
  interface is added to their source interfaces, and sites the router's
  DNS answers with proxy addresses won't open
- With it on, established connections skip the second pass through
  netfilter in Dawnline's network namespace (a software flowtable): about
  12% less router CPU per packet, and about a quarter more speed through a
  proxy on the router. Without kmod-nft-offload the old rules load and the
  service log says so
- Its address at home stays the same when it is turned off and on again;
  it moved down by one each time (.254, .253, .252)
- Less CPU for the same work: the background agent's once-a-second look at
  the tunnels, Status, Health and the portal no longer compile the same
  patterns again for each line, address and device. At idle the agent uses
  about a quarter of the CPU it did; a portal request 5-10 ms less
- On LuCI's Bootstrap themes the admin looks as it does on Outline: cards
  and menus a step above the page, icon tiles that keep their colour,
  Outline's status colours in dark mode. The choices in Settings → Logs &
  history and Updates are LuCI's own controls in every theme
- "Add from devices at home" keeps a device's name readable in a narrow
  window: its ports go under the name
- DESIGN.md describes the portal's look, components and motion, and the
  rules send portal work there first. lint-ucode no longer ends a function
  at a brace inside a template literal
2026-10-21 01:42:23 +03:00
Aemeath db9657c01c Dawnline 0.12.1
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- Device cards on a computer keep "↓ 3.2 GB ↑ 557 MB" on one line, clear of
  the week's bars (0.12.0 let them run together in the two columns). A
  narrow card gets slightly smaller numbers and thinner bars, with the bars
  in the middle of the row; resizing the window, the card glides between
  its two shapes
- A device whose settings changed shows one big Reconnect button, the same
  as Connect on a device not set up yet, in place of its week and the
  orange Update pill. It opens the same question as Reconnect in the ⋯ menu
2026-10-20 21:54:42 +03:00
Aemeath fb73ac3a9f Dawnline 0.12.0
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- The portal's traffic charts show what came down and what went up apart.
  A device's last 7 days and "Data this month" draw each day both ways:
  downloads rise above a thin line, uploads hang below it, on one scale.
  Beside them "↓ 3.2 GB ↑ 557 MB" instead of one total; hover a day for
  its own, its date where "left" stands
- Charts answer the pointer between their bars too, in the portal and in
  the admin's daily charts (a device's week, a device card's 30 days, the
  last 30 days): the figures no longer blink back to the total
- The calendar under "Access until" plays its days in only when the row is
  opened, not again each time the page refreshes with it open
- On a phone the page comes in from the top down: "Your access" first,
  then the devices
- "Show key" on a router's set-up morphs to "Hide key" with its icon, and
  brings the keys themselves into focus (it blurred the field names)
2026-10-20 20:58:36 +03:00
Aemeath 2f2aaf6c34 Dawnline 0.11.0
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- Russian translation. The admin pages, the LuCI menu and the router's
  messages (errors, Health checks, update steps, the service log) come in
  Russian with the new luci-i18n-dawnline-ru package, released beside the
  app. LuCI shows it when its language is Russian, or "auto" with a Russian
  browser
- The translation installs with install.sh when LuCI is in Russian (or with
  --lang ru), follows every Dawnline update and rollback, and can be added
  or removed in Components → Dawnline → Translation
- The portal speaks the visitor's language: it opens in the system's
  language and switches between English and Russian in place, from the
  language pill beside its QR code. It carries its own translation and needs
  no package
- Set-up steps quote the apps' own menus word for word, in Russian too:
  AmneziaVPN is "+ → Insert → Continue" (it has no "Add server"), and the
  Mac and Windows menus of WireGuard and AmneziaWG have their real names
- Questions in the portal (Reconnect, Remove) read from the left edge and
  close with Cancel, without ×
- Numbers, sizes and dates follow the page's language. A server that can't
  start says why in one sentence; event details and service log lines read
  as whole sentences
- README in Russian (README_ru.md). rules/i18n.md: how strings are written
  and translated; CI checks the catalogs (npm run i18n:check)
2026-10-20 19:33:52 +03:00
Aemeath caed976e63 Portal demo for Vercel
- The real portal over a router that lives in the page
  (web/src/portal/demo): "User" on "Dawnline Preview", ten scenarios
  (first visit, settings changed, out of data, outside the hours, paused,
  ended, plain HTTP, plain WireGuard, no free slots), devices kept per
  visitor in localStorage. Keys are random and the server's address never
  resolves: nothing is installed and nothing connects
- A "Demo" chip after the breadcrumb opens a drawer with the scenarios and
  Start over; a new device "connects" a few seconds after its settings
- A GitHub pill beside the page's QR knob; on a phone, where the names
  need the room, a GitHub button in the drawer instead
- npm run build:demo builds the static site into web/dist/demo;
  web/vercel.json deploys it with the portal's CSP and headers, and builds
  only commits on main that change web/
- README links to the live demo (dawnline-demo.vercel.app)
- Tests for the demo router (tests/demo.test.ts)
2026-10-19 23:08:06 +03:00
Aemeath 8e7c15ad56 README: fix the Development link; social preview image
- The Development heading's emoji carried a variation selector, which
  GitHub keeps in the heading's anchor, so the top link to #-development
  went nowhere
- docs/social-preview.png: the repository's social preview, in the
  screenshots' panel style
2026-10-19 21:19:16 +03:00
858 changed files with 169022 additions and 29551 deletions

No files matched your search

+121
View File
@@ -0,0 +1,121 @@
name: Bug report
description: Something in Dawnline does not work as it should
type: Bug
labels: ["needs triage"]
body:
- type: markdown
attributes:
value: |
Thanks for taking the time. A report that can be reproduced gets fixed much sooner, so the fields below ask for what is usually needed.
**Before you send it, remove from everything you paste:** private and preshared keys, people's personal links (`https://…/p/…`) and public IP addresses. The service log masks keys by itself; `awg show` does not.
- type: dropdown
id: area
attributes:
label: Where it happens
options:
- Admin (LuCI)
- Personal portal
- A device connecting, or the tunnel itself
- Access, limits and end dates
- Site to site (networks behind a router)
- Install and updates
- Something else
validations:
required: true
- type: textarea
id: what
attributes:
label: What happened
description: What you saw. A screenshot can be dragged into this field.
validations:
required: true
- type: textarea
id: expected
attributes:
label: What you expected
validations:
required: true
- type: textarea
id: steps
attributes:
label: Steps to reproduce
placeholder: |
1. Open …
2. Press …
3. See …
validations:
required: true
- type: input
id: version
attributes:
label: Dawnline version
description: Settings → Updates, or `cat /usr/share/dawnline/version` on the router.
placeholder: "0.20.0"
validations:
required: true
- type: dropdown
id: openwrt
attributes:
label: OpenWrt
options:
- "25.12"
- "24.10"
- Snapshot
- Other (say which below)
validations:
required: true
- type: input
id: router
attributes:
label: Router and architecture
description: The model, and the architecture from `ubus call system board` (the `target` line) or `/etc/openwrt_release`.
placeholder: Xiaomi AX3000T, aarch64_cortex-a53
validations:
required: true
- type: input
id: client
attributes:
label: App and system of the device
description: For a problem with one device, the app it connects with and its system. For a problem in a page, the browser.
placeholder: AmneziaWG 2.0 on iOS 26 · Safari 26
- type: textarea
id: health
attributes:
label: Health
description: Overview → Health. Which checks are not green, and what they say.
- type: textarea
id: log
attributes:
label: Service log
description: The last lines around the moment it happened. Settings → History and storage → System log set to «verbose» says more.
placeholder: logread -e dawnline | tail -n 100
render: text
- type: textarea
id: awg
attributes:
label: awg show
description: For a device that does not connect or cannot reach something. Remove the keys and replace public addresses before you paste.
placeholder: awg show
render: text
- type: checkboxes
id: checks
attributes:
label: Before sending
options:
- label: I removed keys, personal links and public addresses from what I pasted.
required: true
- label: I looked through the open issues and did not find this one.
required: true
+33
View File
@@ -0,0 +1,33 @@
name: Feature request
description: Something Dawnline cannot do yet, or could do better
type: Feature
labels: ["needs triage"]
body:
- type: textarea
id: problem
attributes:
label: What you are trying to do
description: The situation, not the solution. Who connects, from where, to what, and where it stops today.
validations:
required: true
- type: textarea
id: wish
attributes:
label: What would help
description: How you imagine it working, in the admin, the portal or on the router.
validations:
required: true
- type: textarea
id: today
attributes:
label: What you do today
description: A workaround you use, or another tool that does it.
- type: input
id: setup
attributes:
label: Your setup
description: Dawnline and OpenWrt versions, the router, how many people and servers. Only what matters for the request.
placeholder: Dawnline 0.20.0, OpenWrt 25.12, one server, 12 people
+33
View File
@@ -0,0 +1,33 @@
name: Question
description: How something works, or how to set it up
labels: ["question"]
body:
- type: markdown
attributes:
value: |
The [README](https://github.com/tickcount/luci-app-dawnline#readme) answers the common ones in its FAQ. If yours is not there, ask here.
Remove private keys, personal links and public IP addresses from anything you paste.
- type: textarea
id: question
attributes:
label: What you want to do
description: What you are setting up and what you want to end up with.
validations:
required: true
- type: textarea
id: tried
attributes:
label: What you tried
description: The settings you changed and what came of it.
- type: input
id: setup
attributes:
label: Versions
description: Dawnline and OpenWrt, and the router.
placeholder: Dawnline 0.20.0, OpenWrt 25.12, Xiaomi AX3000T
validations:
required: true
+8
View File
@@ -0,0 +1,8 @@
blank_issues_enabled: false
contact_links:
- name: Security problem
url: https://github.com/tickcount/luci-app-dawnline/security/advisories/new
about: Report a vulnerability privately. Please do not open a public issue for it.
- name: Install, requirements and FAQ
url: https://github.com/tickcount/luci-app-dawnline#readme
about: How to install and update, what Dawnline needs, and answers to common questions.
+57 -17
View File
@@ -1,9 +1,10 @@
name: Build packages
# A tag vX.Y.Z builds luci-app-dawnline as .ipk (OpenWrt 24.10, opkg) and .apk
# (OpenWrt 25.12, apk) with the OpenWrt SDK and publishes a GitHub release with
# both packages and sha256sums.txt, the files install.sh and the in-app updater
# download. The release notes list what changed since the previous tag, from the
# A tag vX.Y.Z builds luci-app-dawnline and its translation packages
# (luci-i18n-dawnline-<lang>) as .ipk (OpenWrt 24.10, opkg) and .apk (OpenWrt
# 25.12, apk) with the OpenWrt SDK and publishes a GitHub release with the
# packages, the Go backend's binary for each architecture (dawnlined-<version>-<goarch>) and sha256sums.txt,
# the files install.sh and the in-app updater download. The release notes list what changed since the previous tag, from the
# commits (scripts/release-notes.sh). Run it by hand to get the packages as
# workflow artifacts only.
on:
@@ -32,7 +33,7 @@ jobs:
version: ${{ steps.version.outputs.version }}
steps:
- name: Checkout
uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
@@ -40,8 +41,8 @@ jobs:
id: version
run: |
v="$(sed -n 's/^PKG_VERSION:=//p' package/luci-app-dawnline/Makefile)"
grep -q "^export const VERSION = '$v';" package/luci-app-dawnline/root/usr/share/ucode/dawnline/util.uc ||
{ echo "::error::util.uc VERSION differs from the Makefile ($v)"; exit 1; }
[ "$(cat package/luci-app-dawnline/root/usr/share/dawnline/version)" = "$v" ] ||
{ echo "::error::usr/share/dawnline/version differs from the Makefile ($v)"; exit 1; }
grep -q "\"version\": \"$v\"" web/package.json ||
{ echo "::error::web/package.json version differs from the Makefile ($v)"; exit 1; }
if [ "${GITHUB_REF_TYPE}" = tag ] && [ "${GITHUB_REF_NAME}" != "v$v" ]; then
@@ -62,14 +63,14 @@ jobs:
- { type: apk, sdk: 'itdoginfo/openwrt-sdk-apk:25.12.3@sha256:6d832008ddd1b8ec4edc84f5e6e0bf22a13ba2d6888083a8e69fa26d752c276f' }
steps:
- name: Checkout
uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Setup Node.js
uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: '22'
node-version: '26'
cache: npm
cache-dependency-path: web/package-lock.json
@@ -86,24 +87,56 @@ jobs:
run: scripts/build-packages.sh ${{ matrix.type }}
- name: Upload
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: package-${{ matrix.type }}
path: dist/luci-app-dawnline-${{ needs.version.outputs.version }}.${{ matrix.type }}
# The app and its translation packages (luci-i18n-dawnline-<lang>).
path: dist/luci-*-${{ needs.version.outputs.version }}.${{ matrix.type }}
if-no-files-found: error
retention-days: 7
# The Go backend (dawnlined), one static binary an architecture: the package is `all`-architecture, so the
# binary travels beside it as its own release file, with its sha256 in sha256sums.txt like the packages.
backend:
name: Backend ${{ matrix.goarch }}
runs-on: ubuntu-24.04
needs: [version, ci]
strategy:
fail-fast: true
matrix:
goarch: [arm64, arm, mips, mipsle, amd64]
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Build dawnlined for ${{ matrix.goarch }}
env:
GO_IMAGE: golang:1.27-alpine@sha256:eb61c3873c97416fff0116553fff65d67a3549f72e11d9758849a689cdd8b523
run: |
bin="$(scripts/build-backend.sh ${{ matrix.goarch }} | tail -n1)"
cp "$bin" "dist/dawnlined-${{ needs.version.outputs.version }}-${{ matrix.goarch }}"
- name: Upload
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: backend-${{ matrix.goarch }}
path: dist/dawnlined-${{ needs.version.outputs.version }}-${{ matrix.goarch }}
if-no-files-found: error
retention-days: 7
release:
name: Release
runs-on: ubuntu-24.04
needs: [version, build]
needs: [version, build, backend]
if: github.ref_type == 'tag'
permissions:
contents: write
steps:
# The whole history and the tags: the notes list the commits since the previous tag.
- name: Checkout
uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
fetch-depth: 0
@@ -114,20 +147,27 @@ jobs:
cat notes.md
- name: Download packages
uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
pattern: package-*
path: release
merge-multiple: true
- name: Download the backend's binaries
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
pattern: backend-*
path: release
merge-multiple: true
- name: Checksums
working-directory: release
run: |
sha256sum luci-app-dawnline-* > sha256sums.txt
sha256sum luci-* dawnlined-* > sha256sums.txt
cat sha256sums.txt
- name: Publish the release
uses: softprops/action-gh-release@3bb12739c298aeb8a4eeaf626c5b8d85266b0e65 # v2
uses: softprops/action-gh-release@efb35369e0ad2afab669f228072c1b0d510eae64 # v3.0.3
with:
name: Dawnline ${{ github.ref_name }}
tag_name: ${{ github.ref_name }}
+60 -41
View File
@@ -24,14 +24,14 @@ jobs:
working-directory: web
steps:
- name: Checkout
uses: actions/checkout@v5
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Setup Node.js
uses: actions/setup-node@v5
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: '22'
node-version: '26'
cache: npm
cache-dependency-path: web/package-lock.json
@@ -51,21 +51,44 @@ jobs:
id: types
run: npm run --silent typecheck
# oxlint's correctness rules: what the types let through (a stray expression, a needless escape).
- name: Lint
id: lint
run: npm run --silent lint
# Exports nobody imports, files nobody reaches, dependencies used but not listed (or listed but unused).
- name: Unused code
id: knip
run: |
if ! npm run --silent knip; then
echo "::error::knip found unused code or a dependency problem: remove it, or drop the export when only its own file uses it."
exit 1
fi
# The catalogs follow the code: every string collected, translated, its placeholders kept.
- name: Translations
id: i18n
run: |
if ! npm run --silent i18n:check; then
echo "::error::The translation catalogs are out of date or incomplete. Run 'npm run i18n' in web/, translate the new entries in each ru.po and commit them (rules/i18n.md)."
exit 1
fi
- name: Tests
id: tests
run: npm test
# The bundles under package/ are committed: a build must change nothing
# and leave no new file behind (an output nobody added to git would be
# missing from the package).
- name: Build bundles and check they are committed
# The bundles under package/ are not committed (build.yml builds them
# before packaging): a build must touch no tracked file and leave every
# output it writes ignored by .gitignore, so none ends up in a commit.
- name: Build bundles and check they stay out of git
id: build
run: |
npm run --silent build
changed="$(git status --porcelain --untracked-files=all -- ../package)"
if [ -n "$changed" ]; then
echo "$changed"
echo "::error::The committed bundles are out of date or the build left new files. Run 'npm run build' in web/ and commit the result."
echo "::error::The build changed a tracked file under package/ or wrote one .gitignore does not cover."
exit 1
fi
@@ -79,6 +102,9 @@ jobs:
echo "|---|---|"
echo "| Formatting | ${{ steps.format.outcome }} |"
echo "| Types | ${{ steps.types.outcome }} |"
echo "| Lint | ${{ steps.lint.outcome }} |"
echo "| Unused code | ${{ steps.knip.outcome }} |"
echo "| Translations | ${{ steps.i18n.outcome }} |"
echo "| Tests | ${{ steps.tests.outcome }} |"
echo "| Bundles | ${{ steps.build.outcome }} |"
} >> "$GITHUB_STEP_SUMMARY"
@@ -88,7 +114,7 @@ jobs:
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v5
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
@@ -99,46 +125,58 @@ jobs:
run: |
shellcheck --version
files="$(grep -rlIE '^#!(/usr)?/bin/(sh|bash|env (sh|bash))( |$)' \
install.sh uninstall.sh package/luci-app-dawnline/root scripts tests | sort)"
install.sh uninstall.sh package/luci-app-dawnline/root scripts | sort)"
echo "$files"
for f in package/luci-app-dawnline/root/etc/init.d/dawnline \
package/luci-app-dawnline/root/etc/hotplug.d/iface/95-dawnline \
package/luci-app-dawnline/root/usr/libexec/dawnline/resolver \
package/luci-app-dawnline/root/usr/libexec/dawnline/heartbeat \
package/luci-app-dawnline/root/usr/libexec/dawnline/postupdate \
package/luci-app-dawnline/root/usr/share/dawnline/portal/www/cgi-bin/portal; do
package/luci-app-dawnline/root/usr/libexec/dawnline/fetch-backend; do
echo "$files" | grep -qx "$f" || { echo "::error file=$f::not found by the shebang scan"; exit 1; }
done
# shellcheck disable=SC2086
shellcheck -S warning $files
ucode:
name: ucode static checks
backend:
name: "Backend (Go: vet, tests, a real ubusd and uhttpd)"
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v5
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Lint (use before definition, exports, POSIX regexes, rpcd methods)
run: python3 scripts/lint-ucode.py
# The goldens in backend/*/testdata are the ucode backend's own answers, taken before it was removed
# (0.18.0): they are fixed now, and hold the Go backend to what that one did.
# gofmt, go vet and the unit tests in the Go toolchain image, then the object, the calls, the stream
# and a page's whole path against the ubusd, rpcd and uhttpd of OpenWrt 24.10 and 25.12.
- name: Format, vet, unit tests, and the tests in the OpenWrt containers
run: scripts/backend-container.sh
package:
name: Package files
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- name: Executables keep their mode
run: |
for f in usr/bin/dawnline usr/libexec/dawnline/agent usr/libexec/dawnline/updater usr/libexec/dawnline/resolver usr/libexec/dawnline/heartbeat usr/libexec/dawnline/postupdate \
etc/init.d/dawnline etc/uci-defaults/90_dawnline usr/share/dawnline/portal/www/cgi-bin/portal usr/share/dawnline/portal/cgi.uc; do
for f in usr/bin/dawnline usr/libexec/dawnline/updater usr/libexec/dawnline/fetch-backend usr/libexec/dawnline/resolver usr/libexec/dawnline/postupdate \
etc/init.d/dawnline etc/uci-defaults/90_dawnline etc/hotplug.d/iface/95-dawnline; do
[ -x "package/luci-app-dawnline/root/$f" ] || { echo "::error file=package/luci-app-dawnline/root/$f::not executable (git update-index --chmod=+x $f)"; exit 1; }
done
for f in scripts/check-ucode.sh scripts/compile-ucode.sh scripts/ucode-container.sh tests/ucode/run.sh tests/ucode/fixtures/bin/awg; do
for f in scripts/backend-container.sh scripts/build-backend.sh scripts/build-packages.sh scripts/build-release.sh scripts/deploy.sh; do
[ -x "$f" ] || { echo "::error file=$f::not executable (git update-index --chmod=+x $f)"; exit 1; }
done
- name: Versions agree
run: |
v="$(sed -n 's/^PKG_VERSION:=//p' package/luci-app-dawnline/Makefile)"
grep -q "^export const VERSION = '$v';" package/luci-app-dawnline/root/usr/share/ucode/dawnline/util.uc ||
{ echo "::error::util.uc VERSION differs from the Makefile ($v)"; exit 1; }
[ "$(cat package/luci-app-dawnline/root/usr/share/dawnline/version)" = "$v" ] ||
{ echo "::error::usr/share/dawnline/version differs from the Makefile ($v)"; exit 1; }
grep -q "\"version\": \"$v\"" web/package.json ||
{ echo "::error::web/package.json version differs from the Makefile ($v)"; exit 1; }
python3 - "$v" <<'PY'
@@ -151,22 +189,3 @@ jobs:
sys.exit(1)
PY
echo "Version $v"
ucode-compile:
# scripts/compile-ucode.sh and tests/ucode on the release's own ucode and
# libuci, in the OpenWrt rootfs containers scripts/ucode-container.sh pins
# by digest (24.10 = opkg, 25.12 = apk: the two the packages are built for).
name: ucode compiles and tests pass (OpenWrt ${{ matrix.release }})
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
release: ['24.10', '25.12']
steps:
- name: Checkout
uses: actions/checkout@v5
with:
persist-credentials: false
- name: Compile every ucode file and run tests/ucode
run: scripts/ucode-container.sh ${{ matrix.release }}
+3
View File
@@ -2,3 +2,6 @@ node_modules/
dist/
.DS_Store
web/coverage/
# The web bundles: web/ builds them (npm run build), CI before packaging.
package/luci-app-dawnline/htdocs/luci-static/resources/dawnline/
package/luci-app-dawnline/root/usr/share/dawnline/portal/assets/
+22 -14
View File
@@ -2,37 +2,45 @@
Guidance for coding agents working in this repository (`tickcount/luci-app-dawnline`): an AmneziaWG access server for OpenWrt with a LuCI admin UI and a personal portal. `CLAUDE.md` points here.
## Important
Always build code cleanly, modularly, and with proper architecture and separation of concerns. Never simply bolt on a feature because I asked for it. If you can’t find an easy way to build it naturally, and you'd recommend a refactor to make the whole thing cleanly, stop your work ahead of time and say so.
## Rules
- `rules/code-standards.md` — how all code is written: decomposition, one locked writer, fail closed, validation at the boundary, comments and commits.
- `rules/backend.md` — ucode on the router: module layers, conventions, what things cost there (CGI portal, rpcd, the agent's watcher).
- `rules/frontend.md` — TypeScript admin UI and portal: layout of `web/src`, pollers and the live revision.
- `rules/backend-go.md` — the backend (`backend/`, Go: the program `dawnlined`): packages, the stream's rules, what things cost on the router, checks. `docs/backend.md` — how it came to be, the stream's protocol, what each part is held to.
- `rules/frontend.md` — TypeScript admin UI and portal: layout of `web/src`, the admin's four LuCI modules and their size budget, pollers and the live revision.
- `rules/shell.md` — install, uninstall, init and CI scripts.
- `rules/ui-design.md` — how the admin UI and the portal look: modern product quality, mock first from the real CSS and components, verified in the browser.
- `rules/ui-design.md` — how the admin UI and the portal look: one design system inside LuCI, modern product quality, mock first from the real code and CSS, verified in the browser.
- `DESIGN.md` — the design system of the personal portal and the admin: tokens, components, layout and motion, measured on the live page. Read it before any work on the portal (`web/src/portal`, and what it uses from `web/src/shared`) or the admin's pages (`web/src/admin`); a change to how they look or move updates it in the same change.
- `rules/i18n.md` — translations: every visible string and backend message wrapped, the catalogs, the Russian style guide and glossary. Anything added to the admin UI or the portal is localized in the same change; anything removed takes its catalog entries with it.
- `rules/release-notes.md` — how a release's points and any list of «what changed» are collected: from the code against production, key changes only, a rework told whole, in English.
This repo usually sits inside the router-admin workspace (`../AGENTS.md`). Its rules apply to anything that touches the router: read-only first, backup before mutating, router shell as a literal stdin heredoc over SSH (`../rules/`).
## Layout
- `package/luci-app-dawnline/root/` — what lands on the router: ucode modules in `usr/share/ucode/dawnline/` (`rpc/` for the LuCI backend), the agent and updater in `usr/libexec/dawnline/`, the portal in `usr/share/dawnline/portal/`, the init script, rpcd ACL.
- `web/` — TypeScript sources of the admin UI (`src/admin`) and the portal (`src/portal`), shared code in `src/shared`; the built bundles are committed under `package/`.
- `package/luci-app-dawnline/root/` — what lands on the router beside the backend's binary: the init script, the shell around the backend in `usr/bin/dawnline` and `usr/libexec/dawnline/` (`updater`, `fetch-backend`, `postupdate`, `resolver`), the portal's assets in `usr/share/dawnline/portal/`, the version in `usr/share/dawnline/version`, the rpcd ACL and the LuCI menu. There is no ucode: the backend is Go.
- `backend/` — the backend (`dawnlined`), one static binary: the stream and the pages' methods (`ubus`, `hub`, `api`, `methods`), the watcher and the minute's work (`watch`, `chores`, `watchdog`, `geo`), putting the settings into effect (`apply`, `nft`, `store`, `uci`, `model`), the portal (`portal`), the updater and the command line (`updater`, `pkgmgr`, `cli`), `daemon` and `cmd/dawnlined`. A release publishes it for each architecture as `dawnlined-<version>-<goarch>`; `fetch-backend` puts the right one on a router (`docs/backend.md`).
- `web/` — TypeScript sources of the admin UI (`src/admin`: the LuCI modules `dawnline.app`, `dawnline.style`, `dawnline.map` and `dawnline.qr`) and the portal (`src/portal`), shared code in `src/shared`; `npm run build` writes the bundles under `package/` (git-ignored: CI builds them before packaging); `npm run build:demo` writes the two demos into `web/dist/` (the portal's, which is the public preview, and the admin's, which the README's pictures are taken from). `web/i18n/` — the string catalogs' tools (`extract.ts`, `po.ts`, `lmo.ts`).
- `package/luci-app-dawnline/po/` — the admin's catalog (`templates/dawnline.pot`, `ru/dawnline.po`): luci.mk builds the `luci-i18n-dawnline-ru` package from it. The portal's catalog is `web/src/portal/i18n/ru.po`, bundled into the portal.
- `install.sh`, `uninstall.sh` — the standalone installer and uninstaller (both have `--dry-run`).
- `scripts/` — `lint-ucode.py`, `compile-ucode.sh` (compiles every ucode file of a tree wherever ucode runs), `check-ucode.sh` (the same on the router, without installing), `ucode-container.sh` (compile and unit tests in the OpenWrt rootfs containers CI uses), `deploy.sh` (dev deploy over SSH), `build-packages.sh`, `build-release.sh`, `release-notes.sh`.
- `tests/ucode/` — unit tests of the ucode modules (`run.uc` runs every `*.test.uc`; fixtures for `/etc/config` and a stand-in `awg`), run in a container by `run.sh`.
- `scripts/` — `backend-container.sh` (the backend's checks, needs docker), `build-backend.sh` (the binary for one architecture), `deploy.sh` (dev deploy over SSH), `build-packages.sh`, `build-release.sh`, `release-notes.sh`, and the screenshot tools.
## Checks before a commit or deploy
- `python3 scripts/lint-ucode.py` (use before definition, exports, POSIX regexes, rpcd methods; layering notes are informational).
- `scripts/ucode-container.sh` (needs docker): compiles every ucode file and runs `tests/ucode` on OpenWrt 24.10 and 25.12, exactly what CI does. `scripts/check-ucode.sh` compiles on the router instead when there is no docker. New ucode entry points are added to the compile list in `scripts/compile-ucode.sh`, to `lint-ucode.py`, and to the executables list in `.github/workflows/ci.yml`; a pure function gets a case in `tests/ucode/<module>.test.uc`.
- In `web/`: `npm run format:check`, `npm run typecheck` (sources and tests), `npm test`, `npm run build` — and commit the rebuilt bundles (CI fails on a changed or new file under `package/`).
- `scripts/backend-container.sh` (needs docker) after any change under `backend/` or to the rpcd ACL: gofmt, vet, unit tests, and the tests against a real ubusd, rpcd and uhttpd on OpenWrt 24.10 and 25.12. The goldens in `backend/*/testdata` are the removed ucode backend's own answers (0.18.0), fixed: a deliberate change of behaviour changes its golden by hand, in the same change, and says why.
- In `web/`: `npm run format:check`, `npm run typecheck` (sources and tests), `npm run lint` (oxlint), `npm run knip` (unused exports and dependencies), `npm run i18n:check` (after changing visible text: `npm run i18n`, translate the new entries in each `ru.po`), `npm test`, `npm run build` — and commit the catalogs (the bundles stay out of git; CI fails when the build leaves a file under `package/` that `.gitignore` does not cover).
- `shellcheck -S warning` for the shell scripts (CI finds them by shebang).
- A tag builds only after CI passes on it (`build.yml` calls `ci.yml`).
## Releases
- A release is one commit on `main` titled `Dawnline X.Y.Z`; its body is `- ` points, which become the GitHub release notes and the Components "What's new". Work from a branch is squashed into that one commit.
- Bump the version in `package/luci-app-dawnline/Makefile` (`PKG_VERSION`), `util.uc` (`VERSION`), `web/package.json` and `web/package-lock.json` (CI checks they agree).
- A release is one commit on `main` titled `Dawnline X.Y.Z`; its body is `- ` points, which become the GitHub release notes and "What's new" in Settings › Updates. Work from a branch is squashed into that one commit.
- The points are collected as `rules/release-notes.md` says: from the difference with the last release, not from the commit messages alone.
- Bump the version in `package/luci-app-dawnline/Makefile` (`PKG_VERSION`), `package/luci-app-dawnline/root/usr/share/dawnline/version`, `web/package.json` and `web/package-lock.json` (CI checks they agree).
- No `Co-Authored-By` or other agent attribution in commits.
- Push `main`, then an annotated tag `vX.Y.Z` ("Dawnline X.Y.Z"); the "Build packages" workflow publishes the `.ipk`, `.apk` and `sha256sums.txt`.
- Push `main`, then an annotated tag `vX.Y.Z` ("Dawnline X.Y.Z"); the "Build packages" workflow publishes the `.ipk`, `.apk` (the app and `luci-i18n-dawnline-ru`) and `sha256sums.txt`.
- Re-cutting a release (fixes before anyone installs) rewrites `main`: force-push, `gh release delete vX.Y.Z --cleanup-tag`, cancel a running build of the old tag, tag again. Only with the user's go-ahead.
- On the router, install a release the way Components does: `/usr/libexec/dawnline/updater install app@vX.Y.Z` (verified against the release's sha256).
- On the router, install a release the way Settings → Updates does: `/usr/libexec/dawnline/updater install app@vX.Y.Z` (verified against the release's sha256).
+1667
View File
File diff suppressed because it is too large. Load diff
+94 -31
View File
@@ -18,7 +18,9 @@
<a href="https://openwrt.org/"><picture><source media="(prefers-color-scheme: dark)" srcset="https://shieldcn.dev/badge/OpenWrt-25.12%20%C2%B7%20apk.svg?variant=secondary&font=geist&logo=openwrt&mode=dark"><img alt="OpenWrt 25.12" src="https://shieldcn.dev/badge/OpenWrt-25.12%20%C2%B7%20apk.svg?variant=secondary&font=geist&logo=openwrt&mode=light"></picture></a>
<a href="https://docs.amnezia.org/"><picture><source media="(prefers-color-scheme: dark)" srcset="https://shieldcn.dev/badge/AmneziaWG-2.0%20%C2%B7%203.1.svg?variant=secondary&font=geist&logo=wireguard&mode=dark"><img alt="AmneziaWG 2.0 and 3.1" src="https://shieldcn.dev/badge/AmneziaWG-2.0%20%C2%B7%203.1.svg?variant=secondary&font=geist&logo=wireguard&mode=light"></picture></a>
[**Quick start**](#-quick-start) &nbsp;·&nbsp; [Features](#-features) &nbsp;·&nbsp; [How it works](#-how-it-works) &nbsp;·&nbsp; [Screenshots](#-a-closer-look) &nbsp;·&nbsp; [FAQ](#-faq) &nbsp;·&nbsp; [Development](#-development)
[**Quick start**](#-quick-start) &nbsp;·&nbsp; [**Live demo**](https://dawnline-demo.vercel.app) &nbsp;·&nbsp; [Features](#-features) &nbsp;·&nbsp; [How it works](#-how-it-works) &nbsp;·&nbsp; [Screenshots](#-a-closer-look) &nbsp;·&nbsp; [FAQ](#-faq) &nbsp;·&nbsp; [Development](#-development)
**English** &nbsp;·&nbsp; [Русский](README_ru.md)
</div>
@@ -28,12 +30,13 @@ Dawnline runs an [AmneziaWG] server on your OpenWrt router and gives every
person you invite **a personal link**. They open it, **add their own phones and
computers** and connect with AmneziaVPN, AmneziaWG or WireGuard. You decide
**what each of them may reach** (the home network, the internet, or both) and
**for how long, how much and when**.
**for how long and how much**.
<p align="center">
<img src="docs/screenshots/portal.png" alt="The personal portal: devices and access, and adding a phone in three steps" width="860">
<a href="https://dawnline-demo.vercel.app"><img src="docs/screenshots/portal.png" alt="The personal portal: devices and access, and adding a phone in three steps" width="860"></a>
<br>
<sub>A person's own page: their devices and access. Adding a phone takes three steps, and the page sees it connect.</sub>
<sub>A person's own page: their devices and access. Adding a phone takes three steps, and the page sees it connect.
<a href="https://dawnline-demo.vercel.app"><b>Try it in the live demo</b></a>: nothing installs and nothing connects.</sub>
</p>
## 💡 Why Dawnline?
@@ -61,7 +64,7 @@ follows a modern design.
sh -c "$(wget -qO- https://raw.githubusercontent.com/tickcount/luci-app-dawnline/main/install.sh)"
```
2. **Set up** in LuCI: <kbd>Network</kbd> › <kbd>Dawnline</kbd>, and follow the
2. **Set up** in LuCI: <kbd>Services</kbd> › <kbd>Dawnline</kbd>, and follow the
wizard. It suggests the port, the public address and the networks.
3. **Invite** someone: <kbd>Add person</kbd>, then send them the link or the
invite card with its QR code. They do the rest.
@@ -86,7 +89,7 @@ follows a modern design.
<tr>
<td valign="top">
<h3>👥 People, not peers</h3>
<p>One card per person with their devices, who is online, traffic, end date and data limit. Filter by who needs attention or is paused.</p>
<p>A card for each person that says only what matters in their state: who waits for a decision and the one button that settles it, who is online since when and how fast, what each moved this week and this month. Filter, order, search by name, device or IP; a person or a device opens as a page of its own and Back returns you to the same place, and the same people stand on a map of where they connect from.</p>
</td>
<td valign="top">
<h3>🧭 Who reaches what</h3>
@@ -96,7 +99,7 @@ follows a modern design.
<tr>
<td valign="top">
<h3>⏳ Limits</h3>
<p>End dates, monthly or total data limits, allowed hours, a pause button. Changes apply at once, without dropping anyone else's connection.</p>
<p>End dates, monthly or total data limits, a pause button. Changes apply at once, without dropping anyone else's connection.</p>
</td>
<td valign="top">
<h3>🕶️ Disguise</h3>
@@ -106,11 +109,11 @@ follows a modern design.
<tr>
<td valign="top">
<h3>🏠 Internet like a home device</h3>
<p>People's devices go online the way the devices at home do, so your proxies, ad and DNS filters cover them too.</p>
<p>Turn it on and people's devices go online the way the devices at home do, so your proxies, ad and DNS filters cover them too. Off by default: it costs speed.</p>
</td>
<td valign="top">
<h3>🗺️ Overview and places</h3>
<p>Who is online with what, speed now, the last 30 days, and a map of where people connect from, at city level.</p>
<p>One page that says whether everything works: the health checks, who waits for a decision, who is online, speed now and who moves most, today by the hour, the last 30 days against the 30 before, and the servers with who still has to take a new configuration. Where people connect from, at city level, is a map in People.</p>
</td>
</tr>
<tr>
@@ -123,6 +126,13 @@ follows a modern design.
<p>Dawnline and AmneziaWG built for your firmware in one place; nightly backups, and export and import to move to another router.</p>
</td>
</tr>
<tr>
<td valign="top">
<h3>🌐 English and Russian</h3>
<p>The portal follows the visitor's language and switches in its header. The admin pages and the router's messages come in Russian with the <code>luci-i18n-dawnline-ru</code> package.</p>
</td>
<td valign="top"></td>
</tr>
</table>
## 🧩 How it works
@@ -150,20 +160,30 @@ flowchart LR
## 📸 A closer look
<p align="center">
<img src="docs/screenshots/overview.png" alt="Overview: who is online, speed, the last 30 days and where people connect from" width="720">
<img src="docs/screenshots/people.png" alt="People: who needs a decision, who is online, everyone else, who is paused" width="720">
<br>
<sub><b>Overview</b>: who is online, speed now, the last 30 days and a map of where people connect from.</sub>
<sub><b>People</b>: who needs a decision, who is online, everyone else, who is paused.</sub>
</p>
<table>
<tr>
<td width="50%" valign="top">
<img alt="People: everyone on the server, with their devices, data and state" src="docs/screenshots/people.png">
<p align="center"><sub><b>People</b>: online, ending soon, over the limit, outside their hours, paused.</sub></p>
<img alt="A person: their traffic, devices, places, term, usage and what they reach" src="docs/screenshots/person.png">
<p align="center"><sub><b>A person</b>: their traffic, devices and places; the term, the usage and what they reach.</sub></p>
</td>
<td width="50%" valign="top">
<img alt="A person: their devices, traffic, places and limits" src="docs/screenshots/person.png">
<p align="center"><sub><b>A person</b>: their devices, traffic, places and limits.</sub></p>
<img alt="Places: where devices connect from, on the map and in a list" src="docs/screenshots/places.png">
<p align="center"><sub><b>Places</b>: where devices connect from, now and over the last 30 days.</sub></p>
</td>
</tr>
<tr>
<td width="50%" valign="top">
<img alt="A device: when it was online, its traffic, places and set-up" src="docs/screenshots/device.png">
<p align="center"><sub><b>A device</b>: when it was online, its traffic, places and set-up.</sub></p>
</td>
<td width="50%" valign="top">
<img alt="A server's core: the disguise, MTU and keep-alive of its AmneziaWG tunnel" src="docs/screenshots/core.png">
<p align="center"><sub><b>A server's core</b>: the disguise, MTU and keep-alive of its AmneziaWG tunnel.</sub></p>
</td>
</tr>
</table>
@@ -186,7 +206,8 @@ The installer checks the firmware, the clock, free space and DNS, lets you pick
a release (the latest is the default), shows the plan and asks before it
changes anything. It downloads the package for your package manager, checks it
against `sha256sums.txt`, installs AmneziaWG built for your firmware if it is
missing, then installs Dawnline and checks that LuCI can reach it.
missing, then installs Dawnline (with its Russian translation when LuCI is in
Russian) and checks that LuCI can reach it.
> [!NOTE]
> Before changing anything it saves `/etc/config/{dawnline,network,firewall,dhcp,uhttpd}`
@@ -207,6 +228,7 @@ wget -O /tmp/dawnline-install.sh https://raw.githubusercontent.com/tickcount/luc
sh /tmp/dawnline-install.sh --dry-run # check everything, change nothing
sh /tmp/dawnline-install.sh -y --tag 0.3.1 # a given release, no questions
sh /tmp/dawnline-install.sh --awg grey # AmneziaWG from 2Grey
sh /tmp/dawnline-install.sh --lang ru # with the admin's Russian translation
```
| Option | |
@@ -215,8 +237,9 @@ sh /tmp/dawnline-install.sh --awg grey # AmneziaWG from 2Grey
| `-n`, `--dry-run` | Check the system, resolve, download and verify the release; change nothing |
| `-v`, `--verbose` / `-q`, `--quiet` | More detail / only warnings and errors |
| `--tag X.Y.Z` | A given release (`sh install.sh 0.3.1` works too) |
| `--awg auto\|slava\|grey\|skip` | Where AmneziaWG comes from when it is missing: [Slava-Shchipunov], then [2Grey] (`auto`, the default), only one of them, or not at all (install it later from **Dawnline → Updates**). `DAWNLINE_AWG` does the same |
| `--awg auto\|slava\|grey\|skip` | Where AmneziaWG comes from when it is missing: [Slava-Shchipunov], then [2Grey] (`auto`, the default), only one of them, or not at all (install it later from **Dawnline → Settings → Updates**). `DAWNLINE_AWG` does the same |
| `--no-awg` | Same as `--awg skip` |
| `--lang auto\|ru\|none` | The admin's translation, `luci-i18n-dawnline-<lang>`: `auto` (the default) adds it when LuCI is set to that language or has its LuCI translation; `none` adds none. An installed one is always kept up to date. Later: **Dawnline → Settings → Updates**, Dawnline's ⋯. `DAWNLINE_LANG` does the same |
</details>
@@ -247,9 +270,12 @@ opkg update && opkg install ./luci-app-dawnline-0.1.0.ipk # Open
apk update && apk add --allow-untrusted ./luci-app-dawnline-0.1.0.apk # OpenWrt 25.12
```
The Russian translation is `luci-i18n-dawnline-ru-<version>` from the same
release, installed the same way after the app (then `/etc/init.d/uhttpd restart`).
</details>
**Upgrade** from <kbd>Dawnline</kbd> › <kbd>Updates</kbd>, or run the installer
**Upgrade** from <kbd>Dawnline</kbd> › <kbd>Settings</kbd> › <kbd>Updates</kbd>, or run the installer
again. Settings, keys and history are kept, and a backup is made first.
### Remove
@@ -259,7 +285,7 @@ sh -c "$(wget -qO- https://raw.githubusercontent.com/tickcount/luci-app-dawnline
```
It backs up the configuration to `/etc/dawnline/backups`, runs Dawnline's own
removal (the same as <kbd>Settings</kbd> › <kbd>Remove Dawnline from this router</kbd>),
removal (the same as <kbd>Settings</kbd> › <kbd>Remove Dawnline</kbd>),
removes the package and cleans up what is left. After installing Dawnline
again, restore the backup in <kbd>Settings</kbd> › <kbd>Backups</kbd>.
@@ -286,7 +312,7 @@ sh /tmp/dawnline-uninstall.sh --dry-run
| `--remove-awg` | Also remove AmneziaWG, unless another interface still uses it (`--keep-awg`, the default, keeps it) |
| `-n`, `--dry-run` | Show what would be removed; change nothing |
By hand: <kbd>Settings</kbd> › <kbd>Remove Dawnline from this router</kbd>
By hand: <kbd>Settings</kbd> › <kbd>Remove Dawnline</kbd>
(keeps a backup), then `opkg remove luci-app-dawnline` or
`apk del luci-app-dawnline`.
@@ -299,8 +325,10 @@ By hand: <kbd>Settings</kbd> › <kbd>Remove Dawnline from this router</kbd>
<br>
AmneziaVPN or AmneziaWG on phones and computers, and plain WireGuard when the
disguise is off. The portal shows each person the app for their system and how
AmneziaVPN or AmneziaWG on phones and computers, V2RayTun on Android (its
`awg://` link; not with the Maximum disguise, which it cannot
run yet),
and plain WireGuard when the disguise is off. The portal shows each person the app for their system and how
to add the connection in it.
</details>
@@ -315,6 +343,25 @@ keep working, unless you tick the ones to remove with it.
</details>
<details>
<summary><b>Can another place's whole network join (site to site)?</b></summary>
<br>
Yes. Add that place's router to a person as a device of the kind «Router» and
name the networks behind it (a server's Settings → Network → «Networks behind
people's routers»). The tunnel then carries them both ways: home devices and
people with home access reach hosts there, and hosts there reach what the
router's owner reaches. A person can also ask for a router and its networks on
their own page; you accept or decline each (or let them be added at once, per
server). The two ends need different addresses: a network that overlaps your
home network, a tunnel or another router's network is refused. The far router
must route its network into the tunnel without NAT, or your home will not
reach hosts behind it. Hosts at home reach it only where this router is their
gateway (or they have a route to that network through it).
</details>
<details>
<summary><b>Does it touch the rest of my router?</b></summary>
@@ -341,7 +388,7 @@ one, install Dawnline on the new one and import the file there.
<br>
From the address a device connects from, looked up at city level and cached on
the router. It can be turned off in <kbd>Settings</kbd>.
the router. It can be turned off in <kbd>Settings</kbd> › <kbd>History and storage</kbd>.
</details>
@@ -351,7 +398,8 @@ Only objects it created: network interfaces tagged `_dawnline`, firewall
sections named `dawnline_*`, the `inet dawnline` nft table, `/tmp/hosts/dawnline`,
the `dawnline` network namespace with its two veth links (`dlx0`, `dll0`, the
latter a port of the LAN bridge, which needs `kmod-veth`), policy rules for the
tunnel interfaces with one routing table, a second dnsmasq instance on port
tunnel interfaces with one routing table, routes to the networks behind people's
routers (routing protocol 205 in the main table), a second dnsmasq instance on port
5354 (`/var/etc/dawnline-dns.conf`), a cron line for the agent's heartbeat
check and, while the portal is on, a uhttpd instance for it.
`dawnline teardown` removes all of them.
@@ -362,7 +410,15 @@ are kept in `/var/run/dawnline` and flushed to flash at most hourly. Access
changes reach the tunnel with `awg syncconf`, without restarting the interface;
`dawnline log` on the router shows the service log.
## 🛠️ Development
Everything service-related also goes to the router's system log, one line per
event with its syslog priority: `logread -e dawnline` shows
`[info] [service] starting`, `[info] [apply] configuration applied in 310 ms · …`,
tunnels and the portal going down and back, crashes with what syslog knew about
them, every settings change (`admin changed settings · domain: lan → home`) and
people's and devices' changes. Settings → History and storage → System log picks the
level: errors, warnings, normal or verbose (each step of a start and an apply).
## 🧰 Development
```
package/luci-app-dawnline/
@@ -372,8 +428,11 @@ package/luci-app-dawnline/
root/usr/libexec/dawnline/ agent (usage, limits, presence) and updater
root/usr/share/dawnline/portal/ portal CGI wrapper and its (minified) assets
root/usr/bin/dawnline command line (`dawnline help`)
htdocs/ LuCI views and the generated module dawnline/app.js
htdocs/ LuCI views and the generated modules dawnline/app.js (pages),
style.js (their stylesheet), map.js (the map, loaded on demand)
po/ the admin's string catalog (dawnline.pot, ru/dawnline.po)
web/ TypeScript sources of the admin UI and the portal
web/i18n/ catalog tools: collect strings, merge .po files, compile .lmo
build/Dockerfile package build with the OpenWrt SDK
scripts/ build, deploy and check scripts
```
@@ -382,19 +441,23 @@ scripts/ build, deploy and check scripts
cd web
npm ci
npm run format # Prettier
npm run ci # format check, types, tests, minified bundles
npm run ci # format check, types, lint, unused code, catalogs, tests, minified bundles
npm run i18n # collect new strings into the catalogs (then translate them in ru.po)
scripts/lint-ucode.py # static ucode checks
scripts/check-ucode.sh root@router # compile every ucode file on a router
scripts/deploy.sh root@router # install the working tree (backs up configs first)
scripts/build-packages.sh all # .ipk and .apk with the OpenWrt SDK in Docker
scripts/capture-screenshots.mjs --demo # README screenshots from a router, a demo cast in place of its people
scripts/capture-screenshots.mjs # the admin's README screenshots from its demo (no router; needs a build of the Outline theme beside)
scripts/capture-portal.mjs # the portal's from its demo; both in English and Russian
scripts/frame-screenshots.py # frame them for this README
```
The web bundles are committed: CI fails when they are out of date. Pushing a
tag `vX.Y.Z` (matching `PKG_VERSION`) builds both packages with the OpenWrt SDK
and publishes a release with `sha256sums.txt`.
The web bundles are built, not committed (`npm run build` before
`scripts/build-packages.sh`); the catalogs are committed, and CI fails when they
are out of date or a string is untranslated (`rules/i18n.md`). Pushing a tag `vX.Y.Z`
(matching `PKG_VERSION`) builds the packages and their translations with the
OpenWrt SDK and publishes a release with `sha256sums.txt`.
---
+475
View File
@@ -0,0 +1,475 @@
<div align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="docs/assets/logo-dark.svg">
<img alt="Dawnline" src="docs/assets/logo-light.svg" width="88" height="88">
</picture>
<h1>Dawnline</h1>
**Ваш роутер на OpenWrt как личный VPN для тех, кого вы выберете.**
<a href="https://github.com/tickcount/luci-app-dawnline/releases/latest"><picture><source media="(prefers-color-scheme: dark)" srcset="https://shieldcn.dev/github/release/tickcount/luci-app-dawnline.svg?variant=secondary&font=geist&mode=dark"><img alt="Последний релиз" src="https://shieldcn.dev/github/release/tickcount/luci-app-dawnline.svg?variant=secondary&font=geist&mode=light"></picture></a>
<a href="https://github.com/tickcount/luci-app-dawnline/actions/workflows/ci.yml"><picture><source media="(prefers-color-scheme: dark)" srcset="https://shieldcn.dev/github/ci/tickcount/luci-app-dawnline.svg?variant=secondary&font=geist&mode=dark"><img alt="CI" src="https://shieldcn.dev/github/ci/tickcount/luci-app-dawnline.svg?variant=secondary&font=geist&mode=light"></picture></a>
<a href="https://github.com/tickcount/luci-app-dawnline/releases"><picture><source media="(prefers-color-scheme: dark)" srcset="https://shieldcn.dev/github/downloads/tickcount/luci-app-dawnline.svg?variant=secondary&font=geist&mode=dark"><img alt="Загрузки" src="https://shieldcn.dev/github/downloads/tickcount/luci-app-dawnline.svg?variant=secondary&font=geist&mode=light"></picture></a>
<a href="LICENSE"><picture><source media="(prefers-color-scheme: dark)" srcset="https://shieldcn.dev/github/license/tickcount/luci-app-dawnline.svg?variant=secondary&font=geist&mode=dark"><img alt="Лицензия" src="https://shieldcn.dev/github/license/tickcount/luci-app-dawnline.svg?variant=secondary&font=geist&mode=light"></picture></a>
<br>
<a href="https://openwrt.org/"><picture><source media="(prefers-color-scheme: dark)" srcset="https://shieldcn.dev/badge/OpenWrt-24.10%20%C2%B7%20opkg.svg?variant=secondary&font=geist&logo=openwrt&mode=dark"><img alt="OpenWrt 24.10" src="https://shieldcn.dev/badge/OpenWrt-24.10%20%C2%B7%20opkg.svg?variant=secondary&font=geist&logo=openwrt&mode=light"></picture></a>
<a href="https://openwrt.org/"><picture><source media="(prefers-color-scheme: dark)" srcset="https://shieldcn.dev/badge/OpenWrt-25.12%20%C2%B7%20apk.svg?variant=secondary&font=geist&logo=openwrt&mode=dark"><img alt="OpenWrt 25.12" src="https://shieldcn.dev/badge/OpenWrt-25.12%20%C2%B7%20apk.svg?variant=secondary&font=geist&logo=openwrt&mode=light"></picture></a>
<a href="https://docs.amnezia.org/"><picture><source media="(prefers-color-scheme: dark)" srcset="https://shieldcn.dev/badge/AmneziaWG-2.0%20%C2%B7%203.1.svg?variant=secondary&font=geist&logo=wireguard&mode=dark"><img alt="AmneziaWG 2.0 и 3.1" src="https://shieldcn.dev/badge/AmneziaWG-2.0%20%C2%B7%203.1.svg?variant=secondary&font=geist&logo=wireguard&mode=light"></picture></a>
[**Быстрый старт**](#-быстрый-старт) &nbsp;·&nbsp; [**Демо**](https://dawnline-demo.vercel.app) &nbsp;·&nbsp; [Возможности](#-возможности) &nbsp;·&nbsp; [Как это работает](#-как-это-работает) &nbsp;·&nbsp; [Скриншоты](#-как-это-выглядит) &nbsp;·&nbsp; [Вопросы](#-вопросы-и-ответы) &nbsp;·&nbsp; [Разработка](#-разработка)
[English](README.md) &nbsp;·&nbsp; **Русский**
</div>
<br>
Dawnline запускает сервер [AmneziaWG] на вашем роутере с OpenWrt и даёт каждому,
кого вы пригласите, **личную ссылку**. По ней человек **сам добавляет свои
телефоны и компьютеры** и подключается через AmneziaVPN, AmneziaWG или WireGuard.
Вы решаете, **что ему доступно** (домашняя сеть, интернет или и то и другое), и
**на какой срок и сколько трафика**.
<p align="center">
<a href="https://dawnline-demo.vercel.app"><img src="docs/screenshots/portal-ru.png" alt="Личный портал: устройства и доступ, добавление телефона в три шага" width="860"></a>
<br>
<sub>Личная страница человека: его устройства и доступ. Телефон добавляется в три шага, и страница видит, когда он подключился.
<a href="https://dawnline-demo.vercel.app"><b>Попробуйте в демо</b></a>: ничего не устанавливается и никуда не подключается.</sub>
</p>
## 💡 Почему Dawnline?
Обычно, чтобы поделиться домашним VPN, приходится вручную делать файл
конфигурации, отправлять его в мессенджере и надеяться, что дальше он никуда не
уйдёт. Dawnline делает наоборот:
| | VPN вручную | С Dawnline |
|---|---|---|
| **Настройка устройства** | Вы делаете конфиг и отправляете его | Человек добавляет устройство сам по своей ссылке |
| **Кому что можно** | Всем одно и то же | Дом, интернет или и то и другое, для каждого своё |
| **Конец доступа** | Найти и удалить его пиры | Дата, лимит трафика или один клик |
| **Что происходит** | Читать `wg show` | Кто в сети, трафик, места, журнал |
| **Когда что-то ломается** | Узнаёте от людей | Автовосстановление замечает и чинит |
Это преемник [Liminal]: та же идея, перестроенная вокруг людей, а не пиров, с
личным порталом, лимитами, трафиком в реальном времени и современной админкой
в LuCI.
## 🚀 Быстрый старт
1. **Установите** на роутер (установщик спрашивает, прежде чем что-то менять):
```sh
sh -c "$(wget -qO- https://raw.githubusercontent.com/tickcount/luci-app-dawnline/main/install.sh)"
```
2. **Настройте** в LuCI: <kbd>Services</kbd> › <kbd>Dawnline</kbd>, дальше по
мастеру. Он предложит порт, публичный адрес и сети.
3. **Пригласите** человека: <kbd>Добавить человека</kbd>, затем отправьте ему
ссылку или карточку приглашения с QR-кодом. Остальное он сделает сам.
> [!TIP]
> Если портал за Cloudflare, дайте ему порт, который Cloudflare проксирует:
> 2053, 2083, 2087, 2096 или 8443.
## ✨ Возможности
<table>
<tr>
<td width="50%" valign="top">
<h3>🔗 Ссылка вместо аккаунта</h3>
<p>Три слова, например <code>vpn.example.com/kamiro-telosu-bavine</code>. Ссылка и есть ключ: без регистрации и пароля, а новая ссылка сразу отключает старую.</p>
</td>
<td width="50%" valign="top">
<h3>📱 Свои устройства</h3>
<p>Люди сами добавляют, переименовывают и удаляют устройства в пределах лимита. При добавлении портал ведёт по шагам: нужное приложение, затем QR-код, ссылка, ключ или файл.</p>
</td>
</tr>
<tr>
<td valign="top">
<h3>👥 Люди, а не пиры</h3>
<p>Карточка на человека, где сказано только то, что важно в его состоянии: кто ждёт решения и одна кнопка, которая его решает, кто в сети, с какого времени и с какой скоростью, сколько скачано за неделю и за месяц. Фильтры, порядок, поиск по имени, устройству или IP; человек и устройство открываются отдельной страницей, а «Назад» возвращает на то же место, и те же люди стоят на карте мест, откуда они подключаются.</p>
</td>
<td valign="top">
<h3>🧭 Кому что доступно</h3>
<p>Домашняя сеть, интернет или и то и другое; только часть домашних сетей или устройств (NAS, Home Assistant). Семья видит семью, гости видят гостей.</p>
</td>
</tr>
<tr>
<td valign="top">
<h3>⏳ Лимиты</h3>
<p>Даты окончания, лимит трафика на месяц или за всё время, кнопка паузы. Изменения применяются сразу и не обрывают подключения остальных.</p>
</td>
<td valign="top">
<h3>🕶️ Маскировка</h3>
<p>Пресеты, которые мешают распознать и заблокировать VPN-трафик, и что каждый из них требует от приложений.</p>
</td>
</tr>
<tr>
<td valign="top">
<h3>🏠 Интернет как у домашних устройств</h3>
<p>Включите, и устройства людей выйдут в интернет так же, как домашние: ваши прокси, фильтры рекламы и DNS сработают и для них. По умолчанию выключено: это стоит скорости.</p>
</td>
<td valign="top">
<h3>🗺️ Обзор и места</h3>
<p>Одна страница, на которой видно, всё ли работает: проверки состояния, кто ждёт решения, кто в сети, скорость сейчас и кто нагружает больше всех, сегодня по часам, последние 30 дней рядом с предыдущими и серверы с теми, кто ещё не забрал новую настройку. Откуда подключаются (с точностью до города), показывает карта в «Людях».</p>
</td>
</tr>
<tr>
<td valign="top">
<h3>🩺 Чинит себя сам</h3>
<p>Проверки состояния с исправлением в один клик и автовосстановление, которое каждую минуту проверяет туннели, правила доступа и портал и чинит их.</p>
</td>
<td valign="top">
<h3>📦 Обновления и резервные копии</h3>
<p>Dawnline и AmneziaWG, собранный под вашу прошивку, в одном месте; ночные резервные копии, экспорт и импорт для переезда на другой роутер.</p>
</td>
</tr>
<tr>
<td valign="top">
<h3>🌐 Английский и русский</h3>
<p>Портал подстраивается под язык посетителя и переключается в шапке. Админка и сообщения роутера на русском с пакетом <code>luci-i18n-dawnline-ru</code>.</p>
</td>
<td valign="top"></td>
</tr>
</table>
## 🧩 Как это работает
```mermaid
flowchart LR
you(["🛠️ Вы, в LuCI"]) -- "добавляете человека" --> link["🔗 Его ссылка"]
link -- "он открывает её" --> portal["📱 Его портал"]
portal -- "добавляет устройство" --> app["Приложение и подключение"]
app == "туннель AmneziaWG" ==> router["🌅 Dawnline<br>на вашем роутере"]
router -- "если разрешено" --> home["🏠 Домашняя сеть"]
router -- "если разрешено" --> net["🌍 Интернет"]
```
1. **Вы добавляете человека** в LuCI и выбираете, что ему доступно, лимит
устройств, дату окончания или лимит трафика.
2. **Он открывает свою ссылку** на любом устройстве. Портал только его: там его
устройства и его доступ; доступ и лимиты меняете только вы.
3. **Он добавляет устройство.** Портал подсказывает приложение для его системы и
передаёт подключение; роутер выдаёт каждому устройству свои ключи.
4. **Устройство подключается** через сервер AmneziaWG на роутере, а Dawnline
пропускает только то, что доступно этому человеку, считает его трафик и
следит за лимитами.
## 📸 Как это выглядит
<p align="center">
<img src="docs/screenshots/people-ru.png" alt="Люди: кому нужно решение, кто в сети, остальные, кто на паузе" width="720">
<br>
<sub><b>Люди</b>: кому нужно решение, кто в сети, остальные, кто на паузе.</sub>
</p>
<table>
<tr>
<td width="50%" valign="top">
<img alt="Человек: его трафик, устройства, места, срок, использование и куда он доходит" src="docs/screenshots/person-ru.png">
<p align="center"><sub><b>Человек</b>: его трафик, устройства и места; срок, использование и куда он доходит.</sub></p>
</td>
<td width="50%" valign="top">
<img alt="Места: откуда подключаются устройства, на карте и списком" src="docs/screenshots/places-ru.png">
<p align="center"><sub><b>Места</b>: откуда подключаются устройства, сейчас и за последние 30 дней.</sub></p>
</td>
</tr>
<tr>
<td width="50%" valign="top">
<img alt="Устройство: когда было в сети, его трафик, места и настройка" src="docs/screenshots/device-ru.png">
<p align="center"><sub><b>Устройство</b>: когда было в сети, его трафик, места и настройка.</sub></p>
</td>
<td width="50%" valign="top">
<img alt="Ядро сервера: маскировка, MTU и keep-alive туннеля AmneziaWG" src="docs/screenshots/core-ru.png">
<p align="center"><sub><b>Ядро сервера</b>: маскировка, MTU и keep-alive туннеля AmneziaWG.</sub></p>
</td>
</tr>
</table>
## 📋 Требования
| | Что нужно Dawnline |
|---|---|
| **OpenWrt** | 24.10 (opkg, `.ipk`) или 25.12 (apk, `.apk`) |
| **AmneziaWG** | `kmod-amneziawg`, `amneziawg-tools`, `luci-proto-amneziawg`: если их нет, установщик поставит сборку для вашей прошивки |
| **Свободная флеш-память** | около 2 МБ |
## 📥 Установка
```sh
sh -c "$(wget -qO- https://raw.githubusercontent.com/tickcount/luci-app-dawnline/main/install.sh)"
```
Установщик проверяет прошивку, часы, свободное место и DNS, даёт выбрать релиз
(по умолчанию последний), показывает план и спрашивает, прежде чем что-то
менять. Он скачивает пакет для вашего менеджера пакетов, сверяет его с
`sha256sums.txt`, ставит AmneziaWG для вашей прошивки, если его нет, затем
ставит Dawnline (с русским переводом, если LuCI на русском) и проверяет, что
LuCI его видит.
> [!NOTE]
> Прежде чем что-то менять, он сохраняет `/etc/config/{dawnline,network,firewall,dhcp,uhttpd}`
> и состояние служб в `/etc/dawnline/installer-backups/` (хранятся три последние
> копии) и возвращает их, если какой-то шаг не удался. Пакеты не откатываются:
> чтобы вернуться к старому релизу, запустите установщик с `--tag`. Всё, что он
> делает, записывается в `/tmp/dawnline-install.log`.
<details>
<summary><b>Параметры установщика</b></summary>
<br>
С параметрами его нужно сначала скачать:
```sh
wget -O /tmp/dawnline-install.sh https://raw.githubusercontent.com/tickcount/luci-app-dawnline/main/install.sh
sh /tmp/dawnline-install.sh --dry-run # всё проверить, ничего не менять
sh /tmp/dawnline-install.sh -y --tag 0.3.1 # конкретный релиз, без вопросов
sh /tmp/dawnline-install.sh --awg grey # AmneziaWG от 2Grey
sh /tmp/dawnline-install.sh --lang ru # с русским переводом админки
```
| Параметр | |
|---|---|
| `-y`, `--yes` | Без вопросов: последний релиз (или `--tag`) и рекомендуемые варианты |
| `-n`, `--dry-run` | Проверить систему, найти, скачать и проверить релиз; ничего не менять |
| `-v`, `--verbose` / `-q`, `--quiet` | Подробнее / только предупреждения и ошибки |
| `--tag X.Y.Z` | Конкретный релиз (`sh install.sh 0.3.1` тоже работает) |
| `--awg auto\|slava\|grey\|skip` | Откуда брать AmneziaWG, если его нет: [Slava-Shchipunov], затем [2Grey] (`auto`, по умолчанию), только один из них или вообще не ставить (потом можно в **Dawnline → Настройки → Обновления**). То же самое делает `DAWNLINE_AWG` |
| `--no-awg` | То же, что `--awg skip` |
| `--lang auto\|ru\|none` | Перевод админки, `luci-i18n-dawnline-<язык>`: `auto` (по умолчанию) ставит его, если LuCI настроен на этот язык или в нём стоит перевод LuCI на него; `none` не ставит. Уже установленный перевод всегда обновляется вместе с Dawnline. Позже: **Dawnline → Настройки → Обновления**, ⋯ у Dawnline. То же самое делает `DAWNLINE_LANG` |
</details>
<details>
<summary><b>Если GitHub медленный или заблокирован</b></summary>
<br>
Установщик сам повторяет загрузки через `ghfast.top` и `gh-proxy.com`. Чтобы
скачать сам установщик через зеркало:
```sh
sh -c "$(wget -qO- https://cdn.jsdelivr.net/gh/tickcount/luci-app-dawnline@main/install.sh)"
sh -c "$(wget -qO- https://ghfast.top/https://raw.githubusercontent.com/tickcount/luci-app-dawnline/main/install.sh)"
```
</details>
<details>
<summary><b>Установка вручную</b></summary>
<br>
Из [релиза](https://github.com/tickcount/luci-app-dawnline/releases):
```sh
opkg update && opkg install ./luci-app-dawnline-0.1.0.ipk # OpenWrt 24.10
apk update && apk add --allow-untrusted ./luci-app-dawnline-0.1.0.apk # OpenWrt 25.12
```
Русский перевод: `luci-i18n-dawnline-ru-<версия>` из того же релиза, ставится
так же, после приложения (затем `/etc/init.d/uhttpd restart`).
</details>
**Обновление**: в <kbd>Dawnline</kbd> › <kbd>Настройки</kbd> › <kbd>Обновления</kbd> или повторным
запуском установщика. Настройки, ключи и история сохраняются, перед обновлением
делается резервная копия.
### Удаление
```sh
sh -c "$(wget -qO- https://raw.githubusercontent.com/tickcount/luci-app-dawnline/main/uninstall.sh)"
```
Деинсталлятор сохраняет конфигурацию в `/etc/dawnline/backups`, запускает
удаление средствами самого Dawnline (как <kbd>Настройки</kbd> › <kbd>Удалить
Dawnline</kbd>), удаляет пакет и подчищает остатки. После
повторной установки Dawnline восстановите копию в <kbd>Настройки</kbd> ›
<kbd>Резервные копии</kbd>.
> [!IMPORTANT]
> AmneziaWG остаётся установленным: его могут использовать другие туннели.
> Чтобы удалить и его, добавьте `--remove-awg`.
<details>
<summary><b>Параметры деинсталлятора</b></summary>
<br>
С параметрами его нужно сначала скачать:
```sh
wget -O /tmp/dawnline-uninstall.sh https://raw.githubusercontent.com/tickcount/luci-app-dawnline/main/uninstall.sh
sh /tmp/dawnline-uninstall.sh --dry-run
```
| Параметр | |
|---|---|
| `-y`, `--yes` | Не спрашивать подтверждения |
| `-p`, `--purge` | Удалить также конфигурацию, ключи, историю и все резервные копии (`/etc/config/dawnline`, `/etc/dawnline`); копия не делается |
| `--remove-awg` | Удалить также AmneziaWG, если его не использует другой интерфейс (`--keep-awg`, по умолчанию, оставляет его) |
| `-n`, `--dry-run` | Показать, что будет удалено; ничего не менять |
Вручную: <kbd>Настройки</kbd> › <kbd>Удалить Dawnline</kbd>
(резервная копия сохраняется), затем `opkg remove luci-app-dawnline` или
`apk del luci-app-dawnline`.
</details>
## ❓ Вопросы и ответы
<details>
<summary><b>Какими приложениями пользуются люди?</b></summary>
<br>
AmneziaVPN или AmneziaWG на телефонах и компьютерах, V2RayTun на Android (по
ссылке `awg://`; кроме максимальной маскировки, её он пока не
умеет), а
обычный WireGuard, если маскировка выключена. Портал показывает каждому приложение для его системы и
как добавить в нём подключение.
</details>
<details>
<summary><b>Что делать, если человек потерял ссылку?</b></summary>
<br>
Создайте новую на его странице. Старая ссылка сразу перестанет работать; его
устройства продолжат работать, если не отметить те, что нужно удалить вместе с
ней.
</details>
<details>
<summary><b>Можно ли подключить целую сеть другого места (site-to-site)?</b></summary>
<br>
Да. Добавьте роутер того места человеку как устройство типа «Роутер» и укажите
сети за ним (настройки сервера → Сеть → «Сети за роутерами людей»). Туннель
понесёт их в обе стороны: домашние устройства и люди с доступом к дому попадают
в узлы там, а узлам там доступно то же, что владельцу роутера. Человек может и
сам попросить роутер и его сети на своей странице; вы принимаете или отклоняете
каждый запрос (или разрешаете добавлять сразу, для каждого сервера отдельно).
Двум концам нужны разные адреса: сеть, которая пересекается с вашей домашней
сетью, туннелем или сетью другого роутера, не принимается. Дальний роутер
должен маршрутизировать свою сеть в туннель без NAT, иначе из дома до узлов за
ним не добраться. Домашние узлы попадают туда только там, где этот роутер у них
шлюз (или у них есть маршрут в ту сеть через него).
</details>
<details>
<summary><b>Затрагивает ли он остальной роутер?</b></summary>
<br>
Только то, что создал сам, и `dawnline teardown` убирает всё это. Полный список:
[Что Dawnline меняет на роутере](#-что-dawnline-меняет-на-роутере).
</details>
<details>
<summary><b>Как переехать на другой роутер?</b></summary>
<br>
<kbd>Экспорт JSON</kbd> в <kbd>Настройки</kbd> › <kbd>Резервные копии</kbd> на
старом роутере, установите Dawnline на новый и импортируйте там этот файл.
</details>
<details>
<summary><b>Откуда берутся места на карте?</b></summary>
<br>
Из адреса, с которого подключается устройство: город определяется по нему и
кешируется на роутере. Это можно выключить в <kbd>Настройки</kbd> › <kbd>История и хранение</kbd>.
</details>
## 🧾 Что Dawnline меняет на роутере
Только то, что создал сам: сетевые интерфейсы с меткой `_dawnline`, секции
межсетевого экрана `dawnline_*`, таблицу nft `inet dawnline`, `/tmp/hosts/dawnline`,
сетевое пространство имён `dawnline` с двумя veth-связями (`dlx0` и `dll0`,
вторая подключена к мосту LAN и требует `kmod-veth`), правила маршрутизации для
интерфейсов туннелей с одной таблицей маршрутизации, маршруты к сетям за
роутерами людей (протокол маршрутизации 205 в основной таблице), второй экземпляр dnsmasq на
порту 5354 (`/var/etc/dawnline-dns.conf`), строку cron для проверки пульса
агента и, пока портал включён, экземпляр uhttpd для него. `dawnline teardown`
убирает всё это.
Данные хранятся в `/etc/config/dawnline` (права 0600, там приватные ключи) и в
`/etc/dawnline/` (трафик, события, выданные конфигурации, резервные копии).
Текущие счётчики живут в `/var/run/dawnline` и записываются во флеш-память не
чаще раза в час. Изменения доступа доходят до туннеля через `awg syncconf`, без
перезапуска интерфейса; `dawnline log` на роутере показывает журнал служб.
Всё, что касается служб, попадает и в системный журнал роутера, строкой на
событие с приоритетом syslog: `logread -e dawnline` покажет
`[info] [service] starting`, `[info] [apply] configuration applied in 310 ms · …`,
падения и восстановление туннелей и портала, сбои с тем, что о них знал syslog,
каждое изменение настроек (`admin changed settings · domain: lan → home`) и
изменения людей и устройств. Уровень выбирается в «Настройки → История и
хранение → Системный журнал»: ошибки, предупреждения, обычный или подробный
(каждый шаг запуска и применения настроек).
## 🧰 Разработка
```
package/luci-app-dawnline/
root/usr/share/ucode/dawnline/ библиотека на ucode: модель (core), AmneziaWG (awg), apply, live,
ops, view, loop, portal
root/usr/share/rpcd/ucode/ объект RPC luci.dawnline
root/usr/libexec/dawnline/ агент (трафик, лимиты, присутствие) и апдейтер
root/usr/share/dawnline/portal/ CGI-обёртка портала и его (минифицированные) файлы
root/usr/bin/dawnline командная строка (`dawnline help`)
htdocs/ представления LuCI и сгенерированные модули dawnline/app.js (страницы),
style.js (их стили), map.js (карта, загружается по запросу)
po/ каталог строк админки (dawnline.pot, ru/dawnline.po)
web/ исходники админки и портала на TypeScript
web/i18n/ инструменты каталогов: сбор строк, слияние .po, сборка .lmo
build/Dockerfile сборка пакетов в OpenWrt SDK
scripts/ скрипты сборки, деплоя и проверок
```
```sh
cd web
npm ci
npm run format # Prettier
npm run ci # форматирование, типы, линтер, неиспользуемый код, каталоги, тесты, бандлы
npm run i18n # собрать новые строки в каталоги (затем перевести их в ru.po)
scripts/lint-ucode.py # статические проверки ucode
scripts/check-ucode.sh root@router # скомпилировать все файлы ucode на роутере
scripts/deploy.sh root@router # поставить рабочую копию (сначала сохраняет конфиги)
scripts/build-packages.sh all # .ipk и .apk в OpenWrt SDK в Docker
scripts/capture-screenshots.mjs # скриншоты админки для README из её демо (без роутера; рядом нужна сборка темы Outline)
scripts/capture-portal.mjs # скриншоты портала из его демо; и те и другие на английском и русском
scripts/frame-screenshots.py # оформить их для README
```
Бандлы не лежат в репозитории, они собираются (`npm run build` перед
`scripts/build-packages.sh`); каталоги лежат, и CI падает, если они устарели или
строка не переведена (`rules/i18n.md`). Тег `vX.Y.Z` (совпадающий с `PKG_VERSION`)
собирает пакеты и переводы в OpenWrt SDK и публикует релиз с `sha256sums.txt`.
---
<div align="center">
<sub>Сделано для OpenWrt · <a href="LICENSE">лицензия MIT</a> · <a href="#readme">Наверх ↑</a></sub>
</div>
[AmneziaWG]: https://docs.amnezia.org/
[Liminal]: https://github.com/tickcount/openwrt-liminal
[Slava-Shchipunov]: https://github.com/Slava-Shchipunov/awg-openwrt
[2Grey]: https://github.com/2Grey/awg-openwrt
+551
View File
@@ -0,0 +1,551 @@
// Package access is "Check access": it walks the same rules the access table and a device's configuration
// apply, and says in words where a connection stops, each step a whole sentence in the reader's language
// (dawnline.access). Held to ucode's own output on a made-up household (testdata, scripts/ucode-golden.sh).
package access
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/handout"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
)
// Step is one step of the walk.
type Step struct {
OK bool `json:"ok"`
Title string `json:"title"`
Detail string `json:"detail"`
}
// Ctx is what the walk reads besides the settings: each person's state, the tunnels' peers, who is online
// and the router's interfaces.
type Ctx struct {
States map[string]string
Peers map[string]awg.Peer
Online func(d *model.Device, peer awg.Peer) bool
Snap *netview.Snapshot
T *i18n.Catalog
}
// Said is a refusal to check at all, as a sentence to translate and what goes into it.
type Said struct {
Msg string
Args []any
}
func (s Said) Error() string { return fmt.Sprintf(s.Msg, s.Args...) }
type walk struct {
Ctx
m *model.Model
}
func (w *walk) f(msg string, args ...any) string { return fmt.Sprintf(w.T.T(msg), args...) }
// inNets is the first of nets that holds ip, or "".
func inNets(ip string, nets []string) string {
n, ok := ipnet.IP(ip)
if !ok {
return ""
}
for _, s := range nets {
if c, ok := ipnet.Any(s); ok && c.Holds(n) {
return s
}
}
return ""
}
func (w *walk) cantConnect(name, st string) string {
switch st {
case "no_server":
return w.f("%s can't connect: they have no server", name)
case "server_off":
return w.f("%s can't connect: their server is turned off", name)
case "paused":
return w.f("%s can't connect: they are paused", name)
case "expired":
return w.f("%s can't connect: their access has ended", name)
case "quota":
return w.f("%s can't connect: they used up their data", name)
}
return w.f("%s can't connect: %s", name, st)
}
func (w *walk) cantReach(who string, disabled bool, st string) string {
switch {
case disabled:
return w.f("%s can't be reached: it is turned off", who)
case st == "no_server":
return w.f("%s can't be reached: they have no server", who)
case st == "server_off":
return w.f("%s can't be reached: their server is turned off", who)
case st == "paused":
return w.f("%s can't be reached: they are paused", who)
case st == "expired":
return w.f("%s can't be reached: their access has ended", who)
case st == "quota":
return w.f("%s can't be reached: they used up their data", who)
}
return w.f("%s can't be reached: %s", who, st)
}
func (w *walk) accessText(a string) string {
switch a {
case "lan":
return w.T.T("Home only")
case "internet":
return w.T.T("Internet only")
case "both":
return w.T.T("Home and Internet")
}
return a
}
func (w *walk) hasAccess(name, a string) string {
switch a {
case "lan":
return w.f("%s has Home only", name)
case "internet":
return w.f("%s has Internet only", name)
}
return w.f("%s has Home and Internet", name)
}
func (w *walk) reaches(name string, devs []string) string {
if len(devs) > 0 {
return w.f("%s reaches %s.", name, strings.Join(devs, ", "))
}
return w.f("%s reaches no home network.", name)
}
// tunnelOf is a server's tunnel network when it runs one of Dawnline's own, else "".
func tunnelOf(srv *model.Server) string {
if srv == nil || srv.PrivateKey == "" || srv.Blocked != nil || !model.ValidIfname(srv.Ifname) {
return ""
}
if _, ok := ipnet.Subnet(srv.Subnet); !ok {
return ""
}
return srv.Subnet
}
// inTunnels is the tunnel network (of any server) that holds ip: never home, whatever routes cover it.
func (w *walk) inTunnels(ip string) string {
var nets []string
for _, id := range w.m.ServerIDs() {
if t := tunnelOf(w.m.Servers[id]); t != "" {
nets = append(nets, t)
}
}
return inNets(ip, nets)
}
type target struct {
dev *model.Device
ip string
port int // 0: none
// net: the address is a host in this network behind dev (a router): the rules see it as they see the router
net string
}
var address = regexp.MustCompile(`^([0-9.]+)(:([0-9]{1,5}))?$`)
// parse reads "10.0.1.20:445" or a device id.
func (w *walk) parse(s string) (target, bool) {
s = strings.Trim(s, " \t\r\n\v\f")
if d := w.m.Devices[s]; d != nil {
return target{dev: d, ip: d.Address}, true
}
mm := address.FindStringSubmatch(s)
if mm == nil {
return target{}, false
}
if _, ok := ipnet.IP(mm[1]); !ok {
return target{}, false
}
t := target{ip: mm[1]}
if mm[3] != "" {
t.port, _ = strconv.Atoi(mm[3])
}
for _, id := range w.m.DeviceIDs() {
if d := w.m.Devices[id]; d.Address == t.ip {
t.dev = d
break
}
}
// a host behind someone's router: the networks in effect on this router, as the access table has them
if t.dev == nil {
behind := w.Snap.Behind(w.m)
for _, id := range w.m.DeviceIDs() {
if n := inNets(t.ip, behind[id]); n != "" {
t.dev, t.net = w.m.Devices[id], n
break
}
}
}
return t, true
}
// behindStep says that an address is a host behind a router, which the rest of the walk is then about.
func (w *walk) behindStep(t target) Step {
who := t.dev.Name
if p := w.m.People[t.dev.Person]; p != nil {
who = p.Name + " · " + t.dev.Name
}
return Step{true, w.f("%s is behind %s", t.ip, who), w.f("%s: the tunnel carries this network, and the rules see a host there as they see the router.", t.net)}
}
// personHome is the home networks a person reaches: all of the server's, or the interfaces ticked on their page.
func (w *walk) personHome(srv *model.Server, p *model.Person) (all bool, nets, devs []string) {
_, home, every, _ := w.Snap.ServerNet(srv)
if len(p.HomeNets) == 0 {
for _, h := range home {
devs = append(devs, h.Dev)
}
return true, every, devs
}
var picked []string
for _, h := range home {
for _, want := range p.HomeNets {
if want == h.Dev {
picked = append(picked, h.Nets...)
devs = append(devs, h.Dev)
break
}
}
}
return false, ipnet.Collapse(picked), devs
}
// homeDevOf is the home network (interface) of a server that holds an address.
func (w *walk) homeDevOf(srv *model.Server, ip string) string {
_, home, _, _ := w.Snap.ServerNet(srv)
for _, h := range home {
if inNets(ip, h.Nets) != "" {
return h.Dev
}
}
return ""
}
func (w *walk) routerHas(ip string) bool {
for _, d := range w.Snap.Devices {
for _, a := range d.Addrs {
if strings.SplitN(a, "/", 2)[0] == ip {
return true
}
}
}
return false
}
func (w *walk) state(p *model.Person) string {
if st := w.States[p.ID]; st != "" {
return st
}
return "active"
}
func (w *walk) personOK(p *model.Person, d *model.Device) Step {
if d.Disabled {
return Step{false, w.f("%s is turned off", d.Name), w.T.T("Turn the device on again on its page.")}
}
if st := w.state(p); st != "active" {
return Step{false, w.cantConnect(p.Name, st), ""}
}
return Step{true, w.f("%s is active", p.Name), w.accessText(p.Access)}
}
func (w *walk) connected(d *model.Device, who string) Step {
peer, has := w.Peers[d.PublicKey]
if !has || peer.Handshake <= 0 {
return Step{false, w.f("%s has never connected", who), w.T.T("There is no session the router could reach.")}
}
if w.Online == nil || !w.Online(d, peer) {
return Step{false, w.f("%s isn't connected right now", who), w.T.T("The router reaches it only while its VPN is on.")}
}
return Step{true, w.f("%s is connected", who), ""}
}
func (w *walk) circle(p *model.Person) *model.Circle {
if c := w.m.Circles[p.Circle]; c != nil && c.Server == p.Server {
return c
}
return nil
}
// fromDevice: a device to somewhere, the router, home, another device or the internet.
func (w *walk) fromDevice(d *model.Device, t target) []Step {
m, T := w.m, w.T
p := m.People[d.Person]
var srv *model.Server
if p != nil {
srv = m.Servers[p.Server]
}
if p == nil || srv == nil {
return []Step{{false, w.f("%s has no server", d.Name), ""}}
}
steps := []Step{w.personOK(p, d)}
if !steps[0].OK {
return steps
}
all, homeNets, homeDevs := w.personHome(srv, p)
allowed := handout.AllowedIPs(m, srv, p, d, homeNets)
via := func(ip string) Step {
// the configuration's own words: the option and its value
var v4 []string
for _, x := range allowed {
if !strings.Contains(x, ":") {
v4 = append(v4, x)
}
}
what := "AllowedIPs " + strings.Join(v4, ", ")
if inNets(ip, allowed) != "" {
return Step{true, T.T("Its configuration sends it to the router"), what}
}
return Step{false, T.T("Its configuration doesn't send it to the router"), w.f("%s: the device's own network handles it.", what)}
}
// another device
if t.dev != nil {
q := m.People[t.dev.Person]
qname := "?"
if q != nil {
qname = q.Name
}
who := qname + " · " + t.dev.Name
if t.dev.ID == d.ID {
// its own network: a router reaches what is behind it without the tunnel
if t.net != "" {
return []Step{{false, w.f("%s is behind this device", t.ip), T.T("A router reaches its own network without the tunnel.")}}
}
return []Step{{false, T.T("That is the same device"), ""}}
}
if q == nil || q.Server != p.Server {
return append(steps, Step{false, w.f("%s is on another server", who), T.T("Devices of different servers never reach each other.")})
}
if !p.HomeAccess() {
return append(steps, Step{false, w.hasAccess(p.Name, p.Access), T.T("Only people with home access reach other devices.")})
}
if !q.HomeAccess() {
return append(steps, Step{false, w.hasAccess(q.Name, q.Access), T.T("Only devices of people with home access can be reached.")})
}
if srv.Mesh == "off" {
return append(steps, Step{false, w.f(`"Devices reach each other" is off on %s`, srv.Name), T.T("Server → Access → Home network.")})
}
if srv.Mesh == "circles" && q.ID != p.ID {
a, b := w.circle(p), w.circle(q)
if a == nil || b == nil || a.ID != b.ID {
name := func(c *model.Circle) string {
if c == nil {
return T.T("no circle")
}
return c.Name
}
return append(steps, Step{false, w.f("%s and %s are not in one circle", p.Name, q.Name), p.Name + ": " + name(a) + " · " + q.Name + ": " + name(b)})
}
steps = append(steps, Step{true, w.f("Both are in %s", a.Name), T.T("Devices in one circle reach each other.")})
} else if q.ID == p.ID {
steps = append(steps, Step{true, w.f("Both are %s's devices", p.Name), ""})
} else {
steps = append(steps, Step{true, T.T("Devices reach each other on this server"), ""})
}
if st := w.state(q); st != "active" || t.dev.Disabled {
return append(steps, Step{false, w.cantReach(who, t.dev.Disabled, st), ""})
}
return append(steps, via(t.ip), w.connected(t.dev, who))
}
// a device that keeps its own local network out of the tunnel sends a private address there, router or
// home address alike: it never reaches this router's rules
if d.LanBypass && p.Access != "lan" && inNets(t.ip, allowed) == "" {
return append(steps, via(t.ip))
}
// the router itself
if w.routerHas(t.ip) {
s := m.Settings
plain := false
for _, h := range append([]string{s.PortalHost}, s.PortalAlt...) {
if _, ip := ipnet.IP(h); ip {
plain = true
}
}
portal := s.PortalEnabled && t.port != 0 && (t.port == s.PortalPort || (plain && t.port == s.PortalPort+1))
switch {
case t.port == 53:
steps = append(steps, Step{true, w.f("%s is this router: DNS is open to everyone", t.ip), ""})
case portal:
steps = append(steps, Step{true, w.f("%s is this router: the personal portal is open to everyone", t.ip), ""})
case !p.HomeAccess():
detail := w.f("%s has Internet only: only DNS and the portal on the router.", p.Name)
if p.Access == "lan" {
detail = w.f("%s has Home only: only DNS and the portal on the router.", p.Name)
}
return append(steps, Step{false, w.f("%s is this router", t.ip), detail})
default:
// like any home address: only on the networks the person reaches
if !all && inNets(t.ip, homeNets) == "" {
return append(steps, Step{false, w.f("%s is this router on a network %s doesn't reach", t.ip, p.Name), w.reaches(p.Name, homeDevs)})
}
detail := w.f("On one of the networks %s reaches.", p.Name)
if all {
detail = T.T("Home access includes the router.")
}
steps = append(steps, Step{true, w.f("%s is this router", t.ip), detail})
}
return append(steps, via(t.ip))
}
// a tunnel address no device has
if tn := w.inTunnels(t.ip); tn != "" {
return append(steps, Step{false, w.f("No device uses %s", t.ip), w.f("It is in the tunnel network %s.", tn)})
}
// home
wan, _, _, zones := w.Snap.ServerNet(srv)
if hdev := w.homeDevOf(srv, t.ip); hdev != "" {
if p.Access == "internet" {
return append(steps, Step{false, w.f("%s is at home (%s)", t.ip, hdev), w.f("%s has Internet only.", p.Name)})
}
if p.Access == "lan" && len(p.Services) > 0 {
var hit *model.Service
var names []string
for _, sid := range p.Services {
s := m.Services[sid]
if s == nil {
continue
}
names = append(names, s.Name)
open := len(s.Ports) == 0 || t.port == 0
for _, port := range s.Ports {
open = open || port == strconv.Itoa(t.port)
}
if s.Address == t.ip && open {
hit = s
}
}
if hit == nil {
what := t.ip
if t.port != 0 {
what += ":" + strconv.Itoa(t.port)
}
return append(steps, Step{false, w.f("%s is not one of %s's home services", what, p.Name), w.f("Allowed: %s", strings.Join(names, ", "))})
}
detail := T.T("All ports")
if len(hit.Ports) > 0 {
detail = w.f("Ports %s", strings.Join(hit.Ports, ", "))
}
steps = append(steps, Step{true, w.f("%s is %s, one of %s's home services", t.ip, hit.Name, p.Name), detail})
} else {
if inNets(t.ip, homeNets) == "" {
return append(steps, Step{false, w.f("%s is in %s, which %s doesn't reach", t.ip, hdev, p.Name), w.reaches(p.Name, homeDevs)})
}
detail := w.f("One of the networks %s reaches.", p.Name)
if all {
detail = T.T("Home access reaches every home network.")
}
steps = append(steps, Step{true, w.f("%s is in home network %s", t.ip, hdev), detail})
}
to := strings.Join(zones, ", ")
if to == "" {
to = T.T("home")
}
return append(steps, via(t.ip), Step{true, T.T("The firewall lets it through"), srv.Ifname + " → " + to})
}
// another private address, or the internet
if inNets(t.ip, []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "100.64.0.0/10", "169.254.0.0/16"}) != "" {
return append(steps, Step{false, w.f("%s isn't in %s's home network", t.ip, srv.Name), T.T("Private addresses outside the home network are blocked.")})
}
if p.Access == "lan" {
return append(steps, Step{true, w.f("%s is on the internet", t.ip), w.f("%s has Home only: their device's own internet reaches it, not the tunnel.", p.Name)})
}
leaves := T.T("It leaves through the WAN.")
if srv.LanLike {
leaves = T.T("It leaves like a home device would.")
} else if wan.Dev != "" {
leaves = w.f("It leaves through %s.", wan.Dev)
}
return append(steps, via(t.ip), Step{true, w.f("%s is on the internet", t.ip), leaves})
}
// toDevice: a home address to a device.
func (w *walk) toDevice(ip string, d *model.Device) []Step {
m, T := w.m, w.T
p := m.People[d.Person]
var srv *model.Server
if p != nil {
srv = m.Servers[p.Server]
}
if p == nil || srv == nil {
return []Step{{false, w.f("%s has no server", d.Name), ""}}
}
who := p.Name + " · " + d.Name
hdev := ""
if w.inTunnels(ip) == "" {
hdev = w.homeDevOf(srv, ip)
}
if hdev == "" {
return []Step{{false, w.f("%s isn't in %s's home network", ip, srv.Name), T.T("Only home devices reach people's devices.")}}
}
steps := []Step{{true, w.f("%s is at home (%s)", ip, hdev), ""}}
if !p.HomeAccess() {
title := w.hasAccess(p.Name, p.Access)
if p.Access == "lan" {
title = w.f("%s has only some home services", p.Name)
}
return append(steps, Step{false, title, T.T("Home devices reach only people with home access.")})
}
if _, nets, _ := w.personHome(srv, p); inNets(ip, nets) == "" {
return append(steps, Step{false, w.f("%s doesn't reach %s", p.Name, hdev), T.T("Networks a person does not reach cannot reach them either.")})
}
steps = append(steps, w.personOK(p, d))
if !steps[len(steps)-1].OK {
return steps
}
_, _, _, zones := w.Snap.ServerNet(srv)
from := strings.Join(zones, ", ")
if from == "" {
from = T.T("home")
}
return append(steps, Step{true, T.T("The firewall lets it through"), from + " → " + srv.Ifname}, w.connected(d, who))
}
// Check walks from one side to the other: each a device's id, or an IPv4 address (with a port when given).
// ok tells whether every step passed.
func Check(m *model.Model, from, to string, ctx Ctx) (ok bool, steps []Step, err error) {
w := &walk{ctx, m}
a, aok := w.parse(from)
if !aok {
return false, nil, Said{`"%s" is not a device or an IPv4 address.`, []any{from}}
}
b, bok := w.parse(to)
if !bok {
return false, nil, Said{`"%s" is not a device or an IPv4 address.`, []any{to}}
}
switch {
case a.dev != nil:
steps = w.fromDevice(a.dev, b)
case b.dev != nil:
steps = w.toDevice(a.ip, b.dev)
default:
return false, nil, errors.New("Choose a device on at least one side.")
}
// a side that is a host behind a router: said first, for each such side
var lead []Step
for _, t := range []target{a, b} {
if t.net != "" {
lead = append(lead, w.behindStep(t))
}
}
steps = append(lead, steps...)
ok = true
for _, s := range steps {
ok = ok && s.OK
}
return ok, steps, nil
}
+95
View File
@@ -0,0 +1,95 @@
package access
import (
"encoding/json"
"os"
"reflect"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// Every connection of the made-up household, step for step and sentence for sentence as ucode says it
// (testdata/trace.uc on testdata/access.uci, on the router netview's golden is taken from).
func TestHouseholdAsUcode(t *testing.T) {
raw, err := os.ReadFile("testdata/trace.json")
if err != nil {
t.Fatal(err)
}
var trace struct {
States map[string]string `json:"states"`
Peers map[string]awg.Peer `json:"peers"`
Online map[string]bool `json:"online"`
Cases []struct {
From, To string
OK bool `json:"ok"`
Steps []Step `json:"steps"`
Error string `json:"error"`
} `json:"cases"`
}
if err := json.Unmarshal(raw, &trace); err != nil {
t.Fatal(err)
}
text, _ := os.ReadFile("testdata/access.uci")
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
m := model.Load(c, nil)
// the router: what the golden's container said of itself
raw, err = os.ReadFile("../netview/testdata/trace.json")
if err != nil {
t.Fatal(err)
}
var router struct {
Routes json.RawMessage `json:"routes"`
Addrs json.RawMessage `json:"addrs"`
Firewall string `json:"firewall"`
Links map[string]netview.Link `json:"links"`
}
if err := json.Unmarshal(raw, &router); err != nil {
t.Fatal(err)
}
fw, _ := uci.Parse(router.Firewall)
snap := netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return router.Addrs },
Links: func() map[string]netview.Link { return router.Links }, Zones: func() netview.Zones { return netview.ZonesOf(fw) }})
ctx := Ctx{States: trace.States, Peers: trace.Peers, Snap: snap, Online: func(d *model.Device, _ awg.Peer) bool { return trace.Online[d.ID] }}
if len(trace.Cases) < 500 {
t.Fatalf("only %d cases", len(trace.Cases))
}
passed, stopped, refused, titles, bad := 0, 0, 0, map[string]bool{}, 0
for _, want := range trace.Cases {
ok, steps, err := Check(m, want.From, want.To, ctx)
if want.Error != "" {
refused++
if err == nil || err.Error() != want.Error {
t.Errorf("%s → %s: %v, ucode refuses with %q", want.From, want.To, err, want.Error)
}
continue
}
if ok {
passed++
} else {
stopped++
}
for _, s := range steps {
titles[s.Title] = true
}
if err != nil || ok != want.OK || !reflect.DeepEqual(steps, want.Steps) {
if bad++; bad <= 12 {
g, _ := json.Marshal(steps)
u, _ := json.Marshal(want.Steps)
t.Errorf("%s → %s (%v)\n go: %v %s\n ucode: %v %s", want.From, want.To, err, ok, g, want.OK, u)
}
}
}
if bad > 0 {
t.Errorf("%d of %d cases differ", bad, len(trace.Cases))
}
if passed < 30 || stopped < 100 || refused < 3 || len(titles) < 60 {
t.Fatalf("the household exercises too little: %d pass, %d stop, %d refused, %d different steps", passed, stopped, refused, len(titles))
}
}
+95
View File
@@ -0,0 +1,95 @@
package access
import (
"encoding/json"
"os"
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// A host behind someone's router is checked as the router is: the household on the golden's router, with
// Boris's router in Anna's circle (192.168.2.0/24 behind it) and Kira's where everyone reaches everyone.
func TestAHostBehindARouter(t *testing.T) {
text, _ := os.ReadFile("testdata/access.uci")
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
for id, net := range map[string]string{"bphone": "192.168.2.0/24", "kphone": "192.168.3.0/24"} {
c.Set(id, "kind", "router")
c.SetList(id, "behind_net", []string{net})
}
c.Set("laptop", "lan_bypass", "1")
m := model.Load(c, nil)
raw, err := os.ReadFile("../netview/testdata/trace.json")
if err != nil {
t.Fatal(err)
}
var router struct {
Routes json.RawMessage `json:"routes"`
Addrs json.RawMessage `json:"addrs"`
Firewall string `json:"firewall"`
Links map[string]netview.Link `json:"links"`
}
if err := json.Unmarshal(raw, &router); err != nil {
t.Fatal(err)
}
fw, _ := uci.Parse(router.Firewall)
snap := netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return router.Addrs },
Links: func() map[string]netview.Link { return router.Links }, Zones: func() netview.Zones { return netview.ZonesOf(fw) }})
states := map[string]string{}
for _, id := range m.PersonIDs() {
states[id] = "active"
}
// every device has a session and is connected now: what stops a connection here is the rules alone
peers := map[string]awg.Peer{}
for _, id := range m.DeviceIDs() {
peers[m.Devices[id].PublicKey] = awg.Peer{Handshake: 1700000000}
}
ctx := Ctx{States: states, Peers: peers, Snap: snap, Online: func(*model.Device, awg.Peer) bool { return true }}
said := func(steps []Step) string {
var out []string
for _, s := range steps {
out = append(out, s.Title)
}
return strings.Join(out, " | ")
}
for _, c := range []struct {
from, to string
ok bool
has string
}{
// Anna's laptop (it keeps its own local network outside, so its configuration names the network) to a host behind Boris's router, in her circle
{"laptop", "192.168.2.10", true, "192.168.2.10 is behind Boris · Bphone"},
// and back: the host has Boris's access, so it reaches what his router reaches
{"192.168.2.10", "laptop", true, "192.168.2.10 is behind Boris · Bphone"},
// Fedor is in another circle
{"fphone", "192.168.2.10", false, "are not in one circle"},
// a home address reaches it as it reaches the router, and it reaches home
{"192.168.77.20", "192.168.2.10", true, "192.168.77.20 is at home"},
{"192.168.2.10", "192.168.77.20", true, "192.168.2.10 is behind Boris · Bphone"},
// the router itself and what is behind it need no tunnel between them
{"bphone", "192.168.2.10", false, "192.168.2.10 is behind this device"},
// two hosts behind one router likewise
{"192.168.2.10", "192.168.2.20", false, "192.168.2.20 is behind this device"},
// another server's network is another server's
{"phone", "192.168.3.10", false, "is on another server"},
// an address behind nobody is what it always was
{"phone", "192.168.9.10", false, ""},
} {
ok, steps, err := Check(m, c.from, c.to, ctx)
if err != nil || ok != c.ok || !strings.Contains(said(steps), c.has) {
t.Errorf("%s → %s: %v %v, want %v with %q\n %s", c.from, c.to, ok, err, c.ok, c.has, said(steps))
}
}
// a router that waits for the admin's answer stands for nothing yet
m.Devices["bphone"].Waiting = true
if _, steps, _ := Check(m, "laptop", "192.168.2.10", ctx); strings.Contains(said(steps), "is behind") {
t.Errorf("a waiting router's network is checked as its own: %s", said(steps))
}
}
+214
View File
@@ -0,0 +1,214 @@
config settings 'settings'
option portal_enabled '1'
option portal_port '4500'
option portal_host 'vpn.example.org'
list portal_alt '203.0.113.7'
config server 'home'
option name 'Home'
option ifname 'dawn0'
option port '4500'
option subnet '10.77.0.0/24'
option private_key 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA='
option public_key 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbA='
option auto_lan '0'
list lan_device 'dlhome'
list lan_device 'br-lan'
option mesh 'circles'
option lan_like '0'
config server 'cabin'
option name 'Cabin'
option ifname 'dawn1'
option port '4600'
option subnet '10.78.0.0/24'
option private_key 'ccccccccccccccccccccccccccccccccccccccccccA='
option public_key 'ddddddddddddddddddddddddddddddddddddddddddA='
option auto_lan '0'
list lan_device 'dlhome'
option mesh 'off'
config server 'open'
option name 'Open'
option ifname 'dawn2'
option port '4700'
option subnet '10.79.0.0/24'
option private_key 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeA='
option public_key 'ffffffffffffffffffffffffffffffffffffffffffA='
option auto_lan '0'
list lan_device 'br-lan'
option mesh 'all'
config circle 'fam'
option server 'home'
option name 'Family'
config circle 'work'
option server 'home'
option name 'Work'
config service 'nas'
option name 'NAS'
option address '192.168.77.5'
list ports '445'
list ports '139'
config service 'cam'
option name 'Camera'
option address '192.168.50.30'
config person 'anna'
option server 'home'
option name 'Anna'
option access 'both'
option circle 'fam'
config person 'boris'
option server 'home'
option name 'Boris'
option access 'both'
option circle 'fam'
config person 'clara'
option server 'home'
option name 'Clara'
option access 'internet'
config person 'dima'
option server 'home'
option name 'Dima'
option access 'lan'
list home_net 'dlhome'
config person 'eva'
option server 'home'
option name 'Eva'
option access 'lan'
list service 'nas'
list service 'cam'
config person 'fedor'
option server 'home'
option name 'Fedor'
option access 'both'
option circle 'work'
config person 'gleb'
option server 'home'
option name 'Gleb'
option access 'both'
option paused '1'
config person 'hana'
option server 'cabin'
option name 'Hana'
option access 'both'
config person 'ivan'
option server 'cabin'
option name 'Ivan'
option access 'both'
config person 'kira'
option server 'open'
option name 'Kira'
option access 'both'
config person 'lev'
option server 'open'
option name 'Lev'
option access 'both'
config person 'nora'
option server 'home'
option name 'Nora'
option access 'both'
option circle 'fam'
list home_net 'br-lan'
config device 'phone'
option person 'anna'
option name 'Phone'
option address '10.77.0.2'
option public_key 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA='
config device 'laptop'
option person 'anna'
option name 'Laptop'
option address '10.77.0.3'
option public_key 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCA='
option lan_bypass '1'
option kind 'computer'
config device 'old'
option person 'anna'
option name 'Old'
option address '10.77.0.4'
option public_key 'DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDA='
option disabled '1'
config device 'bphone'
option person 'boris'
option name 'Bphone'
option address '10.77.0.5'
option public_key 'EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEA='
config device 'cphone'
option person 'clara'
option name 'Cphone'
option address '10.77.0.6'
option public_key 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA='
config device 'dphone'
option person 'dima'
option name 'Dphone'
option address '10.77.0.7'
option public_key 'GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGA='
config device 'ephone'
option person 'eva'
option name 'Ephone'
option address '10.77.0.8'
option public_key 'HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHA='
config device 'fphone'
option person 'fedor'
option name 'Fphone'
option address '10.77.0.9'
option public_key 'IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIA='
config device 'gphone'
option person 'gleb'
option name 'Gphone'
option address '10.77.0.10'
option public_key 'JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJA='
config device 'hphone'
option person 'hana'
option name 'Hphone'
option address '10.78.0.2'
option public_key 'KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKA='
config device 'iphone'
option person 'ivan'
option name 'Iphone'
option address '10.78.0.3'
option public_key 'LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLA='
config device 'kphone'
option person 'kira'
option name 'Kphone'
option address '10.79.0.2'
option public_key 'MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMA='
config device 'lphone'
option person 'lev'
option name 'Lphone'
option address '10.79.0.3'
option public_key 'NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNA='
config device 'nphone'
option person 'nora'
option name 'Nphone'
option address '10.77.0.11'
option public_key 'OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOA='
+10583
View File
File diff suppressed because it is too large. Load diff
+78
View File
@@ -0,0 +1,78 @@
// Who is looking: producers of live state work only while a page shows it.
// A page that follows the stream says which topics it shows (with every
// question it asks, and a `watch` call for the ones it keeps open); each such
// word holds for a short while. Nothing asked, nobody subscribed: nothing is
// read from the tunnels for the pages, as before the stream existed.
package api
import (
"sync"
"time"
)
// Lease is how long a page's interest in a topic holds after it said so.
// Pages renew it at about a third of that.
const Lease = 30 * time.Second
// Audience remembers who looks at what.
type Audience struct {
mu sync.Mutex
until map[string]time.Time
stream bool
now func() time.Time
}
// NewAudience makes an empty audience.
func NewAudience() *Audience {
return &Audience{until: map[string]time.Time{}, now: time.Now}
}
// everything stands for a page that named no topic: it follows them all.
const everything = "*"
// Touch notes that someone looks at these topics now (none named: at all of
// them, which keeps the audience present but wakes no topic that must be
// asked for by name, like the meter).
func (a *Audience) Touch(topics []string) {
end := a.now().Add(Lease)
a.mu.Lock()
if len(topics) == 0 {
a.until[everything] = end
}
for _, t := range topics {
a.until[t] = end
}
a.mu.Unlock()
}
// Streaming notes whether the event stream has subscribers (ubusd tells).
func (a *Audience) Streaming(on bool) {
a.mu.Lock()
a.stream = on
a.mu.Unlock()
}
// Wants tells whether someone looked at the topic within the lease.
func (a *Audience) Wants(topic string) bool {
a.mu.Lock()
defer a.mu.Unlock()
return a.now().Before(a.until[topic])
}
// Present tells whether any page is there at all: one that follows the
// stream, or one that asked for anything within the lease.
func (a *Audience) Present() bool {
a.mu.Lock()
defer a.mu.Unlock()
if a.stream {
return true
}
now := a.now()
for t, end := range a.until {
if now.Before(end) {
return true
}
delete(a.until, t)
}
return false
}
+58
View File
@@ -0,0 +1,58 @@
// The stream side of the object: every event of the hub goes to the object's
// subscribers as a notification, in order, and a `ping` when nothing happened
// for a while (a page tells a dead connection from a quiet one by it; the
// ping carries the number the stream is at, so a page that missed the last
// event before a silence learns it).
package api
import (
"context"
"time"
"github.com/tickcount/luci-app-dawnline/backend/hub"
"github.com/tickcount/luci-app-dawnline/backend/ubus"
)
// PingEvery is the longest a stream stays silent.
const PingEvery = 15 * time.Second
// Notifier is the bus as the broadcast needs it.
type Notifier interface {
Notify(o *ubus.Object, kind string, data map[string]any) error
}
// Broadcast forwards the hub's events to the object's subscribers until ctx
// ends. A notification the bus did not take (ubusd away, an event over its
// size limit) is not retried: the next ping or event shows a page the gap,
// and it asks for what it missed with sync.
func Broadcast(ctx context.Context, h *hub.Hub, bus Notifier, o *ubus.Object) {
ping := time.NewTicker(PingEvery)
defer ping.Stop()
for ctx.Err() == nil {
sub := h.Follow()
follow(ctx, h, sub, bus, o, ping)
sub.Close()
}
}
// follow forwards one subscription's events; it returns when the hub let the
// subscription go (this side fell behind) or ctx ended.
func follow(ctx context.Context, h *hub.Hub, sub *hub.Sub, bus Notifier, o *ubus.Object, ping *time.Ticker) {
for {
select {
case <-ctx.Done():
return
case e, ok := <-sub.Events():
if !ok {
return
}
t := eventTable(e)
t["epoch"] = h.Epoch()
bus.Notify(o, "event", t)
ping.Reset(PingEvery)
case <-ping.C:
at := h.Sync(h.Epoch(), 0, func(string) bool { return false })
bus.Notify(o, "ping", map[string]any{"epoch": at.Epoch, "seq": at.Seq, "now": time.Now().Unix()})
}
}
}
+205
View File
@@ -0,0 +1,205 @@
// Package api is the admin's door to the live state: the ubus object
// `dawnline`, which LuCI reaches through uhttpd with rpcd's access lists in
// between (acl.d/luci-app-dawnline.json names every method here; a test holds
// the two together).
//
// hello where the stream is and how to follow it
// sync {epoch, seq, topics} a snapshot, or the events since seq
// wait {epoch, seq, topics, timeout}
// the same, held until there is an event (long poll)
// watch {topics} "these topics are on screen", renewed while they are
//
// and the notifications `event` (one change, hub.Event with the epoch) and
// `ping` to whoever subscribed (uhttpd: GET /ubus/subscribe/dawnline, an event
// stream). The two ways to follow give the same events with the same numbers;
// a page uses the stream where the web server can hold one and the long poll
// where it cannot (hello's `stream`).
//
// Arguments are checked here, once; the hub is trusted with what passes.
package api
import (
"context"
"time"
"github.com/tickcount/luci-app-dawnline/backend/hub"
"github.com/tickcount/luci-app-dawnline/backend/ubus"
)
// ObjectName is the backend's object on ubus. The ucode backend's is
// luci.dawnline; methods move from there to here as they are ported.
const ObjectName = "dawnline"
// The limits of what a caller may send.
const (
maxTopics = 32
maxTopicLen = 64
maxEpochLen = 32
// A held call must end before uhttpd gives up on it (script_timeout, 60 s).
maxWait = 50 * time.Second
defaultWait = 25 * time.Second
)
// Info is what hello tells besides the stream's position.
type Info struct {
// Version is the backend's.
Version string
// Stream is how a page should follow: "sse" (GET /ubus/subscribe/dawnline)
// or "poll" (the wait method).
Stream string
// Refresh has the topics read from the router now (the bridge's Look); nil when nothing needs it.
Refresh func(ctx context.Context)
// More is the questions the backend answers besides the stream's (package methods): hello names them,
// so a page asks them here and not of the ucode backend.
More []ubus.Method
}
// Object builds the ubus object over a hub. Who looks is noted in audience.
func Object(h *hub.Hub, audience *Audience, info Info) *ubus.Object {
more := []string{}
for _, m := range info.More {
more = append(more, m.Name)
}
return &ubus.Object{
Name: ObjectName,
Methods: append([]ubus.Method{
{Name: "hello", Handle: func(context.Context, map[string]any) (map[string]any, error) {
at := h.Sync(h.Epoch(), 0, func(string) bool { return false })
return map[string]any{
"version": info.Version, "stream": info.Stream,
"epoch": at.Epoch, "seq": at.Seq, "lease": int(Lease / time.Second),
"now": time.Now().Unix(), "methods": more,
}, nil
}},
{Name: "sync", Params: map[string]ubus.Kind{"epoch": ubus.String, "seq": ubus.Int, "topics": ubus.Array},
Handle: func(_ context.Context, args map[string]any) (map[string]any, error) {
q, err := parse(args)
if err != nil {
return nil, err
}
audience.Touch(q.topics)
return answer(h.Sync(q.epoch, q.seq, q.want())), nil
}},
{Name: "wait", Waits: true,
Params: map[string]ubus.Kind{"epoch": ubus.String, "seq": ubus.Int, "topics": ubus.Array, "timeout": ubus.Int},
Handle: func(ctx context.Context, args map[string]any) (map[string]any, error) {
q, err := parse(args)
if err != nil {
return nil, err
}
audience.Touch(q.topics)
ctx, cancel := context.WithTimeout(ctx, q.timeout)
defer cancel()
a := h.Wait(ctx, q.epoch, q.seq, q.want())
// Still looking after the wait: the next call renews it again.
audience.Touch(q.topics)
return answer(a), nil
}},
// After its own edit a page reads what it wrote: the topics are brought to what is on disk, and
// the answer is where the stream stands then. The page catches up to it (sync).
{Name: "refresh", Waits: true, Handle: func(ctx context.Context, _ map[string]any) (map[string]any, error) {
audience.Touch(nil)
if info.Refresh != nil {
ctx, cancel := context.WithTimeout(ctx, defaultWait)
defer cancel()
info.Refresh(ctx)
}
at := h.Sync(h.Epoch(), 0, func(string) bool { return false })
return map[string]any{"epoch": at.Epoch, "seq": at.Seq}, nil
}},
{Name: "watch", Params: map[string]ubus.Kind{"topics": ubus.Array},
Handle: func(_ context.Context, args map[string]any) (map[string]any, error) {
q, err := parse(args)
if err != nil {
return nil, err
}
audience.Touch(q.topics)
return map[string]any{"lease": int(Lease / time.Second)}, nil
}},
}, info.More...),
Watched: audience.Streaming,
}
}
// query is a caller's arguments, checked.
type query struct {
epoch string
seq uint64
topics []string
timeout time.Duration
}
// want is the topics the caller named; none named means all of them.
func (q query) want() func(string) bool {
if len(q.topics) == 0 {
return nil
}
set := make(map[string]bool, len(q.topics))
for _, t := range q.topics {
set[t] = true
}
return func(topic string) bool { return set[topic] }
}
func parse(args map[string]any) (query, error) {
q := query{timeout: defaultWait}
if v, ok := args["epoch"]; ok {
s, isString := v.(string)
if !isString || len(s) > maxEpochLen {
return q, ubus.StatusInvalidArgument
}
q.epoch = s
}
if v, ok := args["seq"]; ok {
n, isInt := v.(int64)
if !isInt || n < 0 {
return q, ubus.StatusInvalidArgument
}
q.seq = uint64(n)
}
if v, ok := args["timeout"]; ok {
n, isInt := v.(int64)
if !isInt || n < 1 {
return q, ubus.StatusInvalidArgument
}
q.timeout = min(time.Duration(n)*time.Second, maxWait)
}
if v, ok := args["topics"]; ok {
list, isList := v.([]any)
if !isList || len(list) > maxTopics {
return q, ubus.StatusInvalidArgument
}
for _, e := range list {
s, isString := e.(string)
if !isString || s == "" || len(s) > maxTopicLen {
return q, ubus.StatusInvalidArgument
}
q.topics = append(q.topics, s)
}
}
return q, nil
}
// answer is a hub.Answer as a ubus table.
func answer(a hub.Answer) map[string]any {
out := map[string]any{"epoch": a.Epoch, "seq": a.Seq}
if a.Docs != nil {
out["docs"] = a.Docs
}
if a.Events != nil {
events := make([]any, len(a.Events))
for i, e := range a.Events {
events[i] = eventTable(e)
}
out["events"] = events
}
return out
}
func eventTable(e hub.Event) map[string]any {
t := map[string]any{"seq": e.Seq, "topic": e.Topic, "op": e.Op}
if e.Data != nil {
t["data"] = e.Data
}
return t
}
+238
View File
@@ -0,0 +1,238 @@
package apply
import (
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/usage"
)
// The full apply (dawnline.apply apply): the settings turned into router state. Network interfaces, the
// firewall, the home-like path and the Home-only resolver, the reply table, then the runtime sync with the
// access table rebuilt. One lock ("apply") for all of it, the settings read inside it, so a rebuild and a
// runtime sync never interleave and the last one to run works from the newest settings.
// Full is what a full apply came to.
type Full struct {
Network, Firewall bool
// LikeHome is true while the home-like path is up, false when nobody wants it, or why it cannot be:
// {error: a sentence in the admin's language}.
LikeHome any
Peers map[string]string
Warning string
}
// uc writes a value as ucode's "%J" does: the agent compares the signature as text.
func uc(v any) string {
switch x := v.(type) {
case nil:
return "null"
case bool:
return strconv.FormatBool(x)
case string:
return strconv.Quote(x)
case []string:
if len(x) == 0 {
return "[ ]"
}
q := make([]string, len(x))
for i, s := range x {
q[i] = strconv.Quote(s)
}
return "[ " + strings.Join(q, ", ") + " ]"
case []any:
if len(x) == 0 {
return "[ ]"
}
q := make([]string, len(x))
for i, s := range x {
q[i] = uc(s)
}
return "[ " + strings.Join(q, ", ") + " ]"
case [][2]any:
if len(x) == 0 {
return "{ }"
}
q := make([]string, len(x))
for i, kv := range x {
q[i] = strconv.Quote(kv[0].(string)) + ": " + uc(kv[1])
}
return "{ " + strings.Join(q, ", ") + " }"
}
return "null"
}
func orNull(s string) any {
if s == "" {
return nil
}
return s
}
// NetSignature is everything on the router that Dawnline's state is derived from: WAN and LAN, the home
// networks and zones of each server, the netns address and the LAN's DNS (dawnline.apply net_signature).
// An apply is due again when it changes (LAN renumbered, interface added, bridge recreated, DHCP DNS changed).
func (r *Router) NetSignature(m *model.Model) string {
snap := r.Snapshot()
var servers [][2]any
for _, id := range m.ServerIDs() {
wan, home, nets, zones := snap.ServerNet(m.Servers[id])
homes := []any{}
for _, h := range home {
homes = append(homes, [][2]any{{"dev", h.Dev}, {"auto", h.Auto}, {"nets", append([]string{}, h.Nets...)}, {"zone", orNull(h.Zone)}, {"missing", h.Missing}})
}
servers = append(servers, [2]any{id, [][2]any{
{"wan", [][2]any{{"dev", orNull(wan.Dev)}, {"auto", wan.Auto}, {"zone", wan.Zone}, {"missing", wan.Missing}}},
{"home", homes}, {"nets", append([]string{}, nets...)}, {"zones", append([]string{}, zones...)}}})
}
var addrs any
if lan := snap.Devices[snap.LAN]; lan != nil {
addrs = append([]string{}, lan.Addrs...)
}
active, address, known, _ := r.likeHome()
var loop any
if active {
loop = orNull(address)
}
return uc([][2]any{{"wan", orNull(snap.WAN)}, {"lan", orNull(snap.LAN)}, {"addrs", addrs}, {"servers", servers},
{"dns", orNull(r.lanDNS(snap, known))}, {"loop", loop}})
}
// loopErrorText says why the home-like path is off, in the reader's language (dawnline.loop error_text).
func loopErrorText(e map[string]any, t *i18n.Catalog) string {
switch e["code"] {
case "no_lan":
return t.T("No LAN detected.")
case "not_bridge":
return fmt.Sprintf(t.T("%s is not a bridge, so Dawnline cannot join the LAN as a device."), e["dev"])
case "no_ipv4":
return fmt.Sprintf(t.T("%s has no IPv4 address."), e["dev"])
case "no_address":
return fmt.Sprintf(t.T("No free address on %s."), e["net"])
case "no_transit":
return t.T("No free link-local range for the internal link.")
case "no_table":
return t.T("No free routing table.")
}
return fmt.Sprintf(t.T("Setup failed: %s"), e["detail"])
}
func count(n int, one, many string) string {
if n == 1 {
return "1 " + one
}
return strconv.Itoa(n) + " " + many
}
// Apply is the full apply. load reads the settings anew (with the LAN address kept for the home-like
// path) once the lock is held; tools tells whether amneziawg-tools is there, caps what this AmneziaWG runs.
func (r *Router) Apply(load func() (*model.Model, string, error), u *usage.Usage, t *i18n.Catalog, tools bool, caps awg.Caps) Full {
t0 := time.Now()
r.say("debug", "apply", "applying the configuration")
step := func(name string, fn func() string) {
at := time.Now()
what := fn()
if what != "" {
what = " · " + what
}
r.say("debug", "apply", fmt.Sprintf("%s done in %d ms%s", name, time.Since(at).Milliseconds(), what))
}
changed := func(x bool) string {
if x {
return "changed"
}
return "unchanged"
}
unlock := files.Lock(r.RunDir, "apply")
m, saved, err := load()
if err != nil {
unlock()
r.say("error", "apply", "the settings could not be read: nothing was applied · "+err.Error())
return Full{Warning: err.Error(), LikeHome: false}
}
var out Full
var lp Loop
var s Result
step("network (netifd)", func() string { out.Network = r.Netifd(m, tools, caps); return changed(out.Network) })
step("firewall (fw4)", func() string { out.Firewall = r.Fw(m); return changed(out.Firewall) })
step("internet like a home device", func() string { lp = r.LoopEnsure(m, saved, true); return "" })
step("resolver for Home-only devices", func() string { r.SyncResolver(m); return "" })
step("reply routes", func() string { r.ReplyRoutes(m); return "" })
step("access rules and devices", func() string { s = r.sync(m, u, time.Now().Unix(), true, t); return "" })
os.MkdirAll(r.RunDir, 0o755)
os.WriteFile(r.RunDir+"/net.sig", []byte(r.NetSignature(m)), 0o644)
present := s.Table != "" && r.Present()
unlock()
// the address is kept once the lock is released: the settings' own lock is never taken inside it
if lp.Plan != nil && lp.Plan.Address != "" && lp.Plan.Address != saved && r.Remember != nil {
r.Remember(lp.Plan.Address)
}
took := fmt.Sprintf("configuration applied in %d ms · %s, %s, %s", time.Since(t0).Milliseconds(),
count(len(m.Servers), "server", "servers"), count(len(m.People), "person", "people"), count(len(m.Devices), "device", "devices"))
// the same as the warning, for syslog: in English whatever the admin's language
var facts []string
if s.Table != "" {
if present {
facts = append(facts, "access rules not updated, the previous ones stay: "+s.Table)
} else {
facts = append(facts, "access rules not loaded, no device loaded: "+s.Table)
}
}
for _, id := range m.ServerIDs() {
if res, has := s.Peers[id]; has && res != "ok" && res != "down" {
facts = append(facts, m.Servers[id].Name+": devices not loaded: "+res)
}
}
if len(facts) > 0 {
r.say("warn", "apply", took+" · "+strings.Join(facts, " · "))
} else {
r.say("info", "apply", took)
}
out.Peers, out.Warning = s.Peers, s.Warning
out.LikeHome = !lp.Off
if lp.Error != nil {
note := LoopErrorNote(lp.Error)
if lp.Error["code"] == "setup" {
note = fmt.Sprintf("Setup failed: %v", lp.Error["detail"])
}
r.say("warn", "apply", "internet like a home device: "+note)
out.LikeHome = map[string]any{"error": loopErrorText(lp.Error, t)}
}
return out
}
// NetChanged tells whether the router's networks differ from what the last apply worked from.
func (r *Router) NetChanged(m *model.Model) bool {
was, err := os.ReadFile(r.RunDir + "/net.sig")
return err != nil || string(was) != r.NetSignature(m)
}
// Teardown removes everything Dawnline made on the router (dawnline.apply teardown): the home-like path,
// the resolver's configuration, every tunnel interface and firewall section of ours, the reply table, the
// access table and the device names. m is the settings as they stand; nothing of them is written here.
func (r *Router) Teardown(m *model.Model) {
defer files.Lock(r.RunDir, "apply")()
var ifnames []string
for _, id := range m.ServerIDs() {
ifnames = append(ifnames, m.Servers[id].Ifname)
}
r.LoopRemove(ifnames)
os.Remove(r.varEtc() + "/dawnline-dns.conf")
// as with no server and no portal: what is ours is then nobody's, and goes
bare := m.WithoutServers()
bare.Settings.PortalEnabled = false
r.Netifd(bare, true, awg.Caps{})
r.Fw(bare)
r.ReplyRoutes(bare)
r.Exec("nft", "delete", "table", "inet", "dawnline")
os.Remove(r.HostsFile)
os.Remove(r.RunDir + "/net.sig")
r.Exec("killall", "-HUP", "dnsmasq")
}
+243
View File
@@ -0,0 +1,243 @@
package apply
import (
"encoding/json"
"os"
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// The network signature is ucode's own text: its agent applies again when the two differ (testdata/sig.uc).
func TestNetSignatureAsUcode(t *testing.T) {
raw, err := os.ReadFile("testdata/sig.json")
if err != nil {
t.Fatal(err)
}
var g struct {
Sigs []string `json:"sigs"`
}
if err := json.Unmarshal(raw, &g); err != nil || len(g.Sigs) != 3 {
t.Fatal("the golden is not as expected")
}
raw, err = os.ReadFile("../netview/testdata/trace.json")
if err != nil {
t.Fatal(err)
}
var router struct {
Routes json.RawMessage `json:"routes"`
Addrs json.RawMessage `json:"addrs"`
Firewall string `json:"firewall"`
Links map[string]netview.Link `json:"links"`
}
if err := json.Unmarshal(raw, &router); err != nil {
t.Fatal(err)
}
fw, _ := uci.Parse(router.Firewall)
dir := t.TempDir()
r := &Router{RunDir: dir, Snapshot: func() *netview.Snapshot {
return netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return router.Addrs },
Links: func() map[string]netview.Link { return router.Links }, Zones: func() netview.Zones { return netview.ZonesOf(fw) }})
}}
text, _ := os.ReadFile("../access/testdata/access.uci")
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
for i, want := range g.Sigs {
switch i {
case 1:
c.SetList("home", "lan_device", []string{"dlhome", "nope0", "lanp1"})
c.Set("cabin", "auto_lan", "1")
c.Set("open", "auto_wan", "0")
c.Set("open", "wan_device", "wg9")
case 2:
c.Set("open", "wan_device", "gone7")
c.SetList("open", "lan_device", []string{"gone8"})
c.Set("cabin", "lan_zone", "lan")
}
m := model.Load(c, nil)
if got := r.NetSignature(m); got != want {
t.Errorf("signature %d:\n%s\n--- ucode:\n%s", i, got, want)
}
if i == 0 {
if !r.NetChanged(m) {
t.Error("no signature kept, and nothing is due")
}
os.WriteFile(dir+"/net.sig", []byte(want), 0o644)
if r.NetChanged(m) {
t.Error("an apply is due with the networks as they were")
}
} else if !r.NetChanged(m) {
t.Error("the networks changed, and no apply is due")
}
}
}
// A plan of the full apply changes nothing: after a real apply of the same settings it finds nothing to
// do, and after a change it names the lines and the commands, with the router's own files as they were.
func TestApplyPlanChangesNothing(t *testing.T) {
raw, _ := os.ReadFile("../netview/testdata/trace.json")
var router struct {
Routes json.RawMessage `json:"routes"`
Addrs json.RawMessage `json:"addrs"`
Firewall string `json:"firewall"`
Links map[string]netview.Link `json:"links"`
}
if err := json.Unmarshal(raw, &router); err != nil {
t.Fatal(err)
}
dir := t.TempDir()
network, _ := os.ReadFile("testdata/network.uci")
os.WriteFile(dir+"/network", network, 0o644)
os.WriteFile(dir+"/firewall", []byte(router.Firewall), 0o644)
var ran []string
r := &Router{RunDir: dir + "/run", VarEtc: dir + "/etc", ResolverPid: dir + "/pid", SysNet: dir + "/sys", NetnsDir: dir + "/netns", Leases: dir + "/leases",
Network: dir + "/network", Firewall: dir + "/firewall", HostsFile: dir + "/hosts",
Exec: func(name string, args ...string) (string, int) {
line := name + " " + strings.Join(args, " ")
if !asks(name, args) {
ran = append(ran, line)
}
if strings.HasPrefix(line, "nft list") {
return "", 1
}
return "", 0
}}
r.Snapshot = func() *netview.Snapshot {
text, _ := os.ReadFile(dir + "/firewall")
fw, _ := uci.Parse(string(text))
return netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return router.Addrs },
Links: func() map[string]netview.Link { return router.Links }, Zones: func() netview.Zones { return netview.ZonesOf(fw) }})
}
text, _ := os.ReadFile("../access/testdata/access.uci")
c, _ := uci.Parse(string(text))
// nobody like a home device here: that path has its own test, with a router to answer it
for _, id := range []string{"home", "cabin", "open"} {
c.Set(id, "lan_like", "0")
}
load := func() *model.Model {
var blocked map[string]any
if raw, err := os.ReadFile(dir + "/run/netifd-blocked.json"); err == nil {
json.Unmarshal(raw, &blocked)
}
return model.Load(c, blocked)
}
caps := awg.Caps{V2: true, V3: true}
m := load()
r.Netifd(m, true, caps)
r.Fw(m)
r.LoopEnsure(m, "", true)
r.SyncResolver(m)
r.ReplyRoutes(m)
os.WriteFile(dir+"/run/net.sig", []byte(r.NetSignature(m)), 0o644)
keep := func() string {
out := ""
for _, f := range []string{"/network", "/firewall", "/run/netifd-blocked.json", "/run/loop.json", "/run/iface-intent.json", "/etc/dawnline-dns.conf", "/run/net.sig"} {
raw, _ := os.ReadFile(dir + f)
out += f + "\n" + string(raw) + "\n"
}
return out
}
before := keep()
ran = nil
plan := r.ApplyPlan(load(), "", true, caps)
js, _ := json.Marshal(plan)
if plan["network"] != false || plan["firewall"] != false || plan["resolver"] != false || plan["signature"] != true ||
len(plan["would_run"].([]string)) != 0 || len(plan["network_lines"].([]string)) != 0 || plan["like_home"] != "off" {
t.Errorf("something to do right after an apply: %s", js)
}
// a port moved: the plan says so, and the router is as it was
c.Set("home", "port", "4999")
plan = r.ApplyPlan(load(), "", true, caps)
js, _ = json.Marshal(plan)
if plan["network"] != true || plan["firewall"] != true {
t.Errorf("a moved port changes nothing: %s", js)
}
lines := strings.Join(plan["network_lines"].([]string), "\n") + "\n" + strings.Join(plan["firewall_lines"].([]string), "\n")
if !strings.Contains(lines, "- \toption listen_port '4500'") || !strings.Contains(lines, "+ \toption listen_port '4999'") || !strings.Contains(lines, "+ \toption dest_port '4999'") {
t.Errorf("the lines of the change:\n%s", lines)
}
would := strings.Join(plan["would_run"].([]string), "; ")
if !strings.Contains(would, "ubus call network reload") || !strings.Contains(would, "ifup dawn0") || !strings.Contains(would, "fw4 -q reload") {
t.Errorf("the commands of the change: %s", would)
}
if strings.Contains(string(js), "aaaaaaaa") {
t.Error("a key in the plan")
}
if keep() != before || len(ran) != 0 {
t.Errorf("the plan changed the router: ran %v", ran)
}
}
// A teardown leaves the router's network and firewall as they were before Dawnline: what is ours goes,
// what is not stays, an interface of someone else's under a tunnel's name too.
func TestTeardown(t *testing.T) {
raw, _ := os.ReadFile("../netview/testdata/trace.json")
var router struct {
Routes json.RawMessage `json:"routes"`
Addrs json.RawMessage `json:"addrs"`
Firewall string `json:"firewall"`
Links map[string]netview.Link `json:"links"`
}
if err := json.Unmarshal(raw, &router); err != nil {
t.Fatal(err)
}
dir := t.TempDir()
network, _ := os.ReadFile("testdata/network.uci")
os.WriteFile(dir+"/network", network, 0o644)
os.WriteFile(dir+"/firewall", []byte(router.Firewall), 0o644)
os.WriteFile(dir+"/hosts", []byte("10.77.0.2 phone.anna.vpn\n"), 0o644)
var ran []string
r := &Router{RunDir: dir + "/run", VarEtc: dir + "/etc", SysNet: dir + "/sys", NetnsDir: dir + "/netns", Leases: dir + "/leases",
Network: dir + "/network", Firewall: dir + "/firewall", HostsFile: dir + "/hosts",
Exec: func(name string, args ...string) (string, int) {
if !asks(name, args) {
ran = append(ran, name+" "+strings.Join(args, " "))
}
return "", 0
}}
r.Snapshot = func() *netview.Snapshot {
text, _ := os.ReadFile(dir + "/firewall")
fw, _ := uci.Parse(string(text))
return netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return router.Addrs },
Links: func() map[string]netview.Link { return router.Links }, Zones: func() netview.Zones { return netview.ZonesOf(fw) }})
}
text, _ := os.ReadFile("../access/testdata/access.uci")
c, _ := uci.Parse(string(text))
m := model.Load(c, nil)
r.Netifd(m, true, awg.Caps{V2: true, V3: true})
r.Fw(m)
os.WriteFile(dir+"/run/net.sig", []byte("x"), 0o644)
if now, _ := os.ReadFile(dir + "/firewall"); !strings.Contains(string(now), "dawnline_home_zone") {
t.Fatal("nothing was set up to take down")
}
ran = nil
r.Teardown(model.Load(c, nil))
as := func(text []byte) string { c, _ := uci.Parse(string(text)); return c.Format() }
if now, _ := os.ReadFile(dir + "/network"); string(now) != as(network) {
t.Errorf("the network configuration after:\n%s", now)
}
if now, _ := os.ReadFile(dir + "/firewall"); string(now) != as([]byte(router.Firewall)) {
t.Errorf("the firewall after:\n%s", now)
}
for _, f := range []string{"/hosts", "/run/net.sig"} {
if _, err := os.Stat(dir + f); err == nil {
t.Errorf("%s stays", f)
}
}
did := strings.Join(ran, "; ")
for _, want := range []string{"ifdown dawn0", "ifdown dawn1", "ubus call network reload", "fw4 -q reload", "nft delete table inet dawnline", "killall -HUP dnsmasq"} {
if !strings.Contains(did, want) {
t.Errorf("%s was not run: %s", want, did)
}
}
if strings.Contains(did, "ifdown dawn2") || strings.Contains(did, "ifup") {
t.Errorf("more than ours was touched: %s", did)
}
}
+139
View File
@@ -0,0 +1,139 @@
package apply
import (
"os"
"regexp"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/model"
)
// A full apply with nothing applied: every step runs on copies of the files it would write, the commands
// that only ask the router something are run, and the ones that would change it are noted instead. For
// holding Go's apply to what a router runs now: after the ucode backend applied, it must find nothing to do.
var (
asksOnly = regexp.MustCompile(`^ip (-n dawnline )?(-j |-4 |-6 )*(neigh|link|rule|route|addr) show\b[^;|&]*( 2>/dev/null)?$`)
secret = regexp.MustCompile(`((private|preshared|protection)_?key) '[^']*'`)
)
func asks(name string, args []string) bool {
line := strings.Join(args, " ")
switch name {
case "sh":
return len(args) == 2 && args[0] == "-c" && asksOnly.MatchString(args[1])
case "ip":
return asksOnly.MatchString("ip " + line)
case "nft":
return len(args) > 0 && args[0] == "list"
case "ubus":
return len(args) == 3 && args[0] == "call" && strings.HasPrefix(args[1], "network.interface.") && args[2] == "status"
}
return false
}
// changes is the lines two texts differ in ("-" gone, "+" new), keys never shown.
func changes(was, now string) []string {
out := []string{}
if was == now {
return out
}
count := map[string]int{}
for _, l := range strings.Split(was, "\n") {
count[l]++
}
var added []string
for _, l := range strings.Split(now, "\n") {
if count[l] > 0 {
count[l]--
} else {
added = append(added, l)
}
}
for _, l := range strings.Split(was, "\n") {
if count[l] > 0 {
count[l]--
out = append(out, "- "+secret.ReplaceAllString(l, "$1 '***'"))
}
}
for _, l := range added {
out = append(out, "+ "+secret.ReplaceAllString(l, "$1 '***'"))
}
return out
}
// ApplyPlan is what a full apply would do now, with nothing done: which files would change (their lines),
// which commands would be run, what the home-like path would come to. The runtime sync is Plan's.
func (r *Router) ApplyPlan(m *model.Model, saved string, tools bool, caps awg.Caps) map[string]any {
dir, err := os.MkdirTemp("", "dawnline-plan-")
if err != nil {
return map[string]any{"error": err.Error()}
}
defer os.RemoveAll(dir)
read := func(path string) string {
raw, _ := os.ReadFile(path)
return string(raw)
}
etc := r.varEtc()
os.MkdirAll(dir+"/run", 0o755)
os.MkdirAll(dir+"/etc", 0o755)
for _, f := range []string{"netifd-blocked.json", "netifd-no-awg", "iface-intent.json", "loop.json", "reply.nft", "net.sig"} {
if raw, err := os.ReadFile(r.RunDir + "/" + f); err == nil {
os.WriteFile(dir+"/run/"+f, raw, 0o600)
}
}
for _, f := range []string{"dawnline-dns.conf", "dawnline-dns.resolv", "dawnline-ns.nft"} {
if raw, err := os.ReadFile(etc + "/" + f); err == nil {
os.WriteFile(dir+"/etc/"+f, raw, 0o600)
}
}
os.WriteFile(dir+"/network", []byte(read(r.Network)), 0o600)
os.WriteFile(dir+"/firewall", []byte(read(r.Firewall)), 0o600)
would := []string{}
d := *r
d.RunDir, d.VarEtc, d.Network, d.Firewall, d.Log, d.Remember, d.dry = dir+"/run", dir+"/etc", dir+"/network", dir+"/firewall", nil, nil, true
d.ResolverPid = r.resolverPid()
d.Exec = func(name string, args ...string) (string, int) {
if asks(name, args) {
return r.Exec(name, args...)
}
line := name + " " + strings.Join(args, " ")
if name == "sh" && len(args) == 2 {
line = args[1]
}
would = append(would, strings.ReplaceAll(line, dir+"/etc", etc))
return "", 0
}
out := map[string]any{}
out["network"] = d.Netifd(m, tools, caps)
out["network_lines"] = changes(read(r.Network), read(dir+"/network"))
out["firewall"] = d.Fw(m)
out["firewall_lines"] = changes(read(r.Firewall), read(dir+"/firewall"))
at := len(would)
lp := d.LoopEnsure(m, saved, false)
switch {
case lp.Off:
out["like_home"] = "off"
case lp.Error != nil:
out["like_home"] = lp.Error
default:
out["like_home"] = lp.Plan
}
out["like_home_runs"] = append([]string{}, would[at:]...)
out["like_home_state"] = changes(read(r.RunDir+"/loop.json"), read(dir+"/run/loop.json"))
out["resolver"] = d.SyncResolver(m)
out["resolver_lines"] = append(changes(read(etc+"/dawnline-dns.conf"), read(dir+"/etc/dawnline-dns.conf")),
changes(read(etc+"/dawnline-dns.resolv"), read(dir+"/etc/dawnline-dns.resolv"))...)
at = len(would)
d.ReplyRoutes(m)
out["reply_runs"] = append([]string{}, would[at:]...)
out["reply_lines"] = changes(read(r.RunDir+"/reply.nft"), read(dir+"/run/reply.nft"))
sig := r.NetSignature(m)
out["signature"] = sig == read(r.RunDir+"/net.sig")
if sig != read(r.RunDir+"/net.sig") {
out["signature_now"], out["signature_kept"] = sig, read(r.RunDir+"/net.sig")
}
out["would_run"] = would
return out
}
+250
View File
@@ -0,0 +1,250 @@
package apply
import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/view"
)
// fw4 around the tunnels (dawnline.fw): each tunnel's zone, its forwardings to home and WAN, the WAN rules
// for the tunnels and the portal. Only sections named dawnline_* are ever changed.
const loopZone = "dl_loop"
var notWord = regexp.MustCompile(`[^A-Za-z0-9_]`)
// portalPortOK: never SSH, DNS or the LuCI web server's ports for the portal, whatever the settings say.
func portalPortOK(p int) bool {
return p >= 1 && p <= 65535 && p != 22 && p != 53 && p != 80 && p != 443
}
// portalHTTP tells whether the portal also answers plain HTTP: IP addresses have no certificate.
func portalHTTP(m *model.Model) bool {
for _, h := range view.PortalHosts(m) {
if _, is := ipnet.IP(h); is {
return true
}
}
return false
}
// firewall brings a firewall configuration in line with the servers and the portal; whether it changed,
// and what changed section by section (level, line) for the log.
func (r *Router) firewall(c *uci.Config, m *model.Model) (changed bool, notes [][2]string) {
want := map[string]bool{}
sec := func(name, kind string, opts ...opt) {
want[name] = true
created := false
if s := c.Section(name); s == nil || s.Type != kind {
c.DeleteSection(name)
c.Add(kind, name)
created = true
}
var diff []opt
for _, o := range opts {
if put(c, name, o) {
diff = append(diff, o)
}
}
switch {
case created:
changed = true
notes = append(notes, [2]string{"info", fmt.Sprintf("%s %s: created · %s", kind, name, optsText(opts))})
case len(diff) > 0:
changed = true
notes = append(notes, [2]string{"info", fmt.Sprintf("%s %s: updated · %s", kind, name, optsText(diff))})
default:
notes = append(notes, [2]string{"debug", fmt.Sprintf("%s %s: unchanged", kind, name)})
}
}
snap := r.Snapshot()
portalZone := ""
wantLoop, anyLoop := false, false
// Where tunnel DNS is sent (dawnline.nft): into the netns for home names while any server runs "like a
// home device", and to the LAN's DNS for everyone else. A server that does not forward to those zones on
// its own gets a rule for port 53 alone.
for _, srv := range m.Servers {
anyLoop = anyLoop || (srv.LanLike && tunnelOK(srv))
}
_, _, known, _ := r.likeHome()
lanDNS := r.lanDNS(snap, known)
lanDNSZone := ""
if ip, ok := ipnet.IP(lanDNS); ok {
for _, name := range snap.Names() {
d := snap.Devices[name]
if d.Zone == "" || lanDNSZone != "" {
continue
}
for _, n := range d.Nets {
if c, ok := ipnet.Any(n); ok && c.Holds(ip) {
lanDNSZone = d.Zone
break
}
}
}
}
for _, id := range m.ServerIDs() {
srv := m.Servers[id]
if !tunnelOK(srv) {
continue
}
z := "dawnline_" + id
wan, _, _, zones := snap.ServerNet(srv)
if portalZone == "" {
portalZone = wan.Zone
}
if !srv.LanLike && anyLoop {
sec(z+"_loopdns", "rule", opt{key: "name", val: "Dawnline-" + srv.Ifname + "-names"}, opt{key: "src", val: srv.Ifname}, opt{key: "dest", val: loopZone},
opt{key: "proto", val: "tcp udp"}, opt{key: "dest_port", val: "53"}, opt{key: "target", val: "ACCEPT"})
}
reaches := false
for _, zone := range zones {
reaches = reaches || zone == lanDNSZone
}
if lanDNSZone != "" && lanDNSZone != srv.Ifname && !reaches {
sec(z+"_landns", "rule", opt{key: "name", val: "Dawnline-" + srv.Ifname + "-dns"}, opt{key: "src", val: srv.Ifname}, opt{key: "dest", val: lanDNSZone},
opt{key: "dest_ip", val: lanDNS}, opt{key: "proto", val: "tcp udp"}, opt{key: "dest_port", val: "53"}, opt{key: "target", val: "ACCEPT"})
}
// forward within the zone (device to device) is decided by the inet dawnline table
sec(z+"_zone", "zone", opt{key: "name", val: srv.Ifname}, opt{key: "network", list: []string{srv.Ifname}},
opt{key: "input", val: "ACCEPT"}, opt{key: "output", val: "ACCEPT"}, opt{key: "forward", val: "ACCEPT"})
// Access per person is enforced by the inet dawnline table; the zone only needs to be allowed to reach
// each home-network zone and WAN. The home network may open connections to the devices too (a router
// or a NAS on the tunnel); their answers pass the table as established.
for i, zone := range zones {
if zone == wan.Zone || zone == srv.Ifname {
continue
}
sfx := ""
if i > 0 {
sfx = "_" + notWord.ReplaceAllString(zone, "_")
}
sec(z+"_lan"+sfx, "forwarding", opt{key: "src", val: srv.Ifname}, opt{key: "dest", val: zone})
sec(z+"_from_lan"+sfx, "forwarding", opt{key: "src", val: zone}, opt{key: "dest", val: srv.Ifname})
}
sec(z+"_wan", "forwarding", opt{key: "src", val: srv.Ifname}, opt{key: "dest", val: wan.Zone})
if srv.LanLike {
sec(z+"_loop", "forwarding", opt{key: "src", val: srv.Ifname}, opt{key: "dest", val: loopZone})
wantLoop = true
}
off := opt{key: "enabled"}
if !srv.Enabled {
off.val = "0"
}
sec(z+"_port", "rule", opt{key: "name", val: "Dawnline-" + srv.Ifname}, opt{key: "src", val: wan.Zone}, opt{key: "proto", val: "udp"},
opt{key: "dest_port", val: strconv.Itoa(srv.Port)}, opt{key: "target", val: "ACCEPT"}, off)
}
outside := portalZone
if outside == "" {
outside = "wan"
}
// the link into the netns that re-enters the router from the LAN
if wantLoop {
sec("dawnline_loop", "zone", opt{key: "name", val: loopZone}, opt{key: "device", list: []string{"dlx0"}},
opt{key: "input", val: "REJECT"}, opt{key: "output", val: "ACCEPT"}, opt{key: "forward", val: "REJECT"})
// the Home-only resolver in the netns reaches its upstream DNS this way
sec("dawnline_loop_wan", "forwarding", opt{key: "src", val: loopZone}, opt{key: "dest", val: outside})
}
if s := m.Settings; s.PortalEnabled && portalPortOK(s.PortalPort) {
sec("dawnline_portal", "rule", opt{key: "name", val: "Dawnline-Portal"}, opt{key: "src", val: outside}, opt{key: "proto", val: "tcp"},
opt{key: "dest_port", val: strconv.Itoa(s.PortalPort)}, opt{key: "target", val: "ACCEPT"})
// IP addresses in links: plain HTTP next to the HTTPS port (80 and 443 stay with the LuCI web server)
if portalHTTP(m) && portalPortOK(s.PortalPort+1) {
sec("dawnline_portal_http", "rule", opt{key: "name", val: "Dawnline-Portal-HTTP"}, opt{key: "src", val: outside}, opt{key: "proto", val: "tcp"},
opt{key: "dest_port", val: strconv.Itoa(s.PortalPort + 1)}, opt{key: "target", val: "ACCEPT"})
}
}
var stale [][2]string
for _, s := range c.Sections {
if strings.HasPrefix(s.Name, "dawnline_") && !want[s.Name] {
stale = append(stale, [2]string{s.Name, s.Type})
}
}
for _, s := range stale {
notes = append(notes, [2]string{"info", fmt.Sprintf("%s %s: removed", s[1], s[0])})
c.DeleteSection(s[0])
changed = true
}
return changed, notes
}
func (r *Router) firewallConfig() (*uci.Config, bool) {
text, _ := os.ReadFile(r.Firewall)
c, err := uci.Parse(string(text))
if err != nil {
r.say("error", "firewall", r.Firewall+" does not parse: nothing was changed · "+err.Error())
return nil, false
}
return c, true
}
// FirewallDiffers tells whether the firewall configuration differs from what Dawnline wants; nothing is
// written (the watchdog's question).
func (r *Router) FirewallDiffers(m *model.Model) bool {
c, ok := r.firewallConfig()
if !ok {
return false
}
changed, _ := r.firewall(c, m)
return changed
}
// Fw brings fw4's configuration in line with the servers and the portal, and reloads fw4 when it
// changed; true when it did.
func (r *Router) Fw(m *model.Model) bool {
c, ok := r.firewallConfig()
if !ok {
return false
}
changed, notes := r.firewall(c, m)
for _, n := range notes {
r.say(n[0], "firewall", n[1])
}
if !changed {
return false
}
if err := commit(r.Firewall, c); err != nil {
r.say("error", "firewall", r.Firewall+" could not be written · "+err.Error())
return false
}
r.ack("firewall")
r.say("info", "firewall", "fw4 -q reload")
r.Exec("fw4", "-q", "reload")
return true
}
// FirewallDrift is what is wrong with Dawnline's firewall, for the watchdog (dawnline.fw drift): "" when
// nothing is; "off": fw4 is not running at all (not ours to start); "changed": the configuration lost or
// changed Dawnline's sections; "not loaded": fw4 runs without them.
func (r *Router) FirewallDrift(m *model.Model) string {
table, code := r.Exec("nft", "-t", "list", "table", "inet", "fw4")
if code != 0 || strings.Trim(table, " \t\r\n\v\f") == "" {
return "off"
}
if r.FirewallDiffers(m) {
return "changed"
}
// named rules show up in fw4's ruleset as comments
var names []string
for _, id := range m.ServerIDs() {
if srv := m.Servers[id]; srv.Enabled && tunnelOK(srv) {
names = append(names, "Dawnline-"+srv.Ifname)
}
}
if m.Settings.PortalEnabled {
names = append(names, "Dawnline-Portal")
}
for _, n := range names {
if !strings.Contains(table, n+`"`) {
return "not loaded"
}
}
return ""
}
+146
View File
@@ -0,0 +1,146 @@
package apply
import (
"encoding/json"
"os"
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// The router's firewall configuration through a row of changes to access's household: after each, the file
// ucode's firewall sync leaves, whether it changed and what it ran (testdata/fw.uc).
func TestFirewallAsUcode(t *testing.T) {
raw, err := os.ReadFile("testdata/fw.json")
if err != nil {
t.Fatal(err)
}
var trace struct {
Steps []struct {
What string `json:"what"`
Dry bool `json:"dry"`
Changed bool `json:"changed"`
Before string `json:"before"`
Firewall string `json:"firewall"`
Cmds []string `json:"cmds"`
} `json:"steps"`
}
if err := json.Unmarshal(raw, &trace); err != nil {
t.Fatal(err)
}
if len(trace.Steps) != 11 {
t.Fatalf("%d steps", len(trace.Steps))
}
// the router the golden ran on: what its container said of itself (netview's golden, the same run)
raw, err = os.ReadFile("../netview/testdata/trace.json")
if err != nil {
t.Fatal(err)
}
var router struct {
Routes json.RawMessage `json:"routes"`
Addrs json.RawMessage `json:"addrs"`
Links map[string]netview.Link `json:"links"`
}
if err := json.Unmarshal(raw, &router); err != nil {
t.Fatal(err)
}
dir := t.TempDir()
os.WriteFile(dir+"/firewall", []byte(trace.Steps[0].Before), 0o644)
var cmds []string
r := &Router{RunDir: dir + "/run", Firewall: dir + "/firewall", Exec: func(name string, args ...string) (string, int) {
cmds = append(cmds, strings.Join(append([]string{name}, args...), " "))
return "", 0
}}
// as ucode's: the zones are read anew from the firewall as it stands
r.Snapshot = func() *netview.Snapshot {
text, _ := os.ReadFile(dir + "/firewall")
fw, _ := uci.Parse(string(text))
return netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return router.Addrs },
Links: func() map[string]netview.Link { return router.Links }, Zones: func() netview.Zones { return netview.ZonesOf(fw) }})
}
text, _ := os.ReadFile("../access/testdata/access.uci")
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
for i, want := range trace.Steps {
switch i {
case 2:
c.Set("home", "port", "4501")
c.Set("cabin", "enabled", "0")
c.SetList("home", "lan_device", []string{"dlhome", "dlguest"})
case 3:
c.SetList("open", "lan_device", []string{"dlguest"})
case 4:
c.Set("settings", "portal_port", "443")
case 5:
c.Set("settings", "portal_port", "8443")
c.Delete("settings", "portal_alt")
case 6:
c.Set("home", "port", "4502")
case 7:
c.DeleteSection("cabin")
c.Set("home", "lan_like", "1")
case 8:
c.Set("home", "lan_like", "0")
c.Set("open", "lan_like", "0")
case 9:
text, _ := os.ReadFile(dir + "/firewall")
fw, _ := uci.Parse(string(text))
fw.DeleteSection("dawnline_home_zone")
fw.Add("rule", "dawnline_home_zone")
fw.Set("dawnline_home_zone", "name", "Not a zone")
fw.Add("forwarding", "dawnline_old_wan")
fw.Set("dawnline_old_wan", "src", "dawn9")
os.WriteFile(dir+"/firewall", []byte(fw.Format()), 0o644)
c.Set("settings", "portal_enabled", "0")
}
fail := func(what string, got, exp any) {
t.Errorf("step %d (%s): %s\n go: %v\n ucode: %v", i, want.What, what, got, exp)
}
if got, _ := os.ReadFile(dir + "/firewall"); string(got) != want.Before {
fail("the firewall the step starts from", string(got), want.Before)
}
cmds = nil
m := model.Load(c, nil)
var changed bool
if want.Dry {
changed = r.FirewallDiffers(m)
} else {
changed = r.Fw(m)
}
if changed != want.Changed {
fail("whether it changed", changed, want.Changed)
}
if got, _ := os.ReadFile(dir + "/firewall"); string(got) != want.Firewall {
g, w := strings.Split(string(got), "\n"), strings.Split(want.Firewall, "\n")
for n := 0; n < len(g) || n < len(w); n++ {
if n >= len(g) || n >= len(w) || g[n] != w[n] {
a, b := "(the end)", "(the end)"
if n < len(g) {
a = g[n]
}
if n < len(w) {
b = w[n]
}
fail("the firewall configuration, from a line on", a, b)
break
}
}
os.WriteFile(dir+"/firewall", []byte(want.Firewall), 0o644)
}
// the snapshot's own question to netifd is not the sync's
var ran []string
for _, l := range want.Cmds {
if l != "ubus call network.interface dump" {
ran = append(ran, l)
}
}
if strings.Join(cmds, "; ") != strings.Join(ran, "; ") {
fail("what was run", cmds, ran)
}
}
}
+201
View File
@@ -0,0 +1,201 @@
package apply
import (
"fmt"
"os"
"syscall"
"time"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/health"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/servicelog"
"github.com/tickcount/luci-app-dawnline/backend/state"
"github.com/tickcount/luci-app-dawnline/backend/usage"
)
// The keeper is what holds the router to the settings between the admin's changes (the ucode agent's
// steps "Access rules", "Internet like a home device" and "Router"): once a minute it pushes devices and
// access sets when something moved, keeps the home-like path and the resolver in step, and applies
// everything again when the router's networks changed under it.
// resyncEvery is how long at most between two runtime syncs, whatever happened.
const resyncEvery = 600
// Keeper keeps one router. The zero value of what it remembers is "nothing seen yet".
type Keeper struct {
R *Router
// Load reads the settings anew, with the LAN address kept for the home-like path.
Load func() (*model.Model, string, error)
// Usage is the counters as the watcher has them; Dump what the tunnels hold right now.
Usage func() *usage.Usage
Dump func() (peers map[string]awg.Peer, reads map[string]string)
// Config is the settings' file: a change by anyone (a method, the CLI, a restore) is seen by its
// inode and time.
Config string
Tools func() bool
Caps func() awg.Caps
// Words is the catalog the log's sentences are written in (English).
Words *i18n.Catalog
states map[string]string
lastSync int64
peers map[string]string
conf string
seenConf bool
baseNet string
pending string
failed map[string]string
}
func (k *Keeper) step(who string, fn func()) {
defer func() {
if e := recover(); e != nil {
msg := fmt.Sprint(e)
if k.failed == nil {
k.failed = map[string]string{}
}
// a new failure in the service log, a repeat in syslog alone
if k.failed[who] != msg && k.R.Log != nil {
k.R.Log.Write("dawnline", "error", "failed: "+msg, "", who, servicelog.Extra{})
} else {
k.R.say("error", "keeper", who+": "+msg)
}
k.failed[who] = msg
return
}
delete(k.failed, who)
}()
fn()
}
func fileSig(path string) string {
st, err := os.Stat(path)
if err != nil {
return ""
}
ino := uint64(0)
if sys, is := st.Sys().(*syscall.Stat_t); is {
ino = uint64(sys.Ino)
}
return fmt.Sprintf("%d/%d", st.ModTime().UnixNano(), ino)
}
func sameStates(a, b map[string]string) bool {
if len(a) != len(b) {
return false
}
for k, v := range a {
if w, has := b[k]; !has || w != v {
return false
}
}
return true
}
// runtime pushes devices and access sets when people's states changed, the settings' file was written,
// the access table is gone (a flushed ruleset: the firewall alone would let every device reach
// everything, so the sync unloads them until it is back), a running tunnel holds other devices than it
// should, or every ten minutes anyway.
func (k *Keeper) runtime(m *model.Model, u *usage.Usage, now int64) {
states := map[string]string{}
for _, id := range m.PersonIDs() {
states[id] = state.Of(m, m.People[id], u, now)
}
// A running tunnel without all of its devices, or with ones it should not have: now, not up to ten
// minutes later. Only where the last sync took: a server that refuses its devices has said so.
if k.Dump != nil {
peers, reads := k.Dump()
for _, id := range m.ServerIDs() {
srv := m.Servers[id]
last := k.peers[id]
if reads[srv.Ifname] != awg.Read || (last != "" && last != "ok" && last != "down") {
continue
}
if d := health.PeerDiff(health.TunnelDevices(m, id, states), peers, srv.Ifname); d.Missing > 0 || d.Extra > 0 {
k.lastSync = 0
}
}
}
conf := fileSig(k.Config)
changed := k.states == nil || !sameStates(states, k.states) || (k.seenConf && conf != k.conf)
if changed || now-k.lastSync > resyncEvery || (len(m.Servers) > 0 && !k.R.Present()) {
k.peers = k.R.Sync(m, u, now, false, k.Words).Peers
k.lastSync = now
}
k.conf, k.seenConf, k.states = conf, true, states
}
// KeepLoop keeps the home-like path and the resolver in step. Cheap when nothing changed; the access
// table is rebuilt when the netns address appears, moves or goes away (the guard allows DNS to it).
func (r *Router) KeepLoop(m *model.Model, saved string, u *usage.Usage, now int64, t *i18n.Catalog) Loop {
unlock := files.Lock(r.RunDir, "apply")
where := func() string {
active, address, _, _ := r.likeHome()
if !active {
address = ""
}
return address + " " + r.ResolverTarget().Mode
}
before := where()
lp := r.LoopEnsure(m, saved, false)
r.SyncResolver(m)
r.ReplyRoutes(m)
if where() != before {
r.sync(m, u, now, true, t)
}
unlock()
if lp.Plan != nil && lp.Plan.Address != "" && lp.Plan.Address != saved && r.Remember != nil {
r.Remember(lp.Plan.Address)
}
return lp
}
// network applies everything again when the router's networks changed under us. Only a change seen on two
// ticks in a row (a bridge recreated or an interface restarting passes), against what the last apply used.
func (k *Keeper) network(m *model.Model, u *usage.Usage) {
sig := k.R.NetSignature(m)
applied := k.baseNet
if raw, err := os.ReadFile(k.R.RunDir + "/net.sig"); err == nil {
applied = string(raw)
}
if applied == "" || sig == applied {
if k.baseNet == "" {
k.baseNet = sig
}
k.pending = ""
return
}
if k.pending != sig {
k.pending = sig
return
}
k.pending = ""
if k.R.Log != nil {
k.R.Log.Write("dawnline", "info", "network changed - Dawnline set itself up again", "", "Router", servicelog.Extra{})
}
k.R.Apply(k.Load, u, k.Words, k.Tools(), k.Caps())
k.baseNet = sig
if raw, err := os.ReadFile(k.R.RunDir + "/net.sig"); err == nil {
k.baseNet = string(raw)
}
}
// Tick is one minute's keeping. Each step on its own: a failing one is logged and the others still run.
func (k *Keeper) Tick() {
m, saved, err := k.Load()
if err != nil {
k.R.say("error", "keeper", "the settings could not be read · "+err.Error())
return
}
u := k.Usage()
if u == nil {
return
}
now := time.Now().Unix()
k.step("Access rules", func() { k.runtime(m, u, now) })
k.step("Internet like a home device", func() { k.R.KeepLoop(m, saved, u, now, k.Words) })
k.step("Router", func() { k.network(m, u) })
}
+139
View File
@@ -0,0 +1,139 @@
package apply
import (
"encoding/json"
"os"
"strings"
"testing"
"time"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/usage"
)
// The keeper syncs when something moved and not otherwise: the first time, when the settings' file was
// written, when the access table is gone; and it applies everything again only when the router's networks
// stay changed for two ticks.
func TestKeeper(t *testing.T) {
raw, _ := os.ReadFile("../netview/testdata/trace.json")
var router struct {
Routes json.RawMessage `json:"routes"`
Addrs json.RawMessage `json:"addrs"`
Firewall string `json:"firewall"`
Links map[string]netview.Link `json:"links"`
}
if err := json.Unmarshal(raw, &router); err != nil {
t.Fatal(err)
}
dir := t.TempDir()
network, _ := os.ReadFile("testdata/network.uci")
os.WriteFile(dir+"/network", network, 0o644)
os.WriteFile(dir+"/firewall", []byte(router.Firewall), 0o644)
text, _ := os.ReadFile("../access/testdata/access.uci")
c, _ := uci.Parse(string(text))
for _, id := range []string{"home", "cabin", "open"} {
c.Set(id, "lan_like", "0")
}
os.WriteFile(dir+"/dawnline", []byte(c.Format()), 0o600)
present := false
loads, reloads := 0, 0
addrs := router.Addrs
r := &Router{RunDir: dir + "/run", VarEtc: dir + "/etc", SysNet: dir + "/sys", NetnsDir: dir + "/netns", Leases: dir + "/leases",
Network: dir + "/network", Firewall: dir + "/firewall", HostsFile: dir + "/hosts",
Exec: func(name string, args ...string) (string, int) {
line := name + " " + strings.Join(args, " ")
switch {
case line == "nft list table inet dawnline":
if present {
return "", 0
}
return "", 1
case strings.HasPrefix(line, "nft list"):
return "", 1
case name == "nft" && args[0] == "-f":
script, _ := os.ReadFile(args[1])
if strings.HasPrefix(string(script), "flush set") && !present {
return "Error: No such file or directory", 1
}
loads++
present = true
case line == "fw4 -q reload":
reloads++
case name == "awg" && args[0] == "show":
return "", 1
}
return "", 0
}}
r.Snapshot = func() *netview.Snapshot {
text, _ := os.ReadFile(dir + "/firewall")
fw, _ := uci.Parse(string(text))
return netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return addrs },
Links: func() map[string]netview.Link { return router.Links }, Zones: func() netview.Zones { return netview.ZonesOf(fw) }})
}
u := &usage.Usage{}
k := &Keeper{R: r, Config: dir + "/dawnline", Words: i18n.Use("", ""), Usage: func() *usage.Usage { return u },
Tools: func() bool { return true }, Caps: func() awg.Caps { return awg.Caps{V2: true, V3: true} },
Load: func() (*model.Model, string, error) {
text, _ := os.ReadFile(dir + "/dawnline")
c, err := uci.Parse(string(text))
if err != nil {
return nil, "", err
}
return model.Load(c, nil), "", nil
}}
tick := func(what string, want int) {
t.Helper()
before := loads
k.Tick()
if loads-before != want {
t.Errorf("%s: the table was loaded %d times, not %d", what, loads-before, want)
}
}
tick("the first tick", 1)
tick("nothing moved", 0)
// the settings written by anyone: seen by the file itself
time.Sleep(5 * time.Millisecond)
os.WriteFile(dir+"/dawnline.new", []byte(c.Format()), 0o600)
os.Rename(dir+"/dawnline.new", dir+"/dawnline")
tick("the settings' file replaced", 1)
tick("and nothing since", 0)
// a flushed ruleset: the table is put back on the next tick
present = false
tick("the access table gone", 1)
if reloads != 0 {
t.Errorf("the firewall was reloaded %d times with the networks as they were", reloads)
}
// The router's networks: nothing kept of an apply yet, so the first look is the base. A change that
// passes within a tick sets nothing up again; one that stays does, once.
other := json.RawMessage(strings.ReplaceAll(string(router.Addrs), "192.168.77.1", "192.168.78.1"))
if string(other) == string(router.Addrs) {
t.Fatal("the router's addresses are not what this test changes")
}
addrs = other
k.Tick()
addrs = router.Addrs
k.Tick()
if _, err := os.Stat(dir + "/run/net.sig"); err == nil {
t.Error("a change that passed set everything up again")
}
addrs = other
k.Tick()
k.Tick()
sig, err := os.ReadFile(dir + "/run/net.sig")
if err != nil || !strings.Contains(string(sig), "192.168.78.1") {
t.Errorf("a change that stayed did not set everything up again: %s", sig)
}
if now, _ := os.ReadFile(dir + "/firewall"); !strings.Contains(string(now), "dawnline_home_zone") {
t.Error("the apply after the change left no firewall of ours")
}
before := reloads
k.Tick()
k.Tick()
if reloads != before {
t.Error("applied again with the networks as the last apply saw them")
}
}
+807
View File
@@ -0,0 +1,807 @@
package apply
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"time"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/servicelog"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// "Like a home device" (dawnline.loop): tunnel clients' internet traffic enters the router from the LAN,
// so whatever handles LAN traffic there (proxies, DPI bypass, DNS filters, fw4's lan zone) handles it too.
//
// dawn0 ──policy route──▶ dlx0 ═veth═ dlx1 ┐ netns "dawnline" ┌ dll1 ═veth═ dll0 ── br-lan
// └── NAT to a LAN address ┘
//
// The netns is one more device on the LAN bridge with its own address (picked from the LAN's network above
// the DHCP pool) and its own MAC (hashed from the bridge's), the same each time it is made. Everything is
// derived from the router at run time. If the LAN is not a bridge or setup fails, the direct path stays and
// loop.json says why, as a code with its values.
const (
loopNS = "dawnline"
txRoot, txNS, lanRoot, lanNS = "dlx0", "dlx1", "dll0", "dll1"
prefLoopDNS, prefMain, prefLoop = 5205, 5210, 5290
loopWho = "Internet like a home device"
)
// LoopServer is one tunnel on the path: its networks that keep their direct route, and of those the ones
// behind people's routers (nil when there are none: the plan is compared with the one kept in loop.json),
// which the netns answers through the tunnel as it does the tunnel's own subnet.
type LoopServer struct {
Ifname string `json:"ifname"`
Subnet string `json:"subnet"`
Direct []string `json:"direct"`
Behind []string `json:"behind,omitempty"`
}
// LoopPlan is what should exist (dawnline.loop plan), kept in loop.json in ucode's own order of keys.
type LoopPlan struct {
Dev string `json:"dev"`
Gateway string `json:"gateway"`
Address string `json:"address"`
MAC string `json:"mac"`
Prefix int `json:"prefix"`
DNS string `json:"dns"`
Transit uint32 `json:"transit"`
TxRoot string `json:"tx_root"`
TxNS string `json:"tx_ns"`
Table int `json:"table"`
Servers []LoopServer `json:"servers"`
}
// Loop is what bringing the path to the settings came to: off (nobody wants it), a reason it cannot be
// (a code with its values), or the plan that is in place.
type Loop struct {
Off bool
Error map[string]any
Plan *LoopPlan
}
func (r *Router) sysNet() string {
if r.SysNet != "" {
return r.SysNet
}
return "/sys/class/net"
}
func (r *Router) netnsFile() string {
if r.NetnsDir != "" {
return r.NetnsDir + "/" + loopNS
}
return "/var/run/netns/" + loopNS
}
func (r *Router) linkThere(dev string) bool {
_, err := os.Stat(r.sysNet() + "/" + dev)
return err == nil
}
func (r *Router) nsThere() bool {
_, err := os.Stat(r.netnsFile())
return err == nil
}
// sh runs a shell command as ucode's sh(): what it said (both streams) and its exit code.
func (r *Router) sh(cmd string) (string, int) { return r.Exec("sh", "-c", cmd) }
// q quotes a word for the shell.
func q(s string) string { return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" }
func trimmed(s string) string { return strings.Trim(s, " \t\r\n\v\f") }
// run gives the commands that set the path up or take it down, each in the log as it is given; what failed,
// each with what it said.
func (r *Router) run(cmds []string) []string {
var errs []string
for _, c := range cmds {
r.say("info", "loop", "$ "+c)
}
for _, c := range cmds {
if out, code := r.sh(c); code != 0 {
errs = append(errs, c+": "+trimmed(out))
r.say("error", "loop", "failed: "+c+" · "+trimmed(out))
}
}
return errs
}
func (r *Router) loopLog(level, msg, detail string) {
if r.Log != nil {
r.Log.Write("dawnline", level, msg, detail, loopWho, servicelog.Extra{})
}
}
/* ---------- facts about the LAN ---------- */
type dhcpFacts struct {
pool *[2]uint32
dns string
statics []uint32
}
// dhcpOf is the DHCP pool, static leases and the DNS handed to LAN clients, for one netifd interface.
func (r *Router) dhcpOf(logical string, lan ipnet.Net) dhcpFacts {
var f dhcpFacts
raw, err := os.ReadFile(r.DHCP)
if err != nil {
return f
}
c, err := uci.Parse(string(raw))
if err != nil {
return f
}
for _, s := range c.OfType("dhcp") {
iface, _ := s.Get("interface")
if ignore, _ := s.Get("ignore"); iface != logical || ignore == "1" {
continue
}
num := func(key string, dflt float64) (float64, bool) {
v, has := s.Get(key)
if !has {
return dflt, true
}
return model.Number(v)
}
start, ok1 := num("start", 100)
limit, ok2 := num("limit", 150)
f.pool = nil
if ok1 && ok2 {
f.pool = &[2]uint32{lan.Base + uint32(int64(start)), lan.Base + uint32(int64(start)+int64(limit)) - 1}
}
v := s.Values["dhcp_option"]
opts := v.List
if !v.IsList && v.Str != "" {
opts = []string{v.Str}
}
for _, o := range opts {
if m := dnsOption.FindStringSubmatch(o); m != nil && f.dns == "" {
f.dns = m[2]
}
}
}
for _, s := range c.OfType("host") {
v, _ := s.Get("ip")
if ip, ok := ipnet.IP(v); ok {
f.statics = append(f.statics, ip)
}
}
return f
}
// usedAddresses is the addresses other devices are using right now (neighbour table and leases): each
// with the MAC that holds it ("": one is not known).
func (r *Router) usedAddresses(dev string) map[string]string {
used := map[string]string{}
if out, code := r.sh("ip -j -4 neigh show dev " + q(dev)); code == 0 {
var list []struct {
Dst string `json:"dst"`
Lladdr string `json:"lladdr"`
State []string `json:"state"`
}
if json.Unmarshal([]byte(trimmed(out)), &list) == nil {
for _, n := range list {
if n.Dst != "" && !has(n.State, "FAILED") && !has(n.State, "INCOMPLETE") {
used[n.Dst] = n.Lladdr
}
}
}
}
leases := r.Leases
if leases == "" {
leases = "/tmp/dhcp.leases"
}
raw, _ := os.ReadFile(leases)
for _, l := range strings.Split(string(raw), "\n") {
if f := strings.Split(l, " "); len(f) >= 3 {
used[f[2]] = f[1]
}
}
return used
}
func has(list []string, v string) bool {
for _, x := range list {
if x == v {
return true
}
}
return false
}
// ourMAC is the MAC the netns's LAN link has now; "" without the link.
func (r *Router) ourMAC() string {
out, code := r.sh("ip -n " + loopNS + " -j link show " + lanNS + " 2>/dev/null")
if code != 0 {
return ""
}
var list []struct {
Address string `json:"address"`
}
if json.Unmarshal([]byte(trimmed(out)), &list) != nil || len(list) == 0 {
return ""
}
return list[0].Address
}
func fnv1a(s string, h uint32) uint32 {
for i := 0; i < len(s); i++ {
h = (h ^ uint32(s[i])) * 16777619
}
return h
}
// LanMAC is the MAC for the netns's LAN link, from the bridge's: a locally administered unicast address
// hashed from it, so it is the same every time and differs between routers. Not the bridge's MAC with a
// bit flipped: radios and ports often get those.
func LanMAC(bridge string) string {
s := strings.ToLower(trimmed(bridge))
h1 := fnv1a("dawnline lan "+s, 0x811c9dc5)
h2 := fnv1a(s, h1)
return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", ((h1>>24)&0xfc)|0x02, (h1>>16)&0xff, (h1>>8)&0xff, h1&0xff, (h2>>24)&0xff, (h2>>16)&0xff)
}
// addressFree tells whether the netns may use a LAN address: a host of the network, not the router's, not
// a static lease, outside the DHCP pool and not held by another device (an entry with one of the MACs in
// mine is the netns itself, also from before it was made again).
func addressFree(ip, router uint32, lan ipnet.Net, f dhcpFacts, used map[string]string, mine []string) bool {
if ip <= lan.Base || uint64(ip) >= uint64(lan.Base)+lan.Size()-1 || ip == router {
return false
}
for _, s := range f.statics {
if s == ip {
return false
}
}
if f.pool != nil && ip >= f.pool[0] && ip <= f.pool[1] {
return false
}
u, held := used[ipnet.String(ip)]
return !held || (u != "" && has(mine, u))
}
// pickAddress is a LAN address for the netns: the saved one while it is free, otherwise the highest free
// host that does not answer.
func (r *Router) pickAddress(lan ipnet.Net, router uint32, f dhcpFacts, saved, dev string, mine []string) string {
used := r.usedAddresses(dev)
if sv, ok := ipnet.IP(saved); ok && addressFree(sv, router, lan, f, used, mine) {
return ipnet.String(sv)
}
for ip := lan.Base + uint32(lan.Size()) - 2; ip > lan.Base; ip-- {
if !addressFree(ip, router, lan, f, used, mine) {
continue
}
// last check: nobody answers on it
r.sh("ping -c 1 -W 1 -I " + q(dev) + " " + ipnet.String(ip))
if addressFree(ip, router, lan, f, r.usedAddresses(dev), mine) {
return ipnet.String(ip)
}
}
return ""
}
// pickTransit is a /30 in 169.254.0.0/16 that no route or address on the router overlaps.
func (r *Router) pickTransit() (uint32, bool) {
var busy []ipnet.Net
snap := r.Snapshot()
for _, d := range snap.Devices {
for _, n := range append(append([]string{}, d.Nets...), d.Addrs...) {
if c, ok := ipnet.Any(n); ok {
busy = append(busy, c)
}
}
}
for k := 77; k < 77+256; k++ {
base, _ := ipnet.IP(fmt.Sprintf("169.254.%d.0", k%256))
free := true
for _, c := range busy {
if uint64(base) < uint64(c.Base)+c.Size() && uint64(c.Base) < uint64(base)+4 {
free = false
break
}
}
if free {
return base, true
}
}
return 0, false
}
// pickTable is a routing table nobody else uses (ours holds a single route via the transit link).
func (r *Router) pickTable(saved int) (int, bool) {
free := func(t int) bool {
out, code := r.sh("ip -4 route show table " + strconv.Itoa(t))
said := trimmed(out)
if code != 0 || said == "" {
return true
}
return !strings.Contains(said, "\n") && strings.Contains(said, " dev "+txRoot)
}
if saved != 0 && free(saved) {
return saved, true
}
for t := 2077; t < 2177; t++ {
if free(t) {
return t, true
}
}
return 0, false
}
// LoopErrorNote is a reason the path is off, for the service log and syslog: English, like every log line.
func LoopErrorNote(e map[string]any) string {
switch e["code"] {
case "no_lan":
return "No LAN detected."
case "not_bridge":
return fmt.Sprintf("%v is not a bridge, so Dawnline cannot join the LAN as a device.", e["dev"])
case "no_ipv4":
return fmt.Sprintf("%v has no IPv4 address.", e["dev"])
case "no_address":
return fmt.Sprintf("No free address on %v.", e["net"])
case "no_transit":
return "No free link-local range for the internal link."
}
return "No free routing table."
}
/* ---------- plan ---------- */
type loopState struct {
Applied int64 `json:"applied"`
Plan *LoopPlan `json:"plan"`
Transit uint32 `json:"transit"`
Fast *bool `json:"fast"`
Error any `json:"error"`
Off bool `json:"off"`
}
func (r *Router) loopState() loopState {
var s loopState
files.ReadJSON(r.RunDir+"/loop.json", &s)
return s
}
// LoopPlanFor is what should exist for these settings; saved is the LAN address kept from before
// (settings.lan_address). Pure apart from reading the router (and one ping a candidate address).
func (r *Router) LoopPlanFor(m *model.Model, saved string) Loop {
var wanted []*model.Server
for _, id := range m.ServerIDs() {
if s := m.Servers[id]; tunnelOK(s) && s.Enabled && s.LanLike {
wanted = append(wanted, s)
}
}
if len(wanted) == 0 {
return Loop{Off: true}
}
snap := r.Snapshot()
dev := snap.LAN
d := snap.Devices[dev]
if dev == "" || d == nil {
return Loop{Error: map[string]any{"code": "no_lan"}}
}
if d.Kind != "bridge" {
return Loop{Error: map[string]any{"code": "not_bridge", "dev": dev}}
}
var lan ipnet.Net
var router uint32
ok := len(d.Addrs) > 0
if ok {
if lan, ok = ipnet.Subnet(d.Addrs[0]); ok {
router, ok = ipnet.IP(strings.SplitN(d.Addrs[0], "/", 2)[0])
}
}
if !ok {
return Loop{Error: map[string]any{"code": "no_ipv4", "dev": dev}}
}
facts := r.dhcpOf(d.Logical, lan)
bridge, _ := os.ReadFile(r.sysNet() + "/" + dev + "/address")
mac := LanMAC(string(bridge))
// the netns's MACs: the one it gets, and the one it has (a random one from an older version)
addr := r.pickAddress(lan, router, facts, saved, dev, []string{mac, r.ourMAC()})
if addr == "" {
return Loop{Error: map[string]any{"code": "no_address", "net": lan.String()}}
}
last := r.loopState()
tx, ok := r.pickTransit()
if !ok {
return Loop{Error: map[string]any{"code": "no_transit"}}
}
saw := 0
if last.Plan != nil {
saw = last.Plan.Table
}
table, ok := r.pickTable(saw)
if !ok {
return Loop{Error: map[string]any{"code": "no_table"}}
}
p := &LoopPlan{Dev: dev, Gateway: ipnet.String(router), Address: addr, MAC: mac, Prefix: lan.Prefix, DNS: facts.dns, Transit: tx,
TxRoot: ipnet.String(tx + 1), TxNS: ipnet.String(tx + 2), Table: table, Servers: []LoopServer{}}
if p.DNS == "" {
p.DNS = p.Gateway
}
// by the settings alone, whoever is loaded now: the path does not move with people's states
behind := snap.Behind(m)
for _, srv := range wanted {
_, _, nets, _ := snap.ServerNet(srv)
var sites []string
for _, id := range m.DeviceIDs() {
if p := m.People[m.Devices[id].Person]; p != nil && p.Server == srv.ID {
sites = append(sites, behind[id]...)
}
}
if sites != nil {
sites = ipnet.Collapse(sites)
}
p.Servers = append(p.Servers, LoopServer{Ifname: srv.Ifname, Subnet: srv.Subnet,
Direct: ipnet.Collapse(append(append([]string{srv.Subnet}, nets...), sites...)), Behind: sites})
}
return Loop{Plan: p}
}
/* ---------- apply ---------- */
// NSRules is the netns's nft script as text. With fast, a flowtable on the netns's own two links takes
// established TCP and UDP flows past conntrack and the forward hook here. Only dlx1 and dll1: the root
// namespace's links stay out, the guard there sees every packet.
func NSRules(p *LoopPlan, fast bool) string {
ft, add := "", ""
if fast {
ft = "\n\tflowtable ft {\n\t\thook ingress priority 0;\n\t\tdevices = { \"" + txNS + "\", \"" + lanNS + "\" };\n\t}"
add = "\n\t\tct state established meta l4proto { tcp, udp } flow add @ft"
}
return "table inet dawnline\ndelete table inet dawnline\ntable inet dawnline {" + ft + `
chain nat_pre {
type nat hook prerouting priority dstnat; policy accept;
iifname "` + txNS + `" ip daddr ` + p.Address + ` meta l4proto { tcp, udp } th dport 53 dnat ip to ` + p.DNS + `:53
}
chain nat_post {
type nat hook postrouting priority srcnat; policy accept;
oifname "` + lanNS + `" masquerade
}
chain filter_forward {
type filter hook forward priority filter; policy drop;` + add + `
ct state established,related accept
iifname "` + txNS + `" oifname "` + lanNS + `" accept
}
chain filter_input {
type filter hook input priority filter; policy drop;
ct state established,related accept
iifname "lo" accept
icmp type echo-request accept
iifname "` + txNS + `" meta l4proto { tcp, udp } th dport 53 accept
}
}
`
}
// loadNSRules loads the netns's rules, with the flowtable when the kernel takes it, else without (the path
// works either way): slow is why the flowtable failed, err why the rules without it failed too.
func (r *Router) loadNSRules(p *LoopPlan) (fast bool, slow, err string) {
os.MkdirAll(r.varEtc(), 0o755)
path := r.varEtc() + "/dawnline-ns.nft"
load := func(fast bool) string {
os.WriteFile(path, []byte(NSRules(p, fast)), 0o644)
if errs := r.run([]string{"ip netns exec " + loopNS + " nft -f " + path}); len(errs) > 0 {
return errs[0]
}
return ""
}
if slow = load(true); slow == "" {
return true, "", ""
}
return false, slow, load(false)
}
// LoopRule is one policy rule of the path (To "": all).
type LoopRule struct {
Pref int
Iif string
To string
Table string
}
// LoopRules is the policy rules a plan wants, in order. A server interface: DNS to the netns address
// through the netns, home networks and the tunnel network by main, the rest into the netns.
func LoopRules(p *LoopPlan) []LoopRule {
var out []LoopRule
if p == nil {
return out
}
table := strconv.Itoa(p.Table)
for _, s := range p.Servers {
out = append(out, LoopRule{prefLoopDNS, s.Ifname, p.Address + "/32", table})
for _, n := range s.Direct {
out = append(out, LoopRule{prefMain, s.Ifname, n, "main"})
}
out = append(out, LoopRule{prefLoop, s.Ifname, "", table})
}
return out
}
// IPRule is a policy rule as `ip -j -4 rule show` prints it.
type IPRule struct {
Priority int `json:"priority"`
Src string `json:"src"`
Srclen *int `json:"srclen"`
Dst string `json:"dst"`
Dstlen *int `json:"dstlen"`
Iif string `json:"iif"`
Table string `json:"table"`
Detached json.RawMessage `json:"iif_detached"`
}
// currentRules is our policy rules: in our priority band and bound to a Dawnline tunnel interface, or left
// behind by a removed tunnel (its interface is gone).
func (r *Router) currentRules(ifnames []string) []IPRule {
var out []IPRule
said, code := r.sh("ip -j -4 rule show")
if code != 0 {
return out
}
var all []IPRule
if json.Unmarshal([]byte(trimmed(said)), &all) != nil {
return out
}
for _, x := range all {
if x.Priority >= 5200 && x.Priority < 5300 && x.Iif != "" && (has(ifnames, x.Iif) || x.Detached != nil) {
out = append(out, x)
}
}
return out
}
// ruleNet is a source or destination as `ip -j rule` has it, or "" for all.
func ruleNet(addr string, n *int) string {
if addr == "" || addr == "all" {
return ""
}
if n != nil {
return addr + "/" + strconv.Itoa(*n)
}
return addr
}
func ruleKey(pref int, from, iif, to, table string) string {
// `ip -j rule` shows a /32 source or destination without its length
full := func(n string) string {
switch {
case n == "":
return "all"
case !strings.Contains(n, "/"):
return n + "/32"
}
return n
}
return fmt.Sprintf("%d %s %s %s %s", pref, full(from), iif, full(to), table)
}
func ruleDel(x IPRule) string {
cmd := "ip -4 rule del priority " + strconv.Itoa(x.Priority)
if from := ruleNet(x.Src, x.Srclen); from != "" {
cmd += " from " + from
}
cmd += " iif " + q(x.Iif)
if to := ruleNet(x.Dst, x.Dstlen); to != "" {
cmd += " to " + to
}
return cmd + " lookup " + x.Table
}
// RuleCmds is the commands that turn the rules there are into the ones a plan wants: the missing ones
// added first, then the ones no longer wanted removed, so a device's traffic never falls between two
// rules while others change. None when they are as wanted.
func RuleCmds(p *LoopPlan, current []IPRule) []string {
have, want := map[string]IPRule{}, map[string]bool{}
var order, cmds, twice []string
for _, x := range current {
k := ruleKey(x.Priority, ruleNet(x.Src, x.Srclen), x.Iif, ruleNet(x.Dst, x.Dstlen), x.Table)
if _, seen := have[k]; seen {
twice = append(twice, ruleDel(x))
} else {
order = append(order, k)
}
have[k] = x
}
for _, w := range LoopRules(p) {
k := ruleKey(w.Pref, "", w.Iif, w.To, w.Table)
if _, there := have[k]; !there && !want[k] {
cmd := "ip -4 rule add priority " + strconv.Itoa(w.Pref) + " iif " + q(w.Iif)
if w.To != "" {
cmd += " to " + w.To
}
cmds = append(cmds, cmd+" lookup "+w.Table)
}
want[k] = true
}
for _, k := range order {
if !want[k] {
cmds = append(cmds, ruleDel(have[k]))
}
}
// the same rule twice (one deletion removes one of them)
return append(cmds, twice...)
}
// LoopRemove takes away the netns, links and rules, and the router's neighbour entry for the netns.
func (r *Router) LoopRemove(ifnames []string) {
last := r.loopState().Plan
// read while the link is there: the entry is ours when it carries the netns's MAC
stale := false
if last != nil && last.Dev != "" {
held := r.usedAddresses(last.Dev)[last.Address]
stale = held != "" && (held == last.MAC || held == r.ourMAC())
}
var cmds []string
for _, x := range r.currentRules(ifnames) {
cmds = append(cmds, ruleDel(x))
}
if last != nil && last.Table != 0 {
cmds = append(cmds, "ip -4 route flush table "+strconv.Itoa(last.Table))
}
if r.linkThere(txRoot) {
cmds = append(cmds, "ip link del "+txRoot)
}
if r.linkThere(lanRoot) {
cmds = append(cmds, "ip link del "+lanRoot)
}
if r.nsThere() {
cmds = append(cmds, "ip netns del "+loopNS)
}
// left there, it would hold the address for a MAC nobody has
if stale {
cmds = append(cmds, "ip -4 neigh del "+q(last.Address)+" dev "+q(last.Dev))
}
r.run(cmds)
os.Remove(r.RunDir + "/loop.json")
}
func sameJSON(a, b any) bool {
x, _ := json.Marshal(a)
y, _ := json.Marshal(b)
return string(x) == string(y)
}
// LoopEnsure brings the path to the plan. Cheap when nothing changed: it compares with the last applied
// plan and only checks that links, bridge port and rules exist. saved is the LAN address kept from before.
func (r *Router) LoopEnsure(m *model.Model, saved string, force bool) Loop {
state := r.RunDir + "/loop.json"
var ifnames []string
for _, id := range m.ServerIDs() {
ifnames = append(ifnames, m.Servers[id].Ifname)
}
res := r.LoopPlanFor(m, saved)
if res.Plan == nil {
// read before the removal, which deletes it: the error is logged once, not every tick
was := r.loopState()
if was.Plan != nil || r.linkThere(txRoot) || r.linkThere(lanRoot) || len(r.currentRules(ifnames)) > 0 {
r.LoopRemove(ifnames)
}
os.MkdirAll(r.RunDir, 0o755)
var why any
if res.Error != nil {
why = res.Error
}
files.WriteJSON(state, map[string]any{"error": why, "off": res.Off}, 0o644)
if res.Error != nil && !sameJSON(res.Error, was.Error) {
r.loopLog("error", "is off", LoopErrorNote(res.Error))
}
return res
}
p := res.Plan
last := r.loopState()
same := !force && last.Applied != 0 && last.Plan != nil && reflect.DeepEqual(last.Plan, p)
master, _ := os.Readlink(r.sysNet() + "/" + lanRoot + "/master")
healthy := r.linkThere(txRoot) && r.linkThere(lanRoot) && r.nsThere() && master != "" && filepath.Base(master) == p.Dev
if same && healthy && len(RuleCmds(p, r.currentRules(ifnames))) == 0 {
return res
}
// A link made by an older version has a random MAC (its plan had none, so this runs once after an
// update): it gets the stable one.
remac := r.linkThere(lanRoot) && r.ourMAC() != p.MAC
var cmds []string
if !r.nsThere() {
cmds = append(cmds, "ip netns add "+loopNS)
}
if !r.linkThere(txRoot) {
cmds = append(cmds, "ip link add "+txRoot+" type veth peer name "+txNS+" netns "+loopNS)
}
if !r.linkThere(lanRoot) {
cmds = append(cmds, "ip link add "+lanRoot+" type veth peer name "+lanNS+" address "+p.MAC+" netns "+loopNS)
}
if remac {
cmds = append(cmds, "ip -n "+loopNS+" link set "+lanNS+" down", "ip -n "+loopNS+" link set "+lanNS+" address "+p.MAC)
}
errs := r.run(cmds)
n := "ip -n " + loopNS + " "
cmds = []string{
"ip addr flush dev " + txRoot,
"ip addr add " + p.TxRoot + "/30 dev " + txRoot,
"ip link set " + txRoot + " up",
"ip link set " + lanRoot + " master " + q(p.Dev),
"ip link set " + lanRoot + " up",
n + "link set lo up",
n + "addr flush dev " + txNS,
n + "addr add " + p.TxNS + "/30 dev " + txNS,
n + "link set " + txNS + " up",
n + "addr flush dev " + lanNS,
n + "addr add " + p.Address + "/" + strconv.Itoa(p.Prefix) + " dev " + lanNS,
n + "link set " + lanNS + " up",
n + "route replace default via " + p.Gateway + " dev " + lanNS,
// the resolver's own queries (from the transit address) go back out the transit link
n + "route replace default via " + p.TxRoot + " dev " + txNS + " table 100",
n + "rule del from " + p.TxNS + " lookup 100 2>/dev/null; " + n + "rule add from " + p.TxNS + " lookup 100",
"ip netns exec " + loopNS + " sh -c 'echo 1 > /proc/sys/net/ipv4/ip_forward; echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter; echo 0 > /proc/sys/net/ipv4/conf/default/rp_filter'",
"ip -4 route replace default via " + p.TxNS + " dev " + txRoot + " table " + strconv.Itoa(p.Table),
}
// the networks behind routers are answered through the tunnel too; the ones of the plan before go first
sites := false
for _, s := range p.Servers {
sites = sites || len(s.Behind) > 0
}
if last.Plan != nil {
for _, s := range last.Plan.Servers {
sites = sites || len(s.Behind) > 0
}
}
if sites {
cmds = append(cmds, n+"route flush proto "+routeProto+" 2>/dev/null || true")
}
for _, s := range p.Servers {
cmds = append(cmds, n+"route replace "+q(s.Subnet)+" via "+q(p.TxRoot)+" dev "+txNS)
for _, b := range s.Behind {
cmds = append(cmds, n+"route replace "+q(b)+" via "+q(p.TxRoot)+" dev "+txNS+" proto "+routeProto)
}
}
// the router knows the address by the old MAC: ask again now, not when the entry expires
if remac {
cmds = append(cmds, "ip -4 neigh flush to "+p.Address+" dev "+q(p.Dev))
}
errs = append(errs, r.run(cmds)...)
fast, slow, err := r.loadNSRules(p)
if err != "" {
errs = append(errs, err)
}
errs = append(errs, r.run(RuleCmds(p, r.currentRules(ifnames)))...)
if len(errs) > 0 {
why := map[string]any{"code": "setup", "detail": errs[0]}
if !sameJSON(r.loopState().Error, why) {
r.loopLog("error", "failed - internet goes out directly for now", errs[0])
}
r.LoopRemove(ifnames)
files.WriteJSON(state, map[string]any{"error": why}, 0o644)
return Loop{Error: why}
}
os.MkdirAll(r.RunDir, 0o755)
files.WriteJSON(state, loopState2{Applied: time.Now().Unix(), Plan: p, Transit: p.Transit, Fast: fast}, 0o644)
switch {
case last.Applied == 0 || last.Plan == nil || last.Plan.Dev != p.Dev || last.Plan.Address != p.Address:
r.loopLog("up", fmt.Sprintf("is on · traffic enters %s as %s", p.Dev, p.Address), "")
case !healthy:
r.loopLog("repair", fmt.Sprintf("was broken - set up again (%s as %s)", p.Dev, p.Address), "")
}
// once, not again on every apply while the module is still missing
if !fast && (last.Fast == nil || *last.Fast) {
r.loopLog("info", "runs without its fast path",
"Without kmod-nft-offload every packet on this path passes the firewall twice. Install it and restart Dawnline.\n"+slow)
}
return res
}
// loopState2 is loop.json while the path is up, in ucode's own order of keys.
type loopState2 struct {
Applied int64 `json:"applied"`
Plan *LoopPlan `json:"plan"`
Transit uint32 `json:"transit"`
Fast bool `json:"fast"`
}
+372
View File
@@ -0,0 +1,372 @@
package apply
import (
"encoding/json"
"os"
"reflect"
"strconv"
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/servicelog"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
type loopGolden struct {
Bridge string `json:"bridge"`
DHCP string `json:"dhcp"`
Steps []struct {
What string `json:"what"`
Force bool `json:"force"`
Result json.RawMessage `json:"result"`
State any `json:"state"`
NSRules string `json:"ns_rules"`
Log []map[string]any `json:"log"`
Ran []string `json:"ran"`
Failed []string `json:"failed"`
Pings []string `json:"pings"`
Sys struct {
Dlx0 bool `json:"dlx0"`
Dll0 bool `json:"dll0"`
Netns bool `json:"netns"`
} `json:"sys"`
} `json:"steps"`
Macs [][2]*string `json:"macs"`
Rules struct {
Plan *LoopPlan `json:"plan"`
Want []struct {
Pref int `json:"pref"`
Iif string `json:"iif"`
To *string `json:"to"`
Table string `json:"table"`
} `json:"want"`
Current []IPRule `json:"current"`
Cmds []string `json:"cmds"`
FromNothing []string `json:"from_nothing"`
ToNothing []string `json:"to_nothing"`
} `json:"rules"`
NSRules []string `json:"ns_rules"`
Notes []string `json:"notes"`
}
func loopTrace(t *testing.T) loopGolden {
raw, err := os.ReadFile("testdata/loop.json")
if err != nil {
t.Fatal(err)
}
var g loopGolden
if err := json.Unmarshal(raw, &g); err != nil {
t.Fatal(err)
}
return g
}
// The pure parts of the home-like path, on ucode's own answers (testdata/loop.uc).
func TestLoopPartsAsUcode(t *testing.T) {
g := loopTrace(t)
if len(g.Macs) != 5 {
t.Fatal("the golden is too small")
}
for _, m := range g.Macs {
in := ""
if m[0] != nil {
in = *m[0]
}
if got := LanMAC(in); got != *m[1] {
t.Errorf("the MAC from %q: %s, ucode %s", in, got, *m[1])
}
}
want := LoopRules(g.Rules.Plan)
if len(want) != len(g.Rules.Want) || len(want) == 0 {
t.Fatalf("%d rules, ucode %d", len(want), len(g.Rules.Want))
}
for i, w := range g.Rules.Want {
to := ""
if w.To != nil {
to = *w.To
}
if want[i] != (LoopRule{w.Pref, w.Iif, to, w.Table}) {
t.Errorf("rule %d: %v, ucode %v", i, want[i], w)
}
}
if len(LoopRules(nil)) != 0 {
t.Error("rules from no plan")
}
for _, c := range []struct {
what string
got []string
want []string
}{{"from what there is", RuleCmds(g.Rules.Plan, g.Rules.Current), g.Rules.Cmds}, {"from nothing", RuleCmds(g.Rules.Plan, nil), g.Rules.FromNothing},
{"to nothing", RuleCmds(&LoopPlan{}, g.Rules.Current), g.Rules.ToNothing}} {
if strings.Join(c.got, "\n") != strings.Join(c.want, "\n") || len(c.want) == 0 {
t.Errorf("the commands %s:\n%s\n--- ucode:\n%s", c.what, strings.Join(c.got, "\n"), strings.Join(c.want, "\n"))
}
}
if NSRules(g.Rules.Plan, true) != g.NSRules[0] || NSRules(g.Rules.Plan, false) != g.NSRules[1] {
t.Errorf("the netns's rules:\n%s\n--- ucode:\n%s", NSRules(g.Rules.Plan, true), g.NSRules[0])
}
for i, e := range []map[string]any{{"code": "no_lan"}, {"code": "not_bridge", "dev": "eth1"}, {"code": "no_ipv4", "dev": "br-lan"},
{"code": "no_address", "net": "10.0.1.0/24"}, {"code": "no_transit"}, {"code": "no_table"}} {
if got := LoopErrorNote(e); got != g.Notes[i] {
t.Errorf("the note for %v: %q, ucode %q", e, got, g.Notes[i])
}
}
}
// The home-like path through a row of states of the router: the plan, the commands in their order,
// loop.json, the netns's rules and the service log are ucode's own (testdata/loop.uc). The stand-in for ip
// here keeps the same state the golden's does.
func TestLoopAsUcode(t *testing.T) {
g := loopTrace(t)
if len(g.Steps) != 15 {
t.Fatalf("%d steps", len(g.Steps))
}
raw, err := os.ReadFile("../netview/testdata/trace.json")
if err != nil {
t.Fatal(err)
}
var router struct {
Routes json.RawMessage `json:"routes"`
Addrs json.RawMessage `json:"addrs"`
Links map[string]netview.Link `json:"links"`
}
if err := json.Unmarshal(raw, &router); err != nil {
t.Fatal(err)
}
dir := t.TempDir()
sys, etc := dir+"/sys", dir+"/etc"
os.MkdirAll(sys+"/br-lan", 0o755)
os.WriteFile(sys+"/br-lan/address", []byte(g.Bridge+"\n"), 0o644)
os.MkdirAll(dir+"/netns", 0o755)
os.WriteFile(dir+"/dhcp", []byte(g.DHCP), 0o644)
// what the stand-in is told and keeps
var neigh, nsmac, mac, rules, fail string
var ftFail bool
tables := map[string]string{}
var pings, said []string
there := func(p string) bool { _, err := os.Stat(p); return err == nil }
r := &Router{RunDir: dir + "/run", VarEtc: etc, SysNet: sys, NetnsDir: dir + "/netns", Leases: dir + "/leases", DHCP: dir + "/dhcp"}
r.Snapshot = func() *netview.Snapshot {
return netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return router.Addrs },
Links: func() map[string]netview.Link { return router.Links },
Logical: func() map[string]netview.Logical {
return map[string]netview.Logical{"br-lan": {Name: "lan", Proto: "static"}}
}})
}
r.Log = &servicelog.Log{RunDir: dir + "/run", Level: func() string { return "info" }, Syslog: func(_, line string) { said = append(said, line) }}
r.Exec = func(name string, args ...string) (string, int) {
if name != "sh" || len(args) != 2 || args[0] != "-c" {
t.Fatalf("not through the shell: %s %v", name, args)
}
cmd := args[1]
plain := strings.ReplaceAll(cmd, "'", "")
if strings.HasPrefix(cmd, "ping ") {
pings = append(pings, plain)
return "", 1
}
if fail != "" && strings.Contains(plain, fail) {
return "RTNETLINK answers: a made-up failure\n", 2
}
word := strings.Fields(plain)
switch {
case strings.HasPrefix(cmd, "ip -j -4 neigh show dev "):
if neigh == "" {
return "[]\n", 0
}
return neigh, 0
case strings.HasPrefix(cmd, "ip -n dawnline -j link show dll1"):
if !there(sys + "/dll0") {
return "", 1
}
if nsmac != "" {
return `[{"address":"` + nsmac + `"}]`, 0
}
return `[{"address":"` + mac + `"}]`, 0
case cmd == "ip -j -4 rule show":
if rules == "" {
return "[]", 0
}
return rules, 0
case strings.HasPrefix(cmd, "ip -4 route show table "):
return tables[word[5]], 0
case cmd == "ip netns add dawnline":
os.WriteFile(dir+"/netns/dawnline", nil, 0o644)
case cmd == "ip netns del dawnline":
os.Remove(dir + "/netns/dawnline")
case strings.HasPrefix(cmd, "ip netns exec dawnline nft -f "):
if text, _ := os.ReadFile(word[6]); ftFail && strings.Contains(string(text), "flowtable") {
return word[6] + ":4:2-10: Error: Could not process rule: No such file or directory\n", 1
}
case strings.HasPrefix(cmd, "ip link add dlx0 "):
os.MkdirAll(sys+"/dlx0", 0o755)
case strings.HasPrefix(cmd, "ip link add dll0 "):
for i, w := range word {
if w == "address" {
mac = word[i+1]
}
}
os.MkdirAll(sys+"/dll0", 0o755)
case strings.HasPrefix(cmd, "ip link del "):
os.RemoveAll(sys + "/" + word[3])
case strings.HasPrefix(cmd, "ip link set dll0 master "):
os.Remove(sys + "/dll0/master")
os.Symlink("../"+word[5], sys+"/dll0/master")
case strings.HasPrefix(cmd, "ip -n dawnline link set dll1 address "):
mac, nsmac = word[7], ""
}
return "", 0
}
text, _ := os.ReadFile("../access/testdata/access.uci")
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
var last *LoopPlan
// the rules a plan wants, as `ip -j -4 rule show` prints them once they are in
shown := func() []map[string]any {
var out []map[string]any
for _, w := range LoopRules(last) {
o := map[string]any{"priority": w.Pref, "src": "all", "iif": w.Iif, "table": w.Table}
if w.To != "" {
parts := strings.SplitN(w.To, "/", 2)
o["dst"] = parts[0]
if n, _ := strconv.Atoi(parts[1]); n != 32 {
o["dstlen"] = n
}
}
out = append(out, o)
}
return out
}
put := func(list []map[string]any) {
raw, _ := json.Marshal(list)
rules = string(raw)
}
saved, logged := "", 0
for i, want := range g.Steps {
switch i {
case 1, 7, 8, 9, 12, 13:
put(shown())
case 2:
l := shown()
put(append(append([]map[string]any{}, l[1:]...), l[1], map[string]any{"priority": 5210, "src": "10.9.0.2", "iif": "dawn1", "dst": "10.1.0.0", "dstlen": 16, "table": "main"},
map[string]any{"priority": 5290, "src": "all", "iif": "dawn7", "iif_detached": nil, "table": "2077"},
map[string]any{"priority": 5290, "src": "all", "iif": "eth9", "table": "2077"}, map[string]any{"priority": 100, "src": "all", "iif": "dawn1", "table": "main"}))
case 3:
put(shown())
ftFail = true
case 5:
ftFail, nsmac = false, "5e:00:11:22:33:44"
case 6:
nsmac, saved = "", "192.168.77.200"
neigh = `[{"dst":"192.168.77.200","lladdr":"aa:bb:cc:00:00:01","state":["REACHABLE"]},{"dst":"192.168.77.254","lladdr":"aa:bb:cc:00:00:02","state":["FAILED"]},{"dst":"192.168.77.253","state":["STALE"]}]`
os.WriteFile(dir+"/leases", []byte("1700000000 aa:bb:cc:00:00:03 192.168.77.252 phone *\n"), 0o644)
d, _ := uci.Parse(g.DHCP)
d.Add("host", "nas")
d.Set("nas", "ip", "192.168.77.251")
d.Set("lan", "start", "240")
d.Set("lan", "limit", "11")
d.SetList("lan", "dhcp_option", []string{"3,192.168.77.1", "6,192.168.77.53,192.168.77.54"})
os.WriteFile(dir+"/dhcp", []byte(d.Format()), 0o644)
case 14:
put(nil)
}
switch i {
case 7:
saved = "192.168.77.201"
case 8:
tables["2077"] = "default via 10.0.0.1 dev eth5\n10.0.0.0/24 dev eth5\n"
tables["2078"] = "default via 169.254.77.2 dev dlx0\n"
case 9:
fail = "route replace default via 192.168.77.1"
case 11:
fail = ""
case 12:
c.Set("cabin", "lan_like", "0")
case 13:
c.Set("open", "lan_like", "0")
}
pings, said = nil, nil
res := r.LoopEnsure(model.Load(c, nil), saved, want.Force)
if res.Plan != nil {
last = res.Plan
}
fail := func(what string, got, exp any) {
a, _ := json.Marshal(got)
b, _ := json.Marshal(exp)
t.Errorf("step %d (%s): %s\n go: %s\n ucode: %s", i, want.What, what, a, b)
}
same := func(got, exp any) bool {
var a, b any
x, _ := json.Marshal(got)
y, _ := json.Marshal(exp)
json.Unmarshal(x, &a)
json.Unmarshal(y, &b)
return reflect.DeepEqual(a, b)
}
// where the files are kept here is not where the router keeps them
home := func(s string) string { return strings.ReplaceAll(s, etc, "/var/etc") }
var result any = res.Plan
switch {
case res.Off:
result = map[string]any{"off": true}
case res.Error != nil:
result = map[string]any{"error": map[string]any{"code": res.Error["code"], "detail": home(res.Error["detail"].(string))}}
}
if !same(result, want.Result) {
fail("what it came to", result, want.Result)
}
var ran, failed []string
for _, l := range said {
if s, is := strings.CutPrefix(l, "[info] [loop] $ "); is {
ran = append(ran, home(s))
}
if s, is := strings.CutPrefix(l, "[error] [loop] failed: "); is {
failed = append(failed, home(s))
}
}
if strings.Join(ran, "\n") != strings.Join(want.Ran, "\n") {
fail("the commands", ran, want.Ran)
}
if strings.Join(failed, "\n") != strings.Join(want.Failed, "\n") {
fail("what failed", failed, want.Failed)
}
if strings.Join(pings, "\n") != strings.Join(want.Pings, "\n") {
fail("who was asked whether they answer", pings, want.Pings)
}
var state any
if raw, err := os.ReadFile(dir + "/run/loop.json"); err == nil {
json.Unmarshal([]byte(home(string(raw))), &state)
if s, is := state.(map[string]any); is && s["applied"] != nil {
s["applied"] = float64(1)
}
}
if !same(state, want.State) {
fail("loop.json", state, want.State)
}
if got, _ := os.ReadFile(etc + "/dawnline-ns.nft"); string(got) != want.NSRules {
fail("the netns's rules", string(got), want.NSRules)
}
var log []map[string]any
lines, _ := os.ReadFile(dir + "/run/service.log")
all := strings.Split(strings.TrimRight(string(lines), "\n"), "\n")
if len(lines) == 0 {
all = nil
}
for _, l := range all[logged:] {
var e map[string]any
json.Unmarshal([]byte(home(l)), &e)
delete(e, "ts")
log = append(log, e)
}
logged = len(all)
if !same(log, want.Log) && !(len(log) == 0 && len(want.Log) == 0) {
fail("the service log", log, want.Log)
}
if there(sys+"/dlx0") != want.Sys.Dlx0 || there(sys+"/dll0") != want.Sys.Dll0 || there(dir+"/netns/dawnline") != want.Sys.Netns {
fail("what is left on the router", []bool{there(sys + "/dlx0"), there(sys + "/dll0"), there(dir + "/netns/dawnline")}, want.Sys)
}
}
}
+430
View File
@@ -0,0 +1,430 @@
package apply
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/disguise"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/servicelog"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// netifd: one AmneziaWG interface a server, named after its ifname and marked `_dawnline` = the server's
// id (dawnline.netifd). Only interfaces Dawnline made are changed or removed: a name another interface has
// is left alone, and the server is kept down and said so (netifd-blocked.json: a code with its values).
// opt is one option of an interface: a string, a list, or nothing (the option goes).
type opt struct {
key string
val string
list []string
}
// put sets an option only when it differs (nothing deletes it); true when something changed.
func put(c *uci.Config, sec string, o opt) bool {
cur, has := c.Section(sec).Values[o.key]
if o.val == "" && len(o.list) == 0 {
if has {
c.Delete(sec, o.key)
return true
}
return false
}
if o.list != nil {
if has && cur.IsList && strings.Join(cur.List, "\n") == strings.Join(o.list, "\n") {
return false
}
c.SetList(sec, o.key, o.list)
return true
}
if has && !cur.IsList && cur.Str == o.val {
return false
}
c.Set(sec, o.key, o.val)
return true
}
// options is what is written into network.<ifname> apart from the markers: what netifd sets the tunnel up from.
func options(srv *model.Server) []opt {
prefix := ""
if i := strings.Index(srv.Subnet, "/"); i >= 0 {
prefix = srv.Subnet[i+1:]
}
o := []opt{{key: "proto", val: "amneziawg"}, {key: "private_key", val: srv.PrivateKey}, {key: "listen_port", val: strconv.Itoa(srv.Port)},
{key: "addresses", list: []string{model.ServerAddress(srv) + "/" + prefix}}, {key: "mtu", val: strconv.Itoa(model.TunnelMTU(srv))}}
for _, k := range model.ObfKeys {
v := srv.Obf[k]
// DisableCookies is the devices' alone: on the server it would turn off WireGuard's own defence
// against a flood of handshakes. Nothing takes it off an interface that had it.
if k == "disable_cookies" {
v = ""
}
o = append(o, opt{key: "awg_" + k, val: v})
}
off := opt{key: "disabled"}
auto := opt{key: "auto"}
if !srv.Enabled {
off.val, auto.val = "1", "0"
}
return append(o, off, auto)
}
// optsText is options as a log line's words (secrets masked, nothing as "-").
func optsText(list []opt) string {
var out []string
for _, o := range list {
s := "-"
if o.list != nil {
s = strings.Join(o.list, " ")
} else if o.val != "" {
s = o.val
}
if strings.Contains(o.key, "private_key") || strings.Contains(o.key, "preshared_key") || strings.Contains(o.key, "protection_key") ||
strings.Contains(o.key, "privatekey") || strings.Contains(o.key, "presharedkey") || strings.Contains(o.key, "protectionkey") {
s = "***"
}
out = append(out, o.key+"="+s)
}
return strings.Join(out, ", ")
}
// unsupported is why this AmneziaWG cannot run a server's disguise values, or nil when it can
// (dawnline.awg obf_unsupported).
func unsupported(o map[string]string, c awg.Caps) map[string]any {
has := func(keys ...string) bool {
for _, k := range keys {
if o[k] != "" {
return true
}
}
return false
}
installed := c.Kmod
if installed == nil {
installed = c.Tools
}
ranged := false
for _, k := range []string{"h1", "h2", "h3", "h4"} {
ranged = ranged || strings.Contains(o[k], "-")
}
switch {
case has("header_protection_key", "content_padding_addition", "rekey_after_time", "rekey_timeout", "reject_after_time", "keepalive_timeout",
"max_handshake_attempts", "random_trailers", "disable_cookies") && !c.V3:
return map[string]any{"code": "disguise", "need": "3.1", "installed": installed}
case (has("s3", "s4", "i1", "i2", "i3", "i4", "i5") || ranged) && !c.V2:
return map[string]any{"code": "disguise", "need": "2.0", "installed": installed}
}
return nil
}
// blockedNote is why a server is kept down, for the service log (dawnline.awg blocked_note).
func blockedNote(why map[string]any) string {
switch why["code"] {
case "ifname":
return fmt.Sprintf(`has an invalid interface name "%v"`, why["ifname"])
case "taken":
return fmt.Sprintf("can't use the network interface %v: it exists and isn't Dawnline's", why["ifname"])
case "disguise":
if in, _ := why["installed"].(string); in != "" {
return fmt.Sprintf("is kept down: its disguise needs AmneziaWG %v (installed: %s)", why["need"], in)
}
return fmt.Sprintf("is kept down: its disguise needs AmneziaWG %v (installed: none)", why["need"])
case "disguise_invalid":
return "is kept down: its disguise has values AmneziaWG cannot take"
}
return "is kept down"
}
func blockedFix(why map[string]any) string {
switch why["code"] {
case "disguise":
return "Install that AmneziaWG build in Settings → Updates, or switch the disguise in Settings → Disguise."
case "disguise_invalid":
return "Choose a disguise in Settings → Disguise: it replaces the values set by hand."
}
return "Choose another interface name in the server settings."
}
// intent notes why Dawnline itself is about to take a tunnel down, so the hotplug log can tell that from
// a tunnel that stopped on its own. reason: settings | off | removed. Kept for two minutes.
func (r *Router) intent(ifname, reason, name string) {
path := r.RunDir + "/iface-intent.json"
all := map[string]map[string]any{}
files.ReadJSON(path, &all)
now := time.Now().Unix()
for k, v := range all {
if ts, _ := v["ts"].(float64); float64(now)-ts >= 120 {
delete(all, k)
}
}
all[ifname] = map[string]any{"reason": reason, "name": name, "ts": now}
os.MkdirAll(r.RunDir, 0o755)
files.WriteJSON(path, all, 0o600)
}
// commit writes a configuration file in place whole, under a hidden name first.
func commit(path string, c *uci.Config) error {
var b [4]byte
rand.Read(b[:])
tmp := filepath.Join(filepath.Dir(path), "."+filepath.Base(path)+".dl-"+hex.EncodeToString(b[:]))
mode := os.FileMode(0o644)
if st, err := os.Stat(path); err == nil {
mode = st.Mode().Perm()
}
if err := os.WriteFile(tmp, []byte(c.Format()), mode); err != nil {
os.Remove(tmp)
return err
}
os.Chmod(tmp, mode)
if err := os.Rename(tmp, path); err != nil {
os.Remove(tmp)
return err
}
return nil
}
// ack records a configuration's new checksum after Dawnline committed and applied it itself, so a later
// `reload_config` does not treat it as changed and restart services that reload on any network change.
func (r *Router) ack(conf string) {
const md5f = "/var/run/config.md5"
if r.dry {
return
}
raw, err := os.ReadFile(md5f)
if err != nil {
return
}
sum, _ := r.Exec("sh", "-c", "uci show "+conf+" | md5sum | cut -d' ' -f1")
sum = strings.TrimSpace(sum)
if sum == "" {
return
}
path := "/var/run/config.check/" + conf
lines := strings.Split(strings.Trim(string(raw), " \t\r\n\v\f"), "\n")
found := false
for i, l := range lines {
if strings.HasSuffix(l, " "+path) {
lines[i], found = sum+" "+path, true
}
}
if !found {
lines = append(lines, sum+" "+path)
}
os.WriteFile(md5f, []byte(strings.Join(lines, "\n")+"\n"), 0o644)
}
// Netifd brings the router's network configuration in line with the servers; true when it changed. tools
// tells whether amneziawg-tools is there (without its protocol script, changing or bringing up an
// interface would only stop a tunnel that still runs); caps what this AmneziaWG can run. Each server that
// cannot have its interface is marked Blocked in m, as the rest of an apply works from the same model.
func (r *Router) Netifd(m *model.Model, tools bool, caps awg.Caps) bool {
text, _ := os.ReadFile(r.Network)
c, err := uci.Parse(string(text))
if err != nil {
r.say("error", "network", r.Network+" does not parse: nothing was changed · "+err.Error())
return false
}
changed := false
var touched []string
want := map[string]bool{}
was := map[string]any{}
files.ReadJSON(r.RunDir+"/netifd-blocked.json", &was)
blocked := map[string]any{}
block := func(srv *model.Server, why map[string]any, detail string) {
blocked[srv.ID] = why
srv.Blocked = why
a, _ := json.Marshal(was[srv.ID])
b, _ := json.Marshal(why)
if string(a) != string(b) && r.Log != nil {
r.Log.Write("dawnline", "error", blockedNote(why), detail, srv.Name, servicelog.Extra{})
}
}
flag := r.RunDir + "/netifd-no-awg"
_, flagged := os.Stat(flag)
keyed := false
for _, s := range m.Servers {
keyed = keyed || s.PrivateKey != ""
}
if tools && flagged == nil {
os.Remove(flag)
} else if !tools && keyed && flagged != nil {
if r.Log != nil {
r.Log.Write("dawnline", "error", "isn't installed: tunnels were left as they are", "Install it in Dawnline → Components.", "AmneziaWG", servicelog.Extra{})
}
os.MkdirAll(r.RunDir, 0o755)
os.WriteFile(flag, []byte(strconv.FormatInt(time.Now().Unix(), 10)), 0o644)
}
for _, id := range m.ServerIDs() {
srv := m.Servers[id]
if srv.PrivateKey == "" {
continue
}
ifn := srv.Ifname
if !model.ValidIfname(ifn) {
block(srv, map[string]any{"code": "ifname", "ifname": ifn}, "Interface name: 1-12 lowercase letters, digits or _.")
continue
}
// whose the section is: nobody's, this server's, or someone else's
if s := c.Section(ifn); s != nil {
if owner := s.Values["_dawnline"].Str; owner != id {
block(srv, map[string]any{"code": "taken", "ifname": ifn}, "Dawnline never changes interfaces it did not create. Choose another interface name for this server.")
continue
}
}
// disguise values AmneziaWG refuses or crashes on never reach the kernel, and a disguise this build
// cannot run would come up as a tunnel no device reaches: down, and said so
var why map[string]any
if tools {
if len(disguise.Unsafe(srv.Obf)) > 0 {
why = map[string]any{"code": "disguise_invalid"}
} else {
why = unsupported(srv.Obf, caps)
}
}
if why != nil {
block(srv, why, blockedFix(why))
continue
}
srv.Blocked = nil
want[ifn] = true
if !tools {
continue
}
created := false
if s := c.Section(ifn); s == nil || s.Type != "interface" {
c.DeleteSection(ifn)
c.Add("interface", ifn)
created = true
}
mine := created
var diff []opt
for _, o := range options(srv) {
if put(c, ifn, o) {
mine = true
diff = append(diff, o)
}
}
if mine {
how := "updated"
if created {
how = "created"
}
r.say("info", "network", fmt.Sprintf("interface %s (%s): %s · %s", ifn, srv.Name, how, optsText(diff)))
} else {
r.say("debug", "network", fmt.Sprintf("interface %s (%s): unchanged · %s", ifn, srv.Name, optsText(options(srv))))
}
// the markers are saved, but netifd does not read them: a renamed server keeps its tunnel and every session in it
if put(c, ifn, opt{key: "_dawnline", val: id}) {
changed = true
}
if put(c, ifn, opt{key: "_dawnline_name", val: srv.Name}) {
changed = true
}
if mine {
changed = true
touched = append(touched, ifn)
reason := "settings"
if !srv.Enabled {
reason = "off"
}
r.intent(ifn, reason, srv.Name)
}
}
a, _ := json.Marshal(blocked)
b, _ := json.Marshal(was)
if string(a) != string(b) {
os.MkdirAll(r.RunDir, 0o755)
files.WriteJSON(r.RunDir+"/netifd-blocked.json", blocked, 0o644)
}
// interfaces Dawnline made for servers that are no more
var stale []string
for _, s := range c.OfType("interface") {
if s.Values["_dawnline"].Str != "" && !want[s.Name] {
stale = append(stale, s.Name)
}
}
for _, ifn := range stale {
name := c.Section(ifn).Values["_dawnline_name"].Str
shown := name
if name == "" {
name, shown = ifn, "a removed server"
}
r.intent(ifn, "removed", name)
r.say("info", "network", fmt.Sprintf("interface %s (%s): removed, ifdown %s", ifn, shown, ifn))
r.Exec("ifdown", ifn)
c.DeleteSection(ifn)
changed = true
}
if changed {
if err := commit(r.Network, c); err != nil {
r.say("error", "network", r.Network+" could not be written · "+err.Error())
return false
}
r.ack("network")
}
// only the markers changed: nothing for netifd to read
if len(touched) > 0 || len(stale) > 0 {
r.say("info", "network", "ubus call network reload")
r.Exec("ubus", "call", "network", "reload")
for _, ifn := range touched {
if srv := m.Servers[c.Section(ifn).Values["_dawnline"].Str]; srv != nil && srv.Enabled {
r.say("info", "network", "ifup "+ifn)
r.Exec("ifup", ifn)
}
}
}
return changed
}
// NetifdOwner is whose an interface of the network configuration is: has false when there is none, id ""
// when it is not Dawnline's.
func (r *Router) NetifdOwner(ifname string) (id string, has bool) {
text, _ := os.ReadFile(r.Network)
c, err := uci.Parse(string(text))
if err != nil || c.Section(ifname) == nil {
return "", false
}
return c.Section(ifname).Values["_dawnline"].Str, true
}
// NetifdDrift is what of each running server's interface differs from what Netifd writes, by option name
// (a LuCI or uci edit of network.<ifname>, which would otherwise last until the next apply): an empty list
// when it is as Dawnline set it. Only enabled servers whose section exists and is theirs (a missing one is
// the tunnel probe's); the markers are not compared, netifd does not read them. Nothing is saved.
func (r *Router) NetifdDrift(m *model.Model) map[string][]string {
out := map[string][]string{}
text, _ := os.ReadFile(r.Network)
c, err := uci.Parse(string(text))
if err != nil {
return out
}
for _, id := range m.ServerIDs() {
srv := m.Servers[id]
s := c.Section(srv.Ifname)
if !srv.Enabled || !tunnelOK(srv) || s == nil || s.Values["_dawnline"].Str != id || s.Type != "interface" {
continue
}
diff := []string{}
for _, o := range options(srv) {
if put(c, srv.Ifname, o) {
diff = append(diff, o.key)
}
}
out[id] = diff
}
return out
}
// BlockedNote and BlockedFix word why a server is kept down, for the service log.
func BlockedNote(why map[string]any) string { return blockedNote(why) }
func BlockedFix(why map[string]any) string { return blockedFix(why) }
+154
View File
@@ -0,0 +1,154 @@
package apply
import (
"encoding/json"
"os"
"reflect"
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// The router's network configuration through a row of changes to access's household: after each, the file
// ucode's netifd sync leaves, the servers it keeps down and why, and what it ran (testdata/netifd.uc).
func TestNetifdAsUcode(t *testing.T) {
raw, err := os.ReadFile("testdata/netifd.json")
if err != nil {
t.Fatal(err)
}
var trace struct {
Steps []struct {
What string `json:"what"`
Changed bool `json:"changed"`
Network string `json:"network"`
Blocked map[string]any `json:"blocked"`
ModelBlocked map[string]any `json:"model_blocked"`
Cmds []string `json:"cmds"`
Intents map[string]map[string]any `json:"intents"`
NoAWG bool `json:"no_awg"`
} `json:"steps"`
}
if err := json.Unmarshal(raw, &trace); err != nil {
t.Fatal(err)
}
if len(trace.Steps) != 9 {
t.Fatalf("%d steps", len(trace.Steps))
}
text, _ := os.ReadFile("../access/testdata/access.uci")
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
dir := t.TempDir()
network, _ := os.ReadFile("testdata/network.uci")
os.WriteFile(dir+"/network", network, 0o644)
var cmds []string
r := &Router{RunDir: dir + "/run", Network: dir + "/network", Exec: func(name string, args ...string) (string, int) {
cmds = append(cmds, strings.Join(append([]string{name}, args...), " "))
return "", 0
}}
tools := true
caps := awg.Caps{V2: true, V3: true, Tools: "3.1"}
for i, want := range trace.Steps {
switch i {
case 2:
c.Set("home", "port", "4501")
c.Set("cabin", "enabled", "0")
case 3:
c.Set("home", "jmin", "90")
c.Set("home", "jmax", "50")
case 4:
c.Delete("home", "jmin")
c.Delete("home", "jmax")
c.Set("home", "name", "Anna's house")
case 5:
c.Set("home", "name", "House")
case 6:
c.DeleteSection("cabin")
c.Set("open", "ifname", "Not-A-Name")
case 7:
c.Set("home", "header_protection_key", "yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk=")
for _, k := range []string{"s1", "s2", "s3", "s4"} {
c.Set("home", k, "12")
}
c.Set("home", "i1", "<b 0x01><r 2>")
c.Set("home", "disable_cookies", "on")
case 8:
tools = false
c.Set("home", "port", "4502")
}
cmds = nil
// as ucode's: each sync reads the settings anew, with who was kept down before
var before map[string]any
if raw, err := os.ReadFile(dir + "/run/netifd-blocked.json"); err == nil {
json.Unmarshal(raw, &before)
}
m := model.Load(c, before)
changed := r.Netifd(m, tools, caps)
fail := func(what string, got, exp any) {
t.Errorf("step %d (%s): %s\n go: %v\n ucode: %v", i, want.What, what, got, exp)
}
if changed != want.Changed {
fail("whether it changed", changed, want.Changed)
}
if got, _ := os.ReadFile(dir + "/network"); string(got) != want.Network {
g, w := strings.Split(string(got), "\n"), strings.Split(want.Network, "\n")
for n := 0; n < len(g) || n < len(w); n++ {
if n >= len(g) || n >= len(w) || g[n] != w[n] {
a, b := "(the end)", "(the end)"
if n < len(g) {
a = g[n]
}
if n < len(w) {
b = w[n]
}
fail("the network configuration, line "+string(rune('0'+(n+1)/10))+string(rune('0'+(n+1)%10)), a, b)
break
}
}
}
var blocked map[string]any
if raw, err := os.ReadFile(dir + "/run/netifd-blocked.json"); err == nil {
json.Unmarshal(raw, &blocked)
}
if !reflect.DeepEqual(blocked, want.Blocked) {
fail("who is kept down", blocked, want.Blocked)
}
kept := map[string]any{}
for id, srv := range m.Servers {
kept[id] = srv.Blocked
if why, is := srv.Blocked.(map[string]any); is {
kept[id] = why
}
}
if !reflect.DeepEqual(kept, want.ModelBlocked) {
fail("who the model holds as kept down", kept, want.ModelBlocked)
}
if strings.Join(cmds, "; ") != strings.Join(want.Cmds, "; ") {
fail("what was run", cmds, want.Cmds)
}
var all map[string]map[string]any
if raw, err := os.ReadFile(dir + "/run/iface-intent.json"); err == nil {
json.Unmarshal(raw, &all)
}
intents := map[string]map[string]any{}
for k, v := range all {
intents[k] = map[string]any{"reason": v["reason"], "name": v["name"]}
}
if !reflect.DeepEqual(intents, want.Intents) {
fail("why each tunnel goes down", intents, want.Intents)
}
if _, err := os.Stat(dir + "/run/netifd-no-awg"); (err == nil) != want.NoAWG {
fail("whether missing tools were told", err == nil, want.NoAWG)
}
}
left, _ := os.ReadDir(dir)
for _, e := range left {
if strings.HasPrefix(e.Name(), ".") {
t.Errorf("%s stays beside the configuration", e.Name())
}
}
}
+90
View File
@@ -0,0 +1,90 @@
package apply
import (
"fmt"
"os"
"regexp"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/health"
"github.com/tickcount/luci-app-dawnline/backend/model"
)
// Our own answers leave by the main table: the portal's (TCP) and each tunnel's (UDP). Proxies on the
// router mark the router's own traffic in an output route chain, which diverts replies to outside clients
// into lo; a later output chain of our own (table inet dawnline_reply) clears exactly those marks on
// packets from our listening ports (dawnline.fw sync_reply_routes).
const replyTable = "dawnline_reply"
// Older versions kept answers out of proxies with `ip rule` at priority 90; it never matched.
var oldReplyRule = regexp.MustCompile(`(iif lo )?ipproto (tcp|udp) sport ([0-9]+) lookup main`)
// Diverts is the policy rules that send marked traffic into a table with `local default`, the way
// transparent proxies capture the router's own traffic.
func (r *Router) Diverts() []health.Divert {
return health.ProxyDiverts(func(fam string) string {
out, _ := r.Exec("ip", fam, "rule", "show")
return out
}, func(fam, name string) string {
out, _ := r.Exec("ip", fam, "route", "show", "table", name)
return out
})
}
func (r *Router) replyLoaded() bool {
_, code := r.Exec("nft", "list", "table", "inet", replyTable)
return code == 0
}
// ReplyOK tells whether answers from our ports are kept out of these diverts right now (the watchdog's question).
func (r *Router) ReplyOK(m *model.Model, diverts []health.Divert) bool {
s := health.ReplyScript(m, diverts)
if s == "" {
return true
}
was, _ := os.ReadFile(r.RunDir + "/reply.nft")
return s == string(was) && r.replyLoaded()
}
// ReplyRoutes keeps the reply table in step with our ports and the proxies' diverts (an apply, and every
// tick: a proxy may start after Dawnline). Cheap when nothing changed.
func (r *Router) ReplyRoutes(m *model.Model) {
sig := r.RunDir + "/reply.nft"
for _, fam := range []string{"-4", "-6"} {
out, _ := r.Exec("ip", fam, "rule", "show", "priority", "90")
for _, line := range strings.Split(out, "\n") {
if mm := oldReplyRule.FindStringSubmatch(line); mm != nil {
args := []string{fam, "rule", "del", "priority", "90"}
if mm[1] != "" {
args = append(args, "iif", "lo")
}
r.Exec("ip", append(args, "ipproto", mm[2], "sport", mm[3], "lookup", "main")...)
}
}
}
s := health.ReplyScript(m, r.Diverts())
if s == "" {
if r.replyLoaded() {
r.say("info", "nftables", "table inet "+replyTable+": removed (no proxy to keep Dawnline's answers out of)")
r.Exec("nft", "delete", "table", "inet", replyTable)
}
os.Remove(sig)
return
}
if was, _ := os.ReadFile(sig); s == string(was) && r.replyLoaded() {
r.say("debug", "nftables", "table inet "+replyTable+": unchanged")
return
}
if err := r.load("table inet " + replyTable + "\ndelete table inet " + replyTable + "\n" + s); err != "" {
os.Remove(sig)
r.say("error", "nftables", "table inet "+replyTable+": loading failed · "+err)
return
}
r.say("info", "nftables", fmt.Sprintf("table inet %s: loaded · %d lines (Dawnline's own answers kept out of proxies)", replyTable, strings.Count(s, "\n")+1))
for _, l := range strings.Split(strings.TrimRight(s, "\n"), "\n") {
r.say("debug", "nftables", "table inet "+replyTable+": "+l)
}
os.MkdirAll(r.RunDir, 0o755)
os.WriteFile(sig, []byte(s), 0o644)
}
+108
View File
@@ -0,0 +1,108 @@
package apply
import (
"os"
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// The reply table follows the proxies: loaded when one diverts the router's own traffic, left alone while
// nothing changed, taken away with the proxy; the rules older versions left are removed.
func TestReplyRoutes(t *testing.T) {
text, _ := os.ReadFile("../access/testdata/access.uci")
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
m := model.Load(c, nil)
dir := t.TempDir()
proxy, loaded, refuse := true, false, false
var ran []string
var script string
r := &Router{RunDir: dir, Exec: func(name string, args ...string) (string, int) {
line := name + " " + strings.Join(args, " ")
switch {
case line == "ip -4 rule show priority 90":
return "90:\tfrom all iif lo ipproto udp sport 51820 lookup main\n", 0
case line == "ip -6 rule show priority 90":
return "90:\tfrom all ipproto tcp sport 8443 lookup main\n", 0
case line == "ip -4 rule show" && proxy:
return "0:\tfrom all lookup local\n1000:\tfrom all fwmark 0x1/0xff lookup 100\n32766:\tfrom all lookup main\n", 0
case line == "ip -4 route show table 100":
return "local default dev lo scope host\n", 0
case strings.HasPrefix(line, "ip ") && strings.Contains(line, " show"):
return "", 0
case line == "nft list table inet dawnline_reply":
if loaded {
return "", 0
}
return "", 1
case name == "nft" && args[0] == "-f":
raw, _ := os.ReadFile(args[1])
script = string(raw)
if refuse {
return args[1] + ":1:1-5: Error: a made-up failure", 1
}
loaded = true
return "", 0
case line == "nft delete table inet dawnline_reply":
loaded = false
}
ran = append(ran, line)
return "", 0
}}
sig := func() string { raw, _ := os.ReadFile(dir + "/reply.nft"); return string(raw) }
r.ReplyRoutes(m)
if want := "ip -4 rule del priority 90 iif lo ipproto udp sport 51820 lookup main; ip -6 rule del priority 90 ipproto tcp sport 8443 lookup main"; strings.Join(ran, "; ") != want {
t.Errorf("the old rules: %v", ran)
}
if !loaded || !strings.HasPrefix(script, "table inet dawnline_reply\ndelete table inet dawnline_reply\ntable inet dawnline_reply {\n") ||
!strings.Contains(script, "tcp sport { 4500, 4501 } meta mark & 0xff == 0x1 meta mark set meta mark & 0xffffff00\n") ||
!strings.Contains(script, "udp sport { 4500, 4600, 4700 } meta mark & 0xff == 0x1") {
t.Errorf("the table was not loaded as it should be:\n%s", script)
}
if sig() == "" || !strings.HasSuffix(script, sig()) || !r.ReplyOK(m, r.Diverts()) {
t.Error("what was loaded is not remembered")
}
script = ""
r.ReplyRoutes(m)
if script != "" {
t.Error("loaded again with nothing changed")
}
// the table went (fw4 restarted, say): loaded again
loaded = false
if r.ReplyOK(m, r.Diverts()) {
t.Error("fine without the table")
}
r.ReplyRoutes(m)
if !loaded {
t.Error("not loaded again")
}
// a load that fails forgets what was loaded, so the next tick tries again
c.Set("home", "port", "4555")
m = model.Load(c, nil)
refuse = true
r.ReplyRoutes(m)
if sig() != "" {
t.Error("a failed load is remembered")
}
refuse = false
r.ReplyRoutes(m)
if !strings.Contains(sig(), "4555") {
t.Error("not loaded after the failure")
}
// the proxy is gone: so is the table
proxy = false
r.ReplyRoutes(m)
if loaded || sig() != "" || !r.ReplyOK(m, r.Diverts()) {
t.Error("the table stays without a proxy")
}
left, _ := os.ReadDir(dir)
for _, e := range left {
t.Errorf("%s stays in the state directory", e.Name())
}
}
+198
View File
@@ -0,0 +1,198 @@
package apply
import (
"encoding/json"
"os"
"regexp"
"strconv"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// The plain resolver for people with Home-only access (dawnline.resolver): real addresses from the WAN's
// DNS, home names from the LAN's. Their internet does not use the tunnel, so fake-IP answers would break
// it. It runs inside the netns while that exists, otherwise on the tunnel interfaces; procd runs dnsmasq
// through /usr/libexec/dawnline/resolver, which reads where from the configuration's first line.
// ResolverPort is the port on the tunnel addresses while the resolver runs on the router itself.
const ResolverPort = 5354
var (
dotted = regexp.MustCompile(`^[0-9.]+$`)
digits = regexp.MustCompile(`^[0-9]+$`)
netifdOK = regexp.MustCompile(`^[A-Za-z0-9_.-]+$`)
)
// Target is where the resolver runs: "ns" (inside the netns, on Address) or "root" (on the tunnels).
type Target struct {
Mode string
Address string
Port int
}
func (r *Router) varEtc() string {
if r.VarEtc != "" {
return r.VarEtc
}
return "/var/etc"
}
func (r *Router) resolverPid() string {
if r.ResolverPid != "" {
return r.ResolverPid
}
return "/var/run/dawnline-dns.pid"
}
// ResolverTarget is where Home-only devices' DNS goes now: inside the netns while it is up, otherwise on
// the tunnel addresses (dawnline.loop resolver_target).
func (r *Router) ResolverTarget() Target {
if active, _, _, transit := r.likeHome(); active && transit != "" {
return Target{Mode: "ns", Address: transit, Port: 53}
}
return Target{Mode: "root", Port: ResolverPort}
}
// wanDNS is the DNS servers the WAN was given (IPv4), else two public ones.
func (r *Router) wanDNS() []string {
var out []string
snap := r.Snapshot()
wan := ""
if d := snap.Devices[snap.WAN]; d != nil {
wan = d.Logical
}
for _, name := range uniq([]string{wan, "wan", "wan6"}) {
if name == "" || !netifdOK.MatchString(name) {
continue
}
said, _ := r.Exec("ubus", "call", "network.interface."+name, "status")
var st struct {
DNS []string `json:"dns-server"`
}
if json.Unmarshal([]byte(said), &st) != nil {
continue
}
for _, a := range st.DNS {
if dotted.MatchString(a) {
out = append(out, a)
}
}
}
if len(out) == 0 {
return []string{"1.1.1.1", "8.8.8.8"}
}
return uniq(out)
}
// lanDomains is the local domain of the LAN's DNS (dnsmasq `domain`) and Dawnline's own for device names.
func (r *Router) lanDomains(m *model.Model) []string {
var out []string
if raw, err := os.ReadFile(r.DHCP); err == nil {
if c, err := uci.Parse(string(raw)); err == nil {
for _, s := range c.OfType("dnsmasq") {
if d, _ := s.Get("domain"); d != "" {
out = append(out, d)
break
}
}
}
}
if m.Settings.Domain != "" {
out = append(out, m.Settings.Domain)
}
return uniq(out)
}
// ResolverRunning tells whether the resolver's process is alive.
func (r *Router) ResolverRunning() bool {
raw, _ := os.ReadFile(r.resolverPid())
pid := strings.Trim(string(raw), " \t\r\n\v\f")
if !digits.MatchString(pid) {
return false
}
_, err := os.Stat("/proc/" + pid)
return err == nil
}
// Resolver writes the resolver's configuration for target, with home names from lan (the LAN's DNS, "":
// none); true when it changed (the running one is killed, and procd starts it again from the new file).
func (r *Router) Resolver(m *model.Model, target Target, lan string) bool {
etc := r.varEtc()
os.MkdirAll(etc, 0o755)
conf, resolv := etc+"/dawnline-dns.conf", etc+"/dawnline-dns.resolv"
// servers that are on: with none, the resolver idles on loopback
var ifs []string
for _, id := range m.ServerIDs() {
if s := m.Servers[id]; s.Enabled && tunnelOK(s) {
ifs = append(ifs, s.Ifname)
}
}
// the paths dnsmasq is told are the router's own, wherever a test keeps the files
lines := []string{"# mode=" + target.Mode, "# Generated by Dawnline: plain DNS for Home-only tunnel clients.", "port=" + strconv.Itoa(target.Port),
"no-hosts", "bogus-priv", "cache-size=1000", "pid-file=/var/run/dawnline-dns.pid", "user=dnsmasq", "group=dnsmasq"}
upstream := r.wanDNS()
if target.Mode == "ns" {
lines = append(lines, "listen-address="+target.Address, "bind-interfaces", "no-resolv")
// upstream from the transit address: forwarded through the router to the WAN
for _, a := range upstream {
lines = append(lines, "server="+a+"@"+target.Address)
}
} else {
// no tunnel on: loopback only (without any interface= line dnsmasq would answer everywhere)
lines = append(lines, "bind-dynamic")
if len(ifs) == 0 {
lines = append(lines, "listen-address=127.0.0.1")
}
for _, i := range ifs {
lines = append(lines, "interface="+i)
}
lines = append(lines, "resolv-file=/var/etc/dawnline-dns.resolv")
body := ""
for _, a := range upstream {
body += "nameserver " + a + "\n"
}
if was, _ := os.ReadFile(resolv); string(was) != body {
os.WriteFile(resolv, []byte(body), 0o644)
os.Chmod(resolv, 0o644)
}
}
if lan != "" {
for _, d := range r.lanDomains(m) {
lines = append(lines, "server=/"+d+"/"+lan)
}
}
text := strings.Join(lines, "\n") + "\n"
where := "loopback only (no tunnel on)"
if target.Mode == "ns" {
where = target.Address + ":" + strconv.Itoa(target.Port) + " in the netns"
} else if len(ifs) > 0 {
where = "port " + strconv.Itoa(target.Port) + " on " + strings.Join(ifs, ", ")
}
if was, _ := os.ReadFile(conf); string(was) == text {
r.say("debug", "dns", "resolver for Home-only devices: unchanged · "+where)
return false
}
os.WriteFile(conf, []byte(text), 0o644)
os.Chmod(conf, 0o644)
home := ""
if lan != "" {
home = " · home names via " + lan
}
r.say("info", "dns", "resolver for Home-only devices: "+conf+" written · "+where+" · upstream "+strings.Join(upstream, " ")+home+"; restarting it")
for _, l := range lines {
r.say("debug", "dns", "dnsmasq: "+l)
}
raw, _ := os.ReadFile(r.resolverPid())
if pid := strings.Trim(string(raw), " \t\r\n\v\f"); digits.MatchString(pid) {
r.Exec("kill", pid)
}
return true
}
// SyncResolver writes the resolver's configuration where it should run now; true when it changed.
func (r *Router) SyncResolver(m *model.Model) bool {
_, _, known, _ := r.likeHome()
return r.Resolver(m, r.ResolverTarget(), r.lanDNS(r.Snapshot(), known))
}
+94
View File
@@ -0,0 +1,94 @@
package apply
import (
"encoding/json"
"os"
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// The Home-only resolver's configuration in each place it may run: the files ucode's own sync writes, and
// whether it changed (testdata/resolver.uc).
func TestResolverAsUcode(t *testing.T) {
raw, err := os.ReadFile("testdata/resolver.json")
if err != nil {
t.Fatal(err)
}
var trace struct {
DHCP string `json:"dhcp"`
Steps []struct {
What string `json:"what"`
Target struct {
Mode string `json:"mode"`
Address string `json:"address"`
Port int `json:"port"`
} `json:"target"`
Lan string `json:"lan"`
Changed bool `json:"changed"`
Conf string `json:"conf"`
Resolv string `json:"resolv"`
} `json:"steps"`
}
if err := json.Unmarshal(raw, &trace); err != nil {
t.Fatal(err)
}
if len(trace.Steps) != 6 {
t.Fatalf("%d steps", len(trace.Steps))
}
dir := t.TempDir()
os.WriteFile(dir+"/dhcp", []byte(trace.DHCP), 0o644)
wanDNS := true
var killed []string
r := &Router{RunDir: dir + "/run", VarEtc: dir + "/etc", ResolverPid: dir + "/pid", DHCP: dir + "/dhcp",
Snapshot: func() *netview.Snapshot { return &netview.Snapshot{Devices: map[string]*netview.Device{}} },
Exec: func(name string, args ...string) (string, int) {
line := name + " " + strings.Join(args, " ")
switch {
case name == "kill":
killed = append(killed, args...)
case !wanDNS:
case line == "ubus call network.interface.wan status":
return `{ "dns-server": [ "9.9.9.9", "2001:db8::1", "149.112.112.112" ] }`, 0
case line == "ubus call network.interface.wan6 status":
return `{ "dns-server": [ "9.9.9.9" ] }`, 0
}
return "", 0
}}
text, _ := os.ReadFile("../access/testdata/access.uci")
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
for i, want := range trace.Steps {
switch i {
case 3:
c.Set("cabin", "enabled", "0")
os.WriteFile(dir+"/pid", []byte("4242\n"), 0o644)
case 4:
c.Set("home", "enabled", "0")
c.Set("open", "enabled", "0")
os.Remove(dir + "/pid")
case 5:
wanDNS = false
}
killed = nil
changed := r.Resolver(model.Load(c, nil), Target{Mode: want.Target.Mode, Address: want.Target.Address, Port: want.Target.Port}, want.Lan)
if changed != want.Changed {
t.Errorf("step %d (%s): changed %v, ucode %v", i, want.What, changed, want.Changed)
}
if got, _ := os.ReadFile(dir + "/etc/dawnline-dns.conf"); string(got) != want.Conf {
t.Errorf("step %d (%s): the configuration\n%s--- ucode:\n%s", i, want.What, got, want.Conf)
}
if got, _ := os.ReadFile(dir + "/etc/dawnline-dns.resolv"); string(got) != want.Resolv {
t.Errorf("step %d (%s): the upstream file\n%s--- ucode:\n%s", i, want.What, got, want.Resolv)
}
// the one that ran is stopped, so procd starts it from the new file
if stop := i == 3; stop != (strings.Join(killed, " ") == "4242") || (!stop && killed != nil) {
t.Errorf("step %d (%s): stopped %v", i, want.What, killed)
}
}
}
+127
View File
@@ -0,0 +1,127 @@
package apply
import (
"encoding/json"
"os"
"sort"
"github.com/tickcount/luci-app-dawnline/backend/health"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/servicelog"
)
// The networks behind people's routers, as the router's own routes: a packet for one leaves through the
// tunnel of the router it stands behind (the tunnel then picks the device by the peers' AllowedIPs).
//
// Reconciled, not patched: every route of ours carries one routing protocol number, so what the main table
// holds of them is read back and brought to what the settings and people's states want. Netifd takes a
// tunnel's routes away with its interface; the sync that follows the interface coming up puts them back.
// routeProto marks the routes Dawnline keeps (/etc/iproute2/rt_protos has no name for it).
const routeProto = "205"
// RoutesWanted is the networks that get a route, by the tunnel's interface, sorted: those behind the
// devices a tunnel was given (peers: each server's "ok"), so a route never leads into a tunnel that would
// drop the packet.
func RoutesWanted(m *model.Model, states, peers map[string]string, behind map[string][]string) map[string][]string {
want := map[string][]string{}
for _, id := range m.ServerIDs() {
srv := m.Servers[id]
if !srv.Enabled || !tunnelOK(srv) || peers[id] != "ok" {
continue
}
for _, d := range health.TunnelDevices(m, id, states) {
want[srv.Ifname] = append(want[srv.Ifname], behind[d.ID]...)
}
sort.Strings(want[srv.Ifname])
if len(want[srv.Ifname]) == 0 {
delete(want, srv.Ifname)
}
}
return want
}
// IPRoute is a route as `ip -j -4 route show` prints it.
type IPRoute struct {
Dst string `json:"dst"`
Dev string `json:"dev"`
}
// RouteCmds is the `ip` arguments that turn the routes of ours there are into the wanted ones: the missing
// ones first, then the ones nobody has any more. None when they are as wanted.
func RouteCmds(want map[string][]string, have []IPRoute) [][]string {
there, wanted := map[string]bool{}, map[string]bool{}
for _, x := range have {
there[x.Dev+" "+x.Dst] = true
}
var cmds [][]string
ifnames := make([]string, 0, len(want))
for ifname := range want {
ifnames = append(ifnames, ifname)
}
sort.Strings(ifnames)
for _, ifname := range ifnames {
for _, n := range want[ifname] {
if k := ifname + " " + n; !there[k] && !wanted[k] {
cmds = append(cmds, []string{"-4", "route", "replace", n, "dev", ifname, "proto", routeProto, "scope", "link"})
}
wanted[ifname+" "+n] = true
}
}
for _, x := range have {
if !wanted[x.Dev+" "+x.Dst] {
cmds = append(cmds, []string{"-4", "route", "del", x.Dst, "dev", x.Dev, "proto", routeProto})
}
}
return cmds
}
// routes keeps the routes to the networks behind routers as RoutesWanted says: "" when they are, else what
// `ip` said of the first that failed. states is whose devices were loaded, peers what each tunnel answered.
func (r *Router) routes(m *model.Model, states, peers map[string]string, behind map[string][]string) string {
out, code := r.Exec("ip", "-j", "-4", "route", "show", "proto", routeProto)
var have []IPRoute
if code == 0 {
json.Unmarshal([]byte(trimmed(out)), &have)
}
failed := ""
var added, removed []string
for _, args := range RouteCmds(RoutesWanted(m, states, peers, behind), have) {
said, code := r.Exec("ip", args...)
what := args[3] + " dev " + args[5]
switch {
case code != 0 && args[2] == "replace":
if failed == "" {
failed = what + ": " + trimmed(said)
}
case args[2] == "replace":
added = append(added, what)
case code == 0:
removed = append(removed, what)
}
}
if len(added) > 0 || len(removed) > 0 {
line := "networks behind routers:"
if len(added) > 0 {
line += " added " + someOf(added)
}
if len(removed) > 0 {
line += " removed " + someOf(removed)
}
r.say("info", "routes", line)
}
// once per distinct failure, as the access table's
path := r.RunDir + "/routes.err"
was, werr := os.ReadFile(path)
switch {
case failed == "" && werr == nil:
os.Remove(path)
case failed != "" && (werr != nil || string(was) != failed):
if r.Log != nil {
r.Log.Write("dawnline", "error", "couldn't set a route to a network behind a router", failed, "Routes", servicelog.Extra{})
}
os.MkdirAll(r.RunDir, 0o755)
os.WriteFile(path, []byte(failed), 0o644)
}
return failed
}
+191
View File
@@ -0,0 +1,191 @@
package apply
import (
"encoding/json"
"os"
"reflect"
"sort"
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/usage"
)
// routed is a router that keeps the routes it is given, besides what stand keeps.
type routed struct {
stand
routes map[string]bool
fail string
ran []string
}
func (s *routed) exec(name string, args ...string) (string, int) {
if name != "ip" {
return s.stand.exec(name, args...)
}
line := strings.Join(args, " ")
switch {
case line == "-j -4 route show proto "+routeProto:
var list []IPRoute
for k := range s.routes {
f := strings.Fields(k)
list = append(list, IPRoute{Dev: f[0], Dst: f[1]})
}
sort.Slice(list, func(i, j int) bool { return list[i].Dev+list[i].Dst < list[j].Dev+list[j].Dst })
raw, _ := json.Marshal(list)
return string(raw), 0
case strings.HasPrefix(line, "-4 route replace ") && strings.HasSuffix(line, " proto "+routeProto+" scope link"):
s.ran = append(s.ran, line)
if s.fail != "" && strings.Contains(line, s.fail) {
return "RTNETLINK answers: a made-up failure\n", 2
}
s.routes[args[5]+" "+args[3]] = true
case strings.HasPrefix(line, "-4 route del ") && strings.HasSuffix(line, " proto "+routeProto):
s.ran = append(s.ran, line)
delete(s.routes, args[5]+" "+args[3])
default:
return "unknown: ip " + line, 1
}
return "", 0
}
// Access's household on the golden's router, with two routers that have networks behind them and one
// whose network the router has itself.
func sites(t *testing.T) (*uci.Config, *netview.Snapshot) {
t.Helper()
text, err := os.ReadFile("../access/testdata/access.uci")
if err != nil {
t.Fatal(err)
}
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
c.Set("bphone", "kind", "router")
c.SetList("bphone", "behind_net", []string{"192.168.2.0/24", "172.20.0.0/16"})
c.Set("kphone", "kind", "router")
c.SetList("kphone", "behind_net", []string{"192.168.3.0/24"})
c.Set("hphone", "kind", "router")
c.SetList("hphone", "behind_net", []string{"192.168.50.0/24"})
raw, err := os.ReadFile("../netview/testdata/trace.json")
if err != nil {
t.Fatal(err)
}
var router struct {
Routes json.RawMessage `json:"routes"`
Addrs json.RawMessage `json:"addrs"`
Links map[string]netview.Link `json:"links"`
}
if err := json.Unmarshal(raw, &router); err != nil {
t.Fatal(err)
}
return c, netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return router.Addrs },
Links: func() map[string]netview.Link { return router.Links }})
}
func TestRoutesFollowTheTunnels(t *testing.T) {
c, snap := sites(t)
dir := t.TempDir()
s := &routed{stand: stand{dir: dir, refuse: map[string]bool{}, down: map[string]bool{}, confs: map[string]string{}}, routes: map[string]bool{}}
r := &Router{RunDir: dir, HostsFile: dir + "/hosts/dawnline", DHCP: dir + "/no-dhcp", Exec: s.exec, Snapshot: func() *netview.Snapshot { return snap }}
words := i18n.Use("", "")
have := func() []string {
out := []string{}
for k := range s.routes {
out = append(out, k)
}
sort.Strings(out)
return out
}
sync := func() Result {
s.ran = nil
return r.Sync(model.Load(c, nil), &usage.Usage{}, 1700000000, false, words)
}
// each router's networks leave through its own tunnel; what the router has itself gets no route
res := sync()
all := []string{"dawn0 172.20.0.0/16", "dawn0 192.168.2.0/24", "dawn2 192.168.3.0/24"}
if !reflect.DeepEqual(have(), all) || res.Routes != "" || res.Warning != "" {
t.Fatalf("routes %v, %q, %q", have(), res.Routes, res.Warning)
}
// the tunnel is told whose they are, and nobody else gets more than an address
if conf := s.confs["dawn0"]; !strings.Contains(conf, "AllowedIPs = 10.77.0.5/32, 192.168.2.0/24, 172.20.0.0/16\n") ||
strings.Count(conf, "/32\n") != strings.Count(conf, "AllowedIPs = ")-1 {
t.Errorf("dawn0's devices:\n%s", conf)
}
if conf := s.confs["dawn1"]; strings.Contains(conf, "192.168.50.0/24") {
t.Errorf("dawn1 was given a network the router has itself:\n%s", conf)
}
// nothing moved: nothing is run
if sync(); len(s.ran) != 0 {
t.Errorf("ran %v with nothing to do", s.ran)
}
// its owner paused: the route goes with the device
c.Set("boris", "paused", "1")
if sync(); !reflect.DeepEqual(have(), []string{"dawn2 192.168.3.0/24"}) {
t.Errorf("with Boris paused: %v", have())
}
c.Delete("boris", "paused")
// a tunnel that refuses its devices, and one that is down: no route leads into either
s.refuse["dawn0"], s.down["dawn2"] = true, true
if sync(); len(have()) != 0 {
t.Errorf("into tunnels that hold nobody: %v", have())
}
s.refuse["dawn0"], s.down["dawn2"] = false, false
// a route that will not go in is told, once in the log's file, and the others still go in
s.fail = "192.168.2.0/24"
res = sync()
if !reflect.DeepEqual(have(), []string{"dawn0 172.20.0.0/16", "dawn2 192.168.3.0/24"}) || !strings.Contains(res.Routes, "192.168.2.0/24 dev dawn0: RTNETLINK answers: a made-up failure") ||
!strings.Contains(res.Warning, "192.168.2.0/24 dev dawn0") {
t.Errorf("a route refused: %v, %q, %q", have(), res.Routes, res.Warning)
}
if kept, _ := os.ReadFile(dir + "/routes.err"); string(kept) != res.Routes {
t.Errorf("kept %q", kept)
}
s.fail = ""
if res = sync(); !reflect.DeepEqual(have(), all) || res.Routes != "" {
t.Errorf("after the failure: %v, %q", have(), res.Routes)
}
if _, err := os.Stat(dir + "/routes.err"); err == nil {
t.Error("the failure is still kept")
}
// a network taken off, a router that is one no more, and a route of ours nobody wants
c.SetList("bphone", "behind_net", []string{"172.20.0.0/16"})
c.Set("kphone", "kind", "phone")
s.routes["dawn9 192.168.99.0/24"] = true
if sync(); !reflect.DeepEqual(have(), []string{"dawn0 172.20.0.0/16"}) {
t.Errorf("after the changes: %v", have())
}
// a router that waits for the admin's answer: no device of the tunnel, no route to what it asks for
c.Set("kphone", "kind", "router")
c.Set("kphone", "waiting", "1")
if sync(); !reflect.DeepEqual(have(), []string{"dawn0 172.20.0.0/16"}) || strings.Contains(s.confs["dawn2"], "10.79.0.2/32") || !strings.Contains(s.confs["dawn2"], "10.79.0.3/32") {
t.Errorf("with Kira's router waiting: %v\n%s", have(), s.confs["dawn2"])
}
// the plan of a sync names them too
p := r.Plan(model.Load(c, nil), &usage.Usage{}, 1700000000)
if !reflect.DeepEqual(p.Routes, map[string][]string{"dawn0": {"172.20.0.0/16"}}) {
t.Errorf("the plan's routes: %v", p.Routes)
}
}
func TestRouteCommands(t *testing.T) {
want := map[string][]string{"dawn1": {"192.168.3.0/24"}, "dawn0": {"172.20.0.0/16", "192.168.2.0/24"}}
have := []IPRoute{{Dst: "192.168.2.0/24", Dev: "dawn0"}, {Dst: "192.168.2.0/24", Dev: "dawn1"}, {Dst: "10.9.0.0/24", Dev: "dawn7"}}
got := RouteCmds(want, have)
exp := [][]string{
{"-4", "route", "replace", "172.20.0.0/16", "dev", "dawn0", "proto", routeProto, "scope", "link"},
{"-4", "route", "replace", "192.168.3.0/24", "dev", "dawn1", "proto", routeProto, "scope", "link"},
{"-4", "route", "del", "192.168.2.0/24", "dev", "dawn1", "proto", routeProto},
{"-4", "route", "del", "10.9.0.0/24", "dev", "dawn7", "proto", routeProto},
}
if !reflect.DeepEqual(got, exp) {
t.Errorf("commands:\n%v\nwant\n%v", got, exp)
}
if got := RouteCmds(want, []IPRoute{{Dst: "172.20.0.0/16", Dev: "dawn0"}, {Dst: "192.168.2.0/24", Dev: "dawn0"}, {Dst: "192.168.3.0/24", Dev: "dawn1"}}); len(got) != 0 {
t.Errorf("as wanted, yet %v", got)
}
}
+545
View File
@@ -0,0 +1,545 @@
// Package apply puts the settings into effect on the router, as dawnline.apply does. This file is the
// runtime sync: the access table's sets, the devices into the tunnels (AmneziaWG peers) and the hostnames.
// Cheap, and safe to call often.
//
// Fail closed: without the table the firewall would let every loaded device reach everything, so while it
// is missing no device is loaded at all. An older table that survived a failed rebuild keeps limiting them
// with the sets it has, and the devices still follow people's states (a revoked one leaves).
//
// Held to ucode on a router whose `nft` and `awg` are stand-ins that keep what they are given
// (testdata/trace.uc, scripts/ucode-golden.sh): the scripts loaded, the peers of each tunnel, the
// hostnames and what is answered, through a table that is not there yet, one that will not load and a
// tunnel that refuses its devices.
package apply
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/health"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/nft"
"github.com/tickcount/luci-app-dawnline/backend/servicelog"
"github.com/tickcount/luci-app-dawnline/backend/state"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/usage"
"github.com/tickcount/luci-app-dawnline/backend/view"
)
// Router is the router a sync works on.
type Router struct {
// RunDir is the state directory (locks, what was last loaded); HostsFile where dnsmasq reads extra names;
// Network the router's network configuration, Firewall its firewall's.
RunDir, HostsFile, Network, Firewall string
// VarEtc is where generated configurations go ("": /var/etc); ResolverPid the Home-only resolver's pid file.
VarEtc, ResolverPid string
// Remember keeps the LAN address the home-like path chose (settings.lan_address), so it is the same after a restart.
Remember func(address string)
// dry: nothing outside the files this router is given is written (ApplyPlan).
dry bool
// SysNet is where the kernel shows interfaces ("": /sys/class/net), NetnsDir where named network
// namespaces are ("": /var/run/netns), Leases dnsmasq's leases ("": /tmp/dhcp.leases).
SysNet, NetnsDir, Leases string
// Exec runs a command: what it said (both streams) and its exit code.
Exec func(name string, args ...string) (out string, code int)
// Snapshot is the router's interfaces; DHCP the path of its DHCP settings.
Snapshot func() *netview.Snapshot
DHCP string
Log *servicelog.Log
}
// Result is what a sync came to: each server's devices ("ok", "down", or what awg said), people's states,
// the table's error ("" when it loaded), the routes' ("" when they are as wanted) and a sentence for the
// admin when it did not fully take.
type Result struct {
Peers map[string]string
States map[string]string
Table string
Routes string
Warning string
}
func (r *Router) say(level, src, line string) {
if r.Log != nil {
r.Log.Say(level, src, line)
}
}
// someOf names up to ten of a list, and how many more there are.
func someOf(list []string) string {
if len(list) <= 10 {
return strings.Join(list, ", ")
}
return strings.Join(list[:10], ", ") + " and " + strconv.Itoa(len(list)-10) + " more"
}
func minus(a, b []string) []string {
in := map[string]bool{}
for _, x := range b {
in[x] = true
}
var out []string
for _, x := range a {
if !in[x] {
out = append(out, x)
}
}
return out
}
func uniq(list []string) []string {
out := []string{}
seen := map[string]bool{}
for _, x := range list {
if !seen[x] {
seen[x] = true
out = append(out, x)
}
}
return out
}
// tunnelOK tells a server that runs one of Dawnline's own tunnels.
func tunnelOK(srv *model.Server) bool {
_, ok := ipnet.Subnet(srv.Subnet)
return ok && srv.PrivateKey != "" && srv.Blocked == nil && model.ValidIfname(srv.Ifname)
}
// likeHome is the home-like path as it stands (dawnline.loop status): whether it is up, its address on the
// LAN, the LAN's DNS it found and its address on the inner link.
func (r *Router) likeHome() (active bool, address, dns, transit string) {
var s struct {
Plan *struct {
Address string `json:"address"`
DNS string `json:"dns"`
TxNS string `json:"tx_ns"`
} `json:"plan"`
}
files.ReadJSON(r.RunDir+"/loop.json", &s)
if s.Plan == nil {
return false, "", "", ""
}
_, e1 := os.Stat("/sys/class/net/dlx0")
_, e2 := os.Stat("/sys/class/net/dll0")
return e1 == nil && e2 == nil, s.Plan.Address, s.Plan.DNS, s.Plan.TxNS
}
var dnsOption = regexp.MustCompile(`^(option:dns-server|6),([0-9.]+)`)
// lanDNS is the DNS the LAN gets from DHCP (option 6), else the router on the LAN (dawnline.loop lan_dns);
// known is what the home-like path found already.
func (r *Router) lanDNS(snap *netview.Snapshot, known string) string {
out := known
if lan := snap.Devices[snap.LAN]; out == "" && lan != nil && len(lan.Addrs) > 0 {
out = strings.SplitN(lan.Addrs[0], "/", 2)[0]
if _, ok := ipnet.Subnet(lan.Addrs[0]); ok {
if raw, err := os.ReadFile(r.DHCP); err == nil {
if c, err := uci.Parse(string(raw)); err == nil {
found := ""
for _, s := range c.OfType("dhcp") {
iface, _ := s.Get("interface")
if ignore, _ := s.Get("ignore"); iface != lan.Logical || ignore == "1" {
continue
}
v := s.Values["dhcp_option"]
opts := v.List
if !v.IsList && v.Str != "" {
opts = []string{v.Str}
}
for _, o := range opts {
if m := dnsOption.FindStringSubmatch(o); m != nil && found == "" {
found = m[2]
}
}
}
if found != "" {
out = found
}
}
}
}
}
return out
}
// Ctx is what the table is made from besides the settings, read from the router (dawnline.nft router_ctx).
func (r *Router) Ctx(snap *netview.Snapshot) nft.Ctx {
active, address, dns, transit := r.likeHome()
ctx := nft.Ctx{Snap: snap, ResolverPort: 5354}
if active {
ctx.DNSAddr = address
if transit != "" {
ctx.ResolverNS, ctx.ResolverPort = transit, 53
}
}
ctx.LanDNS = r.lanDNS(snap, dns)
return ctx
}
// load runs an nft script (one transaction): "" when it loaded, else what nft said.
func (r *Router) load(script string) string {
os.MkdirAll(r.RunDir, 0o755)
// a file of its own: two writers must never load each other's script
var b [4]byte
rand.Read(b[:])
f := r.RunDir + "/nft." + hex.EncodeToString(b[:]) + ".rules"
if os.WriteFile(f, []byte(script), 0o600) != nil {
return "couldn't write " + f
}
out, code := r.Exec("nft", "-f", f)
os.Remove(f)
if code == 0 {
return ""
}
// without the random file name, so the same failure reads the same every time
if said := strings.ReplaceAll(strings.Trim(out, " \t\r\n\v\f"), f, "rules"); said != "" {
return said
}
return fmt.Sprintf("nft exited with %d", code)
}
// Present tells whether the table is loaded.
func (r *Router) Present() bool {
_, code := r.Exec("nft", "list", "table", "inet", "dawnline")
return code == 0
}
// logSets tells the sets in syslog: each that changed, or that none did; remembered for the next time.
func (r *Router) logSets(sets nft.Sets, how string) {
path := r.RunDir + "/nft-sets.json"
var was map[string][]string
first := !files.ReadJSON(path, &was) || was == nil
now := map[string][]string{}
var diff, counts []string
for _, name := range nft.Names {
a, b := uniq(was[name]), uniq(sets[name])
now[name] = b
counts = append(counts, name+" "+strconv.Itoa(len(b)))
add, del := minus(b, a), minus(a, b)
if len(add) > 0 || len(del) > 0 {
line := fmt.Sprintf("%s %d → %d", name, len(a), len(b))
if len(add) > 0 {
line += ": +" + someOf(add)
}
if len(del) > 0 {
if len(add) > 0 {
line += " -" + someOf(del)
} else {
line += ": -" + someOf(del)
}
}
diff = append(diff, line)
}
}
files.WriteJSON(path, now, 0o600)
switch {
// the first load since the router started: how many in each set (the members at debug)
case first:
r.say("info", "nftables", "table inet dawnline: "+how+" · "+strings.Join(counts, ", "))
for _, n := range nft.Names {
if len(now[n]) > 0 {
r.say("debug", "nftables", "set "+n+": "+strings.Join(now[n], " "))
}
}
case len(diff) > 0:
r.say("info", "nftables", "table inet dawnline: "+how+" · "+strings.Join(diff, "; "))
default:
r.say("debug", "nftables", "table inet dawnline: "+how+" · sets unchanged ("+strings.Join(counts, ", ")+")")
}
}
// rebuild replaces the whole table: "" or the error.
//
// members: a runtime sync, where only who is in the sets may have moved. The table is replaced whole all
// the same: to add an element to a set that exists, nft first reads the router's whole ruleset, every
// other program's sets with it (0.9 s on an MT7981 beside a proxy's lists), where replacing our own table
// reads nothing (0.06 s). The line in the log is then the sets', and the rebuild's own is for a debug log.
func (r *Router) rebuild(m *model.Model, sets nft.Sets, snap *netview.Snapshot, members bool) string {
table := nft.Render(m, sets, r.Ctx(snap))
if err := r.load(nft.Rebuild(table)); err != "" {
r.say("error", "nftables", "table inet dawnline: rebuild failed · "+err)
return err
}
chains, nsets, rules := nft.Shape(table)
level, how := "info", "sets loaded with the table"
if members {
level, how = "debug", "sets updated"
}
r.say(level, "nftables", fmt.Sprintf("table inet dawnline: rebuilt · %d chains, %d sets, %d rules", chains, nsets, rules))
r.logSets(sets, how)
return ""
}
// table has the table in place, replaced whole every time: with `full` after a change of its shape,
// otherwise for who is in its sets. A failure is told in the service log once per distinct error.
func (r *Router) table(m *model.Model, sets nft.Sets, snap *netview.Snapshot, full bool) string {
err := r.rebuild(m, sets, snap, !full)
path := r.RunDir + "/nft.err"
was, werr := os.ReadFile(path)
if err == "" {
if werr == nil {
os.Remove(path)
}
return ""
}
if werr != nil || string(was) != err {
msg := "couldn't load - no device is loaded into the tunnels until they do"
if r.Present() {
msg = "couldn't be updated - the previous rules stay, and changes to access wait until they load"
}
if r.Log != nil {
r.Log.Write("dawnline", "error", msg, err, "Access rules", servicelog.Extra{})
}
os.MkdirAll(r.RunDir, 0o755)
os.WriteFile(path, []byte(err), 0o644)
}
return err
}
// PeersConf is what a tunnel is given to hold: its own key and port, then each device that belongs there
// with the networks behind it (behind: by the device's id), and who they are for a log line
// ("Person · Device (address)").
func PeersConf(m *model.Model, srv *model.Server, states map[string]string, behind map[string][]string) (conf string, who []string) {
lines := []string{"[Interface]", "PrivateKey = " + srv.PrivateKey, "ListenPort = " + strconv.Itoa(srv.Port), ""}
who = []string{}
for _, d := range health.TunnelDevices(m, srv.ID, states) {
lines = append(lines, "[Peer]", "PublicKey = "+d.PublicKey)
if d.PresharedKey != "" {
lines = append(lines, "PresharedKey = "+d.PresharedKey)
}
// a router brings the networks behind it: the tunnel takes their packets from it and sends theirs to it
lines = append(lines, "AllowedIPs = "+strings.Join(append([]string{d.Address + "/32"}, behind[d.ID]...), ", "), "")
name := d.Person
if p := m.People[d.Person]; p != nil {
name = p.Name
}
who = append(who, name+" · "+d.Name+" ("+strings.Join(append([]string{d.Address}, behind[d.ID]...), ", ")+")")
}
return strings.Join(lines, "\n"), who
}
// peers pushes every server's devices to the kernel without restarting its tunnel.
func (r *Router) peers(m *model.Model, states map[string]string, behind map[string][]string) map[string]string {
os.MkdirAll(r.RunDir, 0o755)
res := map[string]string{}
path := r.RunDir + "/peers-seen.json"
seen := map[string][]string{}
files.ReadJSON(path, &seen)
now := map[string][]string{}
for _, id := range m.ServerIDs() {
srv := m.Servers[id]
// only Dawnline's own interfaces: never another tunnel of the same name
if !srv.Enabled || !tunnelOK(srv) {
continue
}
if _, code := r.Exec("awg", "show", srv.Ifname, "public-key"); code != 0 {
res[id] = "down"
r.say("debug", "awg", "interface "+srv.Ifname+" ("+srv.Name+"): down, no devices to load")
continue
}
conf, who := PeersConf(m, srv, states, behind)
f := r.RunDir + "/" + srv.Ifname + ".conf"
os.WriteFile(f, []byte(conf), 0o600)
out, code := r.Exec("awg", "syncconf", srv.Ifname, f)
os.Remove(f)
if code != 0 {
res[id] = strings.Trim(out, " \t\r\n\v\f")
if r.Log != nil {
r.Log.Write("awg", "error", "couldn't load devices into the tunnel", res[id], srv.Name, servicelog.Extra{})
}
continue
}
res[id] = "ok"
// who came in and who left since the last load: info; the same peers: debug
now[srv.Ifname] = who
plural := "peers"
if len(who) == 1 {
plural = "peer"
}
head := fmt.Sprintf("interface %s (%s): awg syncconf · listen port %d · %d %s", srv.Ifname, srv.Name, srv.Port, len(who), plural)
was, had := seen[srv.Ifname]
if !had {
r.say("info", "awg", head+" loaded")
r.say("debug", "awg", "interface "+srv.Ifname+": "+strings.Join(who, ", "))
continue
}
add, del := minus(who, was), minus(was, who)
switch {
case len(add) > 0 || len(del) > 0:
line := head
if len(add) > 0 {
line += " · added " + someOf(add)
}
if len(del) > 0 {
line += " · removed " + someOf(del)
}
r.say("info", "awg", line)
default:
r.say("debug", "awg", head+", unchanged")
}
}
a, _ := json.Marshal(now)
b, _ := json.Marshal(seen)
if string(a) != string(b) {
files.WriteJSON(path, now, 0o600)
}
return res
}
// Hosts is the names dnsmasq is given for people's devices: one label a device, unique within the person
// (the pages show the same), and the router's own on the LAN while the home-like path is up.
func Hosts(m *model.Model, lanAddress string) (body string, names int) {
var lines []string
if m.Settings.Hostnames {
for _, pid := range m.PersonIDs() {
p := m.People[pid]
labels := view.Labels(m, p)
for _, id := range p.Devices {
if d := m.Devices[id]; d != nil {
if _, ok := ipnet.IP(d.Address); ok {
lines = append(lines, d.Address+" "+labels[id]+"."+p.ID+"."+m.Settings.Domain)
}
}
}
}
}
if lanAddress != "" {
lines = append(lines, lanAddress+" dawnline."+m.Settings.Domain)
}
if len(lines) == 0 {
return "", 0
}
return strings.Join(lines, "\n") + "\n", len(lines)
}
// hosts keeps the names file as Hosts says and has dnsmasq read it again when it changed.
func (r *Router) hosts(m *model.Model) bool {
active, address, _, _ := r.likeHome()
if !active {
address = ""
}
body, n := Hosts(m, address)
was, _ := os.ReadFile(r.HostsFile)
// dnsmasq runs as its own user: a file only root reads is no use to it
readable := false
if st, err := os.Stat(r.HostsFile); err == nil {
readable = st.Mode().Perm()&0o004 != 0
}
if string(was) == body && (body == "" || readable) {
r.say("debug", "dns", fmt.Sprintf("%s: unchanged · %d names", r.HostsFile, n))
return false
}
if i := strings.LastIndex(r.HostsFile, "/"); i > 0 {
os.MkdirAll(r.HostsFile[:i], 0o755)
}
if body == "" {
os.Remove(r.HostsFile)
r.say("info", "dns", r.HostsFile+": removed (no device names)")
} else {
os.WriteFile(r.HostsFile, []byte(body), 0o644)
os.Chmod(r.HostsFile, 0o644)
r.say("info", "dns", fmt.Sprintf("%s: %d names written (*.%s) · killall -HUP dnsmasq", r.HostsFile, n, m.Settings.Domain))
}
r.Exec("killall", "-HUP", "dnsmasq")
return true
}
// Sync is the runtime sync: with the apply lock held, the access sets first, then the devices into the
// tunnels, then the hostnames. `full` rebuilds the whole table (its shape changed).
func (r *Router) Sync(m *model.Model, u *usage.Usage, now int64, full bool, t *i18n.Catalog) Result {
defer files.Lock(r.RunDir, "apply")()
return r.sync(m, u, now, full, t)
}
// sync is Sync with the apply lock held already.
func (r *Router) sync(m *model.Model, u *usage.Usage, now int64, full bool, t *i18n.Catalog) Result {
states := map[string]string{}
for _, id := range m.PersonIDs() {
states[id] = state.Of(m, m.People[id], u, now)
}
snap := r.Snapshot()
sets := nft.AccessSets(m, states, snap)
table := r.table(m, sets, snap, full)
// fail closed: with no table at all, nobody is loaded
loaded := states
if table != "" && !r.Present() {
loaded = map[string]string{}
}
behind := snap.Behind(m)
peers := r.peers(m, loaded, behind)
routes := r.routes(m, loaded, peers, behind)
r.hosts(m)
os.MkdirAll(r.RunDir, 0o755)
files.WriteJSON(r.RunDir+"/states.json", states, 0o644)
// what an admin should hear when it did not fully take, with what nft or awg said
var out []string
if table != "" {
if r.Present() {
out = append(out, fmt.Sprintf(t.T("Access rules couldn't be updated (%s): the previous rules stay, so this change isn't in effect yet."), table))
} else {
out = append(out, fmt.Sprintf(t.T("Access rules couldn't load (%s): no device is loaded into the tunnels until they do."), table))
}
}
for _, id := range m.ServerIDs() {
if res, has := peers[id]; has && res != "ok" && res != "down" {
out = append(out, fmt.Sprintf(t.T("Couldn't load devices into %s: %s."), m.Servers[id].Name, res))
}
}
if routes != "" {
out = append(out, fmt.Sprintf(t.T("Couldn't set the routes to the networks behind routers (%s): they aren't reachable yet."), routes))
}
warning := ""
if len(out) > 0 {
warning = strings.Join(append(out, t.T("Open Overview → Health.")), " ")
}
return Result{Peers: peers, States: states, Table: table, Routes: routes, Warning: warning}
}
// Plan is what a sync would load, with nothing loaded: the table's text, the script for its sets, each
// tunnel's peers by interface, the routes to the networks behind routers by interface (as if every tunnel
// took its devices) and the hostnames. For holding the Go sync to what a router runs now.
type Plan struct {
Table string `json:"table"`
Update string `json:"update"`
Peers map[string]string `json:"peers"`
Routes map[string][]string `json:"routes,omitempty"`
Hosts string `json:"hosts"`
}
// Plan works a sync out and loads nothing.
func (r *Router) Plan(m *model.Model, u *usage.Usage, now int64) Plan {
states := map[string]string{}
for _, id := range m.PersonIDs() {
states[id] = state.Of(m, m.People[id], u, now)
}
snap := r.Snapshot()
sets := nft.AccessSets(m, states, snap)
p := Plan{Table: nft.Render(m, sets, r.Ctx(snap)), Update: nft.Update(sets), Peers: map[string]string{}}
behind := snap.Behind(m)
took := map[string]string{}
for _, id := range m.ServerIDs() {
if srv := m.Servers[id]; srv.Enabled && tunnelOK(srv) {
p.Peers[srv.Ifname], _ = PeersConf(m, srv, states, behind)
took[id] = "ok"
}
}
if want := RoutesWanted(m, states, took, behind); len(want) > 0 {
p.Routes = want
}
active, address, _, _ := r.likeHome()
if !active {
address = ""
}
p.Hosts, _ = Hosts(m, address)
return p
}
+190
View File
@@ -0,0 +1,190 @@
package apply
import (
"encoding/json"
"os"
"reflect"
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/usage"
)
// stand is a router whose nft and awg keep what they are given, as the golden's stand-ins do.
type stand struct {
dir string
present, nftFail bool
refuse, down map[string]bool
scripts []string
confs map[string]string
}
func (s *stand) exec(name string, args ...string) (string, int) {
switch {
case name == "nft" && args[0] == "-f":
raw, _ := os.ReadFile(args[1])
s.scripts = append(s.scripts, string(raw))
if s.nftFail {
return args[1] + ":1:1-5: Error: a made-up failure\n", 1
}
if strings.HasPrefix(string(raw), "flush set") {
if !s.present {
return args[1] + ":1:1-9: Error: No such file or directory\n", 1
}
} else {
s.present = true
}
return "", 0
case name == "nft" && args[0] == "list":
if s.present {
return "", 0
}
return "", 1
case name == "awg" && args[0] == "show":
if s.down[args[1]] {
return "", 1
}
return "", 0
case name == "awg" && args[0] == "syncconf":
raw, _ := os.ReadFile(args[2])
s.confs[args[1]] = string(raw)
if s.refuse[args[1]] {
return "Line unrecognized: a made-up refusal\n", 1
}
return "", 0
}
return "", 0
}
// A row of syncs of access's household, each as ucode's sync does it on the golden's router: the scripts
// loaded, each tunnel's peers, the hostnames, the states and what is answered (testdata/trace.uc).
func TestSyncAsUcode(t *testing.T) {
raw, err := os.ReadFile("testdata/trace.json")
if err != nil {
t.Fatal(err)
}
var trace struct {
Steps []struct {
What string `json:"what"`
Peers map[string]string `json:"peers"`
Table *string `json:"table"`
Warning *string `json:"warning"`
States map[string]string `json:"states"`
Scripts []string `json:"scripts"`
Confs map[string]string `json:"confs"`
Hosts *string `json:"hosts"`
HostsMode *bool `json:"hosts_mode"`
NftErr *string `json:"nft_err"`
Present bool `json:"present"`
} `json:"steps"`
}
if err := json.Unmarshal(raw, &trace); err != nil {
t.Fatal(err)
}
if len(trace.Steps) != 9 {
t.Fatalf("%d steps", len(trace.Steps))
}
text, _ := os.ReadFile("../access/testdata/access.uci")
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
raw, _ = os.ReadFile("../netview/testdata/trace.json")
var router struct {
Routes json.RawMessage `json:"routes"`
Addrs json.RawMessage `json:"addrs"`
Firewall string `json:"firewall"`
Links map[string]netview.Link `json:"links"`
}
json.Unmarshal(raw, &router)
fw, _ := uci.Parse(router.Firewall)
snap := netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return router.Addrs },
Links: func() map[string]netview.Link { return router.Links }, Zones: func() netview.Zones { return netview.ZonesOf(fw) }})
dir := t.TempDir()
s := &stand{dir: dir, refuse: map[string]bool{}, down: map[string]bool{}}
r := &Router{RunDir: dir, HostsFile: dir + "/hosts/dawnline", DHCP: dir + "/no-dhcp", Exec: s.exec, Snapshot: func() *netview.Snapshot { return snap }}
str := func(p *string) string {
if p == nil {
return ""
}
return *p
}
for i, want := range trace.Steps {
// what the golden does before each step
switch i {
case 2:
c.Set("anna", "paused", "1")
case 3:
c.Delete("anna", "paused")
s.nftFail = true
case 4:
s.present = false
case 6:
s.nftFail = false
s.refuse["dawn0"] = true
case 7:
s.refuse["dawn0"] = false
s.down["dawn1"] = true
case 8:
s.down["dawn1"] = false
}
s.scripts, s.confs = nil, map[string]string{}
res := r.Sync(model.Load(c, nil), usage.Empty(), 1_800_000_000, false, nil)
fail := func(what string, got, exp any) {
t.Errorf("step %d (%s): %s\n go: %v\n ucode: %v", i, want.What, what, got, exp)
}
if !reflect.DeepEqual(res.Peers, want.Peers) {
fail("each server's devices", res.Peers, want.Peers)
}
if res.Table != str(want.Table) || res.Warning != str(want.Warning) {
fail("the table and the warning", res.Table+" | "+res.Warning, str(want.Table)+" | "+str(want.Warning))
}
if !reflect.DeepEqual(res.States, want.States) {
fail("the states", res.States, want.States)
}
if !reflect.DeepEqual(s.scripts, want.Scripts) {
for n := range want.Scripts {
if n >= len(s.scripts) || s.scripts[n] != want.Scripts[n] {
fail("script "+string(rune('1'+n))+" of "+string(rune('0'+len(want.Scripts))), len(s.scripts), "differs")
break
}
}
if len(s.scripts) != len(want.Scripts) {
fail("how many scripts were loaded", len(s.scripts), len(want.Scripts))
}
}
if !reflect.DeepEqual(s.confs, want.Confs) {
fail("the tunnels' peers", s.confs, want.Confs)
}
hosts, herr := os.ReadFile(r.HostsFile)
if string(hosts) != str(want.Hosts) || (herr == nil) != (want.Hosts != nil) {
fail("the hostnames", string(hosts), str(want.Hosts))
}
if st, err := os.Stat(r.HostsFile); err == nil && want.HostsMode != nil && (st.Mode().Perm()&0o004 != 0) != *want.HostsMode {
fail("who may read the hostnames", st.Mode().Perm(), *want.HostsMode)
}
nftErr, eerr := os.ReadFile(dir + "/nft.err")
if string(nftErr) != str(want.NftErr) || (eerr == nil) != (want.NftErr != nil) {
fail("the table's error kept", string(nftErr), str(want.NftErr))
}
if s.present != want.Present {
fail("whether a table is there", s.present, want.Present)
}
var kept map[string]string
raw, _ := os.ReadFile(dir + "/states.json")
json.Unmarshal(raw, &kept)
if !reflect.DeepEqual(kept, want.States) {
fail("the states kept", kept, want.States)
}
}
// nobody's key stays on disk between syncs, nor a script
left, _ := os.ReadDir(dir)
for _, e := range left {
if strings.HasSuffix(e.Name(), ".conf") || strings.HasSuffix(e.Name(), ".rules") {
t.Errorf("%s stays in the state directory", e.Name())
}
}
}
+122
View File
@@ -0,0 +1,122 @@
{
"steps": [
{
"what": "the first time: a zone a tunnel, the way home and out, the ports",
"dry": false,
"changed": true,
"before": "config defaults\n\toption syn_flood\t1\n\toption input\t\tREJECT\n\toption output\t\tACCEPT\n\toption forward\t\tREJECT\n# Uncomment this line to disable ipv6 rules\n#\toption disable_ipv6\t1\n\nconfig zone\n\toption name\t\tlan\n\tlist network\t\t'lan'\n\toption input\t\tACCEPT\n\toption output\t\tACCEPT\n\toption forward\t\tACCEPT\n\nconfig zone\n\toption name\t\twan\n\tlist network\t\t'wan'\n\tlist network\t\t'wan6'\n\toption input\t\tREJECT\n\toption output\t\tACCEPT\n\toption forward\t\tDROP\n\toption masq\t\t1\n\toption mtu_fix\t\t1\n\nconfig forwarding\n\toption src\t\tlan\n\toption dest\t\twan\n\n# We need to accept udp packets on port 68,\n# see https://dev.openwrt.org/ticket/4108\nconfig rule\n\toption name\t\tAllow-DHCP-Renew\n\toption src\t\twan\n\toption proto\t\tudp\n\toption dest_port\t68\n\toption target\t\tACCEPT\n\toption family\t\tipv4\n\n# Allow IPv4 ping\nconfig rule\n\toption name\t\tAllow-Ping\n\toption src\t\twan\n\toption proto\t\ticmp\n\toption icmp_type\techo-request\n\toption family\t\tipv4\n\toption target\t\tACCEPT\n\nconfig rule\n\toption name\t\tAllow-IGMP\n\toption src\t\twan\n\toption proto\t\tigmp\n\toption family\t\tipv4\n\toption target\t\tACCEPT\n\n# Allow DHCPv6 replies\n# see https://github.com/openwrt/openwrt/issues/5066\nconfig rule\n\toption name\t\tAllow-DHCPv6\n\toption src\t\twan\n\toption proto\t\tudp\n\toption dest_port\t546\n\toption family\t\tipv6\n\toption target\t\tACCEPT\n\nconfig rule\n\toption name\t\tAllow-MLD\n\toption src\t\twan\n\toption proto\t\ticmp\n\toption src_ip\t\tfe80::/10\n\tlist icmp_type\t\t'130/0'\n\tlist icmp_type\t\t'131/0'\n\tlist icmp_type\t\t'132/0'\n\tlist icmp_type\t\t'143/0'\n\toption family\t\tipv6\n\toption target\t\tACCEPT\n\n# Allow essential incoming IPv6 ICMP traffic\nconfig rule\n\toption name\t\tAllow-ICMPv6-Input\n\toption src\t\twan\n\toption proto\ticmp\n\tlist icmp_type\t\techo-request\n\tlist icmp_type\t\techo-reply\n\tlist icmp_type\t\tdestination-unreachable\n\tlist icmp_type\t\tpacket-too-big\n\tlist icmp_type\t\ttime-exceeded\n\tlist icmp_type\t\tbad-header\n\tlist icmp_type\t\tunknown-header-type\n\tlist icmp_type\t\trouter-solicitation\n\tlist icmp_type\t\tneighbour-solicitation\n\tlist icmp_type\t\trouter-advertisement\n\tlist icmp_type\t\tneighbour-advertisement\n\toption limit\t\t1000/sec\n\toption family\t\tipv6\n\toption target\t\tACCEPT\n\n# Allow essential forwarded IPv6 ICMP traffic\nconfig rule\n\toption name\t\tAllow-ICMPv6-Forward\n\toption src\t\twan\n\toption dest\t\t*\n\toption proto\t\ticmp\n\tlist icmp_type\t\techo-request\n\tlist icmp_type\t\techo-reply\n\tlist icmp_type\t\tdestination-unreachable\n\tlist icmp_type\t\tpacket-too-big\n\tlist icmp_type\t\ttime-exceeded\n\tlist icmp_type\t\tbad-header\n\tlist icmp_type\t\tunknown-header-type\n\toption limit\t\t1000/sec\n\toption family\t\tipv6\n\toption target\t\tACCEPT\n\nconfig rule\n\toption name\t\tAllow-IPSec-ESP\n\toption src\t\twan\n\toption dest\t\tlan\n\toption proto\t\tesp\n\toption target\t\tACCEPT\n\nconfig rule\n\toption name\t\tAllow-ISAKMP\n\toption src\t\twan\n\toption dest\t\tlan\n\toption dest_port\t500\n\toption proto\t\tudp\n\toption target\t\tACCEPT\n\n\n### EXAMPLE CONFIG SECTIONS\n# do not allow a specific ip to access wan\n#config rule\n#\toption src\t\tlan\n#\toption src_ip\t192.168.45.2\n#\toption dest\t\twan\n#\toption proto\ttcp\n#\toption target\tREJECT\n\n# block a specific mac on wan\n#config rule\n#\toption dest\t\twan\n#\toption src_mac\t00:11:22:33:44:66\n#\toption target\tREJECT\n\n# block incoming ICMP traffic on a zone\n#config rule\n#\toption src\t\tlan\n#\toption proto\tICMP\n#\toption target\tDROP\n\n# port redirect port coming in on wan to lan\n#config redirect\n#\toption src\t\t\twan\n#\toption src_dport\t80\n#\toption dest\t\t\tlan\n#\toption dest_ip\t\t192.168.16.235\n#\toption dest_port\t80\n#\toption proto\t\ttcp\n\n# port redirect of remapped ssh port (22001) on wan\n#config redirect\n#\toption src\t\twan\n#\toption src_dport\t22001\n#\toption dest\t\tlan\n#\toption dest_port\t22\n#\toption proto\t\ttcp\n\n### FULL CONFIG SECTIONS\n#config rule\n#\toption src\t\tlan\n#\toption src_ip\t192.168.45.2\n#\toption src_mac\t00:11:22:33:44:55\n#\toption src_port\t80\n#\toption dest\t\twan\n#\toption dest_ip\t194.25.2.129\n#\toption dest_port\t120\n#\toption proto\ttcp\n#\toption target\tREJECT\n\n#config redirect\n#\toption src\t\tlan\n#\toption src_ip\t192.168.45.2\n#\toption src_mac\t00:11:22:33:44:55\n#\toption src_port\t\t1024\n#\toption src_dport\t80\n#\toption dest_ip\t194.25.2.129\n#\toption dest_port\t120\n#\toption proto\ttcp\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n",
"firewall": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig rule 'dawnline_home_loopdns'\n\toption name 'Dawnline-dawn0-names'\n\toption src 'dawn0'\n\toption dest 'dl_loop'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4500'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_cabin_zone'\n\toption name 'dawn1'\n\tlist network 'dawn1'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_cabin_lan'\n\toption src 'dawn1'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_cabin_from_lan'\n\toption src 'home'\n\toption dest 'dawn1'\n\nconfig forwarding 'dawnline_cabin_wan'\n\toption src 'dawn1'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_cabin_loop'\n\toption src 'dawn1'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_cabin_port'\n\toption name 'Dawnline-dawn1'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4600'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'home'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4700Line truncated
"cmds": [
"ubus call network.interface dump",
"fw4 -q reload"
]
},
{
"what": "again: nothing to do",
"dry": false,
"changed": false,
"before": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig rule 'dawnline_home_loopdns'\n\toption name 'Dawnline-dawn0-names'\n\toption src 'dawn0'\n\toption dest 'dl_loop'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4500'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_cabin_zone'\n\toption name 'dawn1'\n\tlist network 'dawn1'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_cabin_lan'\n\toption src 'dawn1'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_cabin_from_lan'\n\toption src 'home'\n\toption dest 'dawn1'\n\nconfig forwarding 'dawnline_cabin_wan'\n\toption src 'dawn1'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_cabin_loop'\n\toption src 'dawn1'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_cabin_port'\n\toption name 'Dawnline-dawn1'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4600'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'home'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4700'\Line truncated
"firewall": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig rule 'dawnline_home_loopdns'\n\toption name 'Dawnline-dawn0-names'\n\toption src 'dawn0'\n\toption dest 'dl_loop'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4500'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_cabin_zone'\n\toption name 'dawn1'\n\tlist network 'dawn1'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_cabin_lan'\n\toption src 'dawn1'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_cabin_from_lan'\n\toption src 'home'\n\toption dest 'dawn1'\n\nconfig forwarding 'dawnline_cabin_wan'\n\toption src 'dawn1'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_cabin_loop'\n\toption src 'dawn1'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_cabin_port'\n\toption name 'Dawnline-dawn1'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4600'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'home'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4700Line truncated
"cmds": [
"ubus call network.interface dump"
]
},
{
"what": "a port changed, a server turned off, a second home zone",
"dry": false,
"changed": true,
"before": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig rule 'dawnline_home_loopdns'\n\toption name 'Dawnline-dawn0-names'\n\toption src 'dawn0'\n\toption dest 'dl_loop'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4500'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_cabin_zone'\n\toption name 'dawn1'\n\tlist network 'dawn1'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_cabin_lan'\n\toption src 'dawn1'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_cabin_from_lan'\n\toption src 'home'\n\toption dest 'dawn1'\n\nconfig forwarding 'dawnline_cabin_wan'\n\toption src 'dawn1'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_cabin_loop'\n\toption src 'dawn1'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_cabin_port'\n\toption name 'Dawnline-dawn1'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4600'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'home'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4700'\Line truncated
"firewall": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig rule 'dawnline_home_loopdns'\n\toption name 'Dawnline-dawn0-names'\n\toption src 'dawn0'\n\toption dest 'dl_loop'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4501'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_cabin_zone'\n\toption name 'dawn1'\n\tlist network 'dawn1'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_cabin_lan'\n\toption src 'dawn1'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_cabin_from_lan'\n\toption src 'home'\n\toption dest 'dawn1'\n\nconfig forwarding 'dawnline_cabin_wan'\n\toption src 'dawn1'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_cabin_loop'\n\toption src 'dawn1'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_cabin_port'\n\toption name 'Dawnline-dawn1'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4600'\n\toption target 'ACCEPT'\n\toption enabled '0'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'home'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\tLine truncated
"cmds": [
"ubus call network.interface dump",
"fw4 -q reload"
]
},
{
"what": "a server that does not reach the zone of the DNS of the LAN",
"dry": false,
"changed": true,
"before": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig rule 'dawnline_home_loopdns'\n\toption name 'Dawnline-dawn0-names'\n\toption src 'dawn0'\n\toption dest 'dl_loop'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4501'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_cabin_zone'\n\toption name 'dawn1'\n\tlist network 'dawn1'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_cabin_lan'\n\toption src 'dawn1'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_cabin_from_lan'\n\toption src 'home'\n\toption dest 'dawn1'\n\nconfig forwarding 'dawnline_cabin_wan'\n\toption src 'dawn1'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_cabin_loop'\n\toption src 'dawn1'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_cabin_port'\n\toption name 'Dawnline-dawn1'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4600'\n\toption target 'ACCEPT'\n\toption enabled '0'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'home'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\topLine truncated
"firewall": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig rule 'dawnline_home_loopdns'\n\toption name 'Dawnline-dawn0-names'\n\toption src 'dawn0'\n\toption dest 'dl_loop'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4501'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_cabin_zone'\n\toption name 'dawn1'\n\tlist network 'dawn1'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_cabin_lan'\n\toption src 'dawn1'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_cabin_from_lan'\n\toption src 'home'\n\toption dest 'dawn1'\n\nconfig forwarding 'dawnline_cabin_wan'\n\toption src 'dawn1'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_cabin_loop'\n\toption src 'dawn1'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_cabin_port'\n\toption name 'Dawnline-dawn1'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4600'\n\toption target 'ACCEPT'\n\toption enabled '0'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\nLine truncated
"cmds": [
"ubus call network.interface dump",
"fw4 -q reload"
]
},
{
"what": "the portal on a port it never takes",
"dry": false,
"changed": true,
"before": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig rule 'dawnline_home_loopdns'\n\toption name 'Dawnline-dawn0-names'\n\toption src 'dawn0'\n\toption dest 'dl_loop'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4501'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_cabin_zone'\n\toption name 'dawn1'\n\tlist network 'dawn1'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_cabin_lan'\n\toption src 'dawn1'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_cabin_from_lan'\n\toption src 'home'\n\toption dest 'dawn1'\n\nconfig forwarding 'dawnline_cabin_wan'\n\toption src 'dawn1'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_cabin_loop'\n\toption src 'dawn1'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_cabin_port'\n\toption name 'Dawnline-dawn1'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4600'\n\toption target 'ACCEPT'\n\toption enabled '0'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\tLine truncated
"firewall": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig rule 'dawnline_home_loopdns'\n\toption name 'Dawnline-dawn0-names'\n\toption src 'dawn0'\n\toption dest 'dl_loop'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4501'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_cabin_zone'\n\toption name 'dawn1'\n\tlist network 'dawn1'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_cabin_lan'\n\toption src 'dawn1'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_cabin_from_lan'\n\toption src 'home'\n\toption dest 'dawn1'\n\nconfig forwarding 'dawnline_cabin_wan'\n\toption src 'dawn1'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_cabin_loop'\n\toption src 'dawn1'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_cabin_port'\n\toption name 'Dawnline-dawn1'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4600'\n\toption target 'ACCEPT'\n\toption enabled '0'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\nLine truncated
"cmds": [
"ubus call network.interface dump",
"fw4 -q reload"
]
},
{
"what": "the portal back, with names only: no plain HTTP",
"dry": false,
"changed": true,
"before": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig rule 'dawnline_home_loopdns'\n\toption name 'Dawnline-dawn0-names'\n\toption src 'dawn0'\n\toption dest 'dl_loop'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4501'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_cabin_zone'\n\toption name 'dawn1'\n\tlist network 'dawn1'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_cabin_lan'\n\toption src 'dawn1'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_cabin_from_lan'\n\toption src 'home'\n\toption dest 'dawn1'\n\nconfig forwarding 'dawnline_cabin_wan'\n\toption src 'dawn1'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_cabin_loop'\n\toption src 'dawn1'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_cabin_port'\n\toption name 'Dawnline-dawn1'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4600'\n\toption target 'ACCEPT'\n\toption enabled '0'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\tLine truncated
"firewall": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig rule 'dawnline_home_loopdns'\n\toption name 'Dawnline-dawn0-names'\n\toption src 'dawn0'\n\toption dest 'dl_loop'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4501'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_cabin_zone'\n\toption name 'dawn1'\n\tlist network 'dawn1'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_cabin_lan'\n\toption src 'dawn1'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_cabin_from_lan'\n\toption src 'home'\n\toption dest 'dawn1'\n\nconfig forwarding 'dawnline_cabin_wan'\n\toption src 'dawn1'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_cabin_loop'\n\toption src 'dawn1'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_cabin_port'\n\toption name 'Dawnline-dawn1'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4600'\n\toption target 'ACCEPT'\n\toption enabled '0'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\nLine truncated
"cmds": [
"ubus call network.interface dump",
"fw4 -q reload"
]
},
{
"what": "would it change: asked only",
"dry": true,
"changed": true,
"before": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig rule 'dawnline_home_loopdns'\n\toption name 'Dawnline-dawn0-names'\n\toption src 'dawn0'\n\toption dest 'dl_loop'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4501'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_cabin_zone'\n\toption name 'dawn1'\n\tlist network 'dawn1'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_cabin_lan'\n\toption src 'dawn1'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_cabin_from_lan'\n\toption src 'home'\n\toption dest 'dawn1'\n\nconfig forwarding 'dawnline_cabin_wan'\n\toption src 'dawn1'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_cabin_loop'\n\toption src 'dawn1'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_cabin_port'\n\toption name 'Dawnline-dawn1'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4600'\n\toption target 'ACCEPT'\n\toption enabled '0'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\tLine truncated
"firewall": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig rule 'dawnline_home_loopdns'\n\toption name 'Dawnline-dawn0-names'\n\toption src 'dawn0'\n\toption dest 'dl_loop'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4501'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_cabin_zone'\n\toption name 'dawn1'\n\tlist network 'dawn1'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_cabin_lan'\n\toption src 'dawn1'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_cabin_from_lan'\n\toption src 'home'\n\toption dest 'dawn1'\n\nconfig forwarding 'dawnline_cabin_wan'\n\toption src 'dawn1'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_cabin_loop'\n\toption src 'dawn1'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_cabin_port'\n\toption name 'Dawnline-dawn1'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4600'\n\toption target 'ACCEPT'\n\toption enabled '0'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\nLine truncated
"cmds": [
"ubus call network.interface dump"
]
},
{
"what": "a server removed, another like a home device",
"dry": false,
"changed": true,
"before": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig rule 'dawnline_home_loopdns'\n\toption name 'Dawnline-dawn0-names'\n\toption src 'dawn0'\n\toption dest 'dl_loop'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4501'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_cabin_zone'\n\toption name 'dawn1'\n\tlist network 'dawn1'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_cabin_lan'\n\toption src 'dawn1'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_cabin_from_lan'\n\toption src 'home'\n\toption dest 'dawn1'\n\nconfig forwarding 'dawnline_cabin_wan'\n\toption src 'dawn1'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_cabin_loop'\n\toption src 'dawn1'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_cabin_port'\n\toption name 'Dawnline-dawn1'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4600'\n\toption target 'ACCEPT'\n\toption enabled '0'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\tLine truncated
"firewall": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4502'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4700'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_loop'\n\toption name 'dl_loop'\n\tlist device 'dlx0'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig forwarding 'dawnline_loop_wan'\n\toption src 'dl_loop'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_home_lan_guest'\n\toption src 'dawn0'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_home_from_lan_guest'\n\toption src 'guest'\n\toption dest 'dawn0'\n\nconfig rule 'dawnline_open_landns'\n\toption name 'Dawnline-dawn2-dns'\n\toption src 'dawn2'\n\toption dest 'home'\n\toption dest_ip '192.168.77.1'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig rule 'dawnline_portal'\n\toption name 'Dawnline-Portal'\n\toption src 'wan'\n\toption proto 'tcp'\n\toption dest_port '8443'\n\toption target 'ACCEPT'\n\nconfig forwarding 'dawnlinLine truncated
"cmds": [
"ubus call network.interface dump",
"fw4 -q reload"
]
},
{
"what": "nobody like a home device",
"dry": false,
"changed": true,
"before": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4502'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_open_loop'\n\toption src 'dawn2'\n\toption dest 'dl_loop'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4700'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_loop'\n\toption name 'dl_loop'\n\tlist device 'dlx0'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig forwarding 'dawnline_loop_wan'\n\toption src 'dl_loop'\n\toption dest 'wan'\n\nconfig forwarding 'dawnline_home_lan_guest'\n\toption src 'dawn0'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_home_from_lan_guest'\n\toption src 'guest'\n\toption dest 'dawn0'\n\nconfig rule 'dawnline_open_landns'\n\toption name 'Dawnline-dawn2-dns'\n\toption src 'dawn2'\n\toption dest 'home'\n\toption dest_ip '192.168.77.1'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig rule 'dawnline_portal'\n\toption name 'Dawnline-Portal'\n\toption src 'wan'\n\toption proto 'tcp'\n\toption dest_port '8443'\n\toption target 'ACCEPT'\n\nconfig forwarding 'dawnline_Line truncated
"firewall": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4502'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4700'\n\toption target 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan_guest'\n\toption src 'dawn0'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_home_from_lan_guest'\n\toption src 'guest'\n\toption dest 'dawn0'\n\nconfig rule 'dawnline_open_landns'\n\toption name 'Dawnline-dawn2-dns'\n\toption src 'dawn2'\n\toption dest 'home'\n\toption dest_ip '192.168.77.1'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig rule 'dawnline_portal'\n\toption name 'Dawnline-Portal'\n\toption src 'wan'\n\toption proto 'tcp'\n\toption dest_port '8443'\n\toption target 'ACCEPT'\n\n",
"cmds": [
"ubus call network.interface dump",
"fw4 -q reload"
]
},
{
"what": "a section of another kind under our name, one of a server long gone, the portal off",
"dry": false,
"changed": true,
"before": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4502'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4700'\n\toption target 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan_guest'\n\toption src 'dawn0'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_home_from_lan_guest'\n\toption src 'guest'\n\toption dest 'dawn0'\n\nconfig rule 'dawnline_open_landns'\n\toption name 'Dawnline-dawn2-dns'\n\toption src 'dawn2'\n\toption dest 'home'\n\toption dest_ip '192.168.77.1'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig rule 'dawnline_portal'\n\toption name 'Dawnline-Portal'\n\toption src 'wan'\n\toption proto 'tcp'\n\toption dest_port '8443'\n\toption target 'ACCEPT'\n\nconfig rule 'dawnline_home_zone'\n\toption name 'Not a zone'\n\nconfig forwarding 'dawnline_old_wan'\n\toption src 'dawn9'\n\n",
"firewall": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4502'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4700'\n\toption target 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan_guest'\n\toption src 'dawn0'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_home_from_lan_guest'\n\toption src 'guest'\n\toption dest 'dawn0'\n\nconfig rule 'dawnline_open_landns'\n\toption name 'Dawnline-dawn2-dns'\n\toption src 'dawn2'\n\toption dest 'home'\n\toption dest_ip '192.168.77.1'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\n",
"cmds": [
"ubus call network.interface dump",
"fw4 -q reload"
]
},
{
"what": "would it change: no",
"dry": true,
"changed": false,
"before": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4502'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4700'\n\toption target 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan_guest'\n\toption src 'dawn0'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_home_from_lan_guest'\n\toption src 'guest'\n\toption dest 'dawn0'\n\nconfig rule 'dawnline_open_landns'\n\toption name 'Dawnline-dawn2-dns'\n\toption src 'dawn2'\n\toption dest 'home'\n\toption dest_ip '192.168.77.1'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\n",
"firewall": "\nconfig defaults\n\toption syn_flood '1'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'REJECT'\n\nconfig zone\n\toption name 'lan'\n\tlist network 'lan'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig zone\n\toption name 'wan'\n\tlist network 'wan'\n\tlist network 'wan6'\n\toption input 'REJECT'\n\toption output 'ACCEPT'\n\toption forward 'DROP'\n\toption masq '1'\n\toption mtu_fix '1'\n\nconfig forwarding\n\toption src 'lan'\n\toption dest 'wan'\n\nconfig rule\n\toption name 'Allow-DHCP-Renew'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '68'\n\toption target 'ACCEPT'\n\toption family 'ipv4'\n\nconfig rule\n\toption name 'Allow-Ping'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption icmp_type 'echo-request'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IGMP'\n\toption src 'wan'\n\toption proto 'igmp'\n\toption family 'ipv4'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-DHCPv6'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '546'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-MLD'\n\toption src 'wan'\n\toption proto 'icmp'\n\toption src_ip 'fe80::/10'\n\tlist icmp_type '130/0'\n\tlist icmp_type '131/0'\n\tlist icmp_type '132/0'\n\tlist icmp_type '143/0'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Input'\n\toption src 'wan'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\tlist icmp_type 'router-solicitation'\n\tlist icmp_type 'neighbour-solicitation'\n\tlist icmp_type 'router-advertisement'\n\tlist icmp_type 'neighbour-advertisement'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ICMPv6-Forward'\n\toption src 'wan'\n\toption dest '*'\n\toption proto 'icmp'\n\tlist icmp_type 'echo-request'\n\tlist icmp_type 'echo-reply'\n\tlist icmp_type 'destination-unreachable'\n\tlist icmp_type 'packet-too-big'\n\tlist icmp_type 'time-exceeded'\n\tlist icmp_type 'bad-header'\n\tlist icmp_type 'unknown-header-type'\n\toption limit '1000/sec'\n\toption family 'ipv6'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-IPSec-ESP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption proto 'esp'\n\toption target 'ACCEPT'\n\nconfig rule\n\toption name 'Allow-ISAKMP'\n\toption src 'wan'\n\toption dest 'lan'\n\toption dest_port '500'\n\toption proto 'udp'\n\toption target 'ACCEPT'\n\nconfig zone\n\toption name 'guest'\n\tlist device 'dlguest'\n\nconfig zone\n\toption name 'home'\n\toption device 'br-lan dlhome'\n\nconfig forwarding 'dawnline_home_lan'\n\toption src 'dawn0'\n\toption dest 'home'\n\nconfig forwarding 'dawnline_home_from_lan'\n\toption src 'home'\n\toption dest 'dawn0'\n\nconfig forwarding 'dawnline_home_wan'\n\toption src 'dawn0'\n\toption dest 'wan'\n\nconfig rule 'dawnline_home_port'\n\toption name 'Dawnline-dawn0'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4502'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_open_zone'\n\toption name 'dawn2'\n\tlist network 'dawn2'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\nconfig forwarding 'dawnline_open_lan'\n\toption src 'dawn2'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_open_from_lan'\n\toption src 'guest'\n\toption dest 'dawn2'\n\nconfig forwarding 'dawnline_open_wan'\n\toption src 'dawn2'\n\toption dest 'wan'\n\nconfig rule 'dawnline_open_port'\n\toption name 'Dawnline-dawn2'\n\toption src 'wan'\n\toption proto 'udp'\n\toption dest_port '4700'\n\toption target 'ACCEPT'\n\nconfig forwarding 'dawnline_home_lan_guest'\n\toption src 'dawn0'\n\toption dest 'guest'\n\nconfig forwarding 'dawnline_home_from_lan_guest'\n\toption src 'guest'\n\toption dest 'dawn0'\n\nconfig rule 'dawnline_open_landns'\n\toption name 'Dawnline-dawn2-dns'\n\toption src 'dawn2'\n\toption dest 'home'\n\toption dest_ip '192.168.77.1'\n\toption proto 'tcp udp'\n\toption dest_port '53'\n\toption target 'ACCEPT'\n\nconfig zone 'dawnline_home_zone'\n\toption name 'dawn0'\n\tlist network 'dawn0'\n\toption input 'ACCEPT'\n\toption output 'ACCEPT'\n\toption forward 'ACCEPT'\n\n",
"cmds": [
"ubus call network.interface dump"
]
}
]
}
+1537
View File
File diff suppressed because it is too large. Load diff
+307
View File
@@ -0,0 +1,307 @@
{
"steps": [
{
"what": "the first time: two tunnels made, a third whose name is taken",
"changed": true,
"network": "\nconfig interface 'loopback'\n\toption device 'lo'\n\toption proto 'static'\n\tlist ipaddr '127.0.0.1/8'\n\nconfig globals 'globals'\n\toption ula_prefix 'fd00:1234::/48'\n\nconfig device\n\toption name 'br-lan'\n\toption type 'bridge'\n\tlist ports 'lan1'\n\tlist ports 'lan2'\n\nconfig interface 'lan'\n\toption device 'br-lan'\n\toption proto 'static'\n\toption ipaddr '192.168.77.1/24'\n\toption note 'it'\\''s the LAN'\n\nconfig interface 'dawn2'\n\toption proto 'wireguard'\n\toption private_key 'someone else'\\''s'\n\nconfig interface 'dawn0'\n\toption proto 'amneziawg'\n\toption private_key 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA='\n\toption listen_port '4500'\n\tlist addresses '10.77.0.1/24'\n\toption mtu '1380'\n\toption _dawnline 'home'\n\toption _dawnline_name 'Home'\n\nconfig interface 'dawn1'\n\toption proto 'amneziawg'\n\toption private_key 'ccccccccccccccccccccccccccccccccccccccccccA='\n\toption listen_port '4600'\n\tlist addresses '10.78.0.1/24'\n\toption mtu '1380'\n\toption _dawnline 'cabin'\n\toption _dawnline_name 'Cabin'\n\n",
"blocked": {
"open": {
"code": "taken",
"ifname": "dawn2"
}
},
"model_blocked": {
"home": null,
"cabin": null,
"open": {
"code": "taken",
"ifname": "dawn2"
}
},
"cmds": [
"ubus call network reload",
"ifup dawn0",
"ifup dawn1"
],
"intents": {
"dawn0": {
"reason": "settings",
"name": "Home"
},
"dawn1": {
"reason": "settings",
"name": "Cabin"
}
},
"no_awg": false
},
{
"what": "again: nothing to do",
"changed": false,
"network": "\nconfig interface 'loopback'\n\toption device 'lo'\n\toption proto 'static'\n\tlist ipaddr '127.0.0.1/8'\n\nconfig globals 'globals'\n\toption ula_prefix 'fd00:1234::/48'\n\nconfig device\n\toption name 'br-lan'\n\toption type 'bridge'\n\tlist ports 'lan1'\n\tlist ports 'lan2'\n\nconfig interface 'lan'\n\toption device 'br-lan'\n\toption proto 'static'\n\toption ipaddr '192.168.77.1/24'\n\toption note 'it'\\''s the LAN'\n\nconfig interface 'dawn2'\n\toption proto 'wireguard'\n\toption private_key 'someone else'\\''s'\n\nconfig interface 'dawn0'\n\toption proto 'amneziawg'\n\toption private_key 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA='\n\toption listen_port '4500'\n\tlist addresses '10.77.0.1/24'\n\toption mtu '1380'\n\toption _dawnline 'home'\n\toption _dawnline_name 'Home'\n\nconfig interface 'dawn1'\n\toption proto 'amneziawg'\n\toption private_key 'ccccccccccccccccccccccccccccccccccccccccccA='\n\toption listen_port '4600'\n\tlist addresses '10.78.0.1/24'\n\toption mtu '1380'\n\toption _dawnline 'cabin'\n\toption _dawnline_name 'Cabin'\n\n",
"blocked": {
"open": {
"code": "taken",
"ifname": "dawn2"
}
},
"model_blocked": {
"home": null,
"cabin": null,
"open": {
"code": "taken",
"ifname": "dawn2"
}
},
"cmds": [
],
"intents": {
"dawn0": {
"reason": "settings",
"name": "Home"
},
"dawn1": {
"reason": "settings",
"name": "Cabin"
}
},
"no_awg": false
},
{
"what": "a port changed, a server turned off",
"changed": true,
"network": "\nconfig interface 'loopback'\n\toption device 'lo'\n\toption proto 'static'\n\tlist ipaddr '127.0.0.1/8'\n\nconfig globals 'globals'\n\toption ula_prefix 'fd00:1234::/48'\n\nconfig device\n\toption name 'br-lan'\n\toption type 'bridge'\n\tlist ports 'lan1'\n\tlist ports 'lan2'\n\nconfig interface 'lan'\n\toption device 'br-lan'\n\toption proto 'static'\n\toption ipaddr '192.168.77.1/24'\n\toption note 'it'\\''s the LAN'\n\nconfig interface 'dawn2'\n\toption proto 'wireguard'\n\toption private_key 'someone else'\\''s'\n\nconfig interface 'dawn0'\n\toption proto 'amneziawg'\n\toption private_key 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA='\n\toption listen_port '4501'\n\tlist addresses '10.77.0.1/24'\n\toption mtu '1380'\n\toption _dawnline 'home'\n\toption _dawnline_name 'Home'\n\nconfig interface 'dawn1'\n\toption proto 'amneziawg'\n\toption private_key 'ccccccccccccccccccccccccccccccccccccccccccA='\n\toption listen_port '4600'\n\tlist addresses '10.78.0.1/24'\n\toption mtu '1380'\n\toption _dawnline 'cabin'\n\toption _dawnline_name 'Cabin'\n\toption disabled '1'\n\toption auto '0'\n\n",
"blocked": {
"open": {
"code": "taken",
"ifname": "dawn2"
}
},
"model_blocked": {
"home": null,
"cabin": null,
"open": {
"code": "taken",
"ifname": "dawn2"
}
},
"cmds": [
"ubus call network reload",
"ifup dawn0"
],
"intents": {
"dawn0": {
"reason": "settings",
"name": "Home"
},
"dawn1": {
"reason": "off",
"name": "Cabin"
}
},
"no_awg": false
},
{
"what": "a disguise AmneziaWG cannot take: the tunnel goes",
"changed": true,
"network": "\nconfig interface 'loopback'\n\toption device 'lo'\n\toption proto 'static'\n\tlist ipaddr '127.0.0.1/8'\n\nconfig globals 'globals'\n\toption ula_prefix 'fd00:1234::/48'\n\nconfig device\n\toption name 'br-lan'\n\toption type 'bridge'\n\tlist ports 'lan1'\n\tlist ports 'lan2'\n\nconfig interface 'lan'\n\toption device 'br-lan'\n\toption proto 'static'\n\toption ipaddr '192.168.77.1/24'\n\toption note 'it'\\''s the LAN'\n\nconfig interface 'dawn2'\n\toption proto 'wireguard'\n\toption private_key 'someone else'\\''s'\n\nconfig interface 'dawn1'\n\toption proto 'amneziawg'\n\toption private_key 'ccccccccccccccccccccccccccccccccccccccccccA='\n\toption listen_port '4600'\n\tlist addresses '10.78.0.1/24'\n\toption mtu '1380'\n\toption _dawnline 'cabin'\n\toption _dawnline_name 'Cabin'\n\toption disabled '1'\n\toption auto '0'\n\n",
"blocked": {
"home": {
"code": "disguise_invalid"
},
"open": {
"code": "taken",
"ifname": "dawn2"
}
},
"model_blocked": {
"home": {
"code": "disguise_invalid"
},
"cabin": null,
"open": {
"code": "taken",
"ifname": "dawn2"
}
},
"cmds": [
"ifdown dawn0",
"ubus call network reload"
],
"intents": {
"dawn0": {
"reason": "removed",
"name": "Home"
},
"dawn1": {
"reason": "off",
"name": "Cabin"
}
},
"no_awg": false
},
{
"what": "put right, and the server renamed",
"changed": true,
"network": "\nconfig interface 'loopback'\n\toption device 'lo'\n\toption proto 'static'\n\tlist ipaddr '127.0.0.1/8'\n\nconfig globals 'globals'\n\toption ula_prefix 'fd00:1234::/48'\n\nconfig device\n\toption name 'br-lan'\n\toption type 'bridge'\n\tlist ports 'lan1'\n\tlist ports 'lan2'\n\nconfig interface 'lan'\n\toption device 'br-lan'\n\toption proto 'static'\n\toption ipaddr '192.168.77.1/24'\n\toption note 'it'\\''s the LAN'\n\nconfig interface 'dawn2'\n\toption proto 'wireguard'\n\toption private_key 'someone else'\\''s'\n\nconfig interface 'dawn1'\n\toption proto 'amneziawg'\n\toption private_key 'ccccccccccccccccccccccccccccccccccccccccccA='\n\toption listen_port '4600'\n\tlist addresses '10.78.0.1/24'\n\toption mtu '1380'\n\toption _dawnline 'cabin'\n\toption _dawnline_name 'Cabin'\n\toption disabled '1'\n\toption auto '0'\n\nconfig interface 'dawn0'\n\toption proto 'amneziawg'\n\toption private_key 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA='\n\toption listen_port '4501'\n\tlist addresses '10.77.0.1/24'\n\toption mtu '1380'\n\toption _dawnline 'home'\n\toption _dawnline_name 'Anna'\\''s house'\n\n",
"blocked": {
"open": {
"code": "taken",
"ifname": "dawn2"
}
},
"model_blocked": {
"home": null,
"cabin": null,
"open": {
"code": "taken",
"ifname": "dawn2"
}
},
"cmds": [
"ubus call network reload",
"ifup dawn0"
],
"intents": {
"dawn0": {
"reason": "settings",
"name": "Anna's house"
},
"dawn1": {
"reason": "off",
"name": "Cabin"
}
},
"no_awg": false
},
{
"what": "renamed again: only the marker moves",
"changed": true,
"network": "\nconfig interface 'loopback'\n\toption device 'lo'\n\toption proto 'static'\n\tlist ipaddr '127.0.0.1/8'\n\nconfig globals 'globals'\n\toption ula_prefix 'fd00:1234::/48'\n\nconfig device\n\toption name 'br-lan'\n\toption type 'bridge'\n\tlist ports 'lan1'\n\tlist ports 'lan2'\n\nconfig interface 'lan'\n\toption device 'br-lan'\n\toption proto 'static'\n\toption ipaddr '192.168.77.1/24'\n\toption note 'it'\\''s the LAN'\n\nconfig interface 'dawn2'\n\toption proto 'wireguard'\n\toption private_key 'someone else'\\''s'\n\nconfig interface 'dawn1'\n\toption proto 'amneziawg'\n\toption private_key 'ccccccccccccccccccccccccccccccccccccccccccA='\n\toption listen_port '4600'\n\tlist addresses '10.78.0.1/24'\n\toption mtu '1380'\n\toption _dawnline 'cabin'\n\toption _dawnline_name 'Cabin'\n\toption disabled '1'\n\toption auto '0'\n\nconfig interface 'dawn0'\n\toption proto 'amneziawg'\n\toption private_key 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA='\n\toption listen_port '4501'\n\tlist addresses '10.77.0.1/24'\n\toption mtu '1380'\n\toption _dawnline 'home'\n\toption _dawnline_name 'House'\n\n",
"blocked": {
"open": {
"code": "taken",
"ifname": "dawn2"
}
},
"model_blocked": {
"home": null,
"cabin": null,
"open": {
"code": "taken",
"ifname": "dawn2"
}
},
"cmds": [
],
"intents": {
"dawn0": {
"reason": "settings",
"name": "Anna's house"
},
"dawn1": {
"reason": "off",
"name": "Cabin"
}
},
"no_awg": false
},
{
"what": "a server removed, another given a name that will not do",
"changed": true,
"network": "\nconfig interface 'loopback'\n\toption device 'lo'\n\toption proto 'static'\n\tlist ipaddr '127.0.0.1/8'\n\nconfig globals 'globals'\n\toption ula_prefix 'fd00:1234::/48'\n\nconfig device\n\toption name 'br-lan'\n\toption type 'bridge'\n\tlist ports 'lan1'\n\tlist ports 'lan2'\n\nconfig interface 'lan'\n\toption device 'br-lan'\n\toption proto 'static'\n\toption ipaddr '192.168.77.1/24'\n\toption note 'it'\\''s the LAN'\n\nconfig interface 'dawn2'\n\toption proto 'wireguard'\n\toption private_key 'someone else'\\''s'\n\nconfig interface 'dawn0'\n\toption proto 'amneziawg'\n\toption private_key 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA='\n\toption listen_port '4501'\n\tlist addresses '10.77.0.1/24'\n\toption mtu '1380'\n\toption _dawnline 'home'\n\toption _dawnline_name 'House'\n\n",
"blocked": {
"open": {
"code": "ifname",
"ifname": "Not-A-Name"
}
},
"model_blocked": {
"home": null,
"open": {
"code": "ifname",
"ifname": "Not-A-Name"
}
},
"cmds": [
"ifdown dawn1",
"ubus call network reload"
],
"intents": {
"dawn0": {
"reason": "settings",
"name": "Anna's house"
},
"dawn1": {
"reason": "removed",
"name": "Cabin"
}
},
"no_awg": false
},
{
"what": "a 3.1 disguise, and a list of DNS-shaped packets",
"changed": true,
"network": "\nconfig interface 'loopback'\n\toption device 'lo'\n\toption proto 'static'\n\tlist ipaddr '127.0.0.1/8'\n\nconfig globals 'globals'\n\toption ula_prefix 'fd00:1234::/48'\n\nconfig device\n\toption name 'br-lan'\n\toption type 'bridge'\n\tlist ports 'lan1'\n\tlist ports 'lan2'\n\nconfig interface 'lan'\n\toption device 'br-lan'\n\toption proto 'static'\n\toption ipaddr '192.168.77.1/24'\n\toption note 'it'\\''s the LAN'\n\nconfig interface 'dawn2'\n\toption proto 'wireguard'\n\toption private_key 'someone else'\\''s'\n\nconfig interface 'dawn0'\n\toption proto 'amneziawg'\n\toption private_key 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA='\n\toption listen_port '4501'\n\tlist addresses '10.77.0.1/24'\n\toption mtu '1368'\n\toption _dawnline 'home'\n\toption _dawnline_name 'House'\n\toption awg_s1 '12'\n\toption awg_s2 '12'\n\toption awg_s3 '12'\n\toption awg_s4 '12'\n\toption awg_i1 '<b 0x01><r 2>'\n\toption awg_header_protection_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\n",
"blocked": {
"open": {
"code": "ifname",
"ifname": "Not-A-Name"
}
},
"model_blocked": {
"home": null,
"open": {
"code": "ifname",
"ifname": "Not-A-Name"
}
},
"cmds": [
"ubus call network reload",
"ifup dawn0"
],
"intents": {
"dawn0": {
"reason": "settings",
"name": "House"
},
"dawn1": {
"reason": "removed",
"name": "Cabin"
}
},
"no_awg": false
},
{
"what": "the tools are gone: nothing is touched",
"changed": false,
"network": "\nconfig interface 'loopback'\n\toption device 'lo'\n\toption proto 'static'\n\tlist ipaddr '127.0.0.1/8'\n\nconfig globals 'globals'\n\toption ula_prefix 'fd00:1234::/48'\n\nconfig device\n\toption name 'br-lan'\n\toption type 'bridge'\n\tlist ports 'lan1'\n\tlist ports 'lan2'\n\nconfig interface 'lan'\n\toption device 'br-lan'\n\toption proto 'static'\n\toption ipaddr '192.168.77.1/24'\n\toption note 'it'\\''s the LAN'\n\nconfig interface 'dawn2'\n\toption proto 'wireguard'\n\toption private_key 'someone else'\\''s'\n\nconfig interface 'dawn0'\n\toption proto 'amneziawg'\n\toption private_key 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA='\n\toption listen_port '4501'\n\tlist addresses '10.77.0.1/24'\n\toption mtu '1368'\n\toption _dawnline 'home'\n\toption _dawnline_name 'House'\n\toption awg_s1 '12'\n\toption awg_s2 '12'\n\toption awg_s3 '12'\n\toption awg_s4 '12'\n\toption awg_i1 '<b 0x01><r 2>'\n\toption awg_header_protection_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='\n\n",
"blocked": {
"open": {
"code": "ifname",
"ifname": "Not-A-Name"
}
},
"model_blocked": {
"home": null,
"open": {
"code": "ifname",
"ifname": "Not-A-Name"
}
},
"cmds": [
],
"intents": {
"dawn0": {
"reason": "settings",
"name": "House"
},
"dawn1": {
"reason": "removed",
"name": "Cabin"
}
},
"no_awg": true
}
]
}
+25
View File
@@ -0,0 +1,25 @@
config interface 'loopback'
option device 'lo'
option proto 'static'
list ipaddr '127.0.0.1/8'
config globals 'globals'
option ula_prefix 'fd00:1234::/48'
config device
option name 'br-lan'
option type 'bridge'
list ports 'lan1'
list ports 'lan2'
config interface 'lan'
option device 'br-lan'
option proto 'static'
option ipaddr '192.168.77.1/24'
option note 'it'\''s the LAN'
config interface 'dawn2'
option proto 'wireguard'
option private_key 'someone else'\''s'
+107
View File
@@ -0,0 +1,107 @@
{
"dhcp": "config dnsmasq\n\toption domainneeded\t1\n\toption boguspriv\t1\n\toption filterwin2k\t0 # enable for dial on demand\n\toption localise_queries\t1\n\toption rebind_protection 1 # disable if upstream must serve RFC1918 addresses\n\toption rebind_localhost 1 # enable for RBL checking and similar services\n\t#list rebind_domain example.lan # whitelist RFC1918 responses for domains\n\toption local\t'/lan/'\n\toption domain\t'lan'\n\toption expandhosts\t1\n\toption nonegcache\t0\n\toption cachesize\t1000\n\toption authoritative\t1\n\toption readethers\t1\n\toption leasefile\t'/tmp/dhcp.leases'\n\toption resolvfile\t'/tmp/resolv.conf.d/resolv.conf.auto'\n\t#list server\t\t'/mycompany.local/1.2.3.4'\n\toption nonwildcard\t1 # bind to & keep track of interfaces\n\t#list interface\t\tbr-lan\n\t#list notinterface\tlo\n\t#list bogusnxdomain '64.94.110.11'\n\toption localservice\t1 # disable to allow DNS requests from non-local subnets\n\toption ednspacket_max\t1232\n\toption filter_aaaa\t0\n\toption filter_a\t\t0\n\t#list addnmount\t\t/some/path # read-only mount path to expose it to dnsmasq\n\nconfig dhcp lan\n\toption interface\tlan\n\toption start \t100\n\toption limit\t150\n\toption leasetime\t12h\n\nconfig dhcp wan\n\toption interface\twan\n\toption ignore\t1\n",
"steps": [
{
"what": "on the tunnels, home names from the LAN",
"target": {
"mode": "root",
"port": 5354
},
"lan": "192.168.77.1",
"changed": true,
"conf": "# mode=root\n# Generated by Dawnline: plain DNS for Home-only tunnel clients.\nport=5354\nno-hosts\nbogus-priv\ncache-size=1000\npid-file=/var/run/dawnline-dns.pid\nuser=dnsmasq\ngroup=dnsmasq\nbind-dynamic\ninterface=dawn0\ninterface=dawn1\ninterface=dawn2\nresolv-file=/var/etc/dawnline-dns.resolv\nserver=/lan/192.168.77.1\n",
"resolv": "nameserver 9.9.9.9\nnameserver 149.112.112.112\n",
"cmds": [
"ubus call network.interface.wan status",
"ubus call network.interface.wan6 status",
"ubus call network.interface.wan status",
"ubus call network.interface.wan6 status"
]
},
{
"what": "again: nothing to do",
"target": {
"mode": "root",
"port": 5354
},
"lan": "192.168.77.1",
"changed": false,
"conf": "# mode=root\n# Generated by Dawnline: plain DNS for Home-only tunnel clients.\nport=5354\nno-hosts\nbogus-priv\ncache-size=1000\npid-file=/var/run/dawnline-dns.pid\nuser=dnsmasq\ngroup=dnsmasq\nbind-dynamic\ninterface=dawn0\ninterface=dawn1\ninterface=dawn2\nresolv-file=/var/etc/dawnline-dns.resolv\nserver=/lan/192.168.77.1\n",
"resolv": "nameserver 9.9.9.9\nnameserver 149.112.112.112\n",
"cmds": [
"ubus call network.interface.wan status",
"ubus call network.interface.wan6 status"
]
},
{
"what": "inside the netns",
"target": {
"mode": "ns",
"address": "100.127.0.2",
"port": 53
},
"lan": "192.168.77.53",
"changed": true,
"conf": "# mode=ns\n# Generated by Dawnline: plain DNS for Home-only tunnel clients.\nport=53\nno-hosts\nbogus-priv\ncache-size=1000\npid-file=/var/run/dawnline-dns.pid\nuser=dnsmasq\ngroup=dnsmasq\nlisten-address=100.127.0.2\nbind-interfaces\nno-resolv\nserver=9.9.9.9@100.127.0.2\nserver=149.112.112.112@100.127.0.2\nserver=/lan/192.168.77.53\n",
"resolv": "nameserver 9.9.9.9\nnameserver 149.112.112.112\n",
"cmds": [
"ubus call network.interface.wan status",
"ubus call network.interface.wan6 status",
"ubus call network.interface.wan status",
"ubus call network.interface.wan6 status"
]
},
{
"what": "back on the tunnels with one of them off, no DNS on the LAN, a running one to stop",
"target": {
"mode": "root",
"port": 5354
},
"lan": null,
"changed": true,
"conf": "# mode=root\n# Generated by Dawnline: plain DNS for Home-only tunnel clients.\nport=5354\nno-hosts\nbogus-priv\ncache-size=1000\npid-file=/var/run/dawnline-dns.pid\nuser=dnsmasq\ngroup=dnsmasq\nbind-dynamic\ninterface=dawn0\ninterface=dawn2\nresolv-file=/var/etc/dawnline-dns.resolv\n",
"resolv": "nameserver 9.9.9.9\nnameserver 149.112.112.112\n",
"cmds": [
"ubus call network.interface.wan status",
"ubus call network.interface.wan6 status",
"ubus call network.interface.wan status",
"ubus call network.interface.wan6 status"
]
},
{
"what": "no tunnel on: loopback only",
"target": {
"mode": "root",
"port": 5354
},
"lan": "192.168.77.1",
"changed": true,
"conf": "# mode=root\n# Generated by Dawnline: plain DNS for Home-only tunnel clients.\nport=5354\nno-hosts\nbogus-priv\ncache-size=1000\npid-file=/var/run/dawnline-dns.pid\nuser=dnsmasq\ngroup=dnsmasq\nbind-dynamic\nlisten-address=127.0.0.1\nresolv-file=/var/etc/dawnline-dns.resolv\nserver=/lan/192.168.77.1\n",
"resolv": "nameserver 9.9.9.9\nnameserver 149.112.112.112\n",
"cmds": [
"ubus call network.interface.wan status",
"ubus call network.interface.wan6 status",
"ubus call network.interface.wan status",
"ubus call network.interface.wan6 status"
]
},
{
"what": "the WAN says nothing of DNS: two public ones",
"target": {
"mode": "ns",
"address": "100.127.0.2",
"port": 53
},
"lan": "192.168.77.1",
"changed": true,
"conf": "# mode=ns\n# Generated by Dawnline: plain DNS for Home-only tunnel clients.\nport=53\nno-hosts\nbogus-priv\ncache-size=1000\npid-file=/var/run/dawnline-dns.pid\nuser=dnsmasq\ngroup=dnsmasq\nlisten-address=100.127.0.2\nbind-interfaces\nno-resolv\nserver=1.1.1.1@100.127.0.2\nserver=8.8.8.8@100.127.0.2\nserver=/lan/192.168.77.1\n",
"resolv": "nameserver 9.9.9.9\nnameserver 149.112.112.112\n",
"cmds": [
"ubus call network.interface.wan status",
"ubus call network.interface.wan6 status",
"ubus call network.interface.wan status",
"ubus call network.interface.wan6 status"
]
}
]
}
+7
View File
@@ -0,0 +1,7 @@
{
"sigs": [
"{ \"wan\": \"eth0\", \"lan\": \"br-lan\", \"addrs\": [ \"192.168.77.1/24\" ], \"servers\": { \"home\": { \"wan\": { \"dev\": \"eth0\", \"auto\": true, \"zone\": \"wan\", \"missing\": false }, \"home\": [ { \"dev\": \"dlhome\", \"auto\": false, \"nets\": [ \"192.168.50.0/24\" ], \"zone\": \"home\", \"missing\": false }, { \"dev\": \"br-lan\", \"auto\": false, \"nets\": [ \"192.168.77.0/24\" ], \"zone\": \"home\", \"missing\": false } ], \"nets\": [ \"192.168.50.0/24\", \"192.168.77.0/24\" ], \"zones\": [ \"home\" ] }, \"cabin\": { \"wan\": { \"dev\": \"eth0\", \"auto\": true, \"zone\": \"wan\", \"missing\": false }, \"home\": [ { \"dev\": \"dlhome\", \"auto\": false, \"nets\": [ \"192.168.50.0/24\" ], \"zone\": \"home\", \"missing\": false } ], \"nets\": [ \"192.168.50.0/24\" ], \"zones\": [ \"home\" ] }, \"open\": { \"wan\": { \"dev\": \"eth0\", \"auto\": true, \"zone\": \"wan\", \"missing\": false }, \"home\": [ { \"dev\": \"br-lan\", \"auto\": false, \"nets\": [ \"192.168.77.0/24\" ], \"zone\": \"home\", \"missing\": false } ], \"nets\": [ \"192.168.77.0/24\" ], \"zones\": [ \"home\" ] } }, \"dns\": \"192.168.77.1\", \"loop\": null }",
"{ \"wan\": \"eth0\", \"lan\": \"br-lan\", \"addrs\": [ \"192.168.77.1/24\" ], \"servers\": { \"home\": { \"wan\": { \"dev\": \"eth0\", \"auto\": true, \"zone\": \"wan\", \"missing\": false }, \"home\": [ { \"dev\": \"dlhome\", \"auto\": false, \"nets\": [ \"192.168.50.0/24\" ], \"zone\": \"home\", \"missing\": false }, { \"dev\": \"nope0\", \"auto\": false, \"nets\": [ ], \"zone\": null, \"missing\": true } ], \"nets\": [ \"192.168.50.0/24\" ], \"zones\": [ \"home\" ] }, \"cabin\": { \"wan\": { \"dev\": \"eth0\", \"auto\": true, \"zone\": \"wan\", \"missing\": false }, \"home\": [ { \"dev\": \"br-lan\", \"auto\": true, \"nets\": [ \"192.168.77.0/24\" ], \"zone\": \"home\", \"missing\": false }, { \"dev\": \"dlhome\", \"auto\": false, \"nets\": [ \"192.168.50.0/24\" ], \"zone\": \"home\", \"missing\": false } ], \"nets\": [ \"192.168.50.0/24\", \"192.168.77.0/24\" ], \"zones\": [ \"home\" ] }, \"open\": { \"wan\": { \"dev\": \"wg9\", \"auto\": false, \"zone\": \"wan\", \"missing\": false }, \"home\": [ { \"dev\": \"br-lan\", \"auto\": false, \"nets\": [ \"192.168.77.0/24\" ], \"zone\": \"home\", \"missing\": false } ], \"nets\": [ \"192.168.77.0/24\" ], \"zones\": [ \"home\" ] } }, \"dns\": \"192.168.77.1\", \"loop\": null }",
"{ \"wan\": \"eth0\", \"lan\": \"br-lan\", \"addrs\": [ \"192.168.77.1/24\" ], \"servers\": { \"home\": { \"wan\": { \"dev\": \"eth0\", \"auto\": true, \"zone\": \"wan\", \"missing\": false }, \"home\": [ { \"dev\": \"dlhome\", \"auto\": false, \"nets\": [ \"192.168.50.0/24\" ], \"zone\": \"home\", \"missing\": false }, { \"dev\": \"nope0\", \"auto\": false, \"nets\": [ ], \"zone\": null, \"missing\": true } ], \"nets\": [ \"192.168.50.0/24\" ], \"zones\": [ \"home\" ] }, \"cabin\": { \"wan\": { \"dev\": \"eth0\", \"auto\": true, \"zone\": \"wan\", \"missing\": false }, \"home\": [ { \"dev\": \"br-lan\", \"auto\": true, \"nets\": [ \"192.168.77.0/24\" ], \"zone\": \"home\", \"missing\": false }, { \"dev\": \"dlhome\", \"auto\": false, \"nets\": [ \"192.168.50.0/24\" ], \"zone\": \"home\", \"missing\": false } ], \"nets\": [ \"192.168.50.0/24\", \"192.168.77.0/24\" ], \"zones\": [ \"home\" ] }, \"open\": { \"wan\": { \"dev\": \"gone7\", \"auto\": false, \"zone\": \"wan\", \"missing\": true }, \"home\": [ { \"dev\": \"gone8\", \"auto\": false, \"nets\": [ ], \"zone\": null, \"missing\": true } ], \"nets\": [ ], \"zones\": [ ] } }, \"dns\": \"192.168.77.1\", \"loop\": null }"
]
}
+328
View File
@@ -0,0 +1,328 @@
{
"steps": [
{
"what": "no table yet: the update fails, the table is built",
"peers": {
"home": "ok",
"cabin": "ok",
"open": "ok"
},
"table": null,
"warning": null,
"states": {
"anna": "active",
"boris": "active",
"clara": "active",
"dima": "active",
"eva": "active",
"fedor": "active",
"gleb": "paused",
"hana": "active",
"ivan": "active",
"kira": "active",
"lev": "active",
"nora": "active"
},
"scripts": [
"table inet dawnline\ndelete table inet dawnline\ntable inet dawnline {\n\tset home_home { type ipv4_addr; flags interval; elements = { 192.168.50.0/24, 192.168.77.0/24 } }\n\tset home_cabin { type ipv4_addr; flags interval; elements = { 192.168.50.0/24 } }\n\tset home_open { type ipv4_addr; flags interval; elements = { 192.168.77.0/24 } }\n\tset tun_nets { type ipv4_addr; flags interval; elements = { 10.77.0.0/24, 10.78.0.0/24, 10.79.0.0/24 } }\n\tset lan_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.7, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset lan_all { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3 } }\n\tset lan_part { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.7 . 192.168.50.0/24, 10.77.0.11 . 192.168.77.0/24 } }\n\tset mesh_pairs { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.2 . 10.77.0.3, 10.77.0.2 . 10.77.0.5, 10.77.0.2 . 10.77.0.11, 10.77.0.3 . 10.77.0.2, 10.77.0.3 . 10.77.0.5, 10.77.0.3 . 10.77.0.11, 10.77.0.5 . 10.77.0.2, 10.77.0.5 . 10.77.0.3, 10.77.0.5 . 10.77.0.11, 10.77.0.11 . 10.77.0.2, 10.77.0.11 . 10.77.0.3, 10.77.0.11 . 10.77.0.5 } }\n\tset inet_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.6, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset svc_ok { type ipv4_addr . ipv4_addr . inet_service; flags interval; }\n\tset svc_host { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.8 . 192.168.77.5, 10.77.0.8 . 192.168.50.30 } }\n\tset dns_real { type ipv4_addr; flags interval; }\n\tchain pre {\n\t\ttype filter hook prerouting priority -160; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } jump guard\n\t}\n\tchain guard {\n\t\tmeta nfproto ipv6 drop\n\t\tct state related accept\n\t\tct status dnat accept\n\t\ticmp type { echo-request, echo-reply } fib daddr type local accept\n\t\tfib daddr type local meta l4proto { tcp, udp } th dport 53 accept\n\t\tfib daddr type local tcp dport { 4500, 4501 } accept\n\t\tfib daddr type local ip saddr @lan_all accept\n\t\tfib daddr type local ip saddr . ip daddr @lan_part accept\n\t\tfib daddr type local counter drop\n\t\tiifname \"dawn0\" ip daddr @home_home ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn1\" ip daddr @home_cabin ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn2\" ip daddr @home_open ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tip saddr . ip daddr @lan_part ip daddr != @tun_nets accept\n\t\tiifname \"dawn0\" ip saddr . ip daddr @mesh_pairs accept\n\t\tiifname \"dawn2\" ip saddr @lan_ok ip daddr @lan_ok accept\n\t\tmeta l4proto { tcp, udp } ip saddr . ip daddr . th dport @svc_ok accept\n\t\tip saddr . ip daddr @svc_host accept\n\t\tip daddr { 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 100.64.0.0/10, 169.254.0.0/16, 224.0.0.0/3 } counter drop\n\t\tip saddr @inet_ok accept\n\t\tcounter drop\n\t}\n\tchain inbound {\n\t\ttype filter hook forward priority filter - 5; policy accept;\n\t\toifname { \"dawn0\", \"dawn1\", \"dawn2\" } iifname != { \"dawn0\", \"dawn1\", \"dawn2\" } ct state new jump to_device\n\t}\n\tchain to_device {\n\t\tmeta nfproto ipv6 drop\n\t\tct status dnat accept\n\t\toifname \"dawn0\" ip saddr @home_home ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn1\" ip saddr @home_cabin ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn2\" ip saddr @home_open ip saddr != @tun_nets ip daddr @lan_all accept\n\t\tip daddr . ip saddr @lan_part ip saddr != @tun_nets accept\n\t\tcounter reject with icmpx admin-prohibited\n\t}\n\tchain dns {\n\t\ttype nat hook prerouting priority dstnat - 5; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } ip saddr @dns_real meta l4proto { tcp, udp } th dport 53 redirect to :5354\n\t}\n\tchain dns_lan {\n\t\ttype nat hook prerouting priority dstnat + 5; policy accept;\n\t\tiifname \"dawn0\" ip daddr 10.77.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn1\" ip daddr 10.78.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn2\" ip daddr 10.79.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t}\n}\n"
],
"confs": {
"dawn0": "[Interface]\nPrivateKey = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA=\nListenPort = 4500\n\n[Peer]\nPublicKey = BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA=\nAllowedIPs = 10.77.0.2/32\n\n[Peer]\nPublicKey = CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCA=\nAllowedIPs = 10.77.0.3/32\n\n[Peer]\nPublicKey = EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEA=\nAllowedIPs = 10.77.0.5/32\n\n[Peer]\nPublicKey = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA=\nAllowedIPs = 10.77.0.6/32\n\n[Peer]\nPublicKey = GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGA=\nAllowedIPs = 10.77.0.7/32\n\n[Peer]\nPublicKey = HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHA=\nAllowedIPs = 10.77.0.8/32\n\n[Peer]\nPublicKey = IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIA=\nAllowedIPs = 10.77.0.9/32\n\n[Peer]\nPublicKey = OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOA=\nAllowedIPs = 10.77.0.11/32\n",
"dawn1": "[Interface]\nPrivateKey = ccccccccccccccccccccccccccccccccccccccccccA=\nListenPort = 4600\n\n[Peer]\nPublicKey = KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKA=\nAllowedIPs = 10.78.0.2/32\n\n[Peer]\nPublicKey = LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLA=\nAllowedIPs = 10.78.0.3/32\n",
"dawn2": "[Interface]\nPrivateKey = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeA=\nListenPort = 4700\n\n[Peer]\nPublicKey = MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMA=\nAllowedIPs = 10.79.0.2/32\n\n[Peer]\nPublicKey = NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNA=\nAllowedIPs = 10.79.0.3/32\n"
},
"hosts": "10.77.0.2 phone.anna.lan\n10.77.0.3 laptop.anna.lan\n10.77.0.4 old.anna.lan\n10.77.0.5 bphone.boris.lan\n10.77.0.6 cphone.clara.lan\n10.77.0.7 dphone.dima.lan\n10.77.0.8 ephone.eva.lan\n10.77.0.9 fphone.fedor.lan\n10.77.0.10 gphone.gleb.lan\n10.78.0.2 hphone.hana.lan\n10.78.0.3 iphone.ivan.lan\n10.79.0.2 kphone.kira.lan\n10.79.0.3 lphone.lev.lan\n10.77.0.11 nphone.nora.lan\n",
"hosts_mode": true,
"nft_err": null,
"present": true
},
{
"what": "again: the sets alone",
"peers": {
"home": "ok",
"cabin": "ok",
"open": "ok"
},
"table": null,
"warning": null,
"states": {
"anna": "active",
"boris": "active",
"clara": "active",
"dima": "active",
"eva": "active",
"fedor": "active",
"gleb": "paused",
"hana": "active",
"ivan": "active",
"kira": "active",
"lev": "active",
"nora": "active"
},
"scripts": [
"table inet dawnline\ndelete table inet dawnline\ntable inet dawnline {\n\tset home_home { type ipv4_addr; flags interval; elements = { 192.168.50.0/24, 192.168.77.0/24 } }\n\tset home_cabin { type ipv4_addr; flags interval; elements = { 192.168.50.0/24 } }\n\tset home_open { type ipv4_addr; flags interval; elements = { 192.168.77.0/24 } }\n\tset tun_nets { type ipv4_addr; flags interval; elements = { 10.77.0.0/24, 10.78.0.0/24, 10.79.0.0/24 } }\n\tset lan_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.7, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset lan_all { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3 } }\n\tset lan_part { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.7 . 192.168.50.0/24, 10.77.0.11 . 192.168.77.0/24 } }\n\tset mesh_pairs { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.2 . 10.77.0.3, 10.77.0.2 . 10.77.0.5, 10.77.0.2 . 10.77.0.11, 10.77.0.3 . 10.77.0.2, 10.77.0.3 . 10.77.0.5, 10.77.0.3 . 10.77.0.11, 10.77.0.5 . 10.77.0.2, 10.77.0.5 . 10.77.0.3, 10.77.0.5 . 10.77.0.11, 10.77.0.11 . 10.77.0.2, 10.77.0.11 . 10.77.0.3, 10.77.0.11 . 10.77.0.5 } }\n\tset inet_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.6, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset svc_ok { type ipv4_addr . ipv4_addr . inet_service; flags interval; }\n\tset svc_host { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.8 . 192.168.77.5, 10.77.0.8 . 192.168.50.30 } }\n\tset dns_real { type ipv4_addr; flags interval; }\n\tchain pre {\n\t\ttype filter hook prerouting priority -160; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } jump guard\n\t}\n\tchain guard {\n\t\tmeta nfproto ipv6 drop\n\t\tct state related accept\n\t\tct status dnat accept\n\t\ticmp type { echo-request, echo-reply } fib daddr type local accept\n\t\tfib daddr type local meta l4proto { tcp, udp } th dport 53 accept\n\t\tfib daddr type local tcp dport { 4500, 4501 } accept\n\t\tfib daddr type local ip saddr @lan_all accept\n\t\tfib daddr type local ip saddr . ip daddr @lan_part accept\n\t\tfib daddr type local counter drop\n\t\tiifname \"dawn0\" ip daddr @home_home ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn1\" ip daddr @home_cabin ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn2\" ip daddr @home_open ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tip saddr . ip daddr @lan_part ip daddr != @tun_nets accept\n\t\tiifname \"dawn0\" ip saddr . ip daddr @mesh_pairs accept\n\t\tiifname \"dawn2\" ip saddr @lan_ok ip daddr @lan_ok accept\n\t\tmeta l4proto { tcp, udp } ip saddr . ip daddr . th dport @svc_ok accept\n\t\tip saddr . ip daddr @svc_host accept\n\t\tip daddr { 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 100.64.0.0/10, 169.254.0.0/16, 224.0.0.0/3 } counter drop\n\t\tip saddr @inet_ok accept\n\t\tcounter drop\n\t}\n\tchain inbound {\n\t\ttype filter hook forward priority filter - 5; policy accept;\n\t\toifname { \"dawn0\", \"dawn1\", \"dawn2\" } iifname != { \"dawn0\", \"dawn1\", \"dawn2\" } ct state new jump to_device\n\t}\n\tchain to_device {\n\t\tmeta nfproto ipv6 drop\n\t\tct status dnat accept\n\t\toifname \"dawn0\" ip saddr @home_home ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn1\" ip saddr @home_cabin ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn2\" ip saddr @home_open ip saddr != @tun_nets ip daddr @lan_all accept\n\t\tip daddr . ip saddr @lan_part ip saddr != @tun_nets accept\n\t\tcounter reject with icmpx admin-prohibited\n\t}\n\tchain dns {\n\t\ttype nat hook prerouting priority dstnat - 5; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } ip saddr @dns_real meta l4proto { tcp, udp } th dport 53 redirect to :5354\n\t}\n\tchain dns_lan {\n\t\ttype nat hook prerouting priority dstnat + 5; policy accept;\n\t\tiifname \"dawn0\" ip daddr 10.77.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn1\" ip daddr 10.78.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn2\" ip daddr 10.79.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t}\n}\n"
],
"confs": {
"dawn0": "[Interface]\nPrivateKey = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA=\nListenPort = 4500\n\n[Peer]\nPublicKey = BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA=\nAllowedIPs = 10.77.0.2/32\n\n[Peer]\nPublicKey = CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCA=\nAllowedIPs = 10.77.0.3/32\n\n[Peer]\nPublicKey = EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEA=\nAllowedIPs = 10.77.0.5/32\n\n[Peer]\nPublicKey = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA=\nAllowedIPs = 10.77.0.6/32\n\n[Peer]\nPublicKey = GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGA=\nAllowedIPs = 10.77.0.7/32\n\n[Peer]\nPublicKey = HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHA=\nAllowedIPs = 10.77.0.8/32\n\n[Peer]\nPublicKey = IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIA=\nAllowedIPs = 10.77.0.9/32\n\n[Peer]\nPublicKey = OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOA=\nAllowedIPs = 10.77.0.11/32\n",
"dawn1": "[Interface]\nPrivateKey = ccccccccccccccccccccccccccccccccccccccccccA=\nListenPort = 4600\n\n[Peer]\nPublicKey = KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKA=\nAllowedIPs = 10.78.0.2/32\n\n[Peer]\nPublicKey = LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLA=\nAllowedIPs = 10.78.0.3/32\n",
"dawn2": "[Interface]\nPrivateKey = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeA=\nListenPort = 4700\n\n[Peer]\nPublicKey = MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMA=\nAllowedIPs = 10.79.0.2/32\n\n[Peer]\nPublicKey = NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNA=\nAllowedIPs = 10.79.0.3/32\n"
},
"hosts": "10.77.0.2 phone.anna.lan\n10.77.0.3 laptop.anna.lan\n10.77.0.4 old.anna.lan\n10.77.0.5 bphone.boris.lan\n10.77.0.6 cphone.clara.lan\n10.77.0.7 dphone.dima.lan\n10.77.0.8 ephone.eva.lan\n10.77.0.9 fphone.fedor.lan\n10.77.0.10 gphone.gleb.lan\n10.78.0.2 hphone.hana.lan\n10.78.0.3 iphone.ivan.lan\n10.79.0.2 kphone.kira.lan\n10.79.0.3 lphone.lev.lan\n10.77.0.11 nphone.nora.lan\n",
"hosts_mode": true,
"nft_err": null,
"present": true
},
{
"what": "someone is paused",
"peers": {
"home": "ok",
"cabin": "ok",
"open": "ok"
},
"table": null,
"warning": null,
"states": {
"anna": "paused",
"boris": "active",
"clara": "active",
"dima": "active",
"eva": "active",
"fedor": "active",
"gleb": "paused",
"hana": "active",
"ivan": "active",
"kira": "active",
"lev": "active",
"nora": "active"
},
"scripts": [
"table inet dawnline\ndelete table inet dawnline\ntable inet dawnline {\n\tset home_home { type ipv4_addr; flags interval; elements = { 192.168.50.0/24, 192.168.77.0/24 } }\n\tset home_cabin { type ipv4_addr; flags interval; elements = { 192.168.50.0/24 } }\n\tset home_open { type ipv4_addr; flags interval; elements = { 192.168.77.0/24 } }\n\tset tun_nets { type ipv4_addr; flags interval; elements = { 10.77.0.0/24, 10.78.0.0/24, 10.79.0.0/24 } }\n\tset lan_ok { type ipv4_addr; flags interval; elements = { 10.77.0.5, 10.77.0.7, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset lan_all { type ipv4_addr; flags interval; elements = { 10.77.0.5, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3 } }\n\tset lan_part { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.7 . 192.168.50.0/24, 10.77.0.11 . 192.168.77.0/24 } }\n\tset mesh_pairs { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.5 . 10.77.0.11, 10.77.0.11 . 10.77.0.5 } }\n\tset inet_ok { type ipv4_addr; flags interval; elements = { 10.77.0.5, 10.77.0.6, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset svc_ok { type ipv4_addr . ipv4_addr . inet_service; flags interval; }\n\tset svc_host { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.8 . 192.168.77.5, 10.77.0.8 . 192.168.50.30 } }\n\tset dns_real { type ipv4_addr; flags interval; }\n\tchain pre {\n\t\ttype filter hook prerouting priority -160; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } jump guard\n\t}\n\tchain guard {\n\t\tmeta nfproto ipv6 drop\n\t\tct state related accept\n\t\tct status dnat accept\n\t\ticmp type { echo-request, echo-reply } fib daddr type local accept\n\t\tfib daddr type local meta l4proto { tcp, udp } th dport 53 accept\n\t\tfib daddr type local tcp dport { 4500, 4501 } accept\n\t\tfib daddr type local ip saddr @lan_all accept\n\t\tfib daddr type local ip saddr . ip daddr @lan_part accept\n\t\tfib daddr type local counter drop\n\t\tiifname \"dawn0\" ip daddr @home_home ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn1\" ip daddr @home_cabin ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn2\" ip daddr @home_open ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tip saddr . ip daddr @lan_part ip daddr != @tun_nets accept\n\t\tiifname \"dawn0\" ip saddr . ip daddr @mesh_pairs accept\n\t\tiifname \"dawn2\" ip saddr @lan_ok ip daddr @lan_ok accept\n\t\tmeta l4proto { tcp, udp } ip saddr . ip daddr . th dport @svc_ok accept\n\t\tip saddr . ip daddr @svc_host accept\n\t\tip daddr { 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 100.64.0.0/10, 169.254.0.0/16, 224.0.0.0/3 } counter drop\n\t\tip saddr @inet_ok accept\n\t\tcounter drop\n\t}\n\tchain inbound {\n\t\ttype filter hook forward priority filter - 5; policy accept;\n\t\toifname { \"dawn0\", \"dawn1\", \"dawn2\" } iifname != { \"dawn0\", \"dawn1\", \"dawn2\" } ct state new jump to_device\n\t}\n\tchain to_device {\n\t\tmeta nfproto ipv6 drop\n\t\tct status dnat accept\n\t\toifname \"dawn0\" ip saddr @home_home ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn1\" ip saddr @home_cabin ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn2\" ip saddr @home_open ip saddr != @tun_nets ip daddr @lan_all accept\n\t\tip daddr . ip saddr @lan_part ip saddr != @tun_nets accept\n\t\tcounter reject with icmpx admin-prohibited\n\t}\n\tchain dns {\n\t\ttype nat hook prerouting priority dstnat - 5; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } ip saddr @dns_real meta l4proto { tcp, udp } th dport 53 redirect to :5354\n\t}\n\tchain dns_lan {\n\t\ttype nat hook prerouting priority dstnat + 5; policy accept;\n\t\tiifname \"dawn0\" ip daddr 10.77.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn1\" ip daddr 10.78.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn2\" ip daddr 10.79.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t}\n}\n"
],
"confs": {
"dawn0": "[Interface]\nPrivateKey = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA=\nListenPort = 4500\n\n[Peer]\nPublicKey = EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEA=\nAllowedIPs = 10.77.0.5/32\n\n[Peer]\nPublicKey = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA=\nAllowedIPs = 10.77.0.6/32\n\n[Peer]\nPublicKey = GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGA=\nAllowedIPs = 10.77.0.7/32\n\n[Peer]\nPublicKey = HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHA=\nAllowedIPs = 10.77.0.8/32\n\n[Peer]\nPublicKey = IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIA=\nAllowedIPs = 10.77.0.9/32\n\n[Peer]\nPublicKey = OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOA=\nAllowedIPs = 10.77.0.11/32\n",
"dawn1": "[Interface]\nPrivateKey = ccccccccccccccccccccccccccccccccccccccccccA=\nListenPort = 4600\n\n[Peer]\nPublicKey = KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKA=\nAllowedIPs = 10.78.0.2/32\n\n[Peer]\nPublicKey = LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLA=\nAllowedIPs = 10.78.0.3/32\n",
"dawn2": "[Interface]\nPrivateKey = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeA=\nListenPort = 4700\n\n[Peer]\nPublicKey = MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMA=\nAllowedIPs = 10.79.0.2/32\n\n[Peer]\nPublicKey = NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNA=\nAllowedIPs = 10.79.0.3/32\n"
},
"hosts": "10.77.0.2 phone.anna.lan\n10.77.0.3 laptop.anna.lan\n10.77.0.4 old.anna.lan\n10.77.0.5 bphone.boris.lan\n10.77.0.6 cphone.clara.lan\n10.77.0.7 dphone.dima.lan\n10.77.0.8 ephone.eva.lan\n10.77.0.9 fphone.fedor.lan\n10.77.0.10 gphone.gleb.lan\n10.78.0.2 hphone.hana.lan\n10.78.0.3 iphone.ivan.lan\n10.79.0.2 kphone.kira.lan\n10.79.0.3 lphone.lev.lan\n10.77.0.11 nphone.nora.lan\n",
"hosts_mode": true,
"nft_err": null,
"present": true
},
{
"what": "the table will not load, the old one stays",
"peers": {
"home": "ok",
"cabin": "ok",
"open": "ok"
},
"table": "rules:1:1-5: Error: a made-up failure",
"warning": "Access rules couldn't be updated (rules:1:1-5: Error: a made-up failure): the previous rules stay, so this change isn't in effect yet. Open Overview → Health.",
"states": {
"anna": "active",
"boris": "active",
"clara": "active",
"dima": "active",
"eva": "active",
"fedor": "active",
"gleb": "paused",
"hana": "active",
"ivan": "active",
"kira": "active",
"lev": "active",
"nora": "active"
},
"scripts": [
"table inet dawnline\ndelete table inet dawnline\ntable inet dawnline {\n\tset home_home { type ipv4_addr; flags interval; elements = { 192.168.50.0/24, 192.168.77.0/24 } }\n\tset home_cabin { type ipv4_addr; flags interval; elements = { 192.168.50.0/24 } }\n\tset home_open { type ipv4_addr; flags interval; elements = { 192.168.77.0/24 } }\n\tset tun_nets { type ipv4_addr; flags interval; elements = { 10.77.0.0/24, 10.78.0.0/24, 10.79.0.0/24 } }\n\tset lan_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.7, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset lan_all { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3 } }\n\tset lan_part { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.7 . 192.168.50.0/24, 10.77.0.11 . 192.168.77.0/24 } }\n\tset mesh_pairs { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.2 . 10.77.0.3, 10.77.0.2 . 10.77.0.5, 10.77.0.2 . 10.77.0.11, 10.77.0.3 . 10.77.0.2, 10.77.0.3 . 10.77.0.5, 10.77.0.3 . 10.77.0.11, 10.77.0.5 . 10.77.0.2, 10.77.0.5 . 10.77.0.3, 10.77.0.5 . 10.77.0.11, 10.77.0.11 . 10.77.0.2, 10.77.0.11 . 10.77.0.3, 10.77.0.11 . 10.77.0.5 } }\n\tset inet_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.6, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset svc_ok { type ipv4_addr . ipv4_addr . inet_service; flags interval; }\n\tset svc_host { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.8 . 192.168.77.5, 10.77.0.8 . 192.168.50.30 } }\n\tset dns_real { type ipv4_addr; flags interval; }\n\tchain pre {\n\t\ttype filter hook prerouting priority -160; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } jump guard\n\t}\n\tchain guard {\n\t\tmeta nfproto ipv6 drop\n\t\tct state related accept\n\t\tct status dnat accept\n\t\ticmp type { echo-request, echo-reply } fib daddr type local accept\n\t\tfib daddr type local meta l4proto { tcp, udp } th dport 53 accept\n\t\tfib daddr type local tcp dport { 4500, 4501 } accept\n\t\tfib daddr type local ip saddr @lan_all accept\n\t\tfib daddr type local ip saddr . ip daddr @lan_part accept\n\t\tfib daddr type local counter drop\n\t\tiifname \"dawn0\" ip daddr @home_home ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn1\" ip daddr @home_cabin ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn2\" ip daddr @home_open ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tip saddr . ip daddr @lan_part ip daddr != @tun_nets accept\n\t\tiifname \"dawn0\" ip saddr . ip daddr @mesh_pairs accept\n\t\tiifname \"dawn2\" ip saddr @lan_ok ip daddr @lan_ok accept\n\t\tmeta l4proto { tcp, udp } ip saddr . ip daddr . th dport @svc_ok accept\n\t\tip saddr . ip daddr @svc_host accept\n\t\tip daddr { 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 100.64.0.0/10, 169.254.0.0/16, 224.0.0.0/3 } counter drop\n\t\tip saddr @inet_ok accept\n\t\tcounter drop\n\t}\n\tchain inbound {\n\t\ttype filter hook forward priority filter - 5; policy accept;\n\t\toifname { \"dawn0\", \"dawn1\", \"dawn2\" } iifname != { \"dawn0\", \"dawn1\", \"dawn2\" } ct state new jump to_device\n\t}\n\tchain to_device {\n\t\tmeta nfproto ipv6 drop\n\t\tct status dnat accept\n\t\toifname \"dawn0\" ip saddr @home_home ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn1\" ip saddr @home_cabin ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn2\" ip saddr @home_open ip saddr != @tun_nets ip daddr @lan_all accept\n\t\tip daddr . ip saddr @lan_part ip saddr != @tun_nets accept\n\t\tcounter reject with icmpx admin-prohibited\n\t}\n\tchain dns {\n\t\ttype nat hook prerouting priority dstnat - 5; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } ip saddr @dns_real meta l4proto { tcp, udp } th dport 53 redirect to :5354\n\t}\n\tchain dns_lan {\n\t\ttype nat hook prerouting priority dstnat + 5; policy accept;\n\t\tiifname \"dawn0\" ip daddr 10.77.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn1\" ip daddr 10.78.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn2\" ip daddr 10.79.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t}\n}\n"
],
"confs": {
"dawn0": "[Interface]\nPrivateKey = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA=\nListenPort = 4500\n\n[Peer]\nPublicKey = BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA=\nAllowedIPs = 10.77.0.2/32\n\n[Peer]\nPublicKey = CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCA=\nAllowedIPs = 10.77.0.3/32\n\n[Peer]\nPublicKey = EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEA=\nAllowedIPs = 10.77.0.5/32\n\n[Peer]\nPublicKey = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA=\nAllowedIPs = 10.77.0.6/32\n\n[Peer]\nPublicKey = GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGA=\nAllowedIPs = 10.77.0.7/32\n\n[Peer]\nPublicKey = HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHA=\nAllowedIPs = 10.77.0.8/32\n\n[Peer]\nPublicKey = IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIA=\nAllowedIPs = 10.77.0.9/32\n\n[Peer]\nPublicKey = OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOA=\nAllowedIPs = 10.77.0.11/32\n",
"dawn1": "[Interface]\nPrivateKey = ccccccccccccccccccccccccccccccccccccccccccA=\nListenPort = 4600\n\n[Peer]\nPublicKey = KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKA=\nAllowedIPs = 10.78.0.2/32\n\n[Peer]\nPublicKey = LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLA=\nAllowedIPs = 10.78.0.3/32\n",
"dawn2": "[Interface]\nPrivateKey = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeA=\nListenPort = 4700\n\n[Peer]\nPublicKey = MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMA=\nAllowedIPs = 10.79.0.2/32\n\n[Peer]\nPublicKey = NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNA=\nAllowedIPs = 10.79.0.3/32\n"
},
"hosts": "10.77.0.2 phone.anna.lan\n10.77.0.3 laptop.anna.lan\n10.77.0.4 old.anna.lan\n10.77.0.5 bphone.boris.lan\n10.77.0.6 cphone.clara.lan\n10.77.0.7 dphone.dima.lan\n10.77.0.8 ephone.eva.lan\n10.77.0.9 fphone.fedor.lan\n10.77.0.10 gphone.gleb.lan\n10.78.0.2 hphone.hana.lan\n10.78.0.3 iphone.ivan.lan\n10.79.0.2 kphone.kira.lan\n10.79.0.3 lphone.lev.lan\n10.77.0.11 nphone.nora.lan\n",
"hosts_mode": true,
"nft_err": "rules:1:1-5: Error: a made-up failure",
"present": true
},
{
"what": "the table will not load and there is none: nobody is loaded",
"peers": {
"home": "ok",
"cabin": "ok",
"open": "ok"
},
"table": "rules:1:1-5: Error: a made-up failure",
"warning": "Access rules couldn't load (rules:1:1-5: Error: a made-up failure): no device is loaded into the tunnels until they do. Open Overview → Health.",
"states": {
"anna": "active",
"boris": "active",
"clara": "active",
"dima": "active",
"eva": "active",
"fedor": "active",
"gleb": "paused",
"hana": "active",
"ivan": "active",
"kira": "active",
"lev": "active",
"nora": "active"
},
"scripts": [
"table inet dawnline\ndelete table inet dawnline\ntable inet dawnline {\n\tset home_home { type ipv4_addr; flags interval; elements = { 192.168.50.0/24, 192.168.77.0/24 } }\n\tset home_cabin { type ipv4_addr; flags interval; elements = { 192.168.50.0/24 } }\n\tset home_open { type ipv4_addr; flags interval; elements = { 192.168.77.0/24 } }\n\tset tun_nets { type ipv4_addr; flags interval; elements = { 10.77.0.0/24, 10.78.0.0/24, 10.79.0.0/24 } }\n\tset lan_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.7, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset lan_all { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3 } }\n\tset lan_part { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.7 . 192.168.50.0/24, 10.77.0.11 . 192.168.77.0/24 } }\n\tset mesh_pairs { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.2 . 10.77.0.3, 10.77.0.2 . 10.77.0.5, 10.77.0.2 . 10.77.0.11, 10.77.0.3 . 10.77.0.2, 10.77.0.3 . 10.77.0.5, 10.77.0.3 . 10.77.0.11, 10.77.0.5 . 10.77.0.2, 10.77.0.5 . 10.77.0.3, 10.77.0.5 . 10.77.0.11, 10.77.0.11 . 10.77.0.2, 10.77.0.11 . 10.77.0.3, 10.77.0.11 . 10.77.0.5 } }\n\tset inet_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.6, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset svc_ok { type ipv4_addr . ipv4_addr . inet_service; flags interval; }\n\tset svc_host { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.8 . 192.168.77.5, 10.77.0.8 . 192.168.50.30 } }\n\tset dns_real { type ipv4_addr; flags interval; }\n\tchain pre {\n\t\ttype filter hook prerouting priority -160; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } jump guard\n\t}\n\tchain guard {\n\t\tmeta nfproto ipv6 drop\n\t\tct state related accept\n\t\tct status dnat accept\n\t\ticmp type { echo-request, echo-reply } fib daddr type local accept\n\t\tfib daddr type local meta l4proto { tcp, udp } th dport 53 accept\n\t\tfib daddr type local tcp dport { 4500, 4501 } accept\n\t\tfib daddr type local ip saddr @lan_all accept\n\t\tfib daddr type local ip saddr . ip daddr @lan_part accept\n\t\tfib daddr type local counter drop\n\t\tiifname \"dawn0\" ip daddr @home_home ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn1\" ip daddr @home_cabin ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn2\" ip daddr @home_open ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tip saddr . ip daddr @lan_part ip daddr != @tun_nets accept\n\t\tiifname \"dawn0\" ip saddr . ip daddr @mesh_pairs accept\n\t\tiifname \"dawn2\" ip saddr @lan_ok ip daddr @lan_ok accept\n\t\tmeta l4proto { tcp, udp } ip saddr . ip daddr . th dport @svc_ok accept\n\t\tip saddr . ip daddr @svc_host accept\n\t\tip daddr { 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 100.64.0.0/10, 169.254.0.0/16, 224.0.0.0/3 } counter drop\n\t\tip saddr @inet_ok accept\n\t\tcounter drop\n\t}\n\tchain inbound {\n\t\ttype filter hook forward priority filter - 5; policy accept;\n\t\toifname { \"dawn0\", \"dawn1\", \"dawn2\" } iifname != { \"dawn0\", \"dawn1\", \"dawn2\" } ct state new jump to_device\n\t}\n\tchain to_device {\n\t\tmeta nfproto ipv6 drop\n\t\tct status dnat accept\n\t\toifname \"dawn0\" ip saddr @home_home ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn1\" ip saddr @home_cabin ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn2\" ip saddr @home_open ip saddr != @tun_nets ip daddr @lan_all accept\n\t\tip daddr . ip saddr @lan_part ip saddr != @tun_nets accept\n\t\tcounter reject with icmpx admin-prohibited\n\t}\n\tchain dns {\n\t\ttype nat hook prerouting priority dstnat - 5; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } ip saddr @dns_real meta l4proto { tcp, udp } th dport 53 redirect to :5354\n\t}\n\tchain dns_lan {\n\t\ttype nat hook prerouting priority dstnat + 5; policy accept;\n\t\tiifname \"dawn0\" ip daddr 10.77.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn1\" ip daddr 10.78.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn2\" ip daddr 10.79.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t}\n}\n"
],
"confs": {
"dawn0": "[Interface]\nPrivateKey = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA=\nListenPort = 4500\n",
"dawn1": "[Interface]\nPrivateKey = ccccccccccccccccccccccccccccccccccccccccccA=\nListenPort = 4600\n",
"dawn2": "[Interface]\nPrivateKey = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeA=\nListenPort = 4700\n"
},
"hosts": "10.77.0.2 phone.anna.lan\n10.77.0.3 laptop.anna.lan\n10.77.0.4 old.anna.lan\n10.77.0.5 bphone.boris.lan\n10.77.0.6 cphone.clara.lan\n10.77.0.7 dphone.dima.lan\n10.77.0.8 ephone.eva.lan\n10.77.0.9 fphone.fedor.lan\n10.77.0.10 gphone.gleb.lan\n10.78.0.2 hphone.hana.lan\n10.78.0.3 iphone.ivan.lan\n10.79.0.2 kphone.kira.lan\n10.79.0.3 lphone.lev.lan\n10.77.0.11 nphone.nora.lan\n",
"hosts_mode": true,
"nft_err": "rules:1:1-5: Error: a made-up failure",
"present": false
},
{
"what": "the same failure again: told once",
"peers": {
"home": "ok",
"cabin": "ok",
"open": "ok"
},
"table": "rules:1:1-5: Error: a made-up failure",
"warning": "Access rules couldn't load (rules:1:1-5: Error: a made-up failure): no device is loaded into the tunnels until they do. Open Overview → Health.",
"states": {
"anna": "active",
"boris": "active",
"clara": "active",
"dima": "active",
"eva": "active",
"fedor": "active",
"gleb": "paused",
"hana": "active",
"ivan": "active",
"kira": "active",
"lev": "active",
"nora": "active"
},
"scripts": [
"table inet dawnline\ndelete table inet dawnline\ntable inet dawnline {\n\tset home_home { type ipv4_addr; flags interval; elements = { 192.168.50.0/24, 192.168.77.0/24 } }\n\tset home_cabin { type ipv4_addr; flags interval; elements = { 192.168.50.0/24 } }\n\tset home_open { type ipv4_addr; flags interval; elements = { 192.168.77.0/24 } }\n\tset tun_nets { type ipv4_addr; flags interval; elements = { 10.77.0.0/24, 10.78.0.0/24, 10.79.0.0/24 } }\n\tset lan_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.7, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset lan_all { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3 } }\n\tset lan_part { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.7 . 192.168.50.0/24, 10.77.0.11 . 192.168.77.0/24 } }\n\tset mesh_pairs { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.2 . 10.77.0.3, 10.77.0.2 . 10.77.0.5, 10.77.0.2 . 10.77.0.11, 10.77.0.3 . 10.77.0.2, 10.77.0.3 . 10.77.0.5, 10.77.0.3 . 10.77.0.11, 10.77.0.5 . 10.77.0.2, 10.77.0.5 . 10.77.0.3, 10.77.0.5 . 10.77.0.11, 10.77.0.11 . 10.77.0.2, 10.77.0.11 . 10.77.0.3, 10.77.0.11 . 10.77.0.5 } }\n\tset inet_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.6, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset svc_ok { type ipv4_addr . ipv4_addr . inet_service; flags interval; }\n\tset svc_host { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.8 . 192.168.77.5, 10.77.0.8 . 192.168.50.30 } }\n\tset dns_real { type ipv4_addr; flags interval; }\n\tchain pre {\n\t\ttype filter hook prerouting priority -160; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } jump guard\n\t}\n\tchain guard {\n\t\tmeta nfproto ipv6 drop\n\t\tct state related accept\n\t\tct status dnat accept\n\t\ticmp type { echo-request, echo-reply } fib daddr type local accept\n\t\tfib daddr type local meta l4proto { tcp, udp } th dport 53 accept\n\t\tfib daddr type local tcp dport { 4500, 4501 } accept\n\t\tfib daddr type local ip saddr @lan_all accept\n\t\tfib daddr type local ip saddr . ip daddr @lan_part accept\n\t\tfib daddr type local counter drop\n\t\tiifname \"dawn0\" ip daddr @home_home ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn1\" ip daddr @home_cabin ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn2\" ip daddr @home_open ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tip saddr . ip daddr @lan_part ip daddr != @tun_nets accept\n\t\tiifname \"dawn0\" ip saddr . ip daddr @mesh_pairs accept\n\t\tiifname \"dawn2\" ip saddr @lan_ok ip daddr @lan_ok accept\n\t\tmeta l4proto { tcp, udp } ip saddr . ip daddr . th dport @svc_ok accept\n\t\tip saddr . ip daddr @svc_host accept\n\t\tip daddr { 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 100.64.0.0/10, 169.254.0.0/16, 224.0.0.0/3 } counter drop\n\t\tip saddr @inet_ok accept\n\t\tcounter drop\n\t}\n\tchain inbound {\n\t\ttype filter hook forward priority filter - 5; policy accept;\n\t\toifname { \"dawn0\", \"dawn1\", \"dawn2\" } iifname != { \"dawn0\", \"dawn1\", \"dawn2\" } ct state new jump to_device\n\t}\n\tchain to_device {\n\t\tmeta nfproto ipv6 drop\n\t\tct status dnat accept\n\t\toifname \"dawn0\" ip saddr @home_home ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn1\" ip saddr @home_cabin ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn2\" ip saddr @home_open ip saddr != @tun_nets ip daddr @lan_all accept\n\t\tip daddr . ip saddr @lan_part ip saddr != @tun_nets accept\n\t\tcounter reject with icmpx admin-prohibited\n\t}\n\tchain dns {\n\t\ttype nat hook prerouting priority dstnat - 5; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } ip saddr @dns_real meta l4proto { tcp, udp } th dport 53 redirect to :5354\n\t}\n\tchain dns_lan {\n\t\ttype nat hook prerouting priority dstnat + 5; policy accept;\n\t\tiifname \"dawn0\" ip daddr 10.77.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn1\" ip daddr 10.78.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn2\" ip daddr 10.79.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t}\n}\n"
],
"confs": {
"dawn0": "[Interface]\nPrivateKey = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA=\nListenPort = 4500\n",
"dawn1": "[Interface]\nPrivateKey = ccccccccccccccccccccccccccccccccccccccccccA=\nListenPort = 4600\n",
"dawn2": "[Interface]\nPrivateKey = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeA=\nListenPort = 4700\n"
},
"hosts": "10.77.0.2 phone.anna.lan\n10.77.0.3 laptop.anna.lan\n10.77.0.4 old.anna.lan\n10.77.0.5 bphone.boris.lan\n10.77.0.6 cphone.clara.lan\n10.77.0.7 dphone.dima.lan\n10.77.0.8 ephone.eva.lan\n10.77.0.9 fphone.fedor.lan\n10.77.0.10 gphone.gleb.lan\n10.78.0.2 hphone.hana.lan\n10.78.0.3 iphone.ivan.lan\n10.79.0.2 kphone.kira.lan\n10.79.0.3 lphone.lev.lan\n10.77.0.11 nphone.nora.lan\n",
"hosts_mode": true,
"nft_err": "rules:1:1-5: Error: a made-up failure",
"present": false
},
{
"what": "it loads again; a tunnel refuses its devices",
"peers": {
"home": "Line unrecognized: a made-up refusal",
"cabin": "ok",
"open": "ok"
},
"table": null,
"warning": "Couldn't load devices into Home: Line unrecognized: a made-up refusal. Open Overview → Health.",
"states": {
"anna": "active",
"boris": "active",
"clara": "active",
"dima": "active",
"eva": "active",
"fedor": "active",
"gleb": "paused",
"hana": "active",
"ivan": "active",
"kira": "active",
"lev": "active",
"nora": "active"
},
"scripts": [
"table inet dawnline\ndelete table inet dawnline\ntable inet dawnline {\n\tset home_home { type ipv4_addr; flags interval; elements = { 192.168.50.0/24, 192.168.77.0/24 } }\n\tset home_cabin { type ipv4_addr; flags interval; elements = { 192.168.50.0/24 } }\n\tset home_open { type ipv4_addr; flags interval; elements = { 192.168.77.0/24 } }\n\tset tun_nets { type ipv4_addr; flags interval; elements = { 10.77.0.0/24, 10.78.0.0/24, 10.79.0.0/24 } }\n\tset lan_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.7, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset lan_all { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3 } }\n\tset lan_part { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.7 . 192.168.50.0/24, 10.77.0.11 . 192.168.77.0/24 } }\n\tset mesh_pairs { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.2 . 10.77.0.3, 10.77.0.2 . 10.77.0.5, 10.77.0.2 . 10.77.0.11, 10.77.0.3 . 10.77.0.2, 10.77.0.3 . 10.77.0.5, 10.77.0.3 . 10.77.0.11, 10.77.0.5 . 10.77.0.2, 10.77.0.5 . 10.77.0.3, 10.77.0.5 . 10.77.0.11, 10.77.0.11 . 10.77.0.2, 10.77.0.11 . 10.77.0.3, 10.77.0.11 . 10.77.0.5 } }\n\tset inet_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.6, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset svc_ok { type ipv4_addr . ipv4_addr . inet_service; flags interval; }\n\tset svc_host { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.8 . 192.168.77.5, 10.77.0.8 . 192.168.50.30 } }\n\tset dns_real { type ipv4_addr; flags interval; }\n\tchain pre {\n\t\ttype filter hook prerouting priority -160; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } jump guard\n\t}\n\tchain guard {\n\t\tmeta nfproto ipv6 drop\n\t\tct state related accept\n\t\tct status dnat accept\n\t\ticmp type { echo-request, echo-reply } fib daddr type local accept\n\t\tfib daddr type local meta l4proto { tcp, udp } th dport 53 accept\n\t\tfib daddr type local tcp dport { 4500, 4501 } accept\n\t\tfib daddr type local ip saddr @lan_all accept\n\t\tfib daddr type local ip saddr . ip daddr @lan_part accept\n\t\tfib daddr type local counter drop\n\t\tiifname \"dawn0\" ip daddr @home_home ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn1\" ip daddr @home_cabin ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn2\" ip daddr @home_open ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tip saddr . ip daddr @lan_part ip daddr != @tun_nets accept\n\t\tiifname \"dawn0\" ip saddr . ip daddr @mesh_pairs accept\n\t\tiifname \"dawn2\" ip saddr @lan_ok ip daddr @lan_ok accept\n\t\tmeta l4proto { tcp, udp } ip saddr . ip daddr . th dport @svc_ok accept\n\t\tip saddr . ip daddr @svc_host accept\n\t\tip daddr { 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 100.64.0.0/10, 169.254.0.0/16, 224.0.0.0/3 } counter drop\n\t\tip saddr @inet_ok accept\n\t\tcounter drop\n\t}\n\tchain inbound {\n\t\ttype filter hook forward priority filter - 5; policy accept;\n\t\toifname { \"dawn0\", \"dawn1\", \"dawn2\" } iifname != { \"dawn0\", \"dawn1\", \"dawn2\" } ct state new jump to_device\n\t}\n\tchain to_device {\n\t\tmeta nfproto ipv6 drop\n\t\tct status dnat accept\n\t\toifname \"dawn0\" ip saddr @home_home ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn1\" ip saddr @home_cabin ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn2\" ip saddr @home_open ip saddr != @tun_nets ip daddr @lan_all accept\n\t\tip daddr . ip saddr @lan_part ip saddr != @tun_nets accept\n\t\tcounter reject with icmpx admin-prohibited\n\t}\n\tchain dns {\n\t\ttype nat hook prerouting priority dstnat - 5; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } ip saddr @dns_real meta l4proto { tcp, udp } th dport 53 redirect to :5354\n\t}\n\tchain dns_lan {\n\t\ttype nat hook prerouting priority dstnat + 5; policy accept;\n\t\tiifname \"dawn0\" ip daddr 10.77.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn1\" ip daddr 10.78.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn2\" ip daddr 10.79.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t}\n}\n"
],
"confs": {
"dawn0": "[Interface]\nPrivateKey = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA=\nListenPort = 4500\n\n[Peer]\nPublicKey = BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA=\nAllowedIPs = 10.77.0.2/32\n\n[Peer]\nPublicKey = CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCA=\nAllowedIPs = 10.77.0.3/32\n\n[Peer]\nPublicKey = EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEA=\nAllowedIPs = 10.77.0.5/32\n\n[Peer]\nPublicKey = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA=\nAllowedIPs = 10.77.0.6/32\n\n[Peer]\nPublicKey = GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGA=\nAllowedIPs = 10.77.0.7/32\n\n[Peer]\nPublicKey = HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHA=\nAllowedIPs = 10.77.0.8/32\n\n[Peer]\nPublicKey = IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIA=\nAllowedIPs = 10.77.0.9/32\n\n[Peer]\nPublicKey = OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOA=\nAllowedIPs = 10.77.0.11/32\n",
"dawn1": "[Interface]\nPrivateKey = ccccccccccccccccccccccccccccccccccccccccccA=\nListenPort = 4600\n\n[Peer]\nPublicKey = KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKA=\nAllowedIPs = 10.78.0.2/32\n\n[Peer]\nPublicKey = LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLA=\nAllowedIPs = 10.78.0.3/32\n",
"dawn2": "[Interface]\nPrivateKey = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeA=\nListenPort = 4700\n\n[Peer]\nPublicKey = MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMA=\nAllowedIPs = 10.79.0.2/32\n\n[Peer]\nPublicKey = NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNA=\nAllowedIPs = 10.79.0.3/32\n"
},
"hosts": "10.77.0.2 phone.anna.lan\n10.77.0.3 laptop.anna.lan\n10.77.0.4 old.anna.lan\n10.77.0.5 bphone.boris.lan\n10.77.0.6 cphone.clara.lan\n10.77.0.7 dphone.dima.lan\n10.77.0.8 ephone.eva.lan\n10.77.0.9 fphone.fedor.lan\n10.77.0.10 gphone.gleb.lan\n10.78.0.2 hphone.hana.lan\n10.78.0.3 iphone.ivan.lan\n10.79.0.2 kphone.kira.lan\n10.79.0.3 lphone.lev.lan\n10.77.0.11 nphone.nora.lan\n",
"hosts_mode": true,
"nft_err": null,
"present": true
},
{
"what": "a tunnel is down",
"peers": {
"home": "ok",
"cabin": "down",
"open": "ok"
},
"table": null,
"warning": null,
"states": {
"anna": "active",
"boris": "active",
"clara": "active",
"dima": "active",
"eva": "active",
"fedor": "active",
"gleb": "paused",
"hana": "active",
"ivan": "active",
"kira": "active",
"lev": "active",
"nora": "active"
},
"scripts": [
"table inet dawnline\ndelete table inet dawnline\ntable inet dawnline {\n\tset home_home { type ipv4_addr; flags interval; elements = { 192.168.50.0/24, 192.168.77.0/24 } }\n\tset home_cabin { type ipv4_addr; flags interval; elements = { 192.168.50.0/24 } }\n\tset home_open { type ipv4_addr; flags interval; elements = { 192.168.77.0/24 } }\n\tset tun_nets { type ipv4_addr; flags interval; elements = { 10.77.0.0/24, 10.78.0.0/24, 10.79.0.0/24 } }\n\tset lan_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.7, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset lan_all { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3 } }\n\tset lan_part { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.7 . 192.168.50.0/24, 10.77.0.11 . 192.168.77.0/24 } }\n\tset mesh_pairs { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.2 . 10.77.0.3, 10.77.0.2 . 10.77.0.5, 10.77.0.2 . 10.77.0.11, 10.77.0.3 . 10.77.0.2, 10.77.0.3 . 10.77.0.5, 10.77.0.3 . 10.77.0.11, 10.77.0.5 . 10.77.0.2, 10.77.0.5 . 10.77.0.3, 10.77.0.5 . 10.77.0.11, 10.77.0.11 . 10.77.0.2, 10.77.0.11 . 10.77.0.3, 10.77.0.11 . 10.77.0.5 } }\n\tset inet_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.6, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset svc_ok { type ipv4_addr . ipv4_addr . inet_service; flags interval; }\n\tset svc_host { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.8 . 192.168.77.5, 10.77.0.8 . 192.168.50.30 } }\n\tset dns_real { type ipv4_addr; flags interval; }\n\tchain pre {\n\t\ttype filter hook prerouting priority -160; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } jump guard\n\t}\n\tchain guard {\n\t\tmeta nfproto ipv6 drop\n\t\tct state related accept\n\t\tct status dnat accept\n\t\ticmp type { echo-request, echo-reply } fib daddr type local accept\n\t\tfib daddr type local meta l4proto { tcp, udp } th dport 53 accept\n\t\tfib daddr type local tcp dport { 4500, 4501 } accept\n\t\tfib daddr type local ip saddr @lan_all accept\n\t\tfib daddr type local ip saddr . ip daddr @lan_part accept\n\t\tfib daddr type local counter drop\n\t\tiifname \"dawn0\" ip daddr @home_home ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn1\" ip daddr @home_cabin ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn2\" ip daddr @home_open ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tip saddr . ip daddr @lan_part ip daddr != @tun_nets accept\n\t\tiifname \"dawn0\" ip saddr . ip daddr @mesh_pairs accept\n\t\tiifname \"dawn2\" ip saddr @lan_ok ip daddr @lan_ok accept\n\t\tmeta l4proto { tcp, udp } ip saddr . ip daddr . th dport @svc_ok accept\n\t\tip saddr . ip daddr @svc_host accept\n\t\tip daddr { 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 100.64.0.0/10, 169.254.0.0/16, 224.0.0.0/3 } counter drop\n\t\tip saddr @inet_ok accept\n\t\tcounter drop\n\t}\n\tchain inbound {\n\t\ttype filter hook forward priority filter - 5; policy accept;\n\t\toifname { \"dawn0\", \"dawn1\", \"dawn2\" } iifname != { \"dawn0\", \"dawn1\", \"dawn2\" } ct state new jump to_device\n\t}\n\tchain to_device {\n\t\tmeta nfproto ipv6 drop\n\t\tct status dnat accept\n\t\toifname \"dawn0\" ip saddr @home_home ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn1\" ip saddr @home_cabin ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn2\" ip saddr @home_open ip saddr != @tun_nets ip daddr @lan_all accept\n\t\tip daddr . ip saddr @lan_part ip saddr != @tun_nets accept\n\t\tcounter reject with icmpx admin-prohibited\n\t}\n\tchain dns {\n\t\ttype nat hook prerouting priority dstnat - 5; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } ip saddr @dns_real meta l4proto { tcp, udp } th dport 53 redirect to :5354\n\t}\n\tchain dns_lan {\n\t\ttype nat hook prerouting priority dstnat + 5; policy accept;\n\t\tiifname \"dawn0\" ip daddr 10.77.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn1\" ip daddr 10.78.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn2\" ip daddr 10.79.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t}\n}\n"
],
"confs": {
"dawn0": "[Interface]\nPrivateKey = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA=\nListenPort = 4500\n\n[Peer]\nPublicKey = BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA=\nAllowedIPs = 10.77.0.2/32\n\n[Peer]\nPublicKey = CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCA=\nAllowedIPs = 10.77.0.3/32\n\n[Peer]\nPublicKey = EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEA=\nAllowedIPs = 10.77.0.5/32\n\n[Peer]\nPublicKey = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA=\nAllowedIPs = 10.77.0.6/32\n\n[Peer]\nPublicKey = GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGA=\nAllowedIPs = 10.77.0.7/32\n\n[Peer]\nPublicKey = HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHA=\nAllowedIPs = 10.77.0.8/32\n\n[Peer]\nPublicKey = IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIA=\nAllowedIPs = 10.77.0.9/32\n\n[Peer]\nPublicKey = OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOA=\nAllowedIPs = 10.77.0.11/32\n",
"dawn2": "[Interface]\nPrivateKey = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeA=\nListenPort = 4700\n\n[Peer]\nPublicKey = MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMA=\nAllowedIPs = 10.79.0.2/32\n\n[Peer]\nPublicKey = NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNA=\nAllowedIPs = 10.79.0.3/32\n"
},
"hosts": "10.77.0.2 phone.anna.lan\n10.77.0.3 laptop.anna.lan\n10.77.0.4 old.anna.lan\n10.77.0.5 bphone.boris.lan\n10.77.0.6 cphone.clara.lan\n10.77.0.7 dphone.dima.lan\n10.77.0.8 ephone.eva.lan\n10.77.0.9 fphone.fedor.lan\n10.77.0.10 gphone.gleb.lan\n10.78.0.2 hphone.hana.lan\n10.78.0.3 iphone.ivan.lan\n10.79.0.2 kphone.kira.lan\n10.79.0.3 lphone.lev.lan\n10.77.0.11 nphone.nora.lan\n",
"hosts_mode": true,
"nft_err": null,
"present": true
},
{
"what": "all is well",
"peers": {
"home": "ok",
"cabin": "ok",
"open": "ok"
},
"table": null,
"warning": null,
"states": {
"anna": "active",
"boris": "active",
"clara": "active",
"dima": "active",
"eva": "active",
"fedor": "active",
"gleb": "paused",
"hana": "active",
"ivan": "active",
"kira": "active",
"lev": "active",
"nora": "active"
},
"scripts": [
"table inet dawnline\ndelete table inet dawnline\ntable inet dawnline {\n\tset home_home { type ipv4_addr; flags interval; elements = { 192.168.50.0/24, 192.168.77.0/24 } }\n\tset home_cabin { type ipv4_addr; flags interval; elements = { 192.168.50.0/24 } }\n\tset home_open { type ipv4_addr; flags interval; elements = { 192.168.77.0/24 } }\n\tset tun_nets { type ipv4_addr; flags interval; elements = { 10.77.0.0/24, 10.78.0.0/24, 10.79.0.0/24 } }\n\tset lan_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.7, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset lan_all { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3 } }\n\tset lan_part { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.7 . 192.168.50.0/24, 10.77.0.11 . 192.168.77.0/24 } }\n\tset mesh_pairs { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.2 . 10.77.0.3, 10.77.0.2 . 10.77.0.5, 10.77.0.2 . 10.77.0.11, 10.77.0.3 . 10.77.0.2, 10.77.0.3 . 10.77.0.5, 10.77.0.3 . 10.77.0.11, 10.77.0.5 . 10.77.0.2, 10.77.0.5 . 10.77.0.3, 10.77.0.5 . 10.77.0.11, 10.77.0.11 . 10.77.0.2, 10.77.0.11 . 10.77.0.3, 10.77.0.11 . 10.77.0.5 } }\n\tset inet_ok { type ipv4_addr; flags interval; elements = { 10.77.0.2, 10.77.0.3, 10.77.0.5, 10.77.0.6, 10.77.0.9, 10.78.0.2, 10.78.0.3, 10.79.0.2, 10.79.0.3, 10.77.0.11 } }\n\tset svc_ok { type ipv4_addr . ipv4_addr . inet_service; flags interval; }\n\tset svc_host { type ipv4_addr . ipv4_addr; flags interval; elements = { 10.77.0.8 . 192.168.77.5, 10.77.0.8 . 192.168.50.30 } }\n\tset dns_real { type ipv4_addr; flags interval; }\n\tchain pre {\n\t\ttype filter hook prerouting priority -160; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } jump guard\n\t}\n\tchain guard {\n\t\tmeta nfproto ipv6 drop\n\t\tct state related accept\n\t\tct status dnat accept\n\t\ticmp type { echo-request, echo-reply } fib daddr type local accept\n\t\tfib daddr type local meta l4proto { tcp, udp } th dport 53 accept\n\t\tfib daddr type local tcp dport { 4500, 4501 } accept\n\t\tfib daddr type local ip saddr @lan_all accept\n\t\tfib daddr type local ip saddr . ip daddr @lan_part accept\n\t\tfib daddr type local counter drop\n\t\tiifname \"dawn0\" ip daddr @home_home ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn1\" ip daddr @home_cabin ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tiifname \"dawn2\" ip daddr @home_open ip daddr != @tun_nets ip saddr @lan_all accept\n\t\tip saddr . ip daddr @lan_part ip daddr != @tun_nets accept\n\t\tiifname \"dawn0\" ip saddr . ip daddr @mesh_pairs accept\n\t\tiifname \"dawn2\" ip saddr @lan_ok ip daddr @lan_ok accept\n\t\tmeta l4proto { tcp, udp } ip saddr . ip daddr . th dport @svc_ok accept\n\t\tip saddr . ip daddr @svc_host accept\n\t\tip daddr { 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 100.64.0.0/10, 169.254.0.0/16, 224.0.0.0/3 } counter drop\n\t\tip saddr @inet_ok accept\n\t\tcounter drop\n\t}\n\tchain inbound {\n\t\ttype filter hook forward priority filter - 5; policy accept;\n\t\toifname { \"dawn0\", \"dawn1\", \"dawn2\" } iifname != { \"dawn0\", \"dawn1\", \"dawn2\" } ct state new jump to_device\n\t}\n\tchain to_device {\n\t\tmeta nfproto ipv6 drop\n\t\tct status dnat accept\n\t\toifname \"dawn0\" ip saddr @home_home ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn1\" ip saddr @home_cabin ip saddr != @tun_nets ip daddr @lan_all accept\n\t\toifname \"dawn2\" ip saddr @home_open ip saddr != @tun_nets ip daddr @lan_all accept\n\t\tip daddr . ip saddr @lan_part ip saddr != @tun_nets accept\n\t\tcounter reject with icmpx admin-prohibited\n\t}\n\tchain dns {\n\t\ttype nat hook prerouting priority dstnat - 5; policy accept;\n\t\tiifname { \"dawn0\", \"dawn1\", \"dawn2\" } ip saddr @dns_real meta l4proto { tcp, udp } th dport 53 redirect to :5354\n\t}\n\tchain dns_lan {\n\t\ttype nat hook prerouting priority dstnat + 5; policy accept;\n\t\tiifname \"dawn0\" ip daddr 10.77.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn1\" ip daddr 10.78.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t\tiifname \"dawn2\" ip daddr 10.79.0.1 meta l4proto { tcp, udp } th dport 53 dnat ip to 192.168.77.1:53\n\t}\n}\n"
],
"confs": {
"dawn0": "[Interface]\nPrivateKey = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA=\nListenPort = 4500\n\n[Peer]\nPublicKey = BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA=\nAllowedIPs = 10.77.0.2/32\n\n[Peer]\nPublicKey = CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCA=\nAllowedIPs = 10.77.0.3/32\n\n[Peer]\nPublicKey = EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEA=\nAllowedIPs = 10.77.0.5/32\n\n[Peer]\nPublicKey = FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA=\nAllowedIPs = 10.77.0.6/32\n\n[Peer]\nPublicKey = GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGA=\nAllowedIPs = 10.77.0.7/32\n\n[Peer]\nPublicKey = HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHA=\nAllowedIPs = 10.77.0.8/32\n\n[Peer]\nPublicKey = IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIA=\nAllowedIPs = 10.77.0.9/32\n\n[Peer]\nPublicKey = OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOA=\nAllowedIPs = 10.77.0.11/32\n",
"dawn1": "[Interface]\nPrivateKey = ccccccccccccccccccccccccccccccccccccccccccA=\nListenPort = 4600\n\n[Peer]\nPublicKey = KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKA=\nAllowedIPs = 10.78.0.2/32\n\n[Peer]\nPublicKey = LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLA=\nAllowedIPs = 10.78.0.3/32\n",
"dawn2": "[Interface]\nPrivateKey = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeA=\nListenPort = 4700\n\n[Peer]\nPublicKey = MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMA=\nAllowedIPs = 10.79.0.2/32\n\n[Peer]\nPublicKey = NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNA=\nAllowedIPs = 10.79.0.3/32\n"
},
"hosts": "10.77.0.2 phone.anna.lan\n10.77.0.3 laptop.anna.lan\n10.77.0.4 old.anna.lan\n10.77.0.5 bphone.boris.lan\n10.77.0.6 cphone.clara.lan\n10.77.0.7 dphone.dima.lan\n10.77.0.8 ephone.eva.lan\n10.77.0.9 fphone.fedor.lan\n10.77.0.10 gphone.gleb.lan\n10.78.0.2 hphone.hana.lan\n10.78.0.3 iphone.ivan.lan\n10.79.0.2 kphone.kira.lan\n10.79.0.3 lphone.lev.lan\n10.77.0.11 nphone.nora.lan\n",
"hosts_mode": true,
"nft_err": null,
"present": true
}
],
"settings": "\nconfig settings 'settings'\n\toption portal_enabled '1'\n\toption portal_port '4500'\n\toption portal_host 'vpn.example.org'\n\tlist portal_alt '203.0.113.7'\n\nconfig server 'home'\n\toption name 'Home'\n\toption ifname 'dawn0'\n\toption port '4500'\n\toption subnet '10.77.0.0/24'\n\toption private_key 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA='\n\toption public_key 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbA='\n\toption auto_lan '0'\n\tlist lan_device 'dlhome'\n\tlist lan_device 'br-lan'\n\toption mesh 'circles'\n\toption lan_like '0'\n\nconfig server 'cabin'\n\toption name 'Cabin'\n\toption ifname 'dawn1'\n\toption port '4600'\n\toption subnet '10.78.0.0/24'\n\toption private_key 'ccccccccccccccccccccccccccccccccccccccccccA='\n\toption public_key 'ddddddddddddddddddddddddddddddddddddddddddA='\n\toption auto_lan '0'\n\tlist lan_device 'dlhome'\n\toption mesh 'off'\n\nconfig server 'open'\n\toption name 'Open'\n\toption ifname 'dawn2'\n\toption port '4700'\n\toption subnet '10.79.0.0/24'\n\toption private_key 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeA='\n\toption public_key 'ffffffffffffffffffffffffffffffffffffffffffA='\n\toption auto_lan '0'\n\tlist lan_device 'br-lan'\n\toption mesh 'all'\n\nconfig circle 'fam'\n\toption server 'home'\n\toption name 'Family'\n\nconfig circle 'work'\n\toption server 'home'\n\toption name 'Work'\n\nconfig service 'nas'\n\toption name 'NAS'\n\toption address '192.168.77.5'\n\tlist ports '445'\n\tlist ports '139'\n\nconfig service 'cam'\n\toption name 'Camera'\n\toption address '192.168.50.30'\n\nconfig person 'anna'\n\toption server 'home'\n\toption name 'Anna'\n\toption access 'both'\n\toption circle 'fam'\n\nconfig person 'boris'\n\toption server 'home'\n\toption name 'Boris'\n\toption access 'both'\n\toption circle 'fam'\n\nconfig person 'clara'\n\toption server 'home'\n\toption name 'Clara'\n\toption access 'internet'\n\nconfig person 'dima'\n\toption server 'home'\n\toption name 'Dima'\n\toption access 'lan'\n\tlist home_net 'dlhome'\n\nconfig person 'eva'\n\toption server 'home'\n\toption name 'Eva'\n\toption access 'lan'\n\tlist service 'nas'\n\tlist service 'cam'\n\nconfig person 'fedor'\n\toption server 'home'\n\toption name 'Fedor'\n\toption access 'both'\n\toption circle 'work'\n\nconfig person 'gleb'\n\toption server 'home'\n\toption name 'Gleb'\n\toption access 'both'\n\toption paused '1'\n\nconfig person 'hana'\n\toption server 'cabin'\n\toption name 'Hana'\n\toption access 'both'\n\nconfig person 'ivan'\n\toption server 'cabin'\n\toption name 'Ivan'\n\toption access 'both'\n\nconfig person 'kira'\n\toption server 'open'\n\toption name 'Kira'\n\toption access 'both'\n\nconfig person 'lev'\n\toption server 'open'\n\toption name 'Lev'\n\toption access 'both'\n\nconfig person 'nora'\n\toption server 'home'\n\toption name 'Nora'\n\toption access 'both'\n\toption circle 'fam'\n\tlist home_net 'br-lan'\n\nconfig device 'phone'\n\toption person 'anna'\n\toption name 'Phone'\n\toption address '10.77.0.2'\n\toption public_key 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA='\n\nconfig device 'laptop'\n\toption person 'anna'\n\toption name 'Laptop'\n\toption address '10.77.0.3'\n\toption public_key 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCA='\n\toption lan_bypass '1'\n\toption kind 'computer'\n\nconfig device 'old'\n\toption person 'anna'\n\toption name 'Old'\n\toption address '10.77.0.4'\n\toption public_key 'DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDA='\n\toption disabled '1'\n\nconfig device 'bphone'\n\toption person 'boris'\n\toption name 'Bphone'\n\toption address '10.77.0.5'\n\toption public_key 'EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEA='\n\nconfig device 'cphone'\n\toption person 'clara'\n\toption name 'Cphone'\n\toption address '10.77.0.6'\n\toption public_key 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA='\n\nconfig device 'dphone'\n\toption person 'dima'\n\toption name 'Dphone'\n\toption address '10.77.0.7'\n\toption public_key 'GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGA='\n\nconfig device 'ephone'\n\toption person 'eva'\n\toption name 'Ephone'\n\toption address '10.77.0.8'\n\toption public_key 'HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHA='\n\nconfig device 'fphone'\n\toption person 'fedor'\n\toption name 'Fphone'\n\toption address '10.77.0.9'\n\toption public_key 'IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIA='\n\nconfig device 'gphone'\n\toption person 'gleb'\n\toption name 'Gphone'\n\toption address '10.77.0.10'\n\toption public_key 'JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJA='\n\nconfig device 'hphone'\n\toption person 'hana'\n\toption name 'Hphone'\n\toption address '10.78.0.2'\n\toption public_key 'KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKA='\n\nconfig device 'iphone'\n\toption person 'ivan'\n\toption name 'Iphone'\n\toption address '10.78.0.3'\n\toption public_key 'LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLA='\n\nconfig device 'kphone'\n\toption person 'kira'\n\toption name 'Kphone'\n\toption address '10.79.0.2'\n\toption publicLine truncated
}
+189
View File
@@ -0,0 +1,189 @@
package awg
import (
"bytes"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"sync"
"github.com/tickcount/luci-app-dawnline/backend/files"
)
// What the installed AmneziaWG can do, as dawnline.awg caps() works it out: the netifd protocol script and
// the awg tool say what the tools know, the kernel module's package version what the kernel runs, and a
// feature counts only when both have it. Held to ucode on testdata/caps.uc (scripts/ucode-golden.sh).
// Caps: V2 is S3/S4, I1-I5 and header ranges (AWG 2.0), V3 header protection, trailers, padding and timing
// (AWG 3.1); Tools and Kmod are their generations ("1.0", "2.0", "3.1"; nil unknown); Pending, an installed
// module waits for a reboot.
type Caps struct {
V2 bool `json:"v2"`
V3 bool `json:"v3"`
Tools any `json:"tools"`
Kmod any `json:"kmod"`
Mismatch bool `json:"mismatch"`
Pending bool `json:"pending"`
}
// Record is what an install of AmneziaWG left in awg.json, as far as the running module is concerned.
type Record struct {
Reloaded *bool `json:"reloaded"`
Boot *string `json:"boot"`
PrevCore *int64 `json:"prev_core"`
PrevGen *string `json:"prev_gen"`
}
var tail = regexp.MustCompile(`([0-9]+\.[0-9]+\.[0-9]{8})(-r?[0-9]+)?$`)
// Gen is the AmneziaWG generation of a package version: 3.x packages carry 3.1; 1.0.x ones are 2.0 from
// July 2025 on and the first generation before. A kernel module's version has the kernel release in front.
func Gen(version string) string {
v := version
if m := tail.FindStringSubmatch(version); m != nil {
v = m[1]
}
switch {
case strings.HasPrefix(v, "3."):
return "3.1"
case strings.HasPrefix(v, "1.0.2024"):
return "1.0"
case len(v) >= 10 && strings.HasPrefix(v, "1.0.20250") && v[9] >= '1' && v[9] <= '6':
return "1.0"
}
return "2.0"
}
// Pending tells an installed update that waits for a reboot: the old module stayed loaded, this is still
// the boot it ran in, and the module loaded is still that one. boot "" and core 0 stand for "not known".
func Pending(rec *Record, boot string, core int64) bool {
return rec != nil && rec.Reloaded != nil && !*rec.Reloaded && rec.Boot != nil && boot != "" && *rec.Boot == boot &&
core != 0 && rec.PrevCore != nil && *rec.PrevCore == core
}
// RunningGen is the generation of the kernel module that runs: the one from before the install while it
// waits for a reboot, else the installed package's (version nil: no package). nil when not known.
func RunningGen(rec *Record, boot string, core int64, version *string) any {
if Pending(rec, boot, core) {
if rec.PrevGen == nil {
return nil
}
return *rec.PrevGen
}
if version == nil {
return nil
}
return Gen(*version)
}
// Found is what of AmneziaWG the router has, for CapsOf.
type Found struct {
// Script is the netifd protocol script; Tool, whether the awg tool is there; Tool3, whether it knows the 3.1 keys.
Script string
Tool, Tool3 bool
Record *Record
Boot string
Core int64
// Version is the kmod-amneziawg package's, nil when it is not a package.
Version *string
}
// CapsOf works the capabilities out of what was found.
func CapsOf(f Found) Caps {
t2 := strings.Contains(f.Script, "awg_s3") && strings.Contains(f.Script, "awg_i1")
t3 := t2 && strings.Contains(f.Script, "awg_header_protection_key") && f.Tool3
var tools any
if f.Tool {
tools = "1.0"
if t3 {
tools = "3.1"
} else if t2 {
tools = "2.0"
}
}
kmod := RunningGen(f.Record, f.Boot, f.Core, f.Version)
return Caps{
V2: t2 && (kmod == nil || kmod != "1.0"), V3: t3 && (kmod == nil || kmod == "3.1"),
Tools: tools, Kmod: kmod, Mismatch: tools != nil && kmod != nil && tools != kmod,
Pending: Pending(f.Record, f.Boot, f.Core),
}
}
const (
toolPath = "/usr/bin/awg"
protoPath = "/lib/netifd/proto/amneziawg.sh"
)
// Version is the installed version of a package (apk on OpenWrt 25.12, opkg before), or nil.
func Version(name string) *string {
if st, err := os.Stat("/usr/bin/apk"); err == nil && st.Mode()&0o111 != 0 {
out, _ := exec.Command("apk", "list", "--installed", name).CombinedOutput()
if m := regexp.MustCompile(`^` + regexp.QuoteMeta(name) + `-([^ ]+) `).FindSubmatch(out); m != nil {
v := string(m[1])
return &v
}
return nil
}
out, _ := exec.Command("opkg", "list-installed").CombinedOutput()
re := regexp.MustCompile(`^` + regexp.QuoteMeta(name) + `([0-9]+)? - (.+)$`)
var found []string
for _, line := range strings.Split(string(out), "\n") {
if m := re.FindStringSubmatch(strings.TrimSpace(line)); m != nil && (found == nil || m[1] == "") {
found = m
}
}
if found == nil {
return nil
}
v := strings.TrimSpace(found[2])
return &v
}
var kept struct {
sync.Mutex
key string
caps Caps
ok bool
}
// SystemCaps is this router's capabilities. Worked out again only when the module's file, the tool, the
// protocol script, the install's record (awg.json in dataDir) or the loaded module changed.
func SystemCaps(dataDir string) Caps {
mtime := func(path string) int64 {
st, err := os.Stat(path)
if err != nil {
return 0
}
return st.ModTime().Unix()
}
read := func(path string) string {
raw, _ := os.ReadFile(path)
return strings.TrimSpace(string(raw))
}
var core int64
fmt.Sscan(read("/sys/module/amneziawg/coresize"), &core)
if core < 0 {
core = 0
}
record := dataDir + "/awg.json"
key := fmt.Sprintf("%d/%d/%d/%d/%d", mtime("/lib/modules/"+read("/proc/sys/kernel/osrelease")+"/amneziawg.ko"), mtime(toolPath), mtime(protoPath), mtime(record), core)
kept.Lock()
defer kept.Unlock()
if kept.ok && kept.key == key {
return kept.caps
}
f := Found{Boot: read("/proc/sys/kernel/random/boot_id"), Core: core, Version: Version("kmod-amneziawg")}
script, _ := os.ReadFile(protoPath)
f.Script = string(script)
if tool, err := os.ReadFile(toolPath); err == nil {
f.Tool, f.Tool3 = true, bytes.Contains(tool, []byte("HeaderProtectionKey"))
}
var rec Record
if files.ReadJSON(record, &rec) {
f.Record = &rec
}
kept.key, kept.caps, kept.ok = key, CapsOf(f), true
return kept.caps
}
+86
View File
@@ -0,0 +1,86 @@
package awg
import (
"encoding/json"
"os"
"testing"
)
// Generations and the wait for a reboot, as ucode tells them (testdata/caps.uc, scripts/ucode-golden.sh).
func TestGenerationsAsUcode(t *testing.T) {
raw, err := os.ReadFile("testdata/caps.json")
if err != nil {
t.Fatal(err)
}
var g struct {
Gens map[string]string `json:"gens"`
Runs []struct {
Rec *Record `json:"rec"`
Boot *string `json:"boot"`
Core *int64 `json:"core"`
Version *string `json:"version"`
Pending bool `json:"pending"`
Gen any `json:"gen"`
} `json:"runs"`
}
if err := json.Unmarshal(raw, &g); err != nil {
t.Fatal(err)
}
if len(g.Gens) < 12 || len(g.Runs) < 100 {
t.Fatalf("too little: %d versions, %d runs", len(g.Gens), len(g.Runs))
}
for v, want := range g.Gens {
if got := Gen(v); got != want {
t.Errorf("Gen(%q) = %s, ucode %s", v, got, want)
}
}
waits := 0
for i, r := range g.Runs {
boot, core := "", int64(0)
if r.Boot != nil {
boot = *r.Boot
}
if r.Core != nil {
core = *r.Core
}
if r.Pending {
waits++
}
if got := Pending(r.Rec, boot, core); got != r.Pending {
t.Errorf("run %d: pending %v, ucode %v", i, got, r.Pending)
}
if got := RunningGen(r.Rec, boot, core, r.Version); got != r.Gen {
t.Errorf("run %d: generation %v, ucode %v", i, got, r.Gen)
}
}
if waits == 0 {
t.Fatal("no run waits for a reboot")
}
}
func TestCapsOf(t *testing.T) {
s := func(v string) *string { return &v }
no := false
core := int64(65536)
v2 := "proto_config_add_int awg_s3\nawg_i1"
v3 := v2 + "\nawg_header_protection_key"
for name, c := range map[string]struct {
f Found
want Caps
}{
"nothing installed": {Found{}, Caps{}},
"3.1 all through": {Found{Script: v3, Tool: true, Tool3: true, Version: s("6.12.94.3.1.20260906-r1")}, Caps{V2: true, V3: true, Tools: "3.1", Kmod: "3.1"}},
"3.1 tools over a 2.0 module": {Found{Script: v3, Tool: true, Tool3: true, Version: s("6.6.73.1.0.20250903-r2")},
Caps{V2: true, Tools: "3.1", Kmod: "2.0", Mismatch: true}},
"a script ahead of its tool": {Found{Script: v3, Tool: true, Version: s("3.1.20260906-r1")}, Caps{V2: true, Tools: "2.0", Kmod: "3.1", Mismatch: true}},
"a module put there by hand": {Found{Script: v3, Tool: true, Tool3: true}, Caps{V2: true, V3: true, Tools: "3.1"}},
"the first generation": {Found{Script: "awg_jc", Tool: true, Version: s("1.0.20240213")}, Caps{Tools: "1.0", Kmod: "1.0"}},
"an update that waits for a reboot": {Found{Script: v3, Tool: true, Tool3: true, Version: s("3.1.20260906-r1"), Boot: "b", Core: core,
Record: &Record{Reloaded: &no, Boot: s("b"), PrevCore: &core, PrevGen: s("1.0")}},
Caps{Tools: "3.1", Kmod: "1.0", Mismatch: true, Pending: true}},
} {
if got := CapsOf(c.f); got != c.want {
t.Errorf("%s: %+v", name, got)
}
}
}
+184
View File
@@ -0,0 +1,184 @@
// Package awg is the backend's one door to AmneziaWG: what the tunnels say of their peers.
//
// This file reads `awg show <ifname> dump`, as dawnline.awg dump() does: Dawnline's own tunnels only, each
// on its own (never `all`: another AmneziaWG interface on the router is not ours to read, and a long one
// would slow a read that runs every second). The tool and /sys are reached through Reader's two functions,
// so a test stands in for both.
package awg
import (
"os"
"os/exec"
"strconv"
"strings"
)
// Peer is a device as its tunnel sees it.
type Peer struct {
Ifname string `json:"ifname"`
// Endpoint is where it last spoke from ("203.0.113.7:41821"), empty when it never did.
Endpoint string `json:"endpoint"`
// Handshake is when its session was last made (unix seconds), 0 for never.
Handshake int64 `json:"handshake"`
// Rx and Tx are the bytes received from it and sent to it since the tunnel last added it.
Rx int64 `json:"rx"`
Tx int64 `json:"tx"`
}
// Iface is a tunnel's own line.
type Iface struct {
PublicKey string `json:"public_key"`
Port int `json:"port"`
// Ifindex is the link's index: a new one each time the tunnel is set up, so it is the epoch of its
// peers' counters.
Ifindex int `json:"ifindex"`
}
// How a tunnel's read went.
const (
Read = "read"
// Down: the interface does not exist; its peers and their counters are gone with it.
Down = "down"
// Failed: it exists but could not be read, or was set up again during the read: that says nothing
// about its peers.
Failed = "error"
)
// Dump is one look at the tunnels. Ifaces and Peers hold only what was read; OK is false when any tunnel's
// read failed.
type Dump struct {
OK bool `json:"ok"`
Reads map[string]string `json:"reads"`
Ifaces map[string]Iface `json:"ifaces"`
Peers map[string]Peer `json:"peers"`
}
// Reader reads tunnels. The zero value is not usable: System() is the router's.
type Reader struct {
// Show is `awg show <ifname> dump`: its output, and whether it ran and ended well.
Show func(ifname string) (string, bool)
// Index is the interface's index while it exists.
Index func(ifname string) (int, bool)
}
// System reads the router's own tunnels.
func System() Reader {
return Reader{
Show: func(ifname string) (string, bool) {
out, err := exec.Command("awg", "show", ifname, "dump").Output()
return string(out), err == nil
},
Index: func(ifname string) (int, bool) {
raw, err := os.ReadFile("/sys/class/net/" + ifname + "/ifindex")
if err != nil {
return 0, false
}
n, err := strconv.Atoi(strings.TrimSpace(string(raw)))
return n, err == nil
},
}
}
// Dump reads these tunnels.
func (r Reader) Dump(ifnames []string) Dump {
d := Dump{OK: true, Reads: map[string]string{}, Ifaces: map[string]Iface{}, Peers: map[string]Peer{}}
for _, ifname := range ifnames {
// Read around the call: a link set up again in between gave counters of either one.
before, there := r.Index(ifname)
// Not there (a tunnel netifd could not bring up, AmneziaWG gone after an upgrade): no process to
// learn it. One that appears is read next time.
if !there {
d.Reads[ifname] = Down
continue
}
out, ran := r.Show(ifname)
after, still := r.Index(ifname)
// Gone during the read: its peers and their counters are gone with it.
if !still {
d.Reads[ifname] = Down
continue
}
var iface *Iface
var peers map[string]Peer
if ran {
iface, peers = Parse(out, ifname)
}
if iface == nil || after != before {
d.Reads[ifname] = Failed
d.OK = false
continue
}
iface.Ifindex = after
d.Reads[ifname] = Read
d.Ifaces[ifname] = *iface
for k, p := range peers {
d.Peers[k] = p
}
}
return d
}
// Parse reads a dump's lines. They are told apart by their shape, not their length (each AmneziaWG
// generation adds fields to the interface line): an interface line carries its private and public key and
// a port; a peer line the peer's key, a preshared key or "(none)", and an endpoint. The interface is nil
// when the dump has no such line.
func Parse(out, ifname string) (*Iface, map[string]Peer) {
var iface *Iface
peers := map[string]Peer{}
for _, line := range strings.Split(out, "\n") {
f := strings.Split(line, "\t")
if len(f) < 4 || !ValidKey(f[0]) {
continue
}
second := ValidKey(f[1])
if second && digits(f[2]) {
port, _ := strconv.Atoi(f[2])
iface = &Iface{PublicKey: f[1], Port: port}
continue
}
if len(f) >= 8 && (second || f[1] == "(none)") {
p := Peer{Ifname: ifname, Handshake: count(f[4]), Rx: count(f[5]), Tx: count(f[6])}
if f[2] != "(none)" {
p.Endpoint = f[2]
}
peers[f[0]] = p
}
}
return iface, peers
}
func digits(s string) bool {
if s == "" {
return false
}
for i := 0; i < len(s); i++ {
if s[i] < '0' || s[i] > '9' {
return false
}
}
return true
}
// count is a counter or a moment of the dump; what is no number counts as none.
func count(s string) int64 {
n, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0
}
return n
}
// ValidKey tells a WireGuard key: 32 bytes in base64, so 43 letters and "=", the last letter one of those
// that end 32 bytes.
func ValidKey(s string) bool {
if len(s) != 44 || s[43] != '=' || !strings.ContainsRune("AEIMQUYcgkosw048", rune(s[42])) {
return false
}
for i := 0; i < 42; i++ {
c := s[i]
if !(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '+' || c == '/') {
return false
}
}
return true
}
+101
View File
@@ -0,0 +1,101 @@
package awg
import (
"os"
"reflect"
"strings"
"testing"
)
const (
serverKey = "xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg="
peerOn = "HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw="
peerNew = "TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0="
)
// The project's fixtures are `awg show all dump` (the ifname first); one interface's dump has no ifname.
func fixture(t *testing.T, name string) string {
raw, err := os.ReadFile("testdata/awg-dump-" + name + ".txt")
if err != nil {
t.Fatal(err)
}
var out []string
for _, l := range strings.Split(string(raw), "\n") {
if l != "" {
out = append(out, strings.TrimPrefix(l, "awgmain\t"))
}
}
return strings.Join(out, "\n") + "\n"
}
// A stand-in for the router: the dump it prints and the index /sys gives, by the read.
func stand(out string, ok bool, indexes ...int) Reader {
i := 0
return Reader{
Show: func(string) (string, bool) { return out, ok },
Index: func(string) (int, bool) {
ix := indexes[min(i, len(indexes)-1)]
i++
return ix, ix != 0
},
}
}
// What dawnline.awg dump() reads of the same fixtures (tests/ucode/awg.test.uc).
func TestDumpGenerations(t *testing.T) {
d := stand(fixture(t, "1.0"), true, 7).Dump([]string{"dawn0"})
want := Dump{OK: true, Reads: map[string]string{"dawn0": Read},
Ifaces: map[string]Iface{"dawn0": {PublicKey: serverKey, Port: 51820, Ifindex: 7}},
Peers: map[string]Peer{
peerOn: {Ifname: "dawn0", Endpoint: "203.0.113.7:41821", Handshake: 1760000000, Rx: 123456, Tx: 654321},
peerNew: {Ifname: "dawn0"},
}}
if !reflect.DeepEqual(d, want) {
t.Fatalf("1.0: %+v", d)
}
d = stand(fixture(t, "2.0"), true, 7).Dump([]string{"dawn0"})
if d.Ifaces["dawn0"].Port != 51820 || len(d.Peers) != 1 || d.Peers[peerOn].Handshake == 0 {
t.Fatalf("2.0: %+v", d)
}
d = stand(fixture(t, "3.1"), true, 7).Dump([]string{"dawn0"})
if d.Ifaces["dawn0"].PublicKey != serverKey || len(d.Peers) != 2 {
t.Fatalf("3.1: %+v", d)
}
}
func TestDumpStates(t *testing.T) {
asked := false
r := Reader{Show: func(string) (string, bool) { asked = true; return "", true }, Index: func(string) (int, bool) { return 0, false }}
if d := r.Dump([]string{"dawn0"}); !d.OK || d.Reads["dawn0"] != Down || asked {
t.Fatalf("a tunnel that is not there: %+v, asked %v", d, asked)
}
one := fixture(t, "1.0")
if d := stand(one, true, 7, 0).Dump([]string{"dawn0"}); d.Reads["dawn0"] != Down || len(d.Peers) != 0 {
t.Fatalf("gone during the read: %+v", d)
}
if d := stand(one, true, 7, 9).Dump([]string{"dawn0"}); d.OK || d.Reads["dawn0"] != Failed || len(d.Peers) != 0 {
t.Fatalf("set up again during the read: %+v", d)
}
if d := stand("", false, 7).Dump([]string{"dawn0"}); d.OK || d.Reads["dawn0"] != Failed {
t.Fatalf("the tool failed: %+v", d)
}
if d := stand("not a dump\n", true, 7).Dump([]string{"dawn0"}); d.Reads["dawn0"] != Failed {
t.Fatalf("no interface line: %+v", d)
}
if d := (Reader{}).Dump(nil); !d.OK || len(d.Reads) != 0 {
t.Fatalf("no tunnels: %+v", d)
}
}
func TestValidKey(t *testing.T) {
for _, k := range []string{serverKey, peerOn, peerNew} {
if !ValidKey(k) {
t.Errorf("%s refused", k)
}
}
for _, k := range []string{"", "(none)", serverKey[:43], serverKey[:42] + "B=", "!" + serverKey[1:], serverKey + "="} {
if ValidKey(k) {
t.Errorf("%q accepted", k)
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
+1730
View File
File diff suppressed because it is too large. Load diff
+133
View File
@@ -0,0 +1,133 @@
// Package bridge has the backend's topics told again when they may have moved without an event of their
// own: the settings or what was handed out changed on disk, somebody starts to look, or the minute passed
// (the counters and the clock move by themselves). It also gives the topics their shape on the stream
// (Split). The name is from the time it carried the ucode backend's answers over; nothing is asked of
// anyone now.
package bridge
import (
"context"
"fmt"
"os"
"time"
"github.com/tickcount/luci-app-dawnline/backend/hub"
)
// Refresh is how old what is published may get while nothing on disk moved: traffic totals and uptimes
// change without an event.
const Refresh = 30 * time.Second
// MeterKeep is how many points of the live meter are kept, one a second.
const MeterKeep = 120
// Audience tells whether anyone looks.
type Audience interface {
Present() bool
Wants(topic string) bool
}
// Bridge has the topics told again.
type Bridge struct {
Hub *hub.Hub
Audience Audience
// Watch lists the files whose change means the topics changed (the settings, the configurations handed
// out, the servers kept down).
Watch []string
// Tell puts the topics again, as the router is at that moment, and says whether it could: false while
// there is nothing to make them from (the settings were not read yet). nil: nobody makes them here, a
// test stand.
Tell func() bool
// Log gets a line for the system log.
Log func(format string, args ...any)
asks chan chan struct{}
sig string
told time.Time
wasAbsent bool
}
// New makes a bridge ready to be asked (Look) before it runs.
func New(b Bridge) *Bridge {
b.asks = make(chan chan struct{})
return &b
}
// Look has the topics told now instead of at the next second, and returns once they hold what is on disk
// (or ctx ended). A page asks after its own edit, to read what it wrote.
func (b *Bridge) Look(ctx context.Context) {
done := make(chan struct{})
select {
case b.asks <- done:
case <-ctx.Done():
return
}
select {
case <-done:
case <-ctx.Done():
}
}
// Run works until ctx ends.
func (b *Bridge) Run(ctx context.Context) {
tick := time.NewTicker(time.Second)
defer tick.Stop()
b.wasAbsent = true
for {
var asked chan struct{}
select {
case <-ctx.Done():
return
case asked = <-b.asks:
case <-tick.C:
}
if asked != nil {
// whoever asks is told at once
b.wasAbsent = true
b.status()
close(asked)
continue
}
if !b.Audience.Present() {
b.wasAbsent = true
continue
}
b.status()
}
}
// status has the topics told when they are due: the first look after nobody looked, a change on disk, or
// Refresh since the last time. Topics that could not be made stay due, and the backend's own topic does not
// say they were: a page that asked in the backend's first second is told with the next one, not after
// Refresh.
func (b *Bridge) status() {
if b.Tell == nil {
return
}
sig := signature(b.Watch)
if !b.wasAbsent && sig == b.sig && time.Since(b.told) < Refresh {
return
}
if !b.Tell() {
return
}
b.sig, b.told, b.wasAbsent = sig, time.Now(), false
// `at`: when the topics were last made, so a page that opens on documents kept from an hour ago
// knows to wait for this instead of showing them
b.Hub.Put("backend", map[string]any{"ok": true, "at": time.Now().Unix()})
}
// signature is the files' identity: a commit of the settings replaces the
// file (a new inode within the same second), a counter is rewritten in place.
func signature(paths []string) string {
s := ""
for _, p := range paths {
st, err := os.Stat(p)
if err != nil {
s += "-;"
continue
}
s += fmt.Sprintf("%d/%d/%d;", st.ModTime().UnixNano(), st.Size(), inode(st))
}
return s
}
+80
View File
@@ -0,0 +1,80 @@
package bridge
import (
"os"
"testing"
"time"
"github.com/tickcount/luci-app-dawnline/backend/hub"
)
// The topics are told again when somebody starts to look, when the settings change and with the minute,
// and not otherwise; the backend's own topic says it is well.
func TestToldWhenDue(t *testing.T) {
dir := t.TempDir()
conf := dir + "/dawnline"
os.WriteFile(conf, []byte("a"), 0o600)
h := hub.New()
told := 0
b := New(Bridge{Hub: h, Log: func(string, ...any) {}, Watch: []string{conf}, Tell: func() bool { told++; return true }})
b.wasAbsent = true
b.status()
b.status()
if told != 1 {
t.Fatalf("the first look: told %d times", told)
}
time.Sleep(10 * time.Millisecond)
os.WriteFile(conf, []byte("bb"), 0o600)
b.status()
b.status()
if told != 2 {
t.Fatalf("after the settings changed: told %d times", told)
}
b.told = time.Now().Add(-2 * Refresh)
b.status()
if told != 3 {
t.Fatalf("a minute later: told %d times", told)
}
if doc, ok := h.Sync("", 0, nil).Docs["backend"].(map[string]any); !ok || doc["ok"] != true {
t.Errorf("the backend's own topic: %v", h.Sync("", 0, nil).Docs["backend"])
}
// a stand with nobody to make the topics tells nothing
quiet := New(Bridge{Hub: hub.New(), Watch: []string{conf}})
quiet.wasAbsent = true
quiet.status()
}
// Topics that could not be made yet (the backend just started: the settings are not read) stay due: they
// are asked for again with the next second, not after Refresh, and until then the backend's own topic does
// not say that they are there.
func TestStaysDueUntilTold(t *testing.T) {
h := hub.New()
asked, ready := 0, false
b := New(Bridge{Hub: h, Tell: func() bool { asked++; return ready }})
b.wasAbsent = true
b.status()
b.status()
if asked != 2 {
t.Fatalf("not told yet: asked %d times", asked)
}
if doc, there := h.Sync("", 0, nil).Docs["backend"]; there {
t.Fatalf("the backend's own topic before any was made: %v", doc)
}
ready = true
b.status()
b.status()
if asked != 3 {
t.Fatalf("once told: asked %d times", asked)
}
if _, there := h.Sync("", 0, nil).Docs["backend"]; !there {
t.Fatal("told, and the backend's own topic is not there")
}
// the same when they are due by their age
ready = false
b.told = time.Now().Add(-2 * Refresh)
b.status()
b.status()
if asked != 5 {
t.Fatalf("due by age and not told: asked %d times", asked)
}
}
+16
View File
@@ -0,0 +1,16 @@
//go:build unix
package bridge
import (
"os"
"syscall"
)
// inode is the file's inode number (0 when the system does not tell).
func inode(st os.FileInfo) uint64 {
if s, ok := st.Sys().(*syscall.Stat_t); ok {
return uint64(s.Ino)
}
return 0
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+142
View File
@@ -0,0 +1,142 @@
// The ucode backend's `status` answer cut into the topics pages follow.
//
// `status` is one large document that every admin page used to fetch whole.
// As topics, a change in one place (a device came online) is a patch of a few
// bytes to one of them, and a page follows only those it shows. Lists of
// things that have an id (people, devices, servers) become keyed lists, so a
// change to one entry does not resend the list.
//
// Pure: a document in, documents out. bridge.go fetches and publishes.
package bridge
// A keyed list is how a list of things with ids is published:
//
// { "order": ["p1", "p2"], "items": { "p1": {…}, "p2": {…} } }
//
// A merge patch replaces an array whole, but reaches into an object: with
// the entries under their ids, one that changed is all that is sent, and
// `order` only when something came, went or moved. A daily series (traffic
// by the day) is keyed by its `day` the same way: every minute moves today's
// entry, and on the router that alone was most of what a refresh resent.
func keyed(list []any) (map[string]any, bool) {
for _, key := range []string{"id", "day"} {
if k, ok := keyedBy(list, key); ok {
return k, true
}
}
return nil, false
}
// keyedBy keys a list by one field, when every entry has it, as a string of
// its own.
func keyedBy(list []any, key string) (map[string]any, bool) {
order := make([]any, 0, len(list))
items := make(map[string]any, len(list))
for _, e := range list {
m, ok := e.(map[string]any)
if !ok {
return nil, false
}
id, ok := m[key].(string)
if !ok || id == "" {
return nil, false
}
if _, twice := items[id]; twice {
return nil, false
}
order = append(order, id)
items[id] = m
}
return map[string]any{"order": order, "items": items}, true
}
// Null stands for a null of the ucode answer: a topic holds none (a merge patch reads null as "remove this"),
// and the pages' code tells "no link trouble" (null) from "not told" (absent). A field that was null is
// published as { "$null": true } and read back as null (web/src/admin/state/join.ts). Only this bridge
// needs it: a producer written for the stream leaves out what it has nothing to say about.
const Null = "$null"
// rekey turns every list of things with ids (or days) inside v into a keyed
// list, at any depth (a person's devices inside the list of people). A list of
// anything else stays a list. An empty list stays one too: it cannot be told
// what it would hold.
func rekey(v any) any {
switch x := v.(type) {
case nil:
return map[string]any{Null: true}
case map[string]any:
for k, e := range x {
x[k] = rekey(e)
}
case []any:
for i, e := range x {
x[i] = rekey(e)
}
if len(x) > 0 {
if k, ok := keyed(x); ok {
return k
}
}
}
return v
}
// Where each field of `status` goes. A field not named here goes to "router",
// so one a later version adds is still published.
var topicOf = map[string]string{
"servers": "servers",
"people": "people",
"circles": "circles",
"services": "services",
"totals": "traffic",
"series": "traffic",
"hours": "traffic",
"geo": "places",
"events": "recent",
"now": "clock",
"tz": "clock",
}
// Topics lists the topics Split fills, for hello and the tests.
var Topics = []string{"router", "servers", "people", "circles", "services", "traffic", "places", "recent", "clock"}
// whole names the topics that are one field of `status` as it is; the others
// are objects of several fields.
var whole = map[string]bool{"servers": true, "people": true, "circles": true, "services": true, "places": true, "recent": true}
// lists names the topics that are a keyed list even while empty, so a page
// reads one shape whether or not there is anyone yet.
var lists = map[string]bool{"servers": true, "people": true, "circles": true, "services": true}
// Split cuts a `status` answer into topics. The answer is taken apart, not
// copied: the caller gives it up.
func Split(status map[string]any) map[string]any {
out := map[string]any{}
for _, t := range Topics {
if !whole[t] {
out[t] = map[string]any{}
}
}
for field, v := range status {
topic, ok := topicOf[field]
if !ok {
topic = "router"
}
if whole[topic] {
// The recent events have no ids and come as a window that moves:
// they stay a list.
if topic != "recent" {
v = rekey(v)
}
if list, isList := v.([]any); isList && lists[topic] {
if k, ok := keyed(list); ok {
v = k
}
}
out[topic] = v
continue
}
out[topic].(map[string]any)[field] = rekey(v)
}
return out
}
+89
View File
@@ -0,0 +1,89 @@
package bridge
import (
"encoding/json"
"os"
"reflect"
"testing"
)
func doc(s string) map[string]any {
var v map[string]any
if err := json.Unmarshal([]byte(s), &v); err != nil {
panic(err)
}
return v
}
// Lists of things with ids become keyed lists, at any depth; the rest stays.
func TestSplit(t *testing.T) {
got := Split(doc(`{
"version": "0.18.0", "now": 1700000000, "tz": "MSK-3", "hostname": "Outline",
"servers": [{"id": "s1", "name": "Home", "series": [1, 2]}],
"people": [
{"id": "p1", "name": "Myata", "devices": [{"id": "d1", "online": true}, {"id": "d2", "online": false}]},
{"id": "p2", "name": "Guest", "devices": []}
],
"circles": [], "services": [],
"totals": {"rx": 1}, "series": [{"day": "2026-11-11", "rx": 1, "tx": 2}],
"geo": {"enabled": true, "home": null, "points": [{"ip": "203.0.113.7"}]},
"events": [{"ts": 1, "type": "connect"}]
}`))
want := doc(`{
"router": {"version": "0.18.0", "hostname": "Outline"},
"clock": {"now": 1700000000, "tz": "MSK-3"},
"servers": {"order": ["s1"], "items": {"s1": {"id": "s1", "name": "Home", "series": [1, 2]}}},
"people": {"order": ["p1", "p2"], "items": {
"p1": {"id": "p1", "name": "Myata", "devices": {"order": ["d1", "d2"], "items": {"d1": {"id": "d1", "online": true}, "d2": {"id": "d2", "online": false}}}},
"p2": {"id": "p2", "name": "Guest", "devices": []}
}},
"circles": {"order": [], "items": {}}, "services": {"order": [], "items": {}},
"traffic": {"totals": {"rx": 1}, "series": {"order": ["2026-11-11"], "items": {"2026-11-11": {"day": "2026-11-11", "rx": 1, "tx": 2}}}},
"places": {"enabled": true, "home": {"$null": true}, "points": [{"ip": "203.0.113.7"}]},
"recent": [{"ts": 1, "type": "connect"}]
}`)
if !reflect.DeepEqual(got, map[string]any(want)) {
g, _ := json.MarshalIndent(got, "", " ")
t.Fatalf("got %s", g)
}
}
// Two entries with one id, or one without: the list is left as it came.
func TestKeyedRefuses(t *testing.T) {
for _, s := range []string{`{"l":[{"id":"a"},{"id":"a"}]}`, `{"l":[{"id":"a"},{"name":"b"}]}`, `{"l":[{"id":1}]}`, `{"l":[1,2]}`} {
in := doc(s)
if _, isList := rekey(in).(map[string]any)["l"].([]any); !isList {
t.Fatalf("%s was keyed", s)
}
}
}
// The fixture the page's side reads too (web/tests/stream.test.ts joins testdata/topics.json back into
// testdata/status.json): what Split makes of a whole status answer. Run with UPDATE=1 to write it anew
// after Split changed, and look at the difference.
func TestSplitFixture(t *testing.T) {
raw, err := os.ReadFile("testdata/status.json")
if err != nil {
t.Fatal(err)
}
var status map[string]any
if err := json.Unmarshal(raw, &status); err != nil {
t.Fatal(err)
}
got, err := json.MarshalIndent(Split(status), "", " ")
if err != nil {
t.Fatal(err)
}
if os.Getenv("UPDATE") != "" {
if err := os.WriteFile("testdata/topics.json", append(got, '\n'), 0o644); err != nil {
t.Fatal(err)
}
}
want, err := os.ReadFile("testdata/topics.json")
if err != nil {
t.Fatal(err)
}
if string(got)+"\n" != string(want) {
t.Fatal("Split no longer makes testdata/topics.json of testdata/status.json: UPDATE=1 writes it anew, and web/src/admin/state/join.ts must still join it back")
}
}
+375
View File
@@ -0,0 +1,375 @@
// Package chores is what Dawnline does on its own once a minute besides keeping the router to the
// settings (the ucode agent's steps "People", "Personal portal", "Router", "Backup" and "updates"):
// people's states that change on their own go to the activity log, an end date is announced when its last
// days begin, a renewed or expiring certificate and a changed public address are told in the service log,
// the settings are backed up at night, and an automatic update is started when one is due.
package chores
import (
"crypto/md5"
"crypto/x509"
"encoding/hex"
"encoding/pem"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/journal"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/servicelog"
"github.com/tickcount/luci-app-dawnline/backend/state"
"github.com/tickcount/luci-app-dawnline/backend/usage"
"github.com/tickcount/luci-app-dawnline/backend/zone"
)
// ExpireWarn is how many days before an end date it is announced.
const ExpireWarn = 3
// Chores does one router's. What it remembers between ticks starts as "nothing seen yet".
type Chores struct {
RunDir, DataDir, Config string
Log *servicelog.Log
// PortalCert is the certificate the portal uses and whether it is Dawnline's own self-signed one.
PortalCert func(s model.Settings) (path string, selfSigned bool)
// WanAddress is the router's public IPv4 as a server sees it ("": none).
WanAddress func(srv *model.Server) string
// Backup saves the settings (a kind of backup: "auto"); Reload has the service read its settings again.
Backup func(kind string) (string, error)
Reload func()
// Updating tells whether an update job runs; Update starts the updater on its own.
Updating func() bool
Update func()
// FlushPlaces puts the places' cache on flash (nil: there is none to keep).
FlushPlaces func()
// Now is the moment (nil: the clock).
Now func() time.Time
states map[string]string
certTime int64
certSeen bool
certCheck int64
backupDay int
notes map[string]any
rateAt int64
rateServers map[string]usage.Pair
ratePeople map[string]usage.Pair
flushed, logFlushed int64
failed map[string]string
}
func (c *Chores) now() time.Time {
if c.Now != nil {
return c.Now()
}
return time.Now()
}
func (c *Chores) slog(src, level, msg, detail, who string) {
if c.Log != nil {
c.Log.Write(src, level, msg, detail, who, servicelog.Extra{})
}
}
// What the service log was already told. On flash: the public address often changes with a reboot
// (PPPoE), and that is when it matters. Written rarely.
func (c *Chores) note(k string, v any) {
c.notes[k] = v
os.MkdirAll(c.DataDir, 0o700)
files.WriteJSON(c.DataDir+"/notes.json", c.notes, 0o600)
}
var stateReason = map[string]string{"paused": "paused by admin", "expired": "end date reached", "quota": "data limit reached",
"no_server": "no server", "server_off": "server turned off", "active": "active"}
// StateEvent is the event for a person's state moving on its own from prev to st, or nil. The admin's own
// pauses and a server turned off or on are logged when they happen, so nothing here; nothing either
// without a state before. The data limit has its own events; everything else is paused or resumed with
// the reason.
func StateEvent(person, prev, st string) journal.Event {
byAdmin := func(x string) bool { return x == "paused" || x == "server_off" }
if prev == "" || prev == st || byAdmin(prev) || byAdmin(st) {
return nil
}
if st == "quota" {
return journal.Event{"type": "quota_reached", "person": person}
}
if prev == "quota" && st == "active" {
return journal.Event{"type": "quota_restored", "person": person}
}
kind := "paused"
if st == "active" {
kind = "resumed"
}
reason := stateReason[st]
if reason == "" {
reason = st
}
return journal.Event{"type": kind, "person": person, "detail": reason}
}
// people is the states that changed on their own, and the end dates whose last days begin (once an end
// date; not on a clock that is not set).
func (c *Chores) people(m *model.Model, u *usage.Usage) []journal.Event {
now := c.now()
ts := now.Unix()
var events []journal.Event
states := map[string]string{}
for _, id := range m.PersonIDs() {
states[id] = state.Of(m, m.People[id], u, ts)
if e := StateEvent(id, c.states[id], states[id]); e != nil && c.states != nil {
events = append(events, e)
}
}
c.states = states
if now.In(zone.Local()).Year() < 2025 {
return events
}
for _, id := range m.PersonIDs() {
p := m.People[id]
end, ok := state.DateEnd(p.Expires)
if p.Expires == "" || !ok || end <= ts {
continue
}
left := (end-ts)/86400 + 1
if left <= ExpireWarn && c.notes["warned:"+id] != p.Expires {
c.note("warned:"+id, p.Expires)
days := "days"
if left == 1 {
days = "day"
}
events = append(events, journal.Event{"type": "expiring", "person": id, "detail": fmt.Sprintf("%d %s", left, days)})
}
}
return events
}
// certEnd is when a certificate ends ("Jan 1 2027", and the moment), from its file.
func certEnd(path string) (string, time.Time, bool) {
raw, err := os.ReadFile(path)
if err != nil {
return "", time.Time{}, false
}
block, _ := pem.Decode(raw)
if block == nil {
return "", time.Time{}, false
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return "", time.Time{}, false
}
end := cert.NotAfter.UTC()
return fmt.Sprintf("%s %d %d", end.Format("Jan"), end.Day(), end.Year()), end, true
}
// cert tells a renewed certificate (and has the service take it), and one that runs out within a week
// (ACME did not renew it): once a certificate, looked at hourly.
func (c *Chores) cert(m *model.Model) {
if !m.Settings.PortalEnabled || c.PortalCert == nil {
return
}
path, self := c.PortalCert(m.Settings)
st, err := os.Stat(path)
var mt int64
if err == nil {
mt = st.ModTime().Unix()
}
if c.certSeen && c.certTime != 0 && mt != 0 && mt != c.certTime {
until := "?"
if s, _, ok := certEnd(path); ok {
until = s
}
c.slog("portal", "up", "certificate renewed · valid until "+until, "", "Personal portal")
if c.Reload != nil {
c.Reload()
}
}
c.certTime, c.certSeen = mt, true
now := c.now().Unix()
if mt == 0 || self || now-c.certCheck < 3600 {
return
}
c.certCheck = now
warned, _ := c.notes["cert_warned"].(float64)
s, end, ok := certEnd(path)
if int64(warned) != mt && (!ok || end.Unix()-now < 604800) {
msg := "certificate expires within 7 days"
if ok {
msg = "certificate expires " + s
}
c.slog("portal", "error", msg, "Browsers will refuse the portal after that. Renew it with ACME, or choose another certificate in Settings.", "Personal portal")
c.note("cert_warned", float64(mt))
}
}
// wan tells that the router's public IPv4 changed. Devices whose configuration holds the address itself
// (a server's public address is an IP) cannot connect until they get a new one; with a hostname only the
// DNS record must follow.
func (c *Chores) wan(m *model.Model) {
if c.WanAddress == nil {
return
}
ip := ""
for _, id := range m.ServerIDs() {
if srv := m.Servers[id]; srv.Enabled && srv.PrivateKey != "" {
ip = c.WanAddress(srv)
break
}
}
was, _ := c.notes["wan"].(string)
if ip == "" || ip == was {
return
}
c.note("wan", ip)
if was == "" {
return
}
var stuck []string
for _, id := range m.ServerIDs() {
if srv := m.Servers[id]; m.EndpointHost(srv) == was {
stuck = append(stuck, srv.Name)
}
}
msg := fmt.Sprintf("public address changed · %s → %s", was, ip)
switch len(stuck) {
case 0:
c.slog("dawnline", "info", msg, "", "Router")
case 1:
c.slog("dawnline", "error", msg, stuck[0]+" still gives devices the old address: set the public address in the server settings (a DDNS name keeps working), then devices need new configurations.", "Router")
default:
c.slog("dawnline", "error", msg, strings.Join(stuck, ", ")+" still give devices the old address: set the public address in the server settings (a DDNS name keeps working), then devices need new configurations.", "Router")
}
}
// backup saves the settings in the night's third hour, once a day, when they changed since the last one.
func (c *Chores) backup() {
t := c.now().In(zone.Local())
if t.Hour() != 3 || c.backupDay == t.Day() || c.Backup == nil {
return
}
c.backupDay = t.Day()
raw, err := os.ReadFile(c.Config)
if err != nil {
return
}
sum := md5.Sum(raw)
now := hex.EncodeToString(sum[:])
var st struct {
Auto string `json:"auto"`
}
files.ReadJSON(c.DataDir+"/backup-state.json", &st)
if now == st.Auto {
return
}
name, err := c.Backup("auto")
if err != nil {
// tried again tomorrow; meanwhile the admin should know (a full flash, mostly)
c.slog("dawnline", "error", "nightly backup failed: "+err.Error(), "", "Backup")
return
}
files.WriteJSON(c.DataDir+"/backup-state.json", map[string]any{"auto": now}, 0o600)
if c.Log != nil {
if name != "" {
name = ": " + name
}
c.Log.Say("info", "backup", "nightly backup saved"+name)
}
}
var intervals = map[string]int64{"6h": 6 * 3600, "12h": 12 * 3600, "1d": 86400, "7d": 7 * 86400}
var clock = regexp.MustCompile(`^([0-9]{1,2}):([0-9]{2})$`)
// slot is the scheduled slot now is in (up to an hour after the set time, also across midnight).
func slot(pol model.Updates, now time.Time) (time.Time, bool) {
hm := clock.FindStringSubmatch(pol.Time)
if hm == nil {
return time.Time{}, false
}
h, _ := strconv.Atoi(hm[1])
mi, _ := strconv.Atoi(hm[2])
for _, day := range []time.Time{now, now.Add(-24 * time.Hour)} {
d := day.In(zone.Local())
// as ucode's: the day's midnight by the clock's own reading, then the set time
at := time.Unix(day.Unix()-int64(d.Hour()*3600+d.Minute()*60+d.Second())+int64(h*3600+mi*60), 0)
if !now.Before(at) && now.Sub(at) < time.Hour {
return at, true
}
}
return time.Time{}, false
}
// Due is what an automatic run should do now (dawnline.updates due): "install" (the scheduled window, at
// most an hour after the set time, once a slot), "check" (the interval has passed), or "".
func Due(pol model.Updates, installDay string, lastCheck int64, now time.Time) string {
if pol.Auto && pol.Mode == "scheduled" && len(pol.Targets) > 0 {
if at, in := slot(pol, now); in && installDay != at.In(zone.Local()).Format("2006-01-02") {
return "install"
}
}
every := intervals[pol.Interval]
if every == 0 {
every = 86400
}
if pol.Check && now.Unix()-lastCheck >= every {
return "check"
}
return ""
}
// updates starts an automatic check or update when one is due. The updater does the work in its own
// process; this only decides when.
func (c *Chores) updates(m *model.Model) {
if c.Update == nil {
return
}
var st struct {
InstallDay string `json:"install_day"`
Check float64 `json:"check"`
}
files.ReadJSON(c.DataDir+"/update-auto.json", &st)
if Due(m.Settings.Updates, st.InstallDay, int64(st.Check), c.now()) != "" && !(c.Updating != nil && c.Updating()) {
c.Update()
}
}
func (c *Chores) step(who string, quiet bool, fn func()) {
defer func() {
e := recover()
if e == nil {
delete(c.failed, who)
return
}
msg := fmt.Sprint(e)
// a new failure in the service log, a repeat or a quiet step's in syslog alone
if !quiet && c.failed[who] != msg {
c.slog("dawnline", "error", "failed: "+msg, "", who)
} else if c.Log != nil {
c.Log.Say("error", "chores", who+": "+msg)
}
c.failed[who] = msg
}()
fn()
}
// Tick is one minute's chores: the events for the activity log (the caller writes them and tells the
// pages), and everything else done. Each step on its own: a failing one is logged and the others still run.
func (c *Chores) Tick(m *model.Model, u *usage.Usage) []journal.Event {
if c.notes == nil {
c.notes = map[string]any{}
files.ReadJSON(c.DataDir+"/notes.json", &c.notes)
c.failed = map[string]string{}
}
var events []journal.Event
c.step("Speed", true, func() { c.rate(m, u) })
c.step("People", false, func() { events = c.people(m, u) })
c.step("Personal portal", false, func() { c.cert(m) })
c.step("Router", true, func() { c.wan(m) })
c.step("Backup", false, c.backup)
c.step("updates", true, func() { c.updates(m) })
c.step("Logs", true, func() { c.keepLogs(m) })
return events
}
+216
View File
@@ -0,0 +1,216 @@
package chores
import (
"encoding/json"
"errors"
"os"
"strings"
"testing"
"time"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/servicelog"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/usage"
"github.com/tickcount/luci-app-dawnline/backend/zone"
)
func TestStateEvent(t *testing.T) {
for _, c := range []struct{ prev, st, kind, detail string }{
{"", "expired", "", ""}, {"active", "active", "", ""}, {"active", "paused", "", ""}, {"paused", "active", "", ""}, {"server_off", "active", "", ""},
{"active", "quota", "quota_reached", ""}, {"quota", "active", "quota_restored", ""}, {"quota", "expired", "paused", "end date reached"},
{"active", "odd", "paused", "odd"},
} {
e := StateEvent("p", c.prev, c.st)
kind, _ := e["type"].(string)
detail, _ := e["detail"].(string)
if kind != c.kind || detail != c.detail {
t.Errorf("%s → %s: %v", c.prev, c.st, e)
}
}
}
// The schedule of automatic updates: the hour after the set time, once a slot, also across midnight; a
// check when its interval passed.
func TestDue(t *testing.T) {
loc := zone.Local()
at := func(s string) time.Time {
x, err := time.ParseInLocation("2006-01-02 15:04", s, loc)
if err != nil {
t.Fatal(err)
}
return x
}
pol := model.Updates{Check: true, Interval: "1d", Auto: true, Mode: "scheduled", Time: "23:30", Targets: []string{"app"}}
fresh := at("2026-11-12 23:00").Unix()
for _, c := range []struct {
now, day string
check int64
want string
}{
{"2026-11-12 23:29", "", fresh, ""}, {"2026-11-12 23:30", "", fresh, "install"}, {"2026-11-13 00:29", "", fresh, "install"},
{"2026-11-13 00:30", "", fresh, ""}, {"2026-11-13 00:10", "2026-11-12", fresh, ""}, {"2026-11-13 23:40", "2026-11-12", at("2026-11-13 23:00").Unix(), "install"},
{"2026-11-12 12:00", "", at("2026-11-11 12:00").Unix(), "check"}, {"2026-11-12 12:00", "", at("2026-11-11 12:01").Unix(), ""},
} {
if got := Due(pol, c.day, c.check, at(c.now)); got != c.want {
t.Errorf("at %s (installed %q): %q, not %q", c.now, c.day, got, c.want)
}
}
off := pol
off.Auto, off.Check = false, false
if Due(off, "", 0, at("2026-11-12 23:40")) != "" {
t.Error("due with everything off")
}
now := pol
now.Mode = "immediate"
if Due(now, "", fresh, at("2026-11-12 23:40")) != "" {
t.Error("a scheduled install without a schedule")
}
}
func TestTick(t *testing.T) {
dir := t.TempDir()
text, _ := os.ReadFile("../access/testdata/access.uci")
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
os.WriteFile(dir+"/dawnline", text, 0o600)
os.WriteFile(dir+"/cert", []byte("not a certificate"), 0o644)
clock := time.Date(2026, 11, 12, 2, 59, 0, 0, zone.Local())
wan := "203.0.113.7"
var said []string
backups, reloads, updates := 0, 0, 0
failing := false
ch := &Chores{RunDir: dir + "/run", DataDir: dir + "/data", Config: dir + "/dawnline", Now: func() time.Time { return clock },
Log: &servicelog.Log{RunDir: dir + "/run", Level: func() string { return "info" }, Syslog: func(_, line string) { said = append(said, line) }},
PortalCert: func(model.Settings) (string, bool) { return dir + "/cert", false },
WanAddress: func(*model.Server) string { return wan },
Backup: func(kind string) (string, error) {
if failing {
return "", errors.New("the flash is full")
}
backups++
return kind + "-1.tar.gz", nil
},
Reload: func() { reloads++ }, Update: func() { updates++ }, Updating: func() bool { return false }}
people := c.OfType("person")
if len(people) == 0 {
t.Fatal("the household has nobody")
}
who := people[0].Name
u := usage.Empty()
load := func() *model.Model { return model.Load(c, nil) }
told := func(part string) bool {
for _, l := range said {
if strings.Contains(l, part) {
return true
}
}
return false
}
// the first tick: nothing to compare with, the address noted, the certificate seen; an update check is due
if ev := ch.Tick(load(), u); len(ev) != 0 || backups != 0 || reloads != 0 || updates != 1 {
t.Fatalf("the first tick: %v, %d backups, %d reloads, %d updates", ev, backups, reloads, updates)
}
if !told("certificate expires within 7 days") {
t.Errorf("a certificate that cannot be read is not told: %v", said)
}
// an end date three days off: announced once; passed: a pause for that reason
c.Set(who, "expires", "2026-11-14")
ev := ch.Tick(load(), u)
if len(ev) != 1 || ev[0]["type"] != "expiring" || ev[0]["detail"] != "3 days" || ev[0]["person"] != who {
t.Errorf("the end date's last days: %v", ev)
}
if ev := ch.Tick(load(), u); len(ev) != 0 {
t.Errorf("announced again: %v", ev)
}
c.Set(who, "expires", "2026-11-11")
ev = ch.Tick(load(), u)
if len(ev) != 1 || ev[0]["type"] != "paused" || ev[0]["detail"] != "end date reached" {
t.Errorf("the end date passed: %v", ev)
}
c.Delete(who, "expires")
if ev := ch.Tick(load(), u); len(ev) != 1 || ev[0]["type"] != "resumed" {
t.Errorf("the end date taken away: %v", ev)
}
// the night's third hour: one backup a day, and only of settings that changed
clock = time.Date(2026, 11, 12, 3, 1, 0, 0, zone.Local())
failing = true
ch.Tick(load(), u)
if !told("nightly backup failed: the flash is full") || backups != 0 {
t.Errorf("a failed backup is not told: %v", said)
}
failing = false
clock = clock.Add(24 * time.Hour)
ch.Tick(load(), u)
ch.Tick(load(), u)
clock = clock.Add(24 * time.Hour)
ch.Tick(load(), u)
if backups != 1 || !told("nightly backup saved: auto-1.tar.gz") {
t.Errorf("%d backups: %v", backups, said)
}
// the certificate's file replaced: told, and the service takes it
os.Chtimes(dir+"/cert", clock, clock.Add(-time.Hour))
ch.Tick(load(), u)
if reloads != 1 || !told("certificate renewed") {
t.Errorf("a renewed certificate: %d reloads, %v", reloads, said)
}
// the public address moved, and a server hands out the old one itself
c.Set("home", "endpoint", "203.0.113.7")
wan = "198.51.100.9"
said = nil
ch.Tick(load(), u)
if !told("public address changed · 203.0.113.7 → 198.51.100.9") {
t.Errorf("a moved address is not told: %v", said)
}
notes, _ := os.ReadFile(dir + "/data/notes.json")
if !strings.Contains(string(notes), `"wan":"198.51.100.9"`) || !strings.Contains(string(notes), "warned:"+who) {
t.Errorf("what was told is not kept: %s", notes)
}
if st, _ := os.Stat(dir + "/data/notes.json"); st.Mode().Perm() != 0o600 {
t.Error("the notes are not private")
}
}
// The speed record: a point a minute from the totals, the last thirty a server and fifteen a person; who
// is gone goes from the files.
func TestRate(t *testing.T) {
dir := t.TempDir()
text, _ := os.ReadFile("../access/testdata/access.uci")
c, _ := uci.Parse(string(text))
m := model.Load(c, nil)
who := m.PersonIDs()[0]
srv := m.People[who].Server
clock := time.Unix(1_800_000_000, 0)
ch := &Chores{RunDir: dir, DataDir: dir + "/data", Now: func() time.Time { return clock }}
u := usage.Empty()
u.People[who] = &usage.Record{Total: &usage.Pair{1000, 500}}
os.WriteFile(dir+"/rate.json", []byte(`{"gone":[[1,2,3]]}`), 0o644)
ch.rate(m, u)
for i := 1; i <= 40; i++ {
clock = clock.Add(time.Minute)
u.People[who].Total = &usage.Pair{1000 + float64(i)*6000, 500 + float64(i)*600}
ch.rate(m, u)
}
var hist, people map[string][][3]int64
raw, _ := os.ReadFile(dir + "/rate.json")
json.Unmarshal(raw, &hist)
raw, _ = os.ReadFile(dir + "/rate_people.json")
json.Unmarshal(raw, &people)
last := [3]int64{clock.Unix(), 100, 10}
if len(hist[srv]) != 30 || hist[srv][29] != last || len(people[who]) != 15 || people[who][14] != last || hist["gone"] != nil {
t.Errorf("the record: %d points a server, %d a person, the last %v", len(hist[srv]), len(people[who]), hist[srv])
}
// a counter that went back (a reset) is no negative speed; a long gap is no point
clock = clock.Add(time.Minute)
u.People[who].Total = &usage.Pair{0, 0}
ch.rate(m, u)
clock = clock.Add(time.Hour)
ch.rate(m, u)
raw, _ = os.ReadFile(dir + "/rate.json")
json.Unmarshal(raw, &hist)
if n := len(hist[srv]); n != 30 || hist[srv][n-1] != [3]int64{clock.Add(-time.Hour).Unix(), 0, 0} {
t.Errorf("after a reset and a gap: %v", hist[srv][len(hist[srv])-2:])
}
}
+108
View File
@@ -0,0 +1,108 @@
package chores
import (
"os"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/journal"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/usage"
)
// point is a minute's speed: the moment, bytes a second down and up.
type point [3]int64
// rate keeps the last half hour of each server's speed (rate.json) and the last quarter of each person's
// (rate_people.json), a point a minute, from the counters' totals.
func (c *Chores) rate(m *model.Model, u *usage.Usage) {
ts := c.now().Unix()
servers := map[string]usage.Pair{}
totals := map[string]usage.Pair{}
for _, id := range m.PersonIDs() {
rec := u.People[id]
if rec == nil || rec.Total == nil {
continue
}
totals[id] = *rec.Total
s := servers[m.People[id].Server]
s[0] += rec.Total[0]
s[1] += rec.Total[1]
servers[m.People[id].Server] = s
}
hist := map[string][]point{}
people := map[string][]point{}
files.ReadJSON(c.RunDir+"/rate.json", &hist)
files.ReadJSON(c.RunDir+"/rate_people.json", &people)
per := func(now, was float64, dt int64) int64 {
if now < was {
return 0
}
return int64((now - was) / float64(dt))
}
if dt := ts - c.rateAt; c.rateAt != 0 && dt > 0 && dt < 600 {
for s, v := range servers {
p, had := c.rateServers[s]
if !had {
p = v
}
hist[s] = append(hist[s], point{ts, per(v[0], p[0], dt), per(v[1], p[1], dt)})
if n := len(hist[s]); n > 30 {
hist[s] = hist[s][n-30:]
}
}
for _, id := range m.PersonIDs() {
t, has := totals[id]
p, had := c.ratePeople[id]
if !has || !had {
continue
}
people[id] = append(people[id], point{ts, per(t[0], p[0], dt), per(t[1], p[1], dt)})
if n := len(people[id]); n > 15 {
people[id] = people[id][n-15:]
}
}
}
for s := range hist {
if m.Servers[s] == nil {
delete(hist, s)
}
}
for id := range people {
if m.People[id] == nil {
delete(people, id)
}
}
c.rateAt, c.rateServers, c.ratePeople = ts, servers, totals
os.MkdirAll(c.RunDir, 0o755)
files.WriteJSON(c.RunDir+"/rate.json", hist, 0o644)
files.WriteJSON(c.RunDir+"/rate_people.json", people, 0o644)
}
// flushEvery is how often what lives in memory is put on flash.
const flushEvery = 3600
var logFlush = map[string]int64{"10min": 600, "hourly": 3600}
// keepLogs trims the service log, and puts it, the places and the activity log on flash as often as each
// is meant to be: flash is written rarely.
func (c *Chores) keepLogs(m *model.Model) {
now := c.now().Unix()
if c.flushed == 0 {
c.flushed, c.logFlushed = now, now
}
persist := now-c.flushed >= flushEvery
if persist {
c.flushed = now
if c.FlushPlaces != nil {
c.FlushPlaces()
}
}
if c.Log != nil {
c.Log.Trim(c.DataDir, persist)
}
pol := journal.PolicyOf(m.Settings.Retention)
if pol.Persist == "off" || now-c.logFlushed >= logFlush[pol.Persist] {
journal.Log{Run: c.RunDir, Data: c.DataDir}.Persist(pol, now)
c.logFlushed = now
}
}
+845
View File
@@ -0,0 +1,845 @@
// Package cli is the `dawnline` command (dawnlined run as `dawnlined cli …`; /usr/bin/dawnline hands its
// words over). What the service itself needs works with no backend running: the apply when it starts, the
// sync and the log's lines when a tunnel comes up or goes down, the portal's certificate. What an admin
// types about people and devices goes through the running backend, the same door the pages use.
package cli
import (
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
"time"
backend "github.com/tickcount/luci-app-dawnline/backend"
"github.com/tickcount/luci-app-dawnline/backend/apply"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/github"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
"github.com/tickcount/luci-app-dawnline/backend/methods"
"github.com/tickcount/luci-app-dawnline/backend/migrate"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/pkgmgr"
"github.com/tickcount/luci-app-dawnline/backend/servicelog"
"github.com/tickcount/luci-app-dawnline/backend/store"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/updater"
"github.com/tickcount/luci-app-dawnline/backend/usage"
"github.com/tickcount/luci-app-dawnline/backend/view"
"github.com/tickcount/luci-app-dawnline/backend/zone"
)
// Env is where the command works.
type Env struct {
RunDir, DataDir, Config string
Out, Err io.Writer
// Run runs a command: what it said (both streams) and its exit code.
Run func(name string, args ...string) (string, int)
// Call asks the running backend (ubus object dawnline): its answer, or why there is none.
Call func(method string, args map[string]any) (map[string]any, error)
Now func() time.Time
}
// System is the router's own.
func System() *Env {
run := func(name string, args ...string) (string, int) {
out, err := exec.Command(name, args...).CombinedOutput()
if err == nil {
return string(out), 0
}
if ee, isExit := err.(*exec.ExitError); isExit {
return string(out), ee.ExitCode()
}
return string(out), 127
}
return &Env{RunDir: "/var/run/dawnline", DataDir: "/etc/dawnline", Config: "/etc/config/dawnline", Out: os.Stdout, Err: os.Stderr, Run: run, Now: time.Now,
Call: func(method string, args map[string]any) (map[string]any, error) {
if args == nil {
args = map[string]any{}
}
raw, _ := json.Marshal(args)
out, err := exec.Command("ubus", "-t", "90", "call", "dawnline", method, string(raw)).Output()
if err != nil {
return nil, fmt.Errorf("Dawnline's backend does not answer (is the service running? /etc/init.d/dawnline start)")
}
var res map[string]any
if json.Unmarshal(out, &res) != nil {
res = map[string]any{}
}
return res, nil
}}
}
type refusal struct{ msg string }
func die(format string, args ...any) { panic(refusal{fmt.Sprintf(format, args...)}) }
func (e *Env) store() store.Store { return store.Store{Config: e.Config, RunDir: e.RunDir} }
func (e *Env) log() *servicelog.Log {
return &servicelog.Log{RunDir: e.RunDir, Level: func() string {
if _, m, err := e.store().Read(); err == nil {
return m.Settings.LogLevel
}
return "info"
}}
}
// logical is the netifd interface on each device, as netifd says it.
func (e *Env) logical() map[string]netview.Logical {
out := map[string]netview.Logical{}
said, code := e.Run("ubus", "call", "network.interface", "dump")
var res struct {
Interface []struct {
L3 string `json:"l3_device"`
Device string `json:"device"`
Name string `json:"interface"`
Proto string `json:"proto"`
} `json:"interface"`
}
if code != 0 || json.Unmarshal([]byte(said), &res) != nil {
return out
}
for _, i := range res.Interface {
dev := i.L3
if dev == "" {
dev = i.Device
}
if _, seen := out[dev]; dev == "" || seen {
continue
}
out[dev] = netview.Logical{Name: i.Name, Proto: i.Proto}
}
return out
}
// router is the router an apply works on.
func (e *Env) router() *apply.Router {
return &apply.Router{RunDir: e.RunDir, HostsFile: "/tmp/hosts/dawnline", DHCP: "/etc/config/dhcp", Log: e.log(),
Network: "/etc/config/network", Firewall: "/etc/config/firewall", Exec: e.Run,
Snapshot: func() *netview.Snapshot { return netview.Take(netview.System(e.logical)) },
Remember: func(address string) {
e.store().Transact(func(c *uci.Config, _ *model.Model) error {
if c.Section("settings") == nil {
c.Add("settings", "settings")
}
c.Set("settings", "lan_address", address)
return nil
})
}}
}
func (e *Env) load() (*model.Model, string, error) {
c, m, err := e.store().Read()
if err != nil {
return nil, "", err
}
saved := ""
if s := c.Section("settings"); s != nil {
saved, _ = s.Get("lan_address")
}
return m, saved, nil
}
func (e *Env) out(v any) {
if s, is := v.(string); is {
fmt.Fprintln(e.Out, s)
return
}
raw, _ := json.MarshalIndent(v, "", "\t")
fmt.Fprintln(e.Out, string(raw))
}
// table prints fixed-width columns: a header, then a line a row.
func (e *Env) table(cols []string, rows [][]string) {
w := make([]int, len(cols))
for i, c := range cols {
w[i] = len([]rune(c))
}
for _, r := range rows {
for i, v := range r {
if n := len([]rune(v)); n > w[i] {
w[i] = n
}
}
}
line := func(r []string) {
var parts []string
for i, v := range r {
parts = append(parts, v+strings.Repeat(" ", w[i]-len([]rune(v))))
}
e.out(strings.TrimRight(strings.Join(parts, " "), " "))
}
line(cols)
for _, r := range rows {
line(r)
}
}
func ago(secs int64) string {
switch {
case secs < 3600:
return fmt.Sprintf("%dm", secs/60)
case secs < 86400:
return fmt.Sprintf("%dh", secs/3600)
}
return fmt.Sprintf("%dd", secs/86400)
}
const help = `
Usage: dawnline <command>
apply Reconcile router configuration and runtime state
sync Push devices and access rules to the tunnels
status People and devices with live state (JSON)
health Run health checks (JSON)
person ls [--json] Everyone: id, name, access, state, devices, last seen
person add <name> [--slug s] [--access lan|internet|both] [--devices n]
[--expires YYYY-MM-DD] [--quota GB] [--server id]
person link <id> Print the personal portal link
person rm <id> [--yes] Remove a person and their devices (--yes when they have any)
device ls [person] [--json]
device add <person> <name> [--kind phone|computer|tablet|router] [--conf]
device conf <id> Print a device configuration (.conf)
device rm <id> Remove a device
backup Create a backup in /etc/dawnline/backups
portal-cert Print the certificate and key the portal uses
portal-http-port Print the plain-HTTP port when an IP is among the portal addresses
log [-n N] [--since 2h|3d|date] [--level error|down|repair|up|info] [--src awg|dawnline|portal] [--json]
Service log: tunnel and service starts, stops, repairs (newest first)
teardown Remove everything Dawnline created on the router
version
Exit codes: 0 done, 1 refused or failed, 2 unknown command. health exits 1 when a check fails.`
var flags = map[string]bool{"json": true, "yes": true, "conf": true}
// Main runs one command and returns its exit code.
func (e *Env) Main(argv []string) (code int) {
opts := map[string]string{}
var args []string
for i := 0; i < len(argv); i++ {
a := argv[i]
if a == "-n" {
a = "--n"
}
if name, is := strings.CutPrefix(a, "--"); is {
if flags[name] {
opts[name] = "1"
} else if i++; i < len(argv) {
opts[name] = argv[i]
} else {
opts[name] = ""
}
continue
}
args = append(args, a)
}
for len(args) < 4 {
args = append(args, "")
}
cmd := args[0]
if cmd == "" {
cmd = "help"
}
defer func() {
if r := recover(); r != nil {
msg := fmt.Sprint(r)
if ref, is := r.(refusal); is {
msg = ref.msg
}
fmt.Fprintf(e.Err, "dawnline: %s\n", msg)
// run from the init script or cron, stderr goes nowhere: syslog keeps why
e.log().Say("error", "cli", "dawnline "+cmd+" failed: "+msg)
code = 1
}
}()
// what the backend answers, or its refusal as this command's
ask := func(method string, a map[string]any) map[string]any {
res, err := e.Call(method, a)
if err != nil {
die("%s", err.Error())
}
if msg, _ := res["error"].(string); msg != "" {
die("%s", msg)
}
return res
}
warned := func(res map[string]any) {
if w, _ := res["warning"].(string); w != "" {
fmt.Fprintf(e.Err, "dawnline: %s\n", w)
}
}
people := func() []map[string]any {
// The stream's own topic, as the pages read it: made once somebody looks, so the first question
// may come before it is there.
var doc map[string]any
for try := 0; try < 8 && doc == nil; try++ {
if try > 0 {
time.Sleep(400 * time.Millisecond)
}
res := ask("sync", map[string]any{"topics": []string{"people"}})
docs, _ := res["docs"].(map[string]any)
doc, _ = docs["people"].(map[string]any)
}
if doc == nil {
die("Dawnline's backend has not looked at the tunnels yet: try again in a moment.")
}
items, _ := doc["items"].(map[string]any)
var ids []string
order, _ := doc["order"].([]any)
for _, id := range order {
if s, is := id.(string); is && items[s] != nil {
ids = append(ids, s)
}
}
if len(ids) != len(items) {
ids = ids[:0]
for id := range items {
ids = append(ids, id)
}
sort.Strings(ids)
}
var out []map[string]any
for _, id := range ids {
if m, is := items[id].(map[string]any); is {
m["id"] = id
// a person's devices are kept the same way: by id, with their order
if devs, keyed := m["devices"].(map[string]any); keyed {
byID, _ := devs["items"].(map[string]any)
list := []any{}
order, _ := devs["order"].([]any)
for _, did := range order {
if d, is := byID[fmt.Sprint(did)].(map[string]any); is {
d["id"] = did
list = append(list, d)
}
}
m["devices"] = list
}
out = append(out, m)
}
}
return out
}
str := func(v any) string {
switch x := v.(type) {
case string:
return x
case float64:
return strconv.FormatFloat(x, 'f', -1, 64)
}
return ""
}
thePerson := func(id string) map[string]any {
if id == "" {
die("Which person? Give their id (dawnline person ls).")
}
for _, p := range people() {
if p["id"] == id {
return p
}
}
die("Unknown person: %s.", id)
return nil
}
now := e.Now().Unix()
switch {
case cmd == "apply":
st := e.store()
migrate.Router{Store: st, NewCode: methods.NewCode, Uhttpd: "/etc/config/uhttpd", CGI: "/www/cgi-bin/dawnline-portal", Log: e.log(),
ReloadUhttpd: func() { e.Run("/etc/init.d/uhttpd", "reload") }}.Run()
_, tool := os.Stat("/usr/bin/awg")
_, proto := os.Stat("/lib/netifd/proto/amneziawg.sh")
full := e.router().Apply(e.load, usage.Load(e.RunDir, e.DataDir, false), nil, tool == nil && proto == nil, awg.SystemCaps(e.DataDir))
res := map[string]any{"network": full.Network, "firewall": full.Firewall, "like_home": full.LikeHome, "peers": full.Peers, "warning": nil}
if full.Warning != "" {
res["warning"] = full.Warning
}
e.out(res)
case cmd == "sync":
m, _, err := e.load()
if err != nil {
die("%s", err.Error())
}
r := e.router().Sync(m, usage.Load(e.RunDir, e.DataDir, false), now, false, nil)
res := map[string]any{"peers": r.Peers, "states": r.States, "table": nil, "warning": nil}
if r.Table != "" {
res["table"] = r.Table
}
if r.Warning != "" {
res["warning"] = r.Warning
}
e.out(res)
case cmd == "status":
e.out(people())
case cmd == "health":
res := ask("health", nil)
checks, _ := res["checks"].([]any)
e.out(checks)
for _, c := range checks {
if m, _ := c.(map[string]any); m["status"] == "error" {
return 1
}
}
case cmd == "person" && args[1] == "ls":
list := people()
sort.SliceStable(list, func(i, j int) bool { return str(list[i]["name"]) < str(list[j]["name"]) })
if opts["json"] != "" {
e.out(list)
break
}
var rows [][]string
for _, p := range list {
devs, _ := p["devices"].([]any)
online, _ := p["online"].(float64)
count := strconv.Itoa(len(devs))
seen := "never"
if online > 0 {
count += fmt.Sprintf(" (%d online)", int(online))
seen = "now"
} else if last, _ := p["last_seen"].(float64); last > 0 {
seen = ago(now-int64(last)) + " ago"
}
rows = append(rows, []string{str(p["id"]), str(p["name"]), str(p["server"]), str(p["access"]), str(p["state"]), count, seen})
}
e.table([]string{"ID", "NAME", "SERVER", "ACCESS", "STATE", "DEVICES", "LAST SEEN"}, rows)
case cmd == "person" && args[1] == "add":
a := map[string]any{"name": args[2], "slug": opts["slug"], "server": opts["server"], "access": opts["access"], "device_limit": opts["devices"],
"expires": opts["expires"], "quota_gb": opts["quota"]}
if opts["access"] == "" {
a["access"] = "internet"
}
res := ask("person_create", a)
id := str(res["id"])
e.out(fmt.Sprintf("Added %s (%s)", args[2], id))
links := ask("person_links", map[string]any{"id": id})
if url := str(links["url"]); url != "" {
e.out(url)
} else {
e.out("No personal link: the portal is off.")
}
warned(res)
case cmd == "person" && args[1] == "link":
p := thePerson(args[2])
if url := str(ask("person_links", map[string]any{"id": p["id"]})["url"]); url != "" {
e.out(url)
} else {
e.out("No personal link: the portal is off.")
}
case cmd == "person" && args[1] == "rm":
p := thePerson(args[2])
devs, _ := p["devices"].([]any)
n := len(devs)
s := "s"
if n == 1 {
s = ""
}
if n > 0 && opts["yes"] == "" {
die("%s has %d device%s: add --yes to remove them too.", str(p["name"]), n, s)
}
res := ask("person_delete", map[string]any{"id": p["id"]})
if n > 0 {
e.out(fmt.Sprintf("Removed %s and %d device%s.", str(p["name"]), n, s))
} else {
e.out(fmt.Sprintf("Removed %s.", str(p["name"])))
}
warned(res)
case cmd == "device" && args[1] == "ls":
type row struct {
m map[string]any
person string
}
var rows []row
for _, p := range people() {
if args[2] != "" && p["id"] != args[2] {
continue
}
devs, _ := p["devices"].([]any)
for _, d := range devs {
if m, is := d.(map[string]any); is {
rows = append(rows, row{m, str(p["id"])})
}
}
}
if args[2] != "" && len(rows) == 0 {
thePerson(args[2])
}
sort.SliceStable(rows, func(i, j int) bool {
if rows[i].person != rows[j].person {
return rows[i].person < rows[j].person
}
return str(rows[i].m["name"]) < str(rows[j].m["name"])
})
if opts["json"] != "" {
out := []map[string]any{}
for _, r := range rows {
hs, _ := r.m["handshake"].(float64)
out = append(out, map[string]any{"id": r.m["id"], "person": r.person, "name": r.m["name"], "kind": r.m["kind"], "address": r.m["address"],
"disabled": r.m["disabled"] == true, "online": r.m["online"] == true, "handshake": hs})
}
e.out(out)
break
}
var lines [][]string
for _, r := range rows {
st := "never connected"
hs, _ := r.m["handshake"].(float64)
switch {
case r.m["disabled"] == true:
st = "off"
case r.m["online"] == true:
st = "online"
case hs > 0:
st = "seen " + ago(now-int64(hs)) + " ago"
}
lines = append(lines, []string{str(r.m["id"]), r.person, str(r.m["name"]), str(r.m["kind"]), str(r.m["address"]), st})
}
e.table([]string{"ID", "PERSON", "NAME", "KIND", "ADDRESS", "STATE"}, lines)
case cmd == "device" && args[1] == "add":
p := thePerson(args[2])
if args[3] == "" {
die("Which name? dawnline device add <person> <name>.")
}
kind := opts["kind"]
if kind == "" {
kind = "phone"
}
res := ask("device_create", map[string]any{"person": p["id"], "name": args[3], "kind": kind})
id := str(res["id"])
if opts["conf"] != "" {
conf, _ := res["config"].(map[string]any)
fmt.Fprint(e.Out, str(conf["conf"]))
} else {
e.out(fmt.Sprintf("%s (dawnline device conf %s prints its configuration)", id, id))
}
warned(res)
case cmd == "device" && args[1] == "conf":
res := ask("device_config", map[string]any{"id": args[2]})
fmt.Fprint(e.Out, str(res["conf"]))
case cmd == "device" && args[1] == "rm":
name, owner := "", ""
for _, p := range people() {
devs, _ := p["devices"].([]any)
for _, d := range devs {
if m, _ := d.(map[string]any); m["id"] == args[2] && args[2] != "" {
name, owner = str(m["name"]), str(p["name"])
}
}
}
if name == "" {
die("Unknown device.")
}
res := ask("device_delete", map[string]any{"id": args[2]})
e.out(fmt.Sprintf("Removed %s (%s).", name, owner))
warned(res)
case cmd == "backup":
// with no backend needed: the updater makes one before it replaces Dawnline itself
st := e.store()
name, err := methods.BackupCreate(methods.Deps{Store: &st, RunDir: e.RunDir, DataDir: e.DataDir, Config: e.Config}, opts["reason"])
if err != nil {
die("%s", err.Error())
}
e.out(name)
case cmd == "portal-cert":
_, m, err := e.store().Read()
if err != nil {
die("%s", err.Error())
}
cert, key := methods.PortalCertAndKey(m.Settings, e.DataDir, e.Run)
e.out(cert + " " + key)
case cmd == "portal-http-port":
_, m, err := e.store().Read()
if err != nil {
die("%s", err.Error())
}
for _, h := range view.PortalHosts(m) {
if _, is := ipnet.IP(h); is {
e.out(strconv.Itoa(m.Settings.PortalPort + 1))
break
}
}
case cmd == "log":
return e.showLog(opts)
case cmd == "iface-event":
e.ifaceEvent(args[1], args[2])
case cmd == "teardown":
_, m, err := e.store().Read()
if err != nil {
die("%s", err.Error())
}
e.router().Teardown(m)
migrate.Router{Store: e.store(), Uhttpd: "/etc/config/uhttpd", CGI: "/www/cgi-bin/dawnline-portal", Log: e.log(),
ReloadUhttpd: func() { e.Run("/etc/init.d/uhttpd", "reload") }}.UhttpdOnly()
e.Run("sh", "-c", "grep -qs /usr/libexec/dawnline/heartbeat /etc/crontabs/root && { sed -i '\\#/usr/libexec/dawnline/heartbeat#d' /etc/crontabs/root; /etc/init.d/cron restart >/dev/null 2>&1; }")
case cmd == "version":
e.out(backend.Version)
case cmd == "help" || cmd == "-h":
e.out("Dawnline " + backend.Version + "\n" + help)
default:
fmt.Fprintf(e.Err, "dawnline: unknown command \"%s\" (dawnline help)\n", strings.TrimSpace(args[0]+" "+args[1]))
return 2
}
return 0
}
var (
sinceRel = regexp.MustCompile(`^([0-9]+)([mhd])$`)
sinceDate = regexp.MustCompile(`^([0-9]{4})-([0-9]{2})-([0-9]{2})$`)
)
// showLog prints the service log, newest first.
func (e *Env) showLog(opts map[string]string) int {
n, _ := strconv.Atoi(opts["n"])
var since int64
if s := opts["since"]; s != "" {
if m := sinceRel.FindStringSubmatch(s); m != nil {
k, _ := strconv.ParseInt(m[1], 10, 64)
since = e.Now().Unix() - k*map[string]int64{"m": 60, "h": 3600, "d": 86400}[m[2]]
} else if m := sinceDate.FindStringSubmatch(s); m != nil {
y, _ := strconv.Atoi(m[1])
mo, _ := strconv.Atoi(m[2])
d, _ := strconv.Atoi(m[3])
since = time.Date(y, time.Month(mo), d, 0, 0, 0, 0, zone.Local()).Unix()
} else {
die("--since takes 30m, 2h, 3d or a date like 2026-10-18.")
}
}
level, src := opts["level"], opts["src"]
if level != "" && !strings.Contains(" info up down repair error ", " "+level+" ") {
die("--level takes error, down, repair, up or info.")
}
if src != "" && !strings.Contains(" awg dawnline portal ", " "+src+" ") {
die("--src takes awg, dawnline or portal.")
}
raw, err := os.ReadFile(e.RunDir + "/service.log")
if err != nil {
raw, _ = os.ReadFile(e.DataDir + "/service.log")
}
lines := strings.Split(string(raw), "\n")
log := []map[string]any{}
for i := len(lines) - 1; i >= 0 && (n == 0 || len(log) < n); i-- {
var ent map[string]any
if lines[i] == "" || json.Unmarshal([]byte(lines[i]), &ent) != nil {
continue
}
ts, _ := ent["ts"].(float64)
if (since != 0 && int64(ts) < since) || (level != "" && ent["level"] != level) || (src != "" && ent["src"] != src) {
continue
}
log = append(log, ent)
}
if opts["json"] != "" {
e.out(log)
return 0
}
str := func(v any) string { s, _ := v.(string); return s }
for _, ent := range log {
ts, _ := ent["ts"].(float64)
t := time.Unix(int64(ts), 0).In(zone.Local())
text := str(ent["msg"])
if who := str(ent["who"]); ent["by"] == "admin" {
parts := []string{"You", str(ent["msg"]), str(ent["kind"]), who}
if note := str(ent["note"]); note != "" {
parts = append(parts, "· "+note)
}
var kept []string
for _, p := range parts {
if p != "" {
kept = append(kept, p)
}
}
text = strings.Join(kept, " ")
} else if who != "" {
text = who + " " + text
}
e.out(fmt.Sprintf("%02d-%02d %02d:%02d %-8s %-6s %s", int(t.Month()), t.Day(), t.Hour(), t.Minute(), str(ent["src"]), str(ent["level"]), text))
if d := str(ent["detail"]); d != "" {
e.out(" " + strings.ReplaceAll(d, "\n", "\n "))
}
}
return 0
}
var ifaceName = regexp.MustCompile(`^[a-z][a-z0-9_]{0,15}$`)
// ifaceErrors is why a tunnel is down, from netifd and the last related syslog lines.
func (e *Env) ifaceErrors(ifname string) string {
var parts []string
said, _ := e.Run("ubus", "call", "network.interface."+ifname, "status")
var st struct {
Errors []struct {
Code string `json:"code"`
Data []string `json:"data"`
} `json:"errors"`
}
json.Unmarshal([]byte(said), &st)
var errs []string
for _, x := range st.Errors {
one := x.Code
if len(x.Data) > 0 {
one += " (" + strings.Join(x.Data, " ") + ")"
}
errs = append(errs, one)
}
if len(errs) > 0 {
parts = append(parts, "netifd: "+strings.Join(errs, ", "))
}
lines, _ := e.Run("sh", "-c", "logread -l 400 2>/dev/null | grep -E '"+ifname+"|amneziawg' | grep -iE 'error|fail|invalid|unrecogni|cannot|unable|not found|refused|denied' | tail -n 6")
if l := strings.Trim(lines, " \t\r\n\v\f"); l != "" {
parts = append(parts, l)
}
return strings.Join(parts, "\n")
}
// ifaceEvent is the iface hotplug script's word: a tunnel started, stopped or failed to start.
func (e *Env) ifaceEvent(action, ifname string) {
if !ifaceName.MatchString(ifname) {
die("Bad interface.")
}
log := e.log()
now := e.Now().Unix()
// Dawnline leaves a note before it takes a tunnel down itself; anything else is unexpected.
var intent map[string]any
if action == "ifdown" {
path := e.RunDir + "/iface-intent.json"
all := map[string]map[string]any{}
files.ReadJSON(path, &all)
if it := all[ifname]; it != nil {
ts, _ := it["ts"].(float64)
used, wasUsed := it["used"].(float64)
if float64(now)-ts < 120 && !(wasUsed && used != 0 && float64(now)-used >= 15) {
intent = it
if !wasUsed || used == 0 {
it["used"] = float64(now)
files.WriteJSON(path, all, 0o600)
}
}
}
}
who := ifname
if name, _ := intent["name"].(string); name != "" {
who = name
}
if _, m, err := e.store().Read(); err == nil {
for _, srv := range m.Servers {
if srv.Ifname == ifname {
who = srv.Name
}
}
}
fails := e.RunDir + "/ifup-failed.json"
type fail struct {
Logged int64 `json:"logged"`
Count int `json:"count"`
Since int64 `json:"since"`
Last int64 `json:"last"`
}
switch action {
case "ifup":
func() {
defer files.Lock(e.RunDir, "ifup-failed")()
all := map[string]*fail{}
files.ReadJSON(fails, &all)
if all[ifname] != nil {
delete(all, ifname)
files.WriteJSON(fails, all, 0o600)
}
}()
msg := "started"
if port, code := e.Run("awg", "show", ifname, "listen-port"); code == 0 && strings.TrimSpace(port) != "" {
msg = "started · accepting connections on port " + strings.TrimSpace(port)
}
log.Write("awg", "up", msg, "", who, servicelog.Extra{})
case "ifdown":
// stops Dawnline made itself are explained by the admin's own line; only an AmneziaWG update gets one here
if intent != nil {
if intent["reason"] == "update" {
log.Write("awg", "info", "restarting for the AmneziaWG update", "", who, servicelog.Extra{})
}
return
}
detail := "Dawnline didn't stop it: the router's network restarted, someone stopped it by hand, or it failed."
if more := e.ifaceErrors(ifname); more != "" {
detail += "\n" + more
}
log.Write("awg", "down", "stopped unexpectedly", detail, who, servicelog.Extra{})
case "ifup-failed":
// netifd retries a tunnel that fails to start every few seconds: one line an interface every ten
// minutes, with how many attempts failed meanwhile
var n int
var mins int64
func() {
defer files.Lock(e.RunDir, "ifup-failed")()
all := map[string]*fail{}
files.ReadJSON(fails, &all)
f := all[ifname]
if f == nil || now-f.Last > 600 {
f = &fail{Since: now}
all[ifname] = f
}
f.Count++
f.Last = now
if now-f.Logged >= 600 {
n, mins = f.Count, (now-f.Since)/60
f.Logged, f.Since, f.Count = now, now, 0
}
os.MkdirAll(e.RunDir, 0o755)
files.WriteJSON(fails, all, 0o600)
}()
if n > 0 {
msg := "couldn't start"
if n > 1 {
msg = fmt.Sprintf("couldn't start · %d failed attempts in %d min", n, mins)
}
log.Write("awg", "error", msg, e.ifaceErrors(ifname), who, servicelog.Extra{})
}
}
}
var catalogFile = regexp.MustCompile(`^dawnline\.([A-Za-z_-]+)\.lmo$`)
// Updater runs the update worker's command line on this router and returns its exit code.
func Updater(args []string) int {
e := System()
sh := func(cmd string) (string, int) { return e.Run("sh", "-c", cmd) }
_, m, err := e.store().Read()
if err != nil {
fmt.Fprintf(os.Stderr, "updater: %s\n", err.Error())
return 1
}
t := i18n.Use(i18n.Dir, os.Getenv("DAWNLINE_LANG"))
pkg := pkgmgr.System(sh)
pkg.T = t.T
return updater.Main(&updater.Env{RunDir: e.RunDir, DataDir: e.DataDir, Version: backend.Version, Policy: m.Settings.Updates, Pkg: pkg, T: t,
GH: &github.Client{RunDir: e.RunDir}, Fetch: github.HTTPFetch, Run: sh, Log: e.log(), Pid: os.Getpid(),
Board: func() (string, string, string) {
said, _ := e.Run("ubus", "call", "system", "board")
var b struct {
Release struct {
Version, Target, Arch string
} `json:"release"`
}
json.Unmarshal([]byte(said), &b)
return b.Release.Version, b.Release.Target, b.Release.Arch
},
Langs: func() []string {
var out []string
entries, _ := os.ReadDir(i18n.Dir)
for _, f := range entries {
if m := catalogFile.FindStringSubmatch(f.Name()); m != nil {
out = append(out, m[1])
}
}
sort.Strings(out)
return out
}}, args, os.Stdout, os.Stderr)
}
+145
View File
@@ -0,0 +1,145 @@
package cli
import (
"bytes"
"encoding/json"
"errors"
"os"
"strings"
"testing"
"time"
)
func env(t *testing.T) (*Env, *bytes.Buffer, *bytes.Buffer, string) {
dir := t.TempDir()
text, _ := os.ReadFile("../access/testdata/access.uci")
os.WriteFile(dir+"/dawnline", text, 0o600)
out, errs := &bytes.Buffer{}, &bytes.Buffer{}
e := &Env{RunDir: dir + "/run", DataDir: dir + "/data", Config: dir + "/dawnline", Out: out, Err: errs,
Now: func() time.Time { return time.Unix(1_800_000_000, 0) },
Run: func(name string, args ...string) (string, int) {
if name == "awg" {
return "4500\n", 0
}
return "", 1
}}
os.MkdirAll(e.RunDir, 0o755)
return e, out, errs, dir
}
func entries(t *testing.T, e *Env) []map[string]any {
raw, _ := os.ReadFile(e.RunDir + "/service.log")
var out []map[string]any
for _, l := range strings.Split(string(raw), "\n") {
if l != "" {
var m map[string]any
json.Unmarshal([]byte(l), &m)
out = append(out, m)
}
}
return out
}
// The hotplug script's words: a start, a stop Dawnline did not make, one it did, and starts that keep failing.
func TestIfaceEvents(t *testing.T) {
e, _, errs, _ := env(t)
if e.Main([]string{"iface-event", "ifup", "dawn0"}) != 0 || e.Main([]string{"iface-event", "ifdown", "dawn0"}) != 0 {
t.Fatal(errs.String())
}
// a stop Dawnline made itself is explained by the admin's own line: nothing here
os.WriteFile(e.RunDir+"/iface-intent.json", []byte(`{"dawn1":{"reason":"settings","name":"Cabin","ts":1799999990},"dawn2":{"reason":"update","name":"Open","ts":1799999990}}`), 0o600)
e.Main([]string{"iface-event", "ifdown", "dawn1"})
e.Main([]string{"iface-event", "ifdown", "dawn2"})
for i := 0; i < 3; i++ {
e.Main([]string{"iface-event", "ifup-failed", "dawn0"})
}
log := entries(t, e)
var got []string
for _, l := range log {
got = append(got, l["who"].(string)+" · "+l["level"].(string)+" · "+l["msg"].(string))
}
want := []string{"Home · up · started · accepting connections on port 4500", "Home · down · stopped unexpectedly", "Open · info · restarting for the AmneziaWG update", "Home · error · couldn't start"}
if strings.Join(got, "\n") != strings.Join(want, "\n") {
t.Errorf("the log:\n%s", strings.Join(got, "\n"))
}
if e.Main([]string{"iface-event", "ifup", "../etc"}) != 1 || !strings.Contains(errs.String(), "Bad interface.") {
t.Errorf("a name that is no interface: %s", errs.String())
}
// the log as the admin reads it, newest first, by level
e2, out, _, _ := env(t)
e2.RunDir = e.RunDir
if e2.Main([]string{"log", "--level", "down"}) != 0 || !strings.Contains(out.String(), "awg down Home stopped unexpectedly") || strings.Contains(out.String(), "accepting connections") {
t.Errorf("the log's lines:\n%s", out.String())
}
out.Reset()
e2.Main([]string{"log", "-n", "1", "--json"})
var list []map[string]any
if json.Unmarshal(out.Bytes(), &list) != nil || len(list) != 1 || list[0]["msg"] != "couldn't start" {
t.Errorf("the newest entry: %s", out.String())
}
if e2.Main([]string{"log", "--since", "soon"}) != 1 {
t.Error("a since that is no time was taken")
}
}
func TestPeopleAndDevices(t *testing.T) {
e, out, errs, _ := env(t)
var asked []string
e.Call = func(method string, args map[string]any) (map[string]any, error) {
raw, _ := json.Marshal(args)
asked = append(asked, method+" "+string(raw))
switch method {
case "sync":
return map[string]any{"docs": map[string]any{"people": map[string]any{"order": []any{"zed", "amy"}, "items": map[string]any{
"zed": map[string]any{"name": "Zed", "server": "home", "access": "both", "state": "active", "online": 1.0, "devices": map[string]any{"order": []any{"d_2", "d_1"}, "items": map[string]any{
"d_2": map[string]any{"name": "Tablet", "kind": "tablet", "address": "10.77.0.3", "handshake": 1799990000.0},
"d_1": map[string]any{"name": "Phone", "kind": "phone", "address": "10.77.0.2", "online": true}}}},
"amy": map[string]any{"name": "Amy", "server": "home", "access": "lan", "state": "paused", "last_seen": 1799900000.0, "devices": map[string]any{"order": []any{}, "items": map[string]any{}}}}}}}, nil
case "person_delete":
return map[string]any{"ok": true, "warning": "Couldn't load devices into Home: busy."}, nil
case "device_config":
if args["id"] != "d_1" {
return map[string]any{"error": "Unknown device."}, nil
}
return map[string]any{"conf": "[Interface]\n"}, nil
case "person_links":
return map[string]any{"url": "https://vpn.example.org:4500/aaaaaa-bbbbbb-cccccc"}, nil
}
return nil, errors.New("no backend")
}
e.Main([]string{"person", "ls"})
want := "ID NAME SERVER ACCESS STATE DEVICES LAST SEEN\namy Amy home lan paused 0 1d ago\nzed Zed home both active 2 (1 online) now\n"
if out.String() != want {
t.Errorf("person ls:\n%s", out.String())
}
out.Reset()
e.Main([]string{"device", "ls", "zed"})
want = "ID PERSON NAME KIND ADDRESS STATE\nd_1 zed Phone phone 10.77.0.2 online\nd_2 zed Tablet tablet 10.77.0.3 seen 2h ago\n"
if out.String() != want {
t.Errorf("device ls:\n%s", out.String())
}
out.Reset()
if e.Main([]string{"person", "rm", "zed"}) != 1 || !strings.Contains(errs.String(), "Zed has 2 devices: add --yes to remove them too.") {
t.Errorf("a person with devices goes without a yes: %s", errs.String())
}
errs.Reset()
if e.Main([]string{"person", "rm", "zed", "--yes"}) != 0 || out.String() != "Removed Zed and 2 devices.\n" || !strings.Contains(errs.String(), "Couldn't load devices into Home: busy.") {
t.Errorf("person rm: %s / %s", out.String(), errs.String())
}
out.Reset()
if e.Main([]string{"device", "conf", "d_1"}) != 0 || out.String() != "[Interface]\n" || e.Main([]string{"device", "conf", "nope"}) != 1 {
t.Errorf("device conf: %q", out.String())
}
out.Reset()
if e.Main([]string{"person", "link", "amy"}) != 0 || !strings.HasPrefix(out.String(), "https://vpn.example.org:4500/") || e.Main([]string{"person", "link", "who"}) != 1 {
t.Errorf("person link: %q", out.String())
}
// what needs no backend: the plain-HTTP port (an IP is among the portal's addresses), the version, help
out.Reset()
if e.Main([]string{"portal-http-port"}) != 0 || out.String() != "4501\n" {
t.Errorf("portal-http-port: %q", out.String())
}
if e.Main([]string{"backup", "--reason", "pre-update"}) != 0 || e.Main([]string{"dance", "now"}) != 2 || e.Main(nil) != 0 {
t.Error("the exit codes")
}
}
+81
View File
@@ -0,0 +1,81 @@
// dawnlined is Dawnline's backend in Go: one long-running program that holds
// the live state and serves it to the pages. procd runs it (the `backend`
// instance of /etc/init.d/dawnline).
//
// It looks at the tunnels, keeps the router to the settings, answers the pages (the object `dawnline` on
// ubus, with the stream of live state) and serves the personal portal. The same binary is the `dawnline`
// command (dawnlined cli …) and the update worker (dawnlined updater …).
package main
import (
"context"
"flag"
"fmt"
"log/syslog"
"os"
"os/signal"
"syscall"
backend "github.com/tickcount/luci-app-dawnline/backend"
"github.com/tickcount/luci-app-dawnline/backend/cli"
"github.com/tickcount/luci-app-dawnline/backend/daemon"
"github.com/tickcount/luci-app-dawnline/backend/ubus"
"github.com/tickcount/luci-app-dawnline/backend/zone"
)
func main() {
// `dawnlined updater …` is the background update worker (/usr/libexec/dawnline/updater hands its words over)
if len(os.Args) > 1 && os.Args[1] == "updater" {
zone.Load(zone.File)
os.Exit(cli.Updater(os.Args[2:]))
}
// `dawnlined cli …` is the dawnline command (/usr/bin/dawnline hands its words over)
if len(os.Args) > 1 && os.Args[1] == "cli" {
zone.Load(zone.File)
os.Exit(cli.System().Main(os.Args[2:]))
}
socket := flag.String("ubus", ubus.Socket, "ubusd's socket")
run := flag.String("run", "/var/run/dawnline", "the directory of runtime state (tmpfs)")
data := flag.String("data", "/etc/dawnline", "the directory of kept state (flash)")
config := flag.String("config", "/etc/config/dawnline", "the settings file")
stream := flag.String("stream", "poll", "how pages follow: sse (the web server can hold an event stream) or poll")
// taken and ignored: the init script of a version before this one passes it
flag.Bool("watch", true, "(always on)")
portalPort := flag.Int("portal-port", 0, "serve the personal portal on this HTTPS port (0: its own web server does)")
portalHTTP := flag.Int("portal-http", 0, "and plain HTTP on this port, for links with an IP address (0: none)")
portalCert := flag.String("portal-cert", "", "the portal's certificate")
portalKey := flag.String("portal-key", "", "and its key")
portalProcs := flag.Int("portal-procs", 2, "(taken and ignored: the portal answers in this process)")
version := flag.Bool("version", false, "print the version and exit")
flag.Parse()
if *version {
fmt.Println(backend.Version)
return
}
if *stream != "sse" && *stream != "poll" {
fmt.Fprintln(os.Stderr, "dawnlined: -stream is sse or poll")
os.Exit(2)
}
say := logger()
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer stop()
say("dawnlined %s started · pages follow by %s", backend.Version, *stream)
o := daemon.Options{Socket: *socket, RunDir: *run, DataDir: *data, Config: *config, Stream: *stream, Watch: true, Log: say}
if *portalPort > 0 {
o.Portal = &daemon.Portal{Port: *portalPort, HTTP: *portalHTTP, Cert: *portalCert, Key: *portalKey, Procs: *portalProcs}
}
daemon.Run(ctx, o)
say("dawnlined stopped")
}
// logger writes to syslog as the ucode backend does (tag dawnline), or to
// stderr where there is none (a test stand).
func logger() func(format string, args ...any) {
w, err := syslog.New(syslog.LOG_DAEMON|syslog.LOG_INFO, "dawnline")
if err != nil {
return func(format string, args ...any) { fmt.Fprintf(os.Stderr, format+"\n", args...) }
}
return func(format string, args ...any) { w.Info(fmt.Sprintf(format, args...)) }
}
+708
View File
@@ -0,0 +1,708 @@
// Package daemon is the running backend: it joins the parts and keeps them
// going until it is told to stop. cmd/dawnlined gives it the router's paths;
// a test gives it a stand's.
package daemon
import (
"context"
"fmt"
"net/http"
"os"
"os/exec"
"sync"
"syscall"
"time"
backend "github.com/tickcount/luci-app-dawnline/backend"
"github.com/tickcount/luci-app-dawnline/backend/api"
"github.com/tickcount/luci-app-dawnline/backend/apply"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/bridge"
minute "github.com/tickcount/luci-app-dawnline/backend/chores"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/geo"
"github.com/tickcount/luci-app-dawnline/backend/github"
"github.com/tickcount/luci-app-dawnline/backend/hub"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/journal"
"github.com/tickcount/luci-app-dawnline/backend/methods"
"github.com/tickcount/luci-app-dawnline/backend/migrate"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/portal"
"github.com/tickcount/luci-app-dawnline/backend/resolve"
"github.com/tickcount/luci-app-dawnline/backend/roster"
"github.com/tickcount/luci-app-dawnline/backend/servicelog"
"github.com/tickcount/luci-app-dawnline/backend/store"
"github.com/tickcount/luci-app-dawnline/backend/ubus"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/usage"
"github.com/tickcount/luci-app-dawnline/backend/view"
"github.com/tickcount/luci-app-dawnline/backend/watch"
"github.com/tickcount/luci-app-dawnline/backend/watchdog"
"github.com/tickcount/luci-app-dawnline/backend/zone"
)
// Options is where the backend finds the router.
type Options struct {
// Socket is ubusd's.
Socket string
// RunDir holds runtime state (tmpfs), DataDir kept state (flash), Config the settings file.
RunDir, DataDir, Config string
// Stream is how pages follow: "sse" or "poll" (api.Info).
Stream string
// Watch: the backend looks at the tunnels and keeps the router to the settings (always, on a router; a
// test stand that only wants the bus and the stream leaves it off).
Watch bool
// Tunnels reads the tunnels the watcher looks at; nil: the router's own (awg.System). A test stand's are
// in its hands.
Tunnels *awg.Reader
// Portal: the backend serves the personal portal itself (package portal); nil: its own web server does.
Portal *Portal
// Log gets the lines for the system log.
Log func(format string, args ...any)
}
// Portal is what the portal's web server runs with (the init script works it out).
type Portal struct {
// Port is the HTTPS port, HTTP the plain one beside it (0: none); Cert and Key the certificate's files.
Port, HTTP int
Cert, Key string
// Procs is how many of the ucode portal run at once.
Procs int
}
// Run works until ctx ends, then waits for its parts.
func Run(ctx context.Context, o Options) {
// The router's zone, before anything counts a day in it (OpenWrt keeps it where Go does not look).
zone.Load(zone.File)
h := hub.New()
audience := api.NewAudience()
var b *bridge.Bridge
// The watcher, when this backend runs it: what the methods it answers itself read.
var w *watch.Watcher
var more []ubus.Method
nets := &homes{}
// the portal's own answers, where this backend keeps the router (set with the watcher)
var native http.Handler
// set when the watcher runs: the topics Go makes itself, told again as the router is at that moment
// (after a change of the settings)
var publish func()
if o.Watch {
tunnels := awg.System()
if o.Tunnels != nil {
tunnels = *o.Tunnels
}
w = &watch.Watcher{
Config: o.Config, RunDir: o.RunDir, DataDir: o.DataDir,
DHCP: "/etc/config/dhcp", Leases: "/tmp/dhcp.leases", Zone: zone.File,
Tunnels: tunnels, Hub: h, Audience: audience, Log: o.Log,
}
names := &resolve.Resolver{}
// The router is this backend's to keep: its own apply, sync, keeper, chores and watchdog. One file gives
// all of it back to the ucode backend, for as long as that one is still shipped: /etc/dawnline/backend-off
// (the init script then starts this process without its watcher, and the agent does the work).
mine := func() bool {
_, err := os.Stat(o.DataDir + "/backend-off")
return err != nil
}
logs := &servicelog.Log{RunDir: o.RunDir, Level: func() string {
if world := w.World(); world != nil {
return world.M.Settings.LogLevel
}
return "info"
}}
started(o, logs)
defer ended(o, logs)
syncer := &apply.Router{RunDir: o.RunDir, HostsFile: "/tmp/hosts/dawnline", DHCP: "/etc/config/dhcp", Log: logs,
Network: "/etc/config/network", Firewall: "/etc/config/firewall",
Remember: func(address string) {
(store.Store{Config: o.Config, RunDir: o.RunDir}).Transact(func(c *uci.Config, _ *model.Model) error {
if c.Section("settings") == nil {
c.Add("settings", "settings")
}
c.Set("settings", "lan_address", address)
return nil
})
},
Snapshot: func() *netview.Snapshot { return netview.Take(netview.System(nets.logical)) },
Exec: func(name string, args ...string) (string, int) {
out, err := exec.Command(name, args...).CombinedOutput()
if err == nil {
return string(out), 0
}
if ee, isExit := err.(*exec.ExitError); isExit {
return string(out), ee.ExitCode()
}
return string(out), 127
}}
// The keeper: once a minute, unless the router is given back to the ucode backend (mine), the runtime
// sync, the home-like path's upkeep and the watch on the router's networks. The ucode agent leaves
// those steps alone while this process is alive (its beat).
keeper := &apply.Keeper{R: syncer, Config: o.Config, Words: i18n.Use(i18n.Dir, ""),
Load: func() (*model.Model, string, error) {
c, m, err := (store.Store{Config: o.Config, RunDir: o.RunDir}).Read()
if err != nil {
return nil, "", err
}
saved := ""
if s := c.Section("settings"); s != nil {
saved, _ = s.Get("lan_address")
}
return m, saved, nil
},
Usage: func() *usage.Usage {
if world := w.World(); world != nil {
return world.Usage
}
return nil
},
Dump: func() (map[string]awg.Peer, map[string]string) {
if world := w.World(); world != nil {
return world.Peers, world.Reads
}
return nil, nil
},
Tools: func() bool {
_, tool := os.Stat("/usr/bin/awg")
_, proto := os.Stat("/lib/netifd/proto/amneziawg.sh")
return tool == nil && proto == nil
},
Caps: func() awg.Caps { return awg.SystemCaps(o.DataDir) }}
// The other things done once a minute (package chores), with the same marker: what happens to people on
// its own goes to the activity log, and their pages are told.
var chores struct {
deps methods.Deps
run *minute.Chores
}
chores.run = &minute.Chores{RunDir: o.RunDir, DataDir: o.DataDir, Config: o.Config, Log: logs,
PortalCert: func(s model.Settings) (string, bool) { return methods.PortalCert(s, o.DataDir) },
WanAddress: func(srv *model.Server) string { return netview.Take(netview.System(nets.logical)).WanAddress(srv) },
Backup: func(kind string) (string, error) { return methods.BackupCreate(chores.deps, kind) },
Reload: func() {
cmd := exec.Command("sh", "-c", "/etc/init.d/dawnline reload >/dev/null 2>&1")
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
cmd.Start()
},
Updating: func() bool { return methods.Updating(o.RunDir) },
Update: func() {
exec.Command("start-stop-daemon", "-S", "-b", "-x", "/usr/libexec/dawnline/updater", "--", "auto").Run()
}}
// The places for the map: lookups run by themselves, a few at a time; what they find goes onto the
// connections logged before it was known, and those people's pages are told.
poke := func(people []string) {
if len(people) == 0 {
return
}
var waiting []string
files.ReadJSON(o.RunDir+"/presence.poke", &waiting)
files.WriteJSON(o.RunDir+"/presence.poke", append(waiting, people...), 0o600)
}
places := &geo.Locator{Store: geo.Store{Run: o.RunDir, Data: o.DataDir}, Get: geo.HTTP,
Usage: func() *usage.Usage {
if world := w.World(); world != nil {
return world.Usage
}
return nil
},
Found: func(where map[string]string, devices []string) {
journal.Log{Run: o.RunDir, Data: o.DataDir}.SetPlaces(where, time.Now().Unix()-3600)
world := w.World()
if world == nil {
return
}
var people []string
for _, id := range devices {
if d := world.M.Devices[id]; d != nil {
people = append(people, d.Person)
}
}
poke(people)
}}
chores.run.FlushPlaces = func() { places.Store.Flush() }
go func() {
// the addresses the watcher saw connect or move: located at once
for {
time.Sleep(time.Second)
if !mine() {
continue
}
var ips []string
if !files.ReadJSON(o.RunDir+"/geo.poke", &ips) {
continue
}
os.Remove(o.RunDir + "/geo.poke")
if world := w.World(); world != nil && world.M.Settings.GeoEnabled {
places.Ask(world.M.Settings.GeoProvider, ips, nil)
}
}
}()
others := func() {
world := w.World()
if world == nil {
return
}
_, m, err := (store.Store{Config: o.Config, RunDir: o.RunDir}).Read()
if err != nil {
return
}
events := chores.run.Tick(m, world.Usage)
wan := ""
if st := chores.deps.Bus; st != nil {
list, _ := st("network.interface.wan", "status", nil)["ipv4-address"].([]any)
if len(list) > 0 {
first, _ := list[0].(map[string]any)
wan, _ = first["address"].(string)
}
}
places.Minute(m.Settings.GeoEnabled, m.Settings.GeoProvider, wan)
if len(events) == 0 {
return
}
log := journal.Log{Run: o.RunDir, Data: o.DataDir}
if err := log.Add(events, journal.PolicyOf(m.Settings.Retention), journal.NamesOf(m), time.Now().Unix()); err != nil {
logs.Say("error", "chores", "the activity log was not written · "+err.Error())
}
// the watcher moves these people's revisions with its next look, so their pages follow
var people []string
for _, e := range events {
if p, _ := e["person"].(string); p != "" {
people = append(people, p)
}
}
poke(people)
}
go func() {
// the first look once the watcher has its own, then every minute
for wait := 15 * time.Second; ; wait = time.Minute {
time.Sleep(wait)
if mine() {
beatNow(o)
keeper.Tick()
others()
// the watchdog: what keeps people connected, looked at and repaired
func() {
defer func() {
if e := recover(); e != nil {
logs.Say("error", "watchdog", fmt.Sprint(e))
}
}()
m, _, err := keeper.Load()
if err != nil {
return
}
facts := methods.WatchdogFacts(chores.deps, m, syncer, func() {
if world := w.World(); world != nil {
syncer.Apply(keeper.Load, world.Usage, keeper.Words, keeper.Tools(), keeper.Caps())
}
})
watchdog.Pass(o.RunDir+"/watchdog.json", watchdog.Probes(m, facts), m.Settings.Watchdog, methods.Updating(o.RunDir), time.Now().Unix(), logs)
}()
}
}
}()
deps := methods.Deps{World: func() *view.World { return roster.Complete(w.World(), nets.of, o.DataDir) }, Events: func() []map[string]any { return w.Recent(0) }, Catalogs: i18n.Dir,
Home: func(srv *model.Server, p *model.Person) []string { return nets.of(srv, p) }, HostIP: names.HostIP, RunDir: o.RunDir, DataDir: o.DataDir, Config: o.Config, Addresses: nets.addresses,
Snapshot: func() *netview.Snapshot { return netview.Take(netview.System(nets.logical)) },
Handler: nets.handler,
Resolve: names.Addresses,
ResetUsage: w.ResetUsage,
Teardown: func(m *model.Model) {
syncer.Teardown(m)
migrate.Router{Store: store.Store{Config: o.Config, RunDir: o.RunDir}, Uhttpd: "/etc/config/uhttpd", CGI: "/www/cgi-bin/dawnline-portal", Log: logs,
ReloadUhttpd: func() { exec.Command("/etc/init.d/uhttpd", "reload").Run() }}.UhttpdOnly()
// the agent's heartbeat check
exec.Command("sh", "-c", "grep -qs /usr/libexec/dawnline/heartbeat /etc/crontabs/root && { sed -i '\\#/usr/libexec/dawnline/heartbeat#d' /etc/crontabs/root; /etc/init.d/cron restart >/dev/null 2>&1; }").Run()
},
// after the answer is out: this process is one of the service's own
Stop: func() {
cmd := exec.Command("sh", "-c", "sleep 1; /etc/init.d/dawnline stop >/dev/null 2>&1; /etc/init.d/dawnline disable >/dev/null 2>&1")
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
cmd.Start()
},
ApplyPlan: func() any {
c, m, err := (store.Store{Config: o.Config, RunDir: o.RunDir}).Read()
if err != nil {
return nil
}
saved := ""
if s := c.Section("settings"); s != nil {
saved, _ = s.Get("lan_address")
}
_, tool := os.Stat("/usr/bin/awg")
_, proto := os.Stat("/lib/netifd/proto/amneziawg.sh")
return syncer.ApplyPlan(m, saved, tool == nil && proto == nil, awg.SystemCaps(o.DataDir))
},
SyncPlan: func() any {
world := w.World()
_, m, err := (store.Store{Config: o.Config, RunDir: o.RunDir}).Read()
if world == nil || err != nil {
return nil
}
return syncer.Plan(m, world.Usage, time.Now().Unix())
},
Background: func(script string) {
cmd := exec.Command("sh", "-c", "( "+script+" ) >/dev/null 2>&1 </dev/null")
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
cmd.Start()
},
Ifup: func(name string) { exec.Command("ifup", name).Run() },
Updater: func(lang string, args []string) {
cmd := exec.Command("start-stop-daemon", append([]string{"-S", "-b", "-x", "/usr/libexec/dawnline/updater", "--"}, args...)...)
if lang != "" {
cmd.Env = append(os.Environ(), "DAWNLINE_LANG="+lang)
}
cmd.Run()
},
Restart: func(network bool) {
script := "/etc/init.d/dawnline enabled || /etc/init.d/dawnline enable; sleep 1; "
if network {
script += "/etc/init.d/network restart; "
}
cmd := exec.Command("sh", "-c", script+"/etc/init.d/dawnline restart >/dev/null 2>&1")
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
cmd.Start()
},
// the full apply
Apply: func(lang string) map[string]any {
// Go's own full apply (unless the router is given back to ucode): the one-time steps,
// then network, firewall, the home-like path, the resolver, the reply table and the sync
if mine() {
if world := w.World(); world != nil {
st := store.Store{Config: o.Config, RunDir: o.RunDir}
migrate.Router{Store: st, NewCode: methods.NewCode, Uhttpd: "/etc/config/uhttpd", CGI: "/www/cgi-bin/dawnline-portal", Log: logs,
ReloadUhttpd: func() { exec.Command("/etc/init.d/uhttpd", "reload").Run() }}.Run()
_, tool := os.Stat("/usr/bin/awg")
_, proto := os.Stat("/lib/netifd/proto/amneziawg.sh")
full := syncer.Apply(func() (*model.Model, string, error) {
c, m, err := st.Read()
if err != nil {
return nil, "", err
}
saved := ""
if s := c.Section("settings"); s != nil {
saved, _ = s.Get("lan_address")
}
return m, saved, nil
}, world.Usage, i18n.Use(i18n.Dir, lang), tool == nil && proto == nil, awg.SystemCaps(o.DataDir))
out := map[string]any{"network": full.Network, "firewall": full.Firewall, "like_home": full.LikeHome, "peers": full.Peers, "warning": nil}
if full.Warning != "" {
out["warning"] = full.Warning
}
return out
}
}
// switched off while it runs (/etc/dawnline/backend-off without a restart): the ucode backend's
// own service applies when it starts
return map[string]any{"warning": "The change is saved. Dawnline's backend is switched off: restart the service to put it into effect (/etc/init.d/dawnline restart)."}
},
// after the answer is out: the reload may restart this very process (its portal's port is in its arguments)
Reload: func() {
cmd := exec.Command("sh", "-c", "sleep 1; /etc/init.d/dawnline reload >/dev/null 2>&1")
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
cmd.Start()
},
AWG: func() map[string]any { return roster.AwgState(nets.handler) },
Store: &store.Store{Config: o.Config, RunDir: o.RunDir},
Told: func() {
if publish != nil {
publish()
}
},
// the runtime sync, and its warning for the admin
Sync: func(lang string) any {
// Go's own sync (unless the router is given back to ucode): the access sets, the devices
// into the tunnels and the hostnames, with no call to ucode
if mine() {
if world := w.World(); world != nil {
if _, m, err := (store.Store{Config: o.Config, RunDir: o.RunDir}).Read(); err == nil {
res := syncer.Sync(m, world.Usage, time.Now().Unix(), false, i18n.Use(i18n.Dir, lang))
if res.Warning == "" {
return nil
}
return res.Warning
}
}
}
return "The change is saved. Dawnline's backend is switched off: restart the service to put it into effect (/etc/init.d/dawnline restart)."
},
Log: &servicelog.Log{RunDir: o.RunDir, Level: func() string {
if world := w.World(); world != nil {
return world.M.Settings.LogLevel
}
return "info"
}},
RouterWAN: nets.routerWAN,
Bus: func(object, method string, args map[string]any) map[string]any {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
res, err := nets.bus.Call(ctx, object, method, args)
if err != nil {
return nil
}
return res
},
WANOf: nets.wanOf,
GitHub: &github.Client{RunDir: o.RunDir},
Board: func() map[string]any {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
res, _ := nets.bus.Call(ctx, "system", "board", nil)
return res
}}
more = methods.List(deps)
chores.deps = deps
native = methods.PortalHandler(deps, true)
}
object := api.Object(h, audience, api.Info{Version: backend.Version, Stream: o.Stream, Refresh: func(ctx context.Context) { b.Look(ctx) }, More: more})
bus := ubus.NewBus(o.Socket, object)
bus.Log = o.Log
b = bridge.New(bridge.Bridge{
Hub: h, Audience: audience, Log: o.Log,
// what the topics are made of besides what the watcher tells of itself: the settings, the
// configurations handed out, the servers kept down, the places found
Watch: []string{o.Config, o.DataDir + "/issued.json", o.RunDir + "/netifd-blocked.json", o.RunDir + "/geo.json"},
})
run := []func(){
func() { bus.Run(ctx) },
func() { api.Broadcast(ctx, h, bus, object) },
func() { b.Run(ctx) },
}
if o.Watch {
run = append(run, func() { w.Run(ctx) })
// The people as Go makes them, held against each status ucode answers (package roster).
nets.bus = bus
ro := &roster.Roster{World: w.World, Home: nets.of, DataDir: o.DataDir, RunDir: o.RunDir, Hub: h, Log: o.Log, Keyed: bridge.Split, Recent: w.Recent, Own: nets.own, Iface: nets.ifaceState, Homes: nets.serverHomes, Handler: nets.handler, WAN: nets.routerWAN}
// The topics as the router is now: the watcher looks first, so what is told is never what it knew a
// second ago. Without that look a page that asked in the backend's first second was told nothing
// until Refresh (the watcher had no world yet), and settings changed on disk were told as they were
// before (the bridge's second comes before the watcher's).
tell := func() bool {
w.Look()
return ro.Publish()
}
publish = func() { tell() }
// told again when the settings change, when somebody starts to look, and with the minute
b.Tell = tell
// the moment the watcher saw someone come, go or move, the pages have Go's own people
w.Changed = func() { go ro.Publish() }
}
if p := o.Portal; p != nil {
bell := &portal.Bell{}
srv := &portal.Server{
TLS: []string{fmt.Sprintf(":%d", p.Port)}, Cert: p.Cert, Key: p.Key,
People: &portal.Settings{Path: o.Config}, Bell: bell, Log: o.Log,
// the portal's answers (methods.Portal)
Native: native,
}
if p.HTTP > 0 {
srv.Plain = []string{fmt.Sprintf(":%d", p.HTTP)}
}
run = append(run, func() { srv.Run(ctx) }, func() { portal.Watch(ctx, bell, o.RunDir, o.DataDir, o.Config) })
}
var parts sync.WaitGroup
for _, part := range run {
parts.Add(1)
go func() {
defer parts.Done()
part()
}()
}
parts.Wait()
}
// homes answers which home networks a person reaches, from a look at the router's interfaces that is kept
// for a few seconds (every person of a status asks).
type homes struct {
bus *ubus.Bus
mu sync.Mutex
snap *netview.Snapshot
at time.Time
}
func (h *homes) of(srv *model.Server, p *model.Person) []string {
h.mu.Lock()
defer h.mu.Unlock()
if h.snap == nil || time.Since(h.at) > 5*time.Second {
h.snap = netview.Take(netview.System(h.logical))
h.at = time.Now()
}
return h.snap.PersonHome(srv, p)
}
// own is the router's own public-side addresses: each server's WAN address, and the wan interface's.
func (h *homes) own(m *model.Model) map[string]bool {
h.mu.Lock()
if h.snap == nil || time.Since(h.at) > 5*time.Second {
h.snap = netview.Take(netview.System(h.logical))
h.at = time.Now()
}
snap := h.snap
h.mu.Unlock()
out := map[string]bool{}
for _, srv := range m.Servers {
a := snap.WanAddress(srv)
if a == "" {
a = h.ifaceAddr(srv.WanZone)
}
if a != "" {
out[a] = true
}
}
if a := h.ifaceAddr("wan"); a != "" {
out[a] = true
}
return out
}
// addresses is every IPv4 address of netifd's interfaces, in its order.
func (h *homes) addresses() []methods.Address {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
res, err := h.bus.Call(ctx, "network.interface", "dump", nil)
if err != nil {
return nil
}
var out []methods.Address
list, _ := res["interface"].([]any)
for _, i := range list {
m, _ := i.(map[string]any)
name, _ := m["interface"].(string)
addrs, _ := m["ipv4-address"].([]any)
for _, a := range addrs {
am, _ := a.(map[string]any)
out = append(out, methods.Address{Iface: name, CIDR: fmt.Sprintf("%v/%v", am["address"], am["mask"])})
}
}
return out
}
// wanOf is the IPv4 address of a server's WAN (its interface's, else its zone's netifd interface's); of
// netifd's wan for no server.
func (h *homes) wanOf(srv *model.Server) string {
if srv == nil {
return h.ifaceAddr("wan")
}
if a := netview.Take(netview.System(h.logical)).WanAddress(srv); a != "" {
return a
}
return h.ifaceAddr(srv.WanZone)
}
// handler tells whether netifd lists the amneziawg protocol.
func (h *homes) handler() bool {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
res, err := h.bus.Call(ctx, "network", "get_proto_handlers", nil)
_, has := res["amneziawg"]
return err == nil && has
}
// routerWAN is the router's WAN address: the first server's with keys, else its zone's interface's.
func (h *homes) routerWAN(m *model.Model) string {
h.mu.Lock()
if h.snap == nil || time.Since(h.at) > 5*time.Second {
h.snap = netview.Take(netview.System(h.logical))
h.at = time.Now()
}
snap := h.snap
h.mu.Unlock()
for _, id := range m.ServerIDs() {
srv := m.Servers[id]
if srv.PrivateKey == "" {
continue
}
if a := snap.WanAddress(srv); a != "" {
return a
}
if a := h.ifaceAddr(srv.WanZone); a != "" {
return a
}
}
return h.ifaceAddr("wan")
}
// ifaceState is whether a netifd interface is up, and for how long.
func (h *homes) ifaceState(name string) (bool, int64) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
res, err := h.bus.Call(ctx, "network.interface."+name, "status", nil)
if err != nil {
return false, 0
}
up, _ := res["up"].(bool)
var uptime int64
switch n := res["uptime"].(type) {
case int64:
uptime = n
case int32:
uptime = int64(n)
case float64:
uptime = int64(n)
case int:
uptime = int64(n)
}
return up, uptime
}
// serverHomes is a server's home interfaces with what each reaches.
func (h *homes) serverHomes(srv *model.Server) []view.HomeNet {
h.mu.Lock()
defer h.mu.Unlock()
if h.snap == nil || time.Since(h.at) > 5*time.Second {
h.snap = netview.Take(netview.System(h.logical))
h.at = time.Now()
}
home, _ := h.snap.ServerHome(srv)
out := []view.HomeNet{}
for _, x := range home {
out = append(out, view.HomeNet{Dev: x.Dev, Nets: x.Nets})
}
return out
}
// ifaceAddr is a netifd interface's first IPv4 address, or "".
func (h *homes) ifaceAddr(name string) string {
if name == "" {
return ""
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
res, err := h.bus.Call(ctx, "network.interface."+name, "status", nil)
if err != nil {
return ""
}
list, _ := res["ipv4-address"].([]any)
if len(list) == 0 {
return ""
}
first, _ := list[0].(map[string]any)
a, _ := first["address"].(string)
return a
}
// logical asks netifd which interface stands on which device.
func (h *homes) logical() map[string]netview.Logical {
out := map[string]netview.Logical{}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
res, err := h.bus.Call(ctx, "network.interface", "dump", nil)
if err != nil {
return out
}
list, _ := res["interface"].([]any)
for _, i := range list {
m, _ := i.(map[string]any)
dev, _ := m["l3_device"].(string)
if dev == "" {
dev, _ = m["device"].(string)
}
if _, seen := out[dev]; dev == "" || seen {
continue
}
name, _ := m["interface"].(string)
proto, _ := m["proto"].(string)
out[dev] = netview.Logical{Name: name, Proto: proto}
}
return out
}
+368
View File
@@ -0,0 +1,368 @@
//go:build e2e
// The whole path a page takes, on a real OpenWrt userland: uhttpd's /ubus,
// rpcd's sessions and access lists (the package's own ACL file), ubusd and the
// backend with its own watcher and topics, over a stand's settings and tunnel.
// scripts/backend-container.sh starts the daemons and runs this.
package daemon
import (
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"sync"
"testing"
"time"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/hub"
"github.com/tickcount/luci-app-dawnline/backend/ubus"
)
const web = "http://127.0.0.1:8099/ubus"
// rpc is a JSON-RPC call through uhttpd, as LuCI's rpc.js makes it. It
// returns ubus's status and the answer.
func rpc(t *testing.T, sid, object, method string, args map[string]any) (int, map[string]any) {
t.Helper()
body, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": 1, "method": "call", "params": []any{sid, object, method, args}})
res, err := http.Post(web, "application/json", bytes.NewReader(body))
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
var out struct {
Result []json.RawMessage
Error *struct {
Code int
Message string
}
}
if err := json.NewDecoder(res.Body).Decode(&out); err != nil {
t.Fatal(err)
}
if out.Error != nil {
return out.Error.Code, nil
}
var code int
json.Unmarshal(out.Result[0], &code)
var data map[string]any
if len(out.Result) > 1 {
json.Unmarshal(out.Result[1], &data)
}
return code, data
}
func at(v any, path ...string) any {
for _, k := range path {
m, _ := v.(map[string]any)
v = m[k]
}
return v
}
// The stand's settings: one server, one person, one device.
const (
standServerKey = "yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk="
standServerPub = "xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg="
standDeviceKey = "HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw="
)
func standSettings(person string) string {
return `
config settings 'settings'
config server 's1'
option name 'Home'
option ifname 'dawn0'
option subnet '10.77.0.0/24'
option private_key '` + standServerKey + `'
option public_key '` + standServerPub + `'
config person 'p1'
option server 's1'
option name '` + person + `'
config device 'd1'
option person 'p1'
option name 'Phone'
option kind 'phone'
option address '10.77.0.2'
option public_key '` + standDeviceKey + `'
`
}
// tunnel is the stand's one tunnel: its device is there, with the handshake the test gave it.
type tunnel struct {
mu sync.Mutex
handshake int64
}
func (tn *tunnel) reader() *awg.Reader {
return &awg.Reader{
// as `awg show <ifname> dump` prints: the interface's line, then a peer a line
Show: func(string) (string, bool) {
tn.mu.Lock()
defer tn.mu.Unlock()
return standServerKey + "\t" + standServerPub + "\t4500\toff\n" +
fmt.Sprintf("%s\t(none)\t203.0.113.7:41821\t10.77.0.2/32\t%d\t0\t0\toff\n", standDeviceKey, tn.handshake), true
},
Index: func(string) (int, bool) { return 7, true },
}
}
func (tn *tunnel) shake(at int64) {
tn.mu.Lock()
tn.handshake = at
tn.mu.Unlock()
}
// follow asks with wait until the documents satisfy `done`, taking the events into them, and returns the
// number the page is at and the events of the last answer. The topics are the page's whole snapshot.
func follow(t *testing.T, sid, epoch string, seq float64, docs map[string]any, topics []any, what string, done func() bool) (float64, []any) {
t.Helper()
deadline := time.Now().Add(25 * time.Second)
for {
args := map[string]any{"epoch": epoch, "seq": seq, "timeout": 10}
if topics != nil {
args["topics"] = topics
}
code, got := rpc(t, sid, "dawnline", "wait", args)
events, _ := got["events"].([]any)
if code != 0 || got["docs"] != nil || (len(events) == 0 && time.Now().After(deadline)) {
t.Fatalf("wait for %s: %d %v", what, code, got)
}
take(docs, events)
seq = got["seq"].(float64)
if done() {
return seq, events
}
}
}
// take applies events to a page's documents, as a page does.
func take(docs map[string]any, events []any) {
for _, e := range events {
topic, _ := at(e, "topic").(string)
switch at(e, "op") {
case "put":
docs[topic] = at(e, "data")
case "patch":
docs[topic] = hub.Merge(docs[topic], at(e, "data"))
}
}
}
func TestThroughUhttpd(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
dir := t.TempDir()
config := filepath.Join(dir, "config")
settings := func(person string) {
if err := os.WriteFile(config, []byte(standSettings(person)), 0o600); err != nil {
t.Fatal(err)
}
}
settings("Myata")
// The stand is no router to keep: the minute's work (the access rules, the watchdog) stays away, the
// watcher and the topics are the backend's own.
if err := os.WriteFile(filepath.Join(dir, "backend-off"), nil, 0o600); err != nil {
t.Fatal(err)
}
// A backend that was restarted: its earlier run left who was online, and nobody came or went since, so
// the watcher's first look finds nothing to tell of by itself.
run := filepath.Join(dir, "run")
os.MkdirAll(run, 0o755)
if err := os.WriteFile(filepath.Join(run, "presence.json"), []byte(`{"online":{"d1":false},"endpoint":{"d1":"203.0.113.7"},"rev":1,"people":{"p1":1}}`), 0o600); err != nil {
t.Fatal(err)
}
tn := &tunnel{}
began := time.Now()
// The backend is stopped, and has stopped, before the test ends: it writes into the stand's folders (its beat,
// its presence) and logs through t, and a test that ended under it failed at removing a folder still written to.
stopped := make(chan struct{})
go func() {
defer close(stopped)
Run(ctx, Options{Socket: ubus.Socket, RunDir: run, DataDir: dir, Config: config, Stream: "sse", Watch: true, Tunnels: tn.reader(), Log: t.Logf})
}()
defer func() {
cancel()
select {
case <-stopped:
case <-time.After(10 * time.Second):
t.Error("the backend did not stop")
}
}()
for exec.CommandContext(ctx, "ubus", "list", "dawnline").Run() != nil {
if ctx.Err() != nil {
t.Fatal("the object never appeared on the bus")
}
time.Sleep(20 * time.Millisecond)
}
// Without a session rpcd refuses; with one, the read ACL lets the page in.
if code, _ := rpc(t, "00000000000000000000000000000000", "dawnline", "hello", nil); code == 0 {
t.Fatal("answered without a session")
}
code, login := rpc(t, "00000000000000000000000000000000", "session", "login", map[string]any{"username": "root", "password": ""})
if code != 0 {
t.Fatalf("login: %d", code)
}
sid := login["ubus_rpc_session"].(string)
code, hello := rpc(t, sid, "dawnline", "hello", map[string]any{})
if code != 0 || hello["stream"] != "sse" || hello["epoch"] == "" {
t.Fatalf("hello: %d %v", code, hello)
}
// A page that was open while the backend restarted asks at once, before the watcher's first look: its
// first question starts the bridge, and the documents come with the bridge's next second. (They once
// came only with the next Refresh, half a minute later: the bridge took topics nobody could make yet
// for told.)
asked := time.Now()
code, snap := rpc(t, sid, "dawnline", "sync", map[string]any{})
if code != 0 {
t.Fatalf("sync: %d", code)
}
epoch, seq := snap["epoch"].(string), snap["seq"].(float64)
docs, _ := snap["docs"].(map[string]any)
if docs == nil {
docs = map[string]any{}
}
whole := func() bool {
return at(docs, "people", "items", "p1", "name") == "Myata" && at(docs, "servers", "items", "s1") != nil && at(docs, "backend", "ok") == true
}
if !whole() {
seq, _ = follow(t, sid, epoch, seq, docs, nil, "the first documents", whole)
}
took := time.Since(asked)
t.Logf("the page asked %v after the backend started and had its first documents %v later", asked.Sub(began).Round(time.Millisecond), took.Round(time.Millisecond))
if took > 3*time.Second {
t.Fatalf("the first documents took %v", took)
}
if at(docs, "people", "items", "p1", "devices", "items", "d1", "online") != false || at(docs, "servers", "order").([]any)[0] != "s1" {
t.Fatalf("documents: %v", docs)
}
// An event stream, as a page opens it; on a web server that cannot hold one this part is skipped.
stream, lines := openStream(t, ctx, sid)
// A device comes online: the long poll returns with a patch that says so, and not the people whole.
online := func() bool { return at(docs, "people", "items", "p1", "devices", "items", "d1", "online") == true }
type result struct {
seq float64
events []any
}
waited := make(chan result, 1)
go func() {
s, events := follow(t, sid, epoch, seq, docs, []any{"people"}, "the device to come online", online)
waited <- result{s, events}
}()
time.Sleep(300 * time.Millisecond)
changed := time.Now()
tn.shake(time.Now().Unix())
var r result
select {
case r = <-waited:
case <-time.After(40 * time.Second):
t.Fatal("the device never came online for the page")
}
var patch any
for _, e := range r.events {
if at(e, "op") == "patch" && at(e, "data", "items", "p1", "devices", "items", "d1", "online") == true {
patch = e
}
}
if patch == nil || at(patch, "data", "items", "p1", "name") != nil || at(patch, "data", "order") != nil {
t.Fatalf("patch: %v", r.events)
}
t.Logf("a device that came online reached the page %v later, as %d bytes", time.Since(changed).Round(time.Millisecond), len(fmt.Sprint(at(patch, "data"))))
if stream {
// The same event came down the stream, with the same number.
deadline := time.After(10 * time.Second)
for found := false; !found; {
select {
case l := <-lines:
var e map[string]any
if strings.HasPrefix(l, "data: ") && json.Unmarshal([]byte(l[6:]), &e) == nil && e["seq"] == at(patch, "seq") {
if e["topic"] != "people" || e["op"] != "patch" || e["epoch"] != epoch || !reflect.DeepEqual(e["data"], at(patch, "data")) {
t.Fatalf("the stream's event differs: %v", e)
}
found = true
}
case <-deadline:
t.Fatal("the event never came down the stream")
}
}
}
// The meter's points come as they are made, to a page that names it.
if code, _ := rpc(t, sid, "dawnline", "watch", map[string]any{"topics": []any{"meter"}}); code != 0 {
t.Fatalf("watch: %d", code)
}
code, got := rpc(t, sid, "dawnline", "wait", map[string]any{"epoch": epoch, "seq": r.seq, "topics": []any{"meter"}, "timeout": 10})
events, _ := got["events"].([]any)
appended := false
for _, e := range events {
appended = appended || at(e, "op") == "append"
}
if code != 0 || !appended {
t.Fatalf("meter: %d %v", code, got)
}
// A page reads its own edit: once refresh answers, the stream holds what is on disk, with no waiting
// for the watcher's second.
settings("Mint")
code, stands := rpc(t, sid, "dawnline", "refresh", map[string]any{})
if code != 0 || stands["epoch"] != epoch {
t.Fatalf("refresh: %d %v", code, stands)
}
code, got = rpc(t, sid, "dawnline", "sync", map[string]any{"epoch": epoch, "seq": r.seq, "topics": []any{"people"}})
events, _ = got["events"].([]any)
take(docs, events)
if code != 0 || at(docs, "people", "items", "p1", "name") != "Mint" || got["seq"].(float64) < stands["seq"].(float64) {
t.Fatalf("after refresh: %d %v (the stream stands at %v)", code, got, stands["seq"])
}
// Arguments are checked at the door.
if code, _ := rpc(t, sid, "dawnline", "sync", map[string]any{"seq": -1}); code != int(ubus.StatusInvalidArgument) {
t.Fatalf("a negative number was accepted: %d", code)
}
if code, _ := rpc(t, sid, "dawnline", "sync", map[string]any{"topics": "people"}); code != int(ubus.StatusInvalidArgument) {
t.Fatalf("topics that are no list were accepted: %d", code)
}
}
// openStream subscribes through uhttpd and returns the stream's lines.
func openStream(t *testing.T, ctx context.Context, sid string) (bool, <-chan string) {
req, _ := http.NewRequestWithContext(ctx, "GET", web+"/subscribe/dawnline", nil)
req.Header.Set("Authorization", "Bearer "+sid)
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
if ct := res.Header.Get("Content-Type"); !strings.HasPrefix(ct, "text/event-stream") {
t.Logf("no event stream from this web server (%s): %s", res.Status, ct)
res.Body.Close()
return false, nil
}
lines := make(chan string, 64)
go func() {
defer res.Body.Close()
sc := bufio.NewScanner(res.Body)
sc.Buffer(make([]byte, 0, 64<<10), 2<<20)
for sc.Scan() {
select {
case lines <- sc.Text():
default:
}
}
}()
return true, lines
}
+93
View File
@@ -0,0 +1,93 @@
package daemon
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"time"
backend "github.com/tickcount/luci-app-dawnline/backend"
"github.com/tickcount/luci-app-dawnline/backend/geo"
"github.com/tickcount/luci-app-dawnline/backend/journal"
"github.com/tickcount/luci-app-dawnline/backend/servicelog"
"github.com/tickcount/luci-app-dawnline/backend/store"
"github.com/tickcount/luci-app-dawnline/backend/updater"
)
// The beat is the time of the backend's last minute: the Health page calls it fine while it is fresh, and
// a beat left behind by a backend that is gone tells the next start that the last one did not end well.
const beatFile = "/agent.beat"
func readTrim(path string) string {
raw, _ := os.ReadFile(path)
return strings.Trim(string(raw), " \t\r\n\v\f")
}
// started says in the service log why this start: an update that was rolled back, a new version, a crash
// (the beat outlives a backend that died), a restart (the init script notes a stop), a boot, or a plain
// start. It also puts back the service log a reboot took from memory, and remembers the kernel an
// AmneziaWG module Dawnline did not install is for.
func started(o Options, logs *servicelog.Log) {
os.MkdirAll(o.RunDir, 0o755)
_, beat := os.Stat(o.RunDir + beatFile)
crashed := beat == nil
stopped, _ := strconv.ParseInt(readTrim(o.RunDir+"/agent.stopped"), 10, 64)
os.Remove(o.RunDir + "/agent.stopped")
was := readTrim(o.DataDir + "/version")
v := backend.Version
if was != v {
os.MkdirAll(o.DataDir, 0o700)
os.WriteFile(o.DataDir+"/version", []byte(v), 0o644)
}
// the service log lives in memory: after a reboot, what flash kept of it
if _, err := os.Stat(o.RunDir + "/service.log"); err != nil {
if raw, err := os.ReadFile(o.DataDir + "/service.log"); err == nil {
os.WriteFile(o.RunDir+"/service.log", raw, 0o600)
}
}
say := func(level, msg, detail string) {
logs.Write("dawnline", level, msg, detail, "Dawnline", servicelog.Extra{})
}
rolled := readTrim(o.RunDir + "/update-rolled-back")
os.Remove(o.RunDir + "/update-rolled-back")
uptime := 0.0
fmt.Sscan(readTrim("/proc/uptime"), &uptime)
switch {
// postupdate put this (kept) version back because the new one did not come up
case rolled != "":
say("error", fmt.Sprintf("update to %s failed its check - rolled back to %s", rolled, v),
"The new version did not answer in LuCI or its agent did not start. Try again from Components, or install it with install.sh to see why.")
case was != "" && was != v:
say("up", fmt.Sprintf("updated · %s → %s", was, v), "")
case crashed:
// why, when syslog knows: procd, the kernel (out of memory, a fault)
why, _ := exec.Command("sh", "-c", `logread -l 400 2>/dev/null | grep -iE "kernel:.*(out of memory|killed process|oom-kill|segfault)|procd:.*dawnline|dawnlined" | tail -n 6`).Output()
say("repair", "stopped unexpectedly - started again", strings.Trim(string(why), " \t\r\n\v\f"))
case stopped != 0 && time.Now().Unix()-stopped < 300:
say("info", "restarted · version "+v, "")
case uptime < 600:
say("info", "started with the router · version "+v, "")
default:
say("info", "started · version "+v, "")
}
updater.Adopt(o.DataDir)
beatNow(o)
}
func beatNow(o Options) {
os.WriteFile(o.RunDir+beatFile, []byte(strconv.FormatInt(time.Now().Unix(), 10)), 0o644)
}
// ended is a clean stop: what lives in memory goes to flash (the activity log, the service log, the places
// found), and the beat goes, so the next start is not taken for a crash. The counters are the watcher's.
func ended(o Options, logs *servicelog.Log) {
now := time.Now().Unix()
if _, m, err := (store.Store{Config: o.Config, RunDir: o.RunDir}).Read(); err == nil {
journal.Log{Run: o.RunDir, Data: o.DataDir}.Persist(journal.PolicyOf(m.Settings.Retention), now)
}
logs.Trim(o.DataDir, true)
geo.Store{Run: o.RunDir, Data: o.DataDir}.Flush()
os.Remove(o.RunDir + beatFile)
}
+197
View File
@@ -0,0 +1,197 @@
// Package disguise checks a server's disguise values (AmneziaWG's obfuscation) as dawnline.awg does.
//
// Unsafe is obf_unsafe: what AmneziaWG would refuse, crash on or break devices with. Its sentences are in
// English, as ucode writes them for a reader with no language; held to ucode's own output on made-up values
// (testdata/trace.uc, scripts/ucode-golden.sh).
package disguise
import (
"fmt"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/awg"
)
// Conf names a value as .conf files do (dawnline.awg OBF_CONF).
var Conf = map[string]string{
"jc": "Jc", "jmin": "Jmin", "jmax": "Jmax", "s1": "S1", "s2": "S2", "s3": "S3", "s4": "S4",
"h1": "H1", "h2": "H2", "h3": "H3", "h4": "H4", "i1": "I1", "i2": "I2", "i3": "I3", "i4": "I4", "i5": "I5",
"header_protection_key": "HeaderProtectionKey", "content_padding_addition": "ContentPaddingAddition",
"rekey_after_time": "RekeyAfterTime", "rekey_timeout": "RekeyTimeout", "reject_after_time": "RejectAfterTime",
"keepalive_timeout": "KeepaliveTimeout", "max_handshake_attempts": "MaxHandshakeAttempts",
"random_trailers": "RandomTrailers", "disable_cookies": "DisableCookies",
}
const space = " \t\r\n\v\f"
// uint is a whole number in digits up to top; ok false for anything else.
func uint(s string, top int64) (int64, bool) {
if s == "" || len(s) > 10 {
return 0, false
}
var n int64
for i := 0; i < len(s); i++ {
if s[i] < '0' || s[i] > '9' {
return 0, false
}
n = n*10 + int64(s[i]-'0')
}
return n, n <= top
}
// span is a number or a range as AmneziaWG writes them ("5", "5-15"), each end up to top.
func span(v string, top int64) (lo, hi int64, ok bool) {
p := strings.Split(strings.Trim(v, space), "-")
if len(p) > 2 {
return 0, 0, false
}
if lo, ok = uint(p[0], top); !ok {
return 0, 0, false
}
hi = lo
if len(p) == 2 {
if hi, ok = uint(p[1], top); !ok {
return 0, 0, false
}
}
return lo, hi, lo <= hi
}
// tagOK tells one tag of a signature packet both the kernel and amneziawg-go read.
func tagOK(tag string) bool {
if tag == "t" {
return true
}
key, val, found := strings.Cut(tag, " ")
if !found {
return false
}
switch key {
case "b":
return len(val) > 2 && len(val)%2 == 0 && val[:2] == "0x" && strings.TrimLeft(val[2:], "0123456789abcdefABCDEF") == ""
case "r", "rc", "rd":
n, ok := uint(val, 1000)
return ok && n >= 1
}
return false
}
// tagError is what is wrong with signature packet name holding s, or "".
func tagError(name, s string) string {
if strings.ContainsAny(s, "#\n\r") {
return name + " may not contain # or a line break."
}
for a := strings.Index(s, "<"); a >= 0; a = strings.Index(s, "<") {
b := strings.Index(s[a:], ">")
if b < 0 {
return name + " has a tag that is not closed with >."
}
tag := s[a+1 : a+b]
if !tagOK(tag) {
if len(tag) > 24 {
tag = tag[:24] + "…"
}
return fmt.Sprintf("%s: <%s> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.", name, tag)
}
s = s[a+b+1:]
}
return ""
}
// Unsafe lists what in disguise values o must never be handed to the kernel; empty when they are safe.
func Unsafe(o map[string]string) []string {
errs := []string{}
given := func(k string) bool { return o[k] != "" }
// a value not given counts as 0
num := func(k string) (int64, bool) {
if !given(k) {
return 0, true
}
return uint(strings.Trim(o[k], space), 65535)
}
for _, k := range []string{"jc", "jmin", "jmax", "s1", "s2", "s3", "s4"} {
if _, ok := num(k); !ok {
errs = append(errs, Conf[k]+" must be a whole number from 0 to 65535.")
}
}
if given("jmin") && given("jmax") {
lo, ok1 := num("jmin")
hi, ok2 := num("jmax")
if ok1 && ok2 && lo > hi {
errs = append(errs, "Jmin must not be larger than Jmax.")
}
}
// packet types: a number or a range each, apart from the others; one not given keeps WireGuard's own
type rng struct {
lo, hi int64
ok bool
}
var hs [4]rng
for i := 0; i < 4; i++ {
k := fmt.Sprintf("h%d", i+1)
hs[i] = rng{int64(i + 1), int64(i + 1), true}
if given(k) {
lo, hi, ok := span(o[k], 4294967295)
hs[i] = rng{lo, hi, ok}
if !ok {
errs = append(errs, Conf[k]+" must be a number or a range like 100-200, up to 4294967295.")
}
}
}
for i := 0; i < 4; i++ {
for j := i + 1; j < 4; j++ {
if hs[i].ok && hs[j].ok && hs[i].lo <= hs[j].hi && hs[j].lo <= hs[i].hi {
errs = append(errs, fmt.Sprintf("%s and %s overlap: each packet type needs values of its own.", Conf[fmt.Sprintf("h%d", i+1)], Conf[fmt.Sprintf("h%d", j+1)]))
}
}
}
// header protection: a 32-byte key, and every padding at least the 12 bytes of its nonce
if given("header_protection_key") {
if !awg.ValidKey(o["header_protection_key"]) {
errs = append(errs, "The header protection key is not a key.")
}
for _, k := range []string{"s1", "s2", "s3", "s4"} {
if n, ok := num(k); ok && n < 12 {
errs = append(errs, Conf[k]+" must be at least 12 with header protection.")
}
}
}
// 3.1 timers and counts: 0 alone is the kernel's default, a 0 in a range would fire at once
for _, k := range []string{"rekey_after_time", "rekey_timeout", "reject_after_time", "keepalive_timeout", "max_handshake_attempts"} {
var lo, hi int64
ok := true
if given(k) {
lo, hi, ok = span(o[k], 65535)
}
if !ok || (lo == 0 && hi > 0) {
errs = append(errs, Conf[k]+" must be a number or a range like 5-15, from 1 to 65535.")
} else if k == "reject_after_time" && hi > 180 {
errs = append(errs, "RejectAfterTime must be at most 180 seconds.")
}
}
if given("content_padding_addition") {
if _, _, ok := span(o["content_padding_addition"], 65535); !ok {
errs = append(errs, Conf["content_padding_addition"]+" must be a number or a range like 10-100, up to 65535.")
}
}
for _, k := range []string{"random_trailers", "disable_cookies"} {
if v := o[k]; given(k) && v != "on" && v != "off" && v != "1" && v != "0" {
errs = append(errs, Conf[k]+" must be on or off.")
}
}
// signature packets: their tags, and all of them in one device's configuration (one QR code)
total := 0
for _, k := range []string{"i1", "i2", "i3", "i4", "i5"} {
if !given(k) {
continue
}
total += len(o[k])
if e := tagError(Conf[k], o[k]); e != "" {
errs = append(errs, e)
}
}
if total > 2000 {
errs = append(errs, fmt.Sprintf("I1-I5 together must be at most 2000 characters, not %d.", total))
}
return errs
}
+107
View File
@@ -0,0 +1,107 @@
package disguise
import (
"encoding/json"
"fmt"
"os"
"reflect"
"regexp"
"strings"
"testing"
)
// Each made-up set of values gets the sentences ucode gives it (testdata/trace.uc, scripts/ucode-golden.sh).
func TestUnsafeAsUcode(t *testing.T) {
raw, err := os.ReadFile("testdata/trace.json")
if err != nil {
t.Fatal(err)
}
var golden struct {
Sets []struct {
O map[string]string `json:"o"`
Errs []string `json:"errs"`
All []string `json:"all"`
} `json:"sets"`
}
if err := json.Unmarshal(raw, &golden); err != nil {
t.Fatal(err)
}
rows := golden.Sets
if len(rows) < 30 {
t.Fatalf("only %d rows", len(rows))
}
said := 0
for i, r := range rows {
got := Unsafe(r.O)
said += len(got)
if !reflect.DeepEqual(got, r.Errs) {
t.Errorf("row %d %v:\n go: %q\n ucode: %q", i, r.O, got, r.Errs)
}
if all := Errors(r.O); !reflect.DeepEqual(all, r.All) {
t.Errorf("row %d %v, with Dawnline's own bounds:\n go: %q\n ucode: %q", i, r.O, all, r.All)
}
}
if said < 40 {
t.Fatalf("the rows exercise too little: %d sentences", said)
}
}
// shape is a value with what the dice decide taken out: digits as 9, hex bytes of a tag as x.
func shape(key, v string) string {
if key == "header_protection_key" {
return fmt.Sprint("key of ", len(v))
}
v = regexp.MustCompile(`0x[0-9a-f]+`).ReplaceAllStringFunc(v, func(s string) string { return "0x" + strings.Repeat("x", len(s)-2) })
// how many digits a drawn number has is the dice's too
return regexp.MustCompile(`[0-9]+`).ReplaceAllString(v, "9")
}
// A preset made here has the names, the order and the shapes of the ones ucode makes, passes its own check,
// and ucode's presets pass it too.
func TestPresets(t *testing.T) {
raw, _ := os.ReadFile("testdata/presets.json")
var golden struct {
Presets map[string][]map[string]string `json:"presets"`
}
json.Unmarshal(raw, &golden)
for _, name := range Methods {
theirs := golden.Presets[name]
if len(theirs) == 0 {
t.Fatalf("the golden has no %s preset", name)
}
shapes := map[string]map[string]bool{}
for _, o := range theirs {
if errs := Errors(o); len(errs) != 0 {
t.Errorf("ucode's own %s preset is refused: %q", name, errs)
}
for k, v := range o {
if shapes[k] == nil {
shapes[k] = map[string]bool{}
}
shapes[k][shape(k, v)] = true
}
}
for i := 0; i < 200; i++ {
mine := Preset(name)
o := map[string]string{}
for _, p := range mine {
if _, twice := o[p.Key]; twice {
t.Fatalf("%s: %s twice", name, p.Key)
}
o[p.Key] = p.Value
if !shapes[p.Key][shape(p.Key, p.Value)] {
t.Fatalf("%s: %s = %q has a shape ucode never makes (%v)", name, p.Key, p.Value, shapes[p.Key])
}
}
if len(o) != len(theirs[0]) {
t.Fatalf("%s: %d values, ucode makes %d", name, len(o), len(theirs[0]))
}
if errs := Errors(o); len(errs) != 0 {
t.Fatalf("%s: a preset its own check refuses: %q\n%v", name, errs, o)
}
}
}
if !reflect.DeepEqual(Preset("off"), []Pair{{"jc", "0"}, {"jmin", "0"}, {"jmax", "0"}, {"s1", "0"}, {"s2", "0"}, {"h1", "1"}, {"h2", "2"}, {"h3", "3"}, {"h4", "4"}}) {
t.Fatal("no disguise is not what it was")
}
}
+132
View File
@@ -0,0 +1,132 @@
package disguise
import (
crand "crypto/rand"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"fmt"
"strconv"
"strings"
)
// Pair is one disguise value by its name; a preset is a row of them, in the order they are written.
type Pair struct{ Key, Value string }
// randInt is a whole number from lo to hi, both in, as dawnline.util rand_int draws it.
func randInt(lo, hi int) int {
var b [4]byte
crand.Read(b[:])
n := int(binary.BigEndian.Uint32(b[:]) & 0x7fffffff)
return lo + n%(hi-lo+1)
}
// A name of the server's own for its signature packets, as tags: <r N> random bytes, <rc N> random
// letters, <b 0x..> fixed bytes.
func dnsName() string {
tld := []string{"com", "net", "org", "io"}[randInt(0, 3)]
n := randInt(5, 9)
return fmt.Sprintf("<b 0x%02x><rc %d><b 0x%02x%s00>", n, n, len(tld), hex.EncodeToString([]byte(tld)))
}
// Signature packets shaped like DNS, made fresh for each server: a query and an answer about a name of
// its own, with its own TTL and address. The same fixed bytes on every router would be one rule to match.
func dnsQuery() string {
// id, flags (standard query, recursion desired), 1 question, name, type A, class IN
return "<r 2><b 0x01000001000000000000>" + dnsName() + "<b 0x00010001>"
}
func dnsAnswer() string {
// id, flags (answer, authoritative, recursion), 1 question, 1 answer, name, A IN, pointer, A IN, ttl, 4 bytes
return "<r 2><b 0x85800001000100000000>" + dnsName() + fmt.Sprintf("<b 0x00010001c00c00010001%08x0004><r 4>", randInt(300, 86400))
}
// Methods are the disguises a server may be given: none, AmneziaWG 2.0's, AmneziaWG 3.1's.
var Methods = []string{"off", "balanced", "max"}
// Preset makes the disguise values of a method (dawnline.awg obf_preset):
//
// off plain WireGuard-compatible packets
// balanced AmneziaWG 2.0: junk packets, random headers, padded cookie and data packets, DNS-shaped
// signature packets
// max AmneziaWG 3.1: protected headers, random trailers, padding, jittered timers and a DNS-like
// first packet, the values AmneziaVPN itself uses for 3.1 servers
func Preset(name string) []Pair {
i := strconv.Itoa
switch name {
case "max":
// 32 random bytes, as `awg genkey` makes them
key := make([]byte, 32)
crand.Read(key)
return []Pair{
{"jc", i(randInt(4, 6))}, {"jmin", "10"}, {"jmax", "50"},
{"s1", "12"}, {"s2", "12"}, {"s3", "12"}, {"s4", "12"},
{"h1", "1"}, {"h2", "2"}, {"h3", "3"}, {"h4", "4"},
{"i1", dnsQuery()}, {"i2", dnsAnswer()},
{"header_protection_key", base64.StdEncoding.EncodeToString(key)},
{"content_padding_addition", "10-100"},
{"rekey_after_time", "100-120"}, {"rekey_timeout", "3-7"}, {"reject_after_time", "150-180"},
{"keepalive_timeout", "5-15"}, {"max_handshake_attempts", "15-20"},
{"random_trailers", "on"}, {"disable_cookies", "on"},
}
case "off":
return []Pair{{"jc", "0"}, {"jmin", "0"}, {"jmax", "0"}, {"s1", "0"}, {"s2", "0"}, {"h1", "1"}, {"h2", "2"}, {"h3", "3"}, {"h4", "4"}}
}
jmin := randInt(64, 128)
s1, s2 := randInt(15, 64), randInt(15, 64)
// a handshake initiation and response never share a size
for s1+56 == s2 {
s2 = randInt(15, 64)
}
o := []Pair{{"jc", i(randInt(4, 8))}, {"jmin", i(jmin)}, {"jmax", i(jmin + randInt(150, 400))}, {"s1", i(s1)}, {"s2", i(s2)}}
used := map[int]bool{}
for _, k := range []string{"h1", "h2", "h3", "h4"} {
v := randInt(5, 2147483647)
for used[v] {
v = randInt(5, 2147483647)
}
used[v] = true
o = append(o, Pair{k, i(v)})
}
// AmneziaWG 2.0: padded cookie and data packets (S4 costs MTU, kept small) and DNS-shaped signature
// packets before each handshake: what 1.0 traffic lacks and gets recognised by
return append(o, Pair{"s3", i(randInt(8, 48))}, Pair{"s4", i(randInt(4, 16))}, Pair{"i1", dnsQuery()}, Pair{"i2", dnsAnswer()})
}
// Junk packets may be tiny; the ceiling keeps them under the path MTU.
var bounds = []struct {
key string
lo, hi int64
}{{"jc", 0, 10}, {"jmin", 0, 1024}, {"jmax", 0, 1024}, {"s1", 0, 64}, {"s2", 0, 64}, {"s3", 0, 64}, {"s4", 0, 32}}
// plus is a value as ucode's unary plus reads it, for the one check that adds two of them.
func plus(s string) (float64, bool) {
s = strings.Trim(s, space)
if n, err := strconv.ParseInt(s, 0, 64); err == nil {
return float64(n), true
}
n, err := strconv.ParseFloat(s, 64)
return n, err == nil
}
// Errors is what is wrong with the disguise values of a server Dawnline sets up: what AmneziaWG refuses
// (Unsafe), and Dawnline's own bounds on top. Its presets pass it. dawnline.awg obf_errors.
func Errors(o map[string]string) []string {
errs := Unsafe(o)
for _, b := range bounds {
if o[b.key] == "" {
continue
}
if n, ok := uint(strings.Trim(o[b.key], space), 65535); ok && (n < b.lo || n > b.hi) {
errs = append(errs, fmt.Sprintf("%s must be from %d to %d.", Conf[b.key], b.lo, b.hi))
}
}
_, has1 := o["s1"]
_, has2 := o["s2"]
if a, ok1 := plus(o["s1"]); has1 && has2 && ok1 {
if b, ok2 := plus(o["s2"]); ok2 && a+56 == b {
errs = append(errs, "S2 must not be S1 + 56: the handshake initiation and response would have the same size.")
}
}
return errs
}
File diff suppressed because it is too large. Load diff
+576
View File
@@ -0,0 +1,576 @@
{
"sets": [
{
"o": {
},
"errs": [
],
"all": [
]
},
{
"o": {
"jc": "4",
"jmin": "10",
"jmax": "50",
"s1": "20",
"s2": "30",
"h1": "100",
"h2": "200",
"h3": "300",
"h4": "400"
},
"errs": [
],
"all": [
]
},
{
"o": {
"jc": "70000",
"jmin": "x",
"jmax": "",
"s1": " 12 ",
"s2": "1 2",
"s3": "-1",
"s4": "00000000012"
},
"errs": [
"Jc must be a whole number from 0 to 65535.",
"Jmin must be a whole number from 0 to 65535.",
"S2 must be a whole number from 0 to 65535.",
"S3 must be a whole number from 0 to 65535.",
"S4 must be a whole number from 0 to 65535."
],
"all": [
"Jc must be a whole number from 0 to 65535.",
"Jmin must be a whole number from 0 to 65535.",
"S2 must be a whole number from 0 to 65535.",
"S3 must be a whole number from 0 to 65535.",
"S4 must be a whole number from 0 to 65535."
]
},
{
"o": {
"jmin": "60",
"jmax": "50"
},
"errs": [
"Jmin must not be larger than Jmax."
],
"all": [
"Jmin must not be larger than Jmax."
]
},
{
"o": {
"jmin": "60"
},
"errs": [
],
"all": [
]
},
{
"o": {
"h1": "100-200",
"h2": "150-300",
"h3": "5",
"h4": "4294967295"
},
"errs": [
"H1 and H2 overlap: each packet type needs values of its own."
],
"all": [
"H1 and H2 overlap: each packet type needs values of its own."
]
},
{
"o": {
"h1": "1",
"h2": "1"
},
"errs": [
"H1 and H2 overlap: each packet type needs values of its own."
],
"all": [
"H1 and H2 overlap: each packet type needs values of its own."
]
},
{
"o": {
"h1": "3"
},
"errs": [
"H1 and H3 overlap: each packet type needs values of its own."
],
"all": [
"H1 and H3 overlap: each packet type needs values of its own."
]
},
{
"o": {
"h1": "200-100",
"h2": "5-",
"h3": "-5",
"h4": "1-2-3"
},
"errs": [
"H1 must be a number or a range like 100-200, up to 4294967295.",
"H2 must be a number or a range like 100-200, up to 4294967295.",
"H3 must be a number or a range like 100-200, up to 4294967295.",
"H4 must be a number or a range like 100-200, up to 4294967295."
],
"all": [
"H1 must be a number or a range like 100-200, up to 4294967295.",
"H2 must be a number or a range like 100-200, up to 4294967295.",
"H3 must be a number or a range like 100-200, up to 4294967295.",
"H4 must be a number or a range like 100-200, up to 4294967295."
]
},
{
"o": {
"h1": "4294967296",
"h2": " 77 "
},
"errs": [
"H1 must be a number or a range like 100-200, up to 4294967295."
],
"all": [
"H1 must be a number or a range like 100-200, up to 4294967295."
]
},
{
"o": {
"header_protection_key": "yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk=",
"s1": "12",
"s2": "11",
"s3": "64",
"s4": "0"
},
"errs": [
"S2 must be at least 12 with header protection.",
"S4 must be at least 12 with header protection."
],
"all": [
"S2 must be at least 12 with header protection.",
"S4 must be at least 12 with header protection."
]
},
{
"o": {
"header_protection_key": "yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk="
},
"errs": [
"S1 must be at least 12 with header protection.",
"S2 must be at least 12 with header protection.",
"S3 must be at least 12 with header protection.",
"S4 must be at least 12 with header protection."
],
"all": [
"S1 must be at least 12 with header protection.",
"S2 must be at least 12 with header protection.",
"S3 must be at least 12 with header protection.",
"S4 must be at least 12 with header protection."
]
},
{
"o": {
"header_protection_key": "not a key",
"s1": "20",
"s2": "20",
"s3": "20",
"s4": "20"
},
"errs": [
"The header protection key is not a key."
],
"all": [
"The header protection key is not a key."
]
},
{
"o": {
"header_protection_key": "yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBml=",
"s1": "99999"
},
"errs": [
"S1 must be a whole number from 0 to 65535.",
"The header protection key is not a key.",
"S2 must be at least 12 with header protection.",
"S3 must be at least 12 with header protection.",
"S4 must be at least 12 with header protection."
],
"all": [
"S1 must be a whole number from 0 to 65535.",
"The header protection key is not a key.",
"S2 must be at least 12 with header protection.",
"S3 must be at least 12 with header protection.",
"S4 must be at least 12 with header protection."
]
},
{
"o": {
"rekey_after_time": "0",
"rekey_timeout": "0-5",
"reject_after_time": "181",
"keepalive_timeout": "5-15",
"max_handshake_attempts": "many"
},
"errs": [
"RekeyTimeout must be a number or a range like 5-15, from 1 to 65535.",
"RejectAfterTime must be at most 180 seconds.",
"MaxHandshakeAttempts must be a number or a range like 5-15, from 1 to 65535."
],
"all": [
"RekeyTimeout must be a number or a range like 5-15, from 1 to 65535.",
"RejectAfterTime must be at most 180 seconds.",
"MaxHandshakeAttempts must be a number or a range like 5-15, from 1 to 65535."
]
},
{
"o": {
"rekey_after_time": "120",
"rekey_timeout": "5",
"reject_after_time": "100-180",
"keepalive_timeout": "65536",
"max_handshake_attempts": "0-0"
},
"errs": [
"KeepaliveTimeout must be a number or a range like 5-15, from 1 to 65535."
],
"all": [
"KeepaliveTimeout must be a number or a range like 5-15, from 1 to 65535."
]
},
{
"o": {
"reject_after_time": "10-999"
},
"errs": [
"RejectAfterTime must be at most 180 seconds."
],
"all": [
"RejectAfterTime must be at most 180 seconds."
]
},
{
"o": {
"content_padding_addition": "10-100"
},
"errs": [
],
"all": [
]
},
{
"o": {
"content_padding_addition": "100-10"
},
"errs": [
"ContentPaddingAddition must be a number or a range like 10-100, up to 65535."
],
"all": [
"ContentPaddingAddition must be a number or a range like 10-100, up to 65535."
]
},
{
"o": {
"content_padding_addition": "0"
},
"errs": [
],
"all": [
]
},
{
"o": {
"content_padding_addition": "lots"
},
"errs": [
"ContentPaddingAddition must be a number or a range like 10-100, up to 65535."
],
"all": [
"ContentPaddingAddition must be a number or a range like 10-100, up to 65535."
]
},
{
"o": {
"random_trailers": "on",
"disable_cookies": "0"
},
"errs": [
],
"all": [
]
},
{
"o": {
"random_trailers": "yes",
"disable_cookies": "true"
},
"errs": [
"RandomTrailers must be on or off.",
"DisableCookies must be on or off."
],
"all": [
"RandomTrailers must be on or off.",
"DisableCookies must be on or off."
]
},
{
"o": {
"i1": "<b 0x0a0b><r 16><t>",
"i2": "text <rc 8> between <rd 1000>",
"i3": "<t>"
},
"errs": [
],
"all": [
]
},
{
"o": {
"i1": "<b 0xabc>",
"i2": "<b 0x>",
"i3": "<b 0xzz>",
"i4": "<b abcd>",
"i5": "<B 0x00>"
},
"errs": [
"I1: <b 0xabc> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I2: <b 0x> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I3: <b 0xzz> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I4: <b abcd> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I5: <B 0x00> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>."
],
"all": [
"I1: <b 0xabc> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I2: <b 0x> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I3: <b 0xzz> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I4: <b abcd> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I5: <B 0x00> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>."
]
},
{
"o": {
"i1": "<r 0>",
"i2": "<r 1001>",
"i3": "<r -5>",
"i4": "<rc>",
"i5": "<c>"
},
"errs": [
"I1: <r 0> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I2: <r 1001> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I3: <r -5> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I4: <rc> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I5: <c> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>."
],
"all": [
"I1: <r 0> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I2: <r 1001> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I3: <r -5> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I4: <rc> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I5: <c> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>."
]
},
{
"o": {
"i1": "<d>",
"i2": "<ds 4>",
"i3": "<dz 4>",
"i4": "<r 5",
"i5": "no tags at all"
},
"errs": [
"I1: <d> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I2: <ds 4> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I3: <dz 4> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I4 has a tag that is not closed with >."
],
"all": [
"I1: <d> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I2: <ds 4> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I3: <dz 4> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I4 has a tag that is not closed with >."
]
},
{
"o": {
"i1": "<b 0x00> # a comment",
"i2": "<r 5>\n<r 6>",
"i3": "<r 5>\r"
},
"errs": [
"I1 may not contain # or a line break.",
"I2 may not contain # or a line break.",
"I3 may not contain # or a line break."
],
"all": [
"I1 may not contain # or a line break.",
"I2 may not contain # or a line break.",
"I3 may not contain # or a line break."
]
},
{
"o": {
"i1": "<this tag is far longer than twenty-four characters>",
"i2": "<t><t ><r 5>"
},
"errs": [
"I1: <this tag is far longer t…> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I2: <t > is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>."
],
"all": [
"I1: <this tag is far longer t…> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>.",
"I2: <t > is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>."
]
},
{
"o": {
"i1": "<b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899><b 0x0011223344556677889900112233445566778899>",
"i2": "<t>"
},
"errs": [
"I1-I5 together must be at most 2000 characters, not 2073."
],
"all": [
"I1-I5 together must be at most 2000 characters, not 2073."
]
},
{
"o": {
"i1": "<r 1>>",
"i2": "><t>",
"i3": "<<t>"
},
"errs": [
"I3: <<t> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>."
],
"all": [
"I3: <<t> is not a tag AmneziaWG apps accept: use <b 0x…> with an even number of hex digits, <r N>, <rc N> or <rd N> with N from 1 to 1000, or <t>."
]
},
{
"o": {
"jc": "5",
"jmin": "50",
"jmax": "1000",
"s1": "15",
"s2": "20",
"s3": "25",
"s4": "30",
"h1": "1000-2000",
"h2": "3000-4000",
"h3": "5000-6000",
"h4": "7000-8000",
"i1": "<b 0x1234><r 20>",
"header_protection_key": "yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk=",
"content_padding_addition": "5-50",
"rekey_after_time": "100-120",
"rekey_timeout": "4-6",
"reject_after_time": "150-180",
"keepalive_timeout": "8-12",
"max_handshake_attempts": "15-20",
"random_trailers": "off",
"disable_cookies": "1"
},
"errs": [
],
"all": [
]
},
{
"o": {
"s1": "8",
"s2": "64"
},
"errs": [
],
"all": [
"S2 must not be S1 + 56: the handshake initiation and response would have the same size."
]
},
{
"o": {
"s1": "8",
"s2": " 64 "
},
"errs": [
],
"all": [
"S2 must not be S1 + 56: the handshake initiation and response would have the same size."
]
},
{
"o": {
"s1": "0",
"s2": "56"
},
"errs": [
],
"all": [
"S2 must not be S1 + 56: the handshake initiation and response would have the same size."
]
},
{
"o": {
"jc": "11",
"jmin": "1025",
"jmax": "2000",
"s1": "65",
"s2": "70",
"s3": "65",
"s4": "33"
},
"errs": [
],
"all": [
"Jc must be from 0 to 10.",
"Jmin must be from 0 to 1024.",
"Jmax must be from 0 to 1024.",
"S1 must be from 0 to 64.",
"S2 must be from 0 to 64.",
"S3 must be from 0 to 64.",
"S4 must be from 0 to 32."
]
},
{
"o": {
"jc": "10",
"jmin": "1024",
"jmax": "1024",
"s1": "64",
"s2": "64",
"s3": "64",
"s4": "32"
},
"errs": [
],
"all": [
]
},
{
"o": {
"s1": "x",
"s2": "56"
},
"errs": [
"S1 must be a whole number from 0 to 65535."
],
"all": [
"S1 must be a whole number from 0 to 65535."
]
},
{
"o": {
"s2": "56"
},
"errs": [
],
"all": [
]
}
]
}
+281
View File
@@ -0,0 +1,281 @@
// Package endpoint tells what a public address points at and whether that reaches this router, as
// dawnline.health does for Health and for the "as you type" check on a server's page: the facts
// (endpoint_facts) and the facts in words in the reader's language (endpoint_words). Held to ucode's own
// output on made-up reads (testdata/trace.uc, scripts/ucode-golden.sh).
package endpoint
import (
"fmt"
"regexp"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
)
// Reads is what the facts are made from, read on the router: the name's addresses, the WAN's IPv4 ("" none)
// and the router's public IPv6 addresses (Known6 false: not read, or `ip` could not tell).
type Reads struct {
IPs []string
WAN string
Router6 []string
Known6 bool
}
// Facts is what a public address points at.
type Facts struct {
Host string `json:"host"`
IPs []string `json:"ips"`
IPs4 []string `json:"ips4"`
IPs6 []string `json:"ips6"`
WAN *string `json:"wan"`
Kind string `json:"kind"`
Cloudflare bool `json:"cloudflare"`
Here bool `json:"here"`
V6Only bool `json:"v6_only"`
Router6 []string `json:"router6"`
Foreign6 []string `json:"foreign6"`
Here6 bool `json:"here6"`
}
// Cloudflare's published ranges (https://www.cloudflare.com/ips/).
var (
cf4 = []string{"173.245.48.0/20", "103.21.244.0/22", "103.22.200.0/22", "103.31.4.0/22", "141.101.64.0/18", "108.162.192.0/18", "190.93.240.0/20",
"188.114.96.0/20", "197.234.240.0/22", "198.41.128.0/17", "162.158.0.0/15", "104.16.0.0/13", "104.24.0.0/14", "172.64.0.0/13", "131.0.72.0/22"}
cf6 = regexp.MustCompile(`(?i)^(2400:cb00:|2606:4700:|2803:f800:|2405:b500:|2405:8100:|2a06:98c[0-7]:|2c0f:f248:)`)
)
// Cloudflare tells an address in Cloudflare's network (its proxies and edge).
func Cloudflare(a string) bool {
n, ok := ipnet.IP(a)
if !ok {
return cf6.MatchString(a)
}
for _, r := range cf4 {
if c, _ := ipnet.Any(r); c.Holds(n) {
return true
}
}
return false
}
// Kind is what kind of IPv4 the WAN has: public, cgnat (100.64/10), private (behind another router), none.
func Kind(wan string) string {
n, ok := ipnet.IP(wan)
if !ok {
return "none"
}
for i, p := range []string{"100.64.0.0/10", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"} {
if r, _ := ipnet.Any(p); r.Holds(n) {
if i == 0 {
return "cgnat"
}
return "private"
}
}
return "public"
}
// Full6 is an IPv6 address as its 32 hex digits, so two spellings of one address compare equal; "" when it is none.
func Full6(s string) string {
halves := strings.Split(strings.ToLower(s), "::")
if len(halves) > 2 {
return ""
}
var head, tail []string
if halves[0] != "" {
head = strings.Split(halves[0], ":")
}
if len(halves) == 2 && halves[1] != "" {
tail = strings.Split(halves[1], ":")
}
gap := 8 - len(head) - len(tail)
if (len(halves) == 2 && gap < 1) || (len(halves) != 2 && gap != 0) {
return ""
}
groups := append([]string{}, head...)
for i := 0; i < gap; i++ {
groups = append(groups, "0")
}
groups = append(groups, tail...)
out := ""
for _, g := range groups {
if len(g) < 1 || len(g) > 4 || strings.Trim(g, "0123456789abcdef") != "" {
return ""
}
out += "0000"[len(g):] + g
}
return out
}
// Own6 is the router's public IPv6 addresses among those `ip -6 -j addr show scope global` lists: unique
// local ones (fc00::/7) and failed duplicates do not count, nothing reaches them from outside.
func Own6(links []struct {
Info []struct {
Family string `json:"family"`
Local string `json:"local"`
DADFailed bool `json:"dadfailed"`
} `json:"addr_info"`
}) []string {
out := []string{}
for _, l := range links {
for _, a := range l.Info {
if k := Full6(a.Local); k != "" && a.Family == "inet6" && !a.DADFailed && !strings.HasPrefix(k, "fc") && !strings.HasPrefix(k, "fd") {
out = append(out, a.Local)
}
}
}
return out
}
// Of makes the facts of what was read.
func Of(host string, r Reads) Facts {
f := Facts{Host: host, IPs: r.IPs, IPs4: []string{}, IPs6: []string{}, Kind: Kind(r.WAN)}
if f.IPs == nil {
f.IPs = []string{}
}
if r.WAN != "" {
f.WAN = &r.WAN
}
for _, a := range f.IPs {
if _, v4 := ipnet.IP(a); v4 {
f.IPs4 = append(f.IPs4, a)
f.Here = f.Here || (r.WAN != "" && a == r.WAN)
} else {
f.IPs6 = append(f.IPs6, a)
}
f.Cloudflare = f.Cloudflare || Cloudflare(a)
}
f.V6Only = len(f.IPs4) == 0 && len(f.IPs6) > 0
if r.Known6 {
f.Router6 = r.Router6
if f.Router6 == nil {
f.Router6 = []string{}
}
own := map[string]bool{}
for _, a := range r.Router6 {
own[Full6(a)] = true
}
f.Foreign6 = []string{}
for _, a := range f.IPs6 {
if !own[Full6(a)] {
f.Foreign6 = append(f.Foreign6, a)
}
}
f.Here6 = len(f.IPs6) > 0 && len(f.Foreign6) == 0
}
return f
}
// aaaa is the name's IPv6 addresses that do not reach this router from outside, in words, or "".
func aaaa(f Facts, t *i18n.Catalog) string {
n := int64(len(f.Foreign6))
if n == 0 {
return ""
}
bad := strings.Join(f.Foreign6, ", ")
if len(f.Router6) == 0 {
return fmt.Sprintf(t.N(n, "%s AAAA → %s, but this router has no public IPv6 address: devices that prefer IPv6 will fail to connect. Remove this AAAA record or set up IPv6 on the router.",
"%s AAAA → %s, but this router has no public IPv6 address: devices that prefer IPv6 will fail to connect. Remove these AAAA records or set up IPv6 on the router."), f.Host, bad)
}
return fmt.Sprintf(t.N(n, "%s AAAA → %s is not a public address of this router: devices that prefer IPv6 will fail to connect. Fix or remove this AAAA record.",
"%s AAAA → %s are not public addresses of this router: devices that prefer IPv6 will fail to connect. Fix or remove these AAAA records."), f.Host, bad)
}
// unreached6 is an IPv6-only name none of whose addresses is a public one of the router, in words, or "".
func unreached6(f Facts, t *i18n.Catalog) string {
n := len(f.Foreign6)
if n == 0 || n < len(f.IPs6) {
return ""
}
all := strings.Join(f.IPs6, ", ")
if len(f.Router6) == 0 {
return fmt.Sprintf(t.T("%s → %s: IPv6 only, but this router has no public IPv6 address: no device can connect. Check your DNS / DDNS, or set up IPv6 on the router."), f.Host, all)
}
return fmt.Sprintf(t.N(int64(n), "%s → %s: IPv6 only, and not a public address of this router: no device can connect. Check your DNS / DDNS.",
"%s → %s: IPv6 only, and not public addresses of this router: no device can connect. Check your DNS / DDNS."), f.Host, all)
}
// Words is the facts as a status (ok, warn, error) and a whole sentence in t's language. note names what
// must get through ("UDP 4500").
func Words(f Facts, note string, t *i18n.Catalog) (status, text string) {
wan := ""
if f.WAN != nil {
wan = *f.WAN
}
if f.Host == "" {
return "error", t.T("No public address: devices will not know where to connect.")
}
if len(f.IPs) == 0 {
return "error", fmt.Sprintf(t.T("%s does not resolve."), f.Host)
}
if f.Cloudflare {
return "error", fmt.Sprintf(t.T("%s → %s (Cloudflare). %s reaches the router only if this name is DNS-only there, or use the router's IP."), f.Host, strings.Join(f.IPs, ", "), note)
}
v6 := aaaa(f, t)
if f.V6Only {
if none := unreached6(f, t); none != "" {
return "warn", none
}
if v6 != "" {
return "warn", v6
}
if f.Kind == "public" {
return "warn", fmt.Sprintf(t.T("%s → %s: IPv6 only, so only devices with IPv6 connect (the router's IPv4 %s has no name)."), f.Host, strings.Join(f.IPs6, ", "), wan)
}
return "ok", fmt.Sprintf(t.T("%s → %s: IPv6 only, so only devices with IPv6 connect."), f.Host, strings.Join(f.IPs6, ", "))
}
v4 := ""
switch {
case f.Kind == "cgnat":
v4 = fmt.Sprintf(t.T("The WAN address %s is CGNAT (shared by the ISP): %s cannot be forwarded to this router from outside unless the ISP gives a public address or IPv6."), wan, note)
case f.Kind == "private":
v4 = fmt.Sprintf(t.T("The WAN address %s is private: this router is behind another one. Forward %s on it, or devices cannot connect."), wan, note)
case wan != "" && !f.Here:
v4 = fmt.Sprintf(t.T("%s points at %s, not this router (WAN %s). Check your DNS / DDNS."), f.Host, strings.Join(f.IPs4, ", "), wan)
}
// two separate facts, each a sentence of its own: the IPv4 one first
switch {
case v4 != "" && v6 != "":
return "warn", v4 + " " + v6
case v4 != "" || v6 != "":
return "warn", v4 + v6
}
// without the router's WAN address only the facts: names and addresses, no words
if f.Here {
at := []string{wan}
if f.Here6 {
at = append(at, f.IPs6...)
}
return "ok", fmt.Sprintf(t.T("%s points at this router (%s)."), f.Host, strings.Join(at, ", "))
}
return "ok", f.Host + " → " + strings.Join(f.IPs, ", ")
}
var label = regexp.MustCompile(`^[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?$`)
var v6 = regexp.MustCompile(`^([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}$`)
// ValidHost tells a host name or an IP address, as a public address or the portal's name may be given.
func ValidHost(s string) bool {
if len(s) > 253 {
return false
}
if _, ok := ipnet.IP(s); ok || (v6.MatchString(s) && !strings.Contains(s, ":::")) {
return true
}
for _, l := range strings.Split(s, ".") {
if len(l) > 63 || !label.MatchString(l) {
return false
}
}
return true
}
// Covers tells a certificate name that covers host: the same name, or a wildcard one level up.
func Covers(name, host string) bool {
if name == host {
return true
}
dot := strings.Index(host, ".")
return strings.HasPrefix(name, "*.") && dot > 0 && host[dot:] == name[1:]
}
+70
View File
@@ -0,0 +1,70 @@
package endpoint
import (
"encoding/json"
"os"
"testing"
)
// The facts and their words for made-up reads, as ucode makes them (testdata/trace.uc, scripts/ucode-golden.sh).
func TestAsUcode(t *testing.T) {
raw, err := os.ReadFile("testdata/trace.json")
if err != nil {
t.Fatal(err)
}
var trace struct {
Rows []struct {
Host string `json:"host"`
Reads struct {
IPs []string `json:"ips"`
WAN *string `json:"wan"`
Router6 *[]string `json:"router6"`
} `json:"reads"`
Facts json.RawMessage `json:"facts"`
Words struct {
Status string `json:"status"`
Text string `json:"text"`
} `json:"words"`
} `json:"rows"`
Hosts map[string]bool `json:"hosts"`
}
if err := json.Unmarshal(raw, &trace); err != nil {
t.Fatal(err)
}
if len(trace.Rows) < 25 || len(trace.Hosts) < 12 {
t.Fatalf("too little: %d rows, %d hosts", len(trace.Rows), len(trace.Hosts))
}
statuses := map[string]int{}
for i, row := range trace.Rows {
r := Reads{IPs: row.Reads.IPs}
if row.Reads.WAN != nil {
r.WAN = *row.Reads.WAN
}
if row.Reads.Router6 != nil {
r.Router6, r.Known6 = *row.Reads.Router6, true
}
f := Of(row.Host, r)
got, _ := json.Marshal(f)
var a, b any
json.Unmarshal(got, &a)
json.Unmarshal(row.Facts, &b)
ga, _ := json.Marshal(a)
gb, _ := json.Marshal(b)
if string(ga) != string(gb) {
t.Errorf("row %d facts\n go: %s\n ucode: %s", i, ga, gb)
}
status, text := Words(f, "UDP 4500", nil)
statuses[status]++
if status != row.Words.Status || text != row.Words.Text {
t.Errorf("row %d words\n go: %s %s\n ucode: %s %s", i, status, text, row.Words.Status, row.Words.Text)
}
}
if statuses["ok"] < 3 || statuses["warn"] < 8 || statuses["error"] < 3 {
t.Fatalf("the rows exercise too little: %v", statuses)
}
for h, want := range trace.Hosts {
if got := ValidHost(h); got != want {
t.Errorf("ValidHost(%q) = %v, ucode %v", h, got, want)
}
}
}
+985
View File
@@ -0,0 +1,985 @@
{
"rows": [
{
"host": "",
"reads": {
"ips": [
],
"wan": "203.0.113.7"
},
"facts": {
"host": "",
"ips": [
],
"ips4": [
],
"ips6": [
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": false,
"v6_only": false,
"router6": null,
"foreign6": null,
"here6": false
},
"words": {
"status": "error",
"text": "No public address: devices will not know where to connect."
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
],
"wan": "203.0.113.7"
},
"facts": {
"host": "vpn.example.org",
"ips": [
],
"ips4": [
],
"ips6": [
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": false,
"v6_only": false,
"router6": null,
"foreign6": null,
"here6": false
},
"words": {
"status": "error",
"text": "vpn.example.org does not resolve."
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
"203.0.113.7"
],
"wan": "203.0.113.7"
},
"facts": {
"host": "vpn.example.org",
"ips": [
"203.0.113.7"
],
"ips4": [
"203.0.113.7"
],
"ips6": [
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": true,
"v6_only": false,
"router6": null,
"foreign6": null,
"here6": false
},
"words": {
"status": "ok",
"text": "vpn.example.org points at this router (203.0.113.7)."
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
"198.51.100.4"
],
"wan": "203.0.113.7"
},
"facts": {
"host": "vpn.example.org",
"ips": [
"198.51.100.4"
],
"ips4": [
"198.51.100.4"
],
"ips6": [
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": false,
"v6_only": false,
"router6": null,
"foreign6": null,
"here6": false
},
"words": {
"status": "warn",
"text": "vpn.example.org points at 198.51.100.4, not this router (WAN 203.0.113.7). Check your DNS / DDNS."
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
"198.51.100.4",
"198.51.100.5"
],
"wan": null
},
"facts": {
"host": "vpn.example.org",
"ips": [
"198.51.100.4",
"198.51.100.5"
],
"ips4": [
"198.51.100.4",
"198.51.100.5"
],
"ips6": [
],
"wan": null,
"kind": "none",
"cloudflare": false,
"here": false,
"v6_only": false,
"router6": null,
"foreign6": null,
"here6": false
},
"words": {
"status": "ok",
"text": "vpn.example.org → 198.51.100.4, 198.51.100.5"
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
"104.16.1.1"
],
"wan": "203.0.113.7"
},
"facts": {
"host": "vpn.example.org",
"ips": [
"104.16.1.1"
],
"ips4": [
"104.16.1.1"
],
"ips6": [
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": true,
"here": false,
"v6_only": false,
"router6": null,
"foreign6": null,
"here6": false
},
"words": {
"status": "error",
"text": "vpn.example.org → 104.16.1.1 (Cloudflare). UDP 4500 reaches the router only if this name is DNS-only there, or use the router's IP."
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
"2606:4700::1"
],
"wan": "203.0.113.7",
"router6": [
]
},
"facts": {
"host": "vpn.example.org",
"ips": [
"2606:4700::1"
],
"ips4": [
],
"ips6": [
"2606:4700::1"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": true,
"here": false,
"v6_only": true,
"router6": [
],
"foreign6": [
"2606:4700::1"
],
"here6": false
},
"words": {
"status": "error",
"text": "vpn.example.org → 2606:4700::1 (Cloudflare). UDP 4500 reaches the router only if this name is DNS-only there, or use the router's IP."
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
"203.0.113.7"
],
"wan": "100.70.1.2"
},
"facts": {
"host": "vpn.example.org",
"ips": [
"203.0.113.7"
],
"ips4": [
"203.0.113.7"
],
"ips6": [
],
"wan": "100.70.1.2",
"kind": "cgnat",
"cloudflare": false,
"here": false,
"v6_only": false,
"router6": null,
"foreign6": null,
"here6": false
},
"words": {
"status": "warn",
"text": "The WAN address 100.70.1.2 is CGNAT (shared by the ISP): UDP 4500 cannot be forwarded to this router from outside unless the ISP gives a public address or IPv6."
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
"203.0.113.7"
],
"wan": "192.168.1.2"
},
"facts": {
"host": "vpn.example.org",
"ips": [
"203.0.113.7"
],
"ips4": [
"203.0.113.7"
],
"ips6": [
],
"wan": "192.168.1.2",
"kind": "private",
"cloudflare": false,
"here": false,
"v6_only": false,
"router6": null,
"foreign6": null,
"here6": false
},
"words": {
"status": "warn",
"text": "The WAN address 192.168.1.2 is private: this router is behind another one. Forward UDP 4500 on it, or devices cannot connect."
}
},
{
"host": "203.0.113.7",
"reads": {
"ips": [
"203.0.113.7"
],
"wan": "10.0.0.2"
},
"facts": {
"host": "203.0.113.7",
"ips": [
"203.0.113.7"
],
"ips4": [
"203.0.113.7"
],
"ips6": [
],
"wan": "10.0.0.2",
"kind": "private",
"cloudflare": false,
"here": false,
"v6_only": false,
"router6": null,
"foreign6": null,
"here6": false
},
"words": {
"status": "warn",
"text": "The WAN address 10.0.0.2 is private: this router is behind another one. Forward UDP 4500 on it, or devices cannot connect."
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
"203.0.113.7",
"2001:db8::7"
],
"wan": "203.0.113.7",
"router6": [
"2001:DB8:0::7"
]
},
"facts": {
"host": "vpn.example.org",
"ips": [
"203.0.113.7",
"2001:db8::7"
],
"ips4": [
"203.0.113.7"
],
"ips6": [
"2001:db8::7"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": true,
"v6_only": false,
"router6": [
"2001:DB8:0::7"
],
"foreign6": [
],
"here6": true
},
"words": {
"status": "ok",
"text": "vpn.example.org points at this router (203.0.113.7, 2001:db8::7)."
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
"203.0.113.7",
"2001:db8::7"
],
"wan": "203.0.113.7",
"router6": [
]
},
"facts": {
"host": "vpn.example.org",
"ips": [
"203.0.113.7",
"2001:db8::7"
],
"ips4": [
"203.0.113.7"
],
"ips6": [
"2001:db8::7"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": true,
"v6_only": false,
"router6": [
],
"foreign6": [
"2001:db8::7"
],
"here6": false
},
"words": {
"status": "warn",
"text": "vpn.example.org AAAA → 2001:db8::7, but this router has no public IPv6 address: devices that prefer IPv6 will fail to connect. Remove this AAAA record or set up IPv6 on the router."
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
"203.0.113.7",
"2001:db8::7",
"2001:db8::8"
],
"wan": "203.0.113.7",
"router6": [
]
},
"facts": {
"host": "vpn.example.org",
"ips": [
"203.0.113.7",
"2001:db8::7",
"2001:db8::8"
],
"ips4": [
"203.0.113.7"
],
"ips6": [
"2001:db8::7",
"2001:db8::8"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": true,
"v6_only": false,
"router6": [
],
"foreign6": [
"2001:db8::7",
"2001:db8::8"
],
"here6": false
},
"words": {
"status": "warn",
"text": "vpn.example.org AAAA → 2001:db8::7, 2001:db8::8, but this router has no public IPv6 address: devices that prefer IPv6 will fail to connect. Remove these AAAA records or set up IPv6 on the router."
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
"203.0.113.7",
"2001:db8::7"
],
"wan": "203.0.113.7",
"router6": [
"2001:db8::99"
]
},
"facts": {
"host": "vpn.example.org",
"ips": [
"203.0.113.7",
"2001:db8::7"
],
"ips4": [
"203.0.113.7"
],
"ips6": [
"2001:db8::7"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": true,
"v6_only": false,
"router6": [
"2001:db8::99"
],
"foreign6": [
"2001:db8::7"
],
"here6": false
},
"words": {
"status": "warn",
"text": "vpn.example.org AAAA → 2001:db8::7 is not a public address of this router: devices that prefer IPv6 will fail to connect. Fix or remove this AAAA record."
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
"203.0.113.7",
"2001:db8::7",
"2001:db8::8"
],
"wan": "203.0.113.7",
"router6": [
"2001:db8::99",
"2001:db8::7"
]
},
"facts": {
"host": "vpn.example.org",
"ips": [
"203.0.113.7",
"2001:db8::7",
"2001:db8::8"
],
"ips4": [
"203.0.113.7"
],
"ips6": [
"2001:db8::7",
"2001:db8::8"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": true,
"v6_only": false,
"router6": [
"2001:db8::99",
"2001:db8::7"
],
"foreign6": [
"2001:db8::8"
],
"here6": false
},
"words": {
"status": "warn",
"text": "vpn.example.org AAAA → 2001:db8::8 is not a public address of this router: devices that prefer IPv6 will fail to connect. Fix or remove this AAAA record."
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
"203.0.113.7",
"2001:db8::7"
],
"wan": "203.0.113.7",
"router6": null
},
"facts": {
"host": "vpn.example.org",
"ips": [
"203.0.113.7",
"2001:db8::7"
],
"ips4": [
"203.0.113.7"
],
"ips6": [
"2001:db8::7"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": true,
"v6_only": false,
"router6": null,
"foreign6": null,
"here6": false
},
"words": {
"status": "ok",
"text": "vpn.example.org points at this router (203.0.113.7)."
}
},
{
"host": "vpn.example.org",
"reads": {
"ips": [
"198.51.100.4",
"2001:db8::7"
],
"wan": "203.0.113.7",
"router6": [
"2001:db8::99"
]
},
"facts": {
"host": "vpn.example.org",
"ips": [
"198.51.100.4",
"2001:db8::7"
],
"ips4": [
"198.51.100.4"
],
"ips6": [
"2001:db8::7"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": false,
"v6_only": false,
"router6": [
"2001:db8::99"
],
"foreign6": [
"2001:db8::7"
],
"here6": false
},
"words": {
"status": "warn",
"text": "vpn.example.org points at 198.51.100.4, not this router (WAN 203.0.113.7). Check your DNS / DDNS. vpn.example.org AAAA → 2001:db8::7 is not a public address of this router: devices that prefer IPv6 will fail to connect. Fix or remove this AAAA record."
}
},
{
"host": "six.example.org",
"reads": {
"ips": [
"2001:db8::7"
],
"wan": "203.0.113.7",
"router6": [
"2001:db8::7"
]
},
"facts": {
"host": "six.example.org",
"ips": [
"2001:db8::7"
],
"ips4": [
],
"ips6": [
"2001:db8::7"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": false,
"v6_only": true,
"router6": [
"2001:db8::7"
],
"foreign6": [
],
"here6": true
},
"words": {
"status": "warn",
"text": "six.example.org → 2001:db8::7: IPv6 only, so only devices with IPv6 connect (the router's IPv4 203.0.113.7 has no name)."
}
},
{
"host": "six.example.org",
"reads": {
"ips": [
"2001:db8::7"
],
"wan": null,
"router6": [
"2001:db8::7"
]
},
"facts": {
"host": "six.example.org",
"ips": [
"2001:db8::7"
],
"ips4": [
],
"ips6": [
"2001:db8::7"
],
"wan": null,
"kind": "none",
"cloudflare": false,
"here": false,
"v6_only": true,
"router6": [
"2001:db8::7"
],
"foreign6": [
],
"here6": true
},
"words": {
"status": "ok",
"text": "six.example.org → 2001:db8::7: IPv6 only, so only devices with IPv6 connect."
}
},
{
"host": "six.example.org",
"reads": {
"ips": [
"2001:db8::7"
],
"wan": "192.168.1.2",
"router6": [
"2001:db8::7"
]
},
"facts": {
"host": "six.example.org",
"ips": [
"2001:db8::7"
],
"ips4": [
],
"ips6": [
"2001:db8::7"
],
"wan": "192.168.1.2",
"kind": "private",
"cloudflare": false,
"here": false,
"v6_only": true,
"router6": [
"2001:db8::7"
],
"foreign6": [
],
"here6": true
},
"words": {
"status": "ok",
"text": "six.example.org → 2001:db8::7: IPv6 only, so only devices with IPv6 connect."
}
},
{
"host": "six.example.org",
"reads": {
"ips": [
"2001:db8::7"
],
"wan": "203.0.113.7",
"router6": [
]
},
"facts": {
"host": "six.example.org",
"ips": [
"2001:db8::7"
],
"ips4": [
],
"ips6": [
"2001:db8::7"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": false,
"v6_only": true,
"router6": [
],
"foreign6": [
"2001:db8::7"
],
"here6": false
},
"words": {
"status": "warn",
"text": "six.example.org → 2001:db8::7: IPv6 only, but this router has no public IPv6 address: no device can connect. Check your DNS / DDNS, or set up IPv6 on the router."
}
},
{
"host": "six.example.org",
"reads": {
"ips": [
"2001:db8::7",
"2001:db8::8"
],
"wan": "203.0.113.7",
"router6": [
"2001:db8::99"
]
},
"facts": {
"host": "six.example.org",
"ips": [
"2001:db8::7",
"2001:db8::8"
],
"ips4": [
],
"ips6": [
"2001:db8::7",
"2001:db8::8"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": false,
"v6_only": true,
"router6": [
"2001:db8::99"
],
"foreign6": [
"2001:db8::7",
"2001:db8::8"
],
"here6": false
},
"words": {
"status": "warn",
"text": "six.example.org → 2001:db8::7, 2001:db8::8: IPv6 only, and not public addresses of this router: no device can connect. Check your DNS / DDNS."
}
},
{
"host": "six.example.org",
"reads": {
"ips": [
"2001:db8::7"
],
"wan": "203.0.113.7",
"router6": [
"2001:db8::99"
]
},
"facts": {
"host": "six.example.org",
"ips": [
"2001:db8::7"
],
"ips4": [
],
"ips6": [
"2001:db8::7"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": false,
"v6_only": true,
"router6": [
"2001:db8::99"
],
"foreign6": [
"2001:db8::7"
],
"here6": false
},
"words": {
"status": "warn",
"text": "six.example.org → 2001:db8::7: IPv6 only, and not a public address of this router: no device can connect. Check your DNS / DDNS."
}
},
{
"host": "six.example.org",
"reads": {
"ips": [
"2001:db8::7",
"2001:db8::8"
],
"wan": "203.0.113.7",
"router6": [
"2001:db8::8"
]
},
"facts": {
"host": "six.example.org",
"ips": [
"2001:db8::7",
"2001:db8::8"
],
"ips4": [
],
"ips6": [
"2001:db8::7",
"2001:db8::8"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": false,
"v6_only": true,
"router6": [
"2001:db8::8"
],
"foreign6": [
"2001:db8::7"
],
"here6": false
},
"words": {
"status": "warn",
"text": "six.example.org AAAA → 2001:db8::7 is not a public address of this router: devices that prefer IPv6 will fail to connect. Fix or remove this AAAA record."
}
},
{
"host": "six.example.org",
"reads": {
"ips": [
"2001:db8::7"
],
"wan": "203.0.113.7",
"router6": null
},
"facts": {
"host": "six.example.org",
"ips": [
"2001:db8::7"
],
"ips4": [
],
"ips6": [
"2001:db8::7"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": false,
"v6_only": true,
"router6": null,
"foreign6": null,
"here6": false
},
"words": {
"status": "warn",
"text": "six.example.org → 2001:db8::7: IPv6 only, so only devices with IPv6 connect (the router's IPv4 203.0.113.7 has no name)."
}
},
{
"host": "odd.example.org",
"reads": {
"ips": [
"not:an:address",
"2001:db8::1::2"
],
"wan": "203.0.113.7",
"router6": [
"2001:db8::7"
]
},
"facts": {
"host": "odd.example.org",
"ips": [
"not:an:address",
"2001:db8::1::2"
],
"ips4": [
],
"ips6": [
"not:an:address",
"2001:db8::1::2"
],
"wan": "203.0.113.7",
"kind": "public",
"cloudflare": false,
"here": false,
"v6_only": true,
"router6": [
"2001:db8::7"
],
"foreign6": [
"not:an:address",
"2001:db8::1::2"
],
"here6": false
},
"words": {
"status": "warn",
"text": "odd.example.org → not:an:address, 2001:db8::1::2: IPv6 only, and not public addresses of this router: no device can connect. Check your DNS / DDNS."
}
}
],
"hosts": {
"vpn.example.org": true,
"a": true,
"a-b.c": true,
"-a.example": false,
"a-.example": false,
"a..b": false,
"a_b.example": false,
"203.0.113.7": true,
"2001:db8::1": true,
":::1": false,
"exa mple": false,
"": false,
"xn--80a.example": true,
"a.": false
}
}
+71
View File
@@ -0,0 +1,71 @@
// Package files is how the backend shares state on disk with the ucode that still runs: the same locks
// (flock on <run>/<name>.lock, dawnline.util lock()) and the same way of writing (a temporary file beside
// the real one, then a rename, so a reader never sees half a file and a full flash leaves the old one).
package files
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"os"
"path/filepath"
"syscall"
)
// Lock takes the named lock of a state directory and returns its release. It waits for whoever holds it
// (the ucode holds these for the length of one read-change-write). A lock that cannot be taken at all (the
// directory is gone) is not an error to stop for: the release then does nothing.
func Lock(dir, name string) (unlock func()) {
os.MkdirAll(dir, 0o755)
f, err := os.OpenFile(filepath.Join(dir, name+".lock"), os.O_WRONLY|os.O_CREATE, 0o644)
if err != nil {
return func() {}
}
if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
f.Close()
return func() {}
}
return func() {
syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
f.Close()
}
}
// Write puts data at path all at once: written beside it and renamed over it.
func Write(path string, data []byte, mode os.FileMode) error {
var r [3]byte
rand.Read(r[:])
tmp := path + ".tmp" + hex.EncodeToString(r[:])
if err := os.WriteFile(tmp, data, mode); err != nil {
os.Remove(tmp)
return err
}
// WriteFile's mode is cut by the umask: the mode asked for is the one a reader finds.
if err := os.Chmod(tmp, mode); err != nil {
os.Remove(tmp)
return err
}
if err := os.Rename(tmp, path); err != nil {
os.Remove(tmp)
return err
}
return nil
}
// WriteJSON writes a value as JSON, all at once.
func WriteJSON(path string, v any, mode os.FileMode) error {
data, err := json.Marshal(v)
if err != nil {
return err
}
return Write(path, data, mode)
}
// ReadJSON reads a file into v; false when it is not there, empty or not JSON (v is then as it was).
func ReadJSON(path string, v any) bool {
data, err := os.ReadFile(path)
if err != nil || len(data) == 0 {
return false
}
return json.Unmarshal(data, v) == nil
}
+341
View File
@@ -0,0 +1,341 @@
// Package geo is the city-level places for the admin's map (dawnline.geo). Client addresses are looked up
// once with a third-party service (ipwho.is, ipapi.co as the other) and kept: the live copy in the run
// directory (tmpfs), put on flash about hourly, like the traffic counters. The pages read the cache.
package geo
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
)
const (
ttl = 30 * 86400
retry = 86400
// limit is the entries kept at least; addresses still shown are never pushed out.
limit = 500
)
// Place is what is kept of an address, as the cache's file has it (lat, lon, city, country, cc, asn, org,
// ts; or fail alone: the moment a lookup found nothing).
type Place = map[string]any
// Cache is the file: the places by address, and the router's own.
type Cache struct {
IPs map[string]Place `json:"ips"`
Home Place `json:"home"`
}
// Store is where the cache lives.
type Store struct {
Run, Data string
}
func (s Store) Load() *Cache {
c := &Cache{}
if !files.ReadJSON(s.Run+"/geo.json", c) {
*c = Cache{}
files.ReadJSON(s.Data+"/geo.json", c)
}
if c.IPs == nil {
c.IPs = map[string]Place{}
}
return c
}
// Save writes the live copy; Flush puts it on flash.
func (s Store) Save(c *Cache) {
os.MkdirAll(s.Run, 0o755)
files.WriteJSON(s.Run+"/geo.json", c, 0o600)
}
// Flush copies the live cache to flash when it is newer; whether it did.
func (s Store) Flush() bool {
live, err := os.Stat(s.Run + "/geo.json")
if err != nil {
return false
}
if kept, err := os.Stat(s.Data + "/geo.json"); err == nil && !live.ModTime().After(kept.ModTime()) {
return false
}
var c Cache
if !files.ReadJSON(s.Run+"/geo.json", &c) {
return false
}
os.MkdirAll(s.Data, 0o700)
return files.WriteJSON(s.Data+"/geo.json", c, 0o600) == nil
}
var private4 = []ipnet.Net{}
func init() {
for _, n := range []string{"0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", "192.168.0.0/16", "198.18.0.0/15", "224.0.0.0/3"} {
if c, ok := ipnet.Any(n); ok {
private4 = append(private4, c)
}
}
}
var (
v6 = regexp.MustCompile(`^[0-9a-f:]+$`)
localV6 = regexp.MustCompile(`^(fe[89ab]|f[cd]|::1$|::$)`)
)
// IsPublic tells an address a location service can say something about.
func IsPublic(ip string) bool {
if ip == "" {
return false
}
if n, ok := ipnet.IP(ip); ok {
for _, c := range private4 {
if c.Holds(n) {
return false
}
}
return true
}
l := strings.ToLower(ip)
return v6.MatchString(l) && !localV6.MatchString(l)
}
func num(v any) (float64, bool) {
switch x := v.(type) {
case float64:
return x, true
case string:
f, err := strconv.ParseFloat(x, 64)
return f, err == nil
}
return 0, false
}
func str(v any) string {
s, _ := v.(string)
return s
}
// Stale is the public addresses of ips the cache does not know or knows from too long ago (a failed lookup
// is tried again after a day), once each and in order.
func Stale(c *Cache, ips []string, now int64) []string {
out := []string{}
seen := map[string]bool{}
for _, ip := range ips {
if seen[ip] || !IsPublic(ip) {
continue
}
seen[ip] = true
if p := c.IPs[ip]; p != nil {
if fail, failed := num(p["fail"]); failed && fail != 0 {
if float64(now)-fail < retry {
continue
}
} else if ts, _ := num(p["ts"]); p["asn"] != nil && float64(now)-ts < ttl {
continue
}
}
out = append(out, ip)
}
return out
}
// Fetch gets a URL's JSON (nil: not there, or not JSON).
type Fetch func(url string) map[string]any
// HTTP is Fetch over the network, five seconds at most.
func HTTP(url string) map[string]any {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil
}
res, err := (&http.Client{Timeout: 5 * time.Second}).Do(req)
if err != nil {
return nil
}
defer res.Body.Close()
body, err := io.ReadAll(io.LimitReader(res.Body, 1<<20))
if err != nil || res.StatusCode != 200 {
return nil
}
var out map[string]any
if json.Unmarshal(body, &out) != nil {
return nil
}
return out
}
func ipwho(get Fetch, ip string) Place {
j := get("https://ipwho.is/" + ip + "?fields=success,ip,latitude,longitude,city,country,country_code,connection")
if j == nil || j["success"] != true || j["latitude"] == nil {
return nil
}
lat, _ := num(j["latitude"])
lon, _ := num(j["longitude"])
cn, _ := j["connection"].(map[string]any)
asn, _ := num(cn["asn"])
org := str(cn["isp"])
if org == "" {
org = str(cn["org"])
}
return Place{"lat": lat, "lon": lon, "city": str(j["city"]), "country": str(j["country"]), "cc": str(j["country_code"]), "ip": j["ip"], "asn": asn, "org": org}
}
var asPrefix = regexp.MustCompile(`(?i)^AS`)
func ipapi(get Fetch, ip string) Place {
url := "https://ipapi.co/json/"
if ip != "" {
url = "https://ipapi.co/" + ip + "/json/"
}
j := get(url)
if j == nil || (j["error"] != nil && j["error"] != false) || j["latitude"] == nil {
return nil
}
lat, _ := num(j["latitude"])
lon, _ := num(j["longitude"])
asn, _ := strconv.ParseFloat(asPrefix.ReplaceAllString(fmt.Sprint(j["asn"]), ""), 64)
return Place{"lat": lat, "lon": lon, "city": str(j["city"]), "country": str(j["country_name"]), "cc": str(j["country_code"]), "ip": j["ip"], "asn": asn, "org": str(j["org"])}
}
// Lookup is one lookup with the chosen provider and the other as fallback (ip "": whoever asks).
func Lookup(get Fetch, provider, ip string) Place {
order := []func(Fetch, string) Place{ipwho, ipapi}
if provider == "ipapi.co" {
order = []func(Fetch, string) Place{ipapi, ipwho}
}
for _, fn := range order {
if p := fn(get, ip); p != nil {
return p
}
}
return nil
}
// LookupAll looks up at most max of ips, within budget seconds (each lookup may wait on the network): a
// place, or nil where the services had nothing, for the ones tried. Network only, no cache.
func LookupAll(get Fetch, provider string, ips []string, max int, budget time.Duration) map[string]Place {
started := time.Now()
out := map[string]Place{}
for _, ip := range ips {
if len(out) >= max || time.Since(started) >= budget {
break
}
p := Lookup(get, provider, ip)
if p != nil {
delete(p, "ip")
}
out[ip] = p
}
return out
}
func when(p Place) float64 {
if ts, ok := num(p["ts"]); ok {
return ts
}
fail, _ := num(p["fail"])
return fail
}
// Merge puts LookupAll's results into the cache; true when it changed. shown is what is still on the map:
// the cache is kept bounded by dropping other entries only, oldest first, so a lookup is never repeated
// for an address that is still on the map.
func Merge(c *Cache, found map[string]Place, shown []string, now int64) bool {
changed := false
for ip, p := range found {
if !IsPublic(ip) {
continue
}
if p != nil {
kept := Place{}
for k, v := range p {
kept[k] = v
}
kept["ts"] = float64(now)
c.IPs[ip] = kept
} else {
c.IPs[ip] = Place{"fail": float64(now)}
}
changed = true
}
if over := len(c.IPs) - limit; over > 0 {
keep := map[string]bool{}
for _, ip := range shown {
keep[ip] = true
}
var other []string
for ip := range c.IPs {
if !keep[ip] {
other = append(other, ip)
}
}
sort.Slice(other, func(i, j int) bool {
a, b := when(c.IPs[other[i]]), when(c.IPs[other[j]])
if a != b {
return a < b
}
return other[i] < other[j]
})
for i := 0; i < over && i < len(other); i++ {
delete(c.IPs, other[i])
changed = true
}
}
return changed
}
// ResolveHome locates the router itself (the arcs' origin), at most every six hours; true when the cache changed.
func ResolveHome(get Fetch, provider string, c *Cache, wan string, now int64) bool {
h := c.Home
if h != nil && h["asn"] != nil {
if ts, _ := num(h["ts"]); float64(now)-ts < 6*3600 && (wan == "" || !IsPublic(wan) || h["wan"] == wan) {
return false
}
}
// behind NAT, the service is asked for our public address instead
ask := ""
if IsPublic(wan) {
ask = wan
}
next := Place{}
if p := Lookup(get, provider, ask); p != nil {
next = p
next["wan"] = wan
if wan == "" {
next["wan"] = nil
}
} else {
for k, v := range h {
next[k] = v
}
}
next["ts"] = float64(now)
c.Home = next
return true
}
// Label is "City · ISP" for the activity log; "" for no place (none known, or a failed lookup).
func Label(p Place) string {
if p == nil {
return ""
}
if fail, failed := num(p["fail"]); failed && fail != 0 {
return ""
}
var parts []string
for _, x := range []string{str(p["city"]), str(p["org"])} {
if x != "" {
parts = append(parts, x)
}
}
return strings.Join(parts, " · ")
}
+155
View File
@@ -0,0 +1,155 @@
package geo
import (
"fmt"
"strings"
"sync"
"testing"
"time"
"github.com/tickcount/luci-app-dawnline/backend/usage"
)
const now = 1790000000
func place() Place {
return Place{"lat": 45.04, "lon": 38.98, "city": "Krasnodar", "country": "Russia", "cc": "RU", "asn": 12389.0, "org": "Rostelecom"}
}
func stamped(ts float64) Place { p := place(); p["ts"] = ts; return p }
// As tests/ucode/geo.test.uc holds the ucode module to.
func TestStaleAndMerge(t *testing.T) {
c := &Cache{IPs: map[string]Place{"37.78.36.76": stamped(now - 86400), "212.233.85.1": stamped(now - 31*86400), "185.135.150.35": {"fail": float64(now - 3600)},
"185.135.150.28": {"fail": float64(now - 2*86400)}, "83.239.111.148": {"lat": 1.0, "lon": 2.0, "city": "Old", "ts": float64(now)}}}
for _, k := range []struct {
ips []string
want string
}{{[]string{"37.78.36.76"}, ""}, {[]string{"212.233.85.1"}, "212.233.85.1"}, {[]string{"185.135.150.35"}, ""}, {[]string{"185.135.150.28"}, "185.135.150.28"},
{[]string{"83.239.111.148"}, "83.239.111.148"}, {[]string{"10.0.1.164", "100.64.1.1", "", "fe80::1"}, ""}, {[]string{"1.1.1.1", "8.8.8.8", "1.1.1.1"}, "1.1.1.1 8.8.8.8"}} {
if got := strings.Join(Stale(c, k.ips, now), " "); got != k.want {
t.Errorf("stale of %v: %q, not %q", k.ips, got, k.want)
}
}
c = &Cache{IPs: map[string]Place{}}
if !Merge(c, map[string]Place{"1.1.1.1": place(), "8.8.8.8": nil}, nil, now) {
t.Error("results changed nothing")
}
if c.IPs["1.1.1.1"]["ts"] != float64(now) || c.IPs["1.1.1.1"]["city"] != "Krasnodar" || len(c.IPs["8.8.8.8"]) != 1 || c.IPs["8.8.8.8"]["fail"] != float64(now) {
t.Errorf("after the merge: %v", c.IPs)
}
if Merge(c, nil, nil, now) || Merge(c, map[string]Place{"10.0.0.1": place()}, nil, now) || c.IPs["10.0.0.1"] != nil {
t.Error("nothing, or a local address, changed the cache")
}
if Label(c.IPs["1.1.1.1"]) != "Krasnodar · Rostelecom" || Label(c.IPs["8.8.8.8"]) != "" || Label(nil) != "" || Label(Place{"city": "X"}) != "X" {
t.Error("the labels")
}
// bounded: the oldest go, never what is still shown
c = &Cache{IPs: map[string]Place{}}
for i := 0; i < 500; i++ {
c.IPs[fmt.Sprintf("1.0.%d.%d", i/250, i%250+1)] = stamped(float64(now - 1000 + i))
}
Merge(c, map[string]Place{"9.9.9.9": place(), "9.9.9.10": place()}, []string{"1.0.0.1"}, now)
if len(c.IPs) != 500 || c.IPs["1.0.0.1"] == nil || c.IPs["1.0.0.2"] != nil || c.IPs["1.0.0.3"] != nil || c.IPs["1.0.0.4"] == nil || c.IPs["9.9.9.9"] == nil {
t.Errorf("the bound: %d entries", len(c.IPs))
}
for _, ip := range []string{"8.8.8.8", "2a00:1450::1"} {
if !IsPublic(ip) {
t.Errorf("%s is not public", ip)
}
}
for _, ip := range []string{"", "192.168.1.1", "172.20.0.1", "224.0.0.1", "fe80::1", "fd00::1", "::1", "nonsense"} {
if IsPublic(ip) {
t.Errorf("%s is public", ip)
}
}
}
func TestLookups(t *testing.T) {
var asked []string
get := func(url string) map[string]any {
asked = append(asked, url)
switch {
case strings.HasPrefix(url, "https://ipwho.is/1.1.1.1?"):
return map[string]any{"success": true, "ip": "1.1.1.1", "latitude": 45.04, "longitude": 38.98, "city": "Krasnodar", "country": "Russia", "country_code": "RU",
"connection": map[string]any{"asn": 12389.0, "isp": "Rostelecom", "org": "Other"}}
case url == "https://ipapi.co/8.8.8.8/json/":
return map[string]any{"ip": "8.8.8.8", "latitude": 37.4, "longitude": -122.0, "city": "Mountain View", "country_name": "United States", "country_code": "US", "asn": "AS15169", "org": "GOOGLE"}
case url == "https://ipapi.co/json/":
return map[string]any{"ip": "203.0.113.7", "latitude": 55.7, "longitude": 37.6, "city": "Moscow", "country_name": "Russia", "country_code": "RU", "asn": "AS1", "org": "ISP"}
case strings.HasPrefix(url, "https://ipwho.is/"):
return map[string]any{"success": false}
}
return nil
}
found := LookupAll(get, "ipwho.is", []string{"1.1.1.1", "8.8.8.8", "4.4.4.4", "5.5.5.5"}, 3, time.Minute)
if len(found) != 3 || found["1.1.1.1"]["org"] != "Rostelecom" || found["1.1.1.1"]["ip"] != nil || found["8.8.8.8"]["asn"] != 15169.0 || found["8.8.8.8"]["country"] != "United States" || found["4.4.4.4"] != nil {
t.Errorf("the lookups: %v", found)
}
asked = nil
Lookup(get, "ipapi.co", "1.1.1.1")
if len(asked) != 2 || !strings.HasPrefix(asked[0], "https://ipapi.co/") {
t.Errorf("the chosen provider is not asked first: %v", asked)
}
// the router's own place: behind NAT the service is asked who we are; kept six hours, or until the address moves
c := &Cache{IPs: map[string]Place{}}
if !ResolveHome(get, "ipwho.is", c, "192.168.1.2", now) || c.Home["city"] != "Moscow" || c.Home["wan"] != "192.168.1.2" {
t.Errorf("home: %v", c.Home)
}
if ResolveHome(get, "ipwho.is", c, "192.168.1.2", now+3600) || !ResolveHome(get, "ipwho.is", c, "192.168.1.2", now+7*3600) {
t.Error("home is asked too often, or never again")
}
c.Home["wan"] = "1.1.1.1"
if ResolveHome(get, "ipwho.is", c, "1.1.1.1", now+7*3600+60) || !ResolveHome(get, "ipwho.is", c, "8.8.8.8", now+7*3600+120) || c.Home["city"] != "Mountain View" {
t.Errorf("home after the address moved: %v", c.Home)
}
}
func TestLocator(t *testing.T) {
dir := t.TempDir()
u := usage.Empty()
u.Devices["d1"] = &usage.Record{IPs: map[string]float64{"1.1.1.1": 1, "10.0.0.9": 1}}
u.Devices["d2"] = &usage.Record{IPs: map[string]float64{"2.2.2.2": 1, "3.3.3.3": 1, "4.4.4.4": 1}}
var mu sync.Mutex
asked := 0
var where map[string]string
var devices []string
done := make(chan bool, 8)
l := &Locator{Store: Store{Run: dir + "/run", Data: dir + "/data"}, Usage: func() *usage.Usage { return u },
Get: func(url string) map[string]any {
mu.Lock()
asked++
mu.Unlock()
if strings.Contains(url, "1.1.1.1") {
return map[string]any{"success": true, "latitude": 1.0, "longitude": 2.0, "city": "Krasnodar", "connection": map[string]any{"asn": 1.0, "isp": "ISP"}}
}
return nil
},
Found: func(w map[string]string, d []string) { where, devices = w, d; done <- true }}
l.Ask("ipwho.is", []string{"1.1.1.1"}, nil)
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("nothing was found")
}
if where["1.1.1.1"] != "Krasnodar · ISP" || len(devices) != 1 || devices[0] != "d1" {
t.Errorf("found %v for %v", where, devices)
}
if c := l.Store.Load(); c.IPs["1.1.1.1"]["city"] != "Krasnodar" {
t.Errorf("the cache: %v", c.IPs)
}
// the minute: what is shown and unknown, three a run; what is known is not asked again
mu.Lock()
asked = 0
mu.Unlock()
l.Minute(true, "ipwho.is", "")
time.Sleep(300 * time.Millisecond)
c := l.Store.Load()
if c.IPs["1.1.1.1"]["city"] != "Krasnodar" || c.IPs["2.2.2.2"]["fail"] == nil || c.IPs["3.3.3.3"]["fail"] == nil || c.IPs["4.4.4.4"]["fail"] == nil || c.IPs["10.0.0.9"] != nil {
t.Errorf("after the minute: %v", c.IPs)
}
if !l.Store.Flush() || l.Store.Flush() {
t.Error("the cache is not put on flash once")
}
l.Minute(false, "ipwho.is", "")
}
+160
View File
@@ -0,0 +1,160 @@
package geo
import (
"sync"
"time"
"github.com/tickcount/luci-app-dawnline/backend/usage"
)
// A lookup run: this many addresses at most, within this long.
const (
perRun = 3
budget = 12 * time.Second
)
// Locator finds the places of addresses for the map, a few at a time and never in the way of who asks: a
// lookup waits on the network for seconds. Addresses that just connected go first; the minute adds
// anything still shown that is missing or stale. One run at a time; what did not fit waits for the next.
type Locator struct {
Store Store
Get Fetch
// Usage is the counters now: they tell which addresses are still shown, and whose they are.
Usage func() *usage.Usage
// Found is told the places found ("City · ISP" by address) and the devices seen from those addresses.
Found func(where map[string]string, devices []string)
mu sync.Mutex
pending []string
running bool
lastHome int64
}
func (l *Locator) shown(u *usage.Usage) []string {
var ips []string
seen := map[string]bool{}
if u == nil {
return ips
}
for _, rec := range u.Devices {
for ip := range rec.IPs {
if !seen[ip] {
seen[ip] = true
ips = append(ips, ip)
}
}
}
return ips
}
// Ask has these addresses located now (provider: the settings' choice), and with them whatever of more is
// missing or stale. It returns at once; the run goes on by itself.
func (l *Locator) Ask(provider string, ips, more []string) {
l.mu.Lock()
for _, ip := range ips {
known := false
for _, q := range l.pending {
known = known || q == ip
}
if !known {
l.pending = append(l.pending, ip)
}
}
if l.running {
l.mu.Unlock()
return
}
todo := Stale(l.Store.Load(), append(append([]string{}, l.pending...), more...), time.Now().Unix())
want := todo
if len(want) > perRun {
want = want[:perRun]
}
// what just connected and did not fit waits for the next run
var rest []string
for _, ip := range todo[len(want):] {
for _, q := range l.pending {
if q == ip {
rest = append(rest, ip)
}
}
}
l.pending = rest
if len(want) == 0 {
l.mu.Unlock()
return
}
l.running = true
l.mu.Unlock()
go func() {
found := LookupAll(l.Get, provider, want, perRun, budget)
l.done(found)
l.mu.Lock()
l.running = false
again := len(l.pending) > 0
l.mu.Unlock()
if again {
l.Ask(provider, nil, nil)
}
}()
}
// done puts a run's results into the cache and tells who waits for them.
func (l *Locator) done(found map[string]Place) {
u := l.Usage()
c := l.Store.Load()
if !Merge(c, found, l.shown(u), time.Now().Unix()) {
return
}
l.Store.Save(c)
where := map[string]string{}
for ip, p := range found {
if s := Label(p); s != "" {
where[ip] = s
}
}
if len(where) == 0 || l.Found == nil {
return
}
var devices []string
if u != nil {
for id, rec := range u.Devices {
for ip := range where {
if _, has := rec.IPs[ip]; has {
devices = append(devices, id)
break
}
}
}
}
l.Found(where, devices)
}
// Minute is the minute's part: addresses still shown that are missing or stale get a run, and the router's
// own place is looked at about hourly (wan: its public address as the router has it, "" when unknown).
func (l *Locator) Minute(enabled bool, provider, wan string) {
if !enabled {
l.mu.Lock()
l.pending = nil
l.mu.Unlock()
return
}
l.Ask(provider, nil, l.shown(l.Usage()))
now := time.Now().Unix()
l.mu.Lock()
due := now-l.lastHome > 3600
if due {
l.lastHome = now
}
l.mu.Unlock()
if due {
go func() {
c := l.Store.Load()
if ResolveHome(l.Get, provider, c, wan, time.Now().Unix()) {
// the places found meanwhile are the file's: only the router's own is put in
fresh := l.Store.Load()
fresh.Home = c.Home
l.Store.Save(fresh)
}
}()
}
}
+422
View File
@@ -0,0 +1,422 @@
// Package github is GitHub for the Components tab, as dawnline.github: releases (with each file's sha256),
// one release by tag, and the changes between two tags, each kept ten minutes in the run directory under
// the name the ucode backend keeps its own (gh-<key>.json: whichever asked first, the other reads it, and
// the two share GitHub's hourly allowance).
//
// Trust: the API is only ever asked directly (TLS to api.github.com).
package github
import (
"encoding/json"
"fmt"
"io"
"net/http"
"regexp"
"strings"
"time"
"crypto/sha256"
"encoding/hex"
"github.com/tickcount/luci-app-dawnline/backend/files"
"os"
)
// Failure is why GitHub could not be asked, as a sentence to translate (Msg) and what goes into it (Args).
type Failure struct {
Msg string
Args []any
}
func (f Failure) Error() string { return fmt.Sprintf(f.Msg, f.Args...) }
// Client asks GitHub. Get fetches a URL (status 0: no answer); nil is the real one.
type Client struct {
RunDir string
Get func(url string) (status int, body []byte)
Now func() time.Time
}
func get(url string) (int, []byte) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return 0, nil
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("User-Agent", "Dawnline")
res, err := (&http.Client{Timeout: 12 * time.Second}).Do(req)
if err != nil {
return 0, nil
}
defer res.Body.Close()
body, err := io.ReadAll(io.LimitReader(res.Body, 8<<20))
if err != nil {
return 0, nil
}
return res.StatusCode, body
}
func (c *Client) now() int64 {
if c.Now != nil {
return c.Now().Unix()
}
return time.Now().Unix()
}
var rateLimit = regexp.MustCompile(`(?i)rate limit`)
// api is JSON from the API, or nil when GitHub did not answer or has no such thing; a Failure on a rate
// limit or another API error.
func (c *Client) api(url string) (any, error) {
fetch := c.Get
if fetch == nil {
fetch = get
}
status, body := fetch(url)
if status == 0 {
return nil, nil
}
var j any
if json.Unmarshal(body, &j) != nil {
return nil, nil
}
msg := ""
if m, ok := j.(map[string]any); ok {
msg, _ = m["message"].(string)
}
if status == 403 || status == 429 || rateLimit.MatchString(msg) {
return nil, Failure{Msg: "GitHub API rate limit reached: try again in an hour."}
}
if status == 404 {
return nil, nil
}
if status >= 400 {
if msg == "" {
msg = fmt.Sprintf("HTTP %d", status)
}
return nil, Failure{Msg: "GitHub: %s", Args: []any{msg}}
}
return j, nil
}
var unsafe = regexp.MustCompile(`[^A-Za-z0-9_.-]`)
// cached answers from the file while it is fresh, else asks; what could not be asked anew is the old answer.
func (c *Client) cached(key string, ttl int64, fn func() (any, error)) (any, error) {
path := c.RunDir + "/gh-" + unsafe.ReplaceAllString(key, "_") + ".json"
var kept struct {
At float64 `json:"at"`
Data any `json:"data"`
}
has := files.ReadJSON(path, &kept)
if has && float64(c.now())-kept.At < float64(ttl) {
return kept.Data, nil
}
data, err := fn()
if err != nil {
return nil, err
}
if data != nil {
files.WriteJSON(path, map[string]any{"at": c.now(), "data": data}, 0o644)
return data, nil
}
return kept.Data, nil
}
var digest = regexp.MustCompile(`^sha256:[0-9a-f]{64}$`)
// slim is what is kept of a release.
func slim(x map[string]any) map[string]any {
digests, ids := map[string]any{}, map[string]any{}
names := []any{}
list, _ := x["assets"].([]any)
for _, a := range list {
m, _ := a.(map[string]any)
name, _ := m["name"].(string)
names = append(names, m["name"])
if d, _ := m["digest"].(string); digest.MatchString(d) {
digests[name] = d[7:]
}
if id, ok := m["id"].(float64); ok && id > 0 && id == float64(int64(id)) {
ids[name] = id
}
}
or := func(v, dflt any) any {
if v == nil {
return dflt
}
return v
}
body, _ := x["body"].(string)
if len(body) > 12000 {
body = body[:12000]
}
pre, _ := x["prerelease"].(bool)
return map[string]any{
"tag": x["tag_name"], "name": or(x["name"], x["tag_name"]), "published": x["published_at"], "prerelease": pre, "url": x["html_url"],
"body": body, "assets": names, "digests": digests, "ids": ids,
}
}
// Releases is a repository's releases, newest first, five a page (drafts skipped).
func (c *Client) Releases(repo string, page int) ([]any, error) {
data, err := c.cached(fmt.Sprintf("rel-%s-%d", repo, page), 600, func() (any, error) {
j, err := c.api(fmt.Sprintf("https://api.github.com/repos/%s/releases?per_page=5&page=%d", repo, page))
list, ok := j.([]any)
if err != nil || !ok {
return nil, err
}
out := []any{}
for _, x := range list {
if m, ok := x.(map[string]any); ok && m["draft"] != true {
out = append(out, slim(m))
}
}
return out, nil
})
list, _ := data.([]any)
if list == nil {
list = []any{}
}
return list, err
}
// Release is one release by tag (its notes, its files and their sha256), or nil.
func (c *Client) Release(repo, tag string) (map[string]any, error) {
data, err := c.cached("tag-"+repo+"-"+tag, 600, func() (any, error) {
j, err := c.api("https://api.github.com/repos/" + repo + "/releases/tags/" + tag)
if m, ok := j.(map[string]any); ok && m["tag_name"] != nil && m["tag_name"] != "" {
return slim(m), err
}
return nil, err
})
m, _ := data.(map[string]any)
return m, err
}
var (
point = regexp.MustCompile(`^\s*[-*]\s+(.*)$`)
wrapped = regexp.MustCompile(`^\s+\S`)
version = regexp.MustCompile(`^(Dawnline )?v?[0-9]+\.[0-9]+(\.[0-9]+)?$`)
)
const space = " \t\r\n\v\f"
// Points is what changed in a commit message: a release commit lists its changes as "- " lines in the
// body, any other commit is its title. Wrapped points are joined back into one line.
func Points(msg string) []string {
lines := strings.Split(msg, "\n")
title := strings.Trim(lines[0], space)
out := []string{}
if strings.HasPrefix(title, "Merge ") {
return out
}
cur, open := "", false
for _, line := range lines[1:] {
if m := point.FindStringSubmatch(line); m != nil {
if open {
out = append(out, cur)
}
cur, open = strings.Trim(m[1], space), true
} else if open && wrapped.MatchString(line) {
cur += " " + strings.Trim(line, space)
} else if open {
out = append(out, cur)
open = false
}
}
if open {
out = append(out, cur)
}
if len(out) == 0 && title != "" && !version.MatchString(title) {
out = append(out, title)
}
return out
}
func message(commit any) string {
m, _ := commit.(map[string]any)
c, _ := m["commit"].(map[string]any)
s, _ := c["message"].(string)
return s
}
// Changes is what changed from one tag to another, newest first, twenty points at most: commit by commit,
// or the release's own commit when there is no base to compare with.
func (c *Client) Changes(repo, base, head string) ([]any, error) {
key := base
if base == "" {
key = "null"
}
data, err := c.cached("changes-"+repo+"-"+key+"-"+head, 600, func() (any, error) {
cut := func(points []any) any {
if len(points) > 20 {
points = points[:20]
}
return points
}
if base != "" {
j, err := c.api("https://api.github.com/repos/" + repo + "/compare/" + base + "..." + head)
if err != nil {
return nil, err
}
m, _ := j.(map[string]any)
if commits, ok := m["commits"].([]any); ok {
out := []any{}
for i := len(commits) - 1; i >= 0; i-- {
for _, p := range Points(message(commits[i])) {
out = append(out, p)
}
}
return cut(out), nil
}
}
j, err := c.api("https://api.github.com/repos/" + repo + "/commits/" + head)
if err != nil {
return nil, err
}
if m, _ := j.(map[string]any); m["commit"] != nil {
out := []any{}
for _, p := range Points(message(j)) {
out = append(out, p)
}
return cut(out), nil
}
return nil, nil
})
list, _ := data.([]any)
if list == nil {
list = []any{}
}
return list, err
}
// Mirrors are prefixes that fetch github.com files when GitHub itself is unreachable. A file may come
// through one only when its sha256 is known from the API, and is kept only if it matches.
var Mirrors = []string{"https://ghfast.top/", "https://gh-proxy.com/", "https://ghproxy.net/"}
// Latest is a repository's latest stable release, or nil when GitHub does not answer.
func (c *Client) Latest(repo string) map[string]any {
data, _ := c.cached("latest-"+repo, 300, func() (any, error) {
j, err := c.api("https://api.github.com/repos/" + repo + "/releases/latest")
if m, ok := j.(map[string]any); ok && m["tag_name"] != nil && m["tag_name"] != "" {
return slim(m), err
}
return nil, err
})
m, _ := data.(map[string]any)
return m
}
// AssetSHA256 is the sha256 GitHub publishes for a release's file, or "".
func (c *Client) AssetSHA256(repo, tag, name string) string {
r, _ := c.Release(repo, tag)
digests, _ := r["digests"].(map[string]any)
s, _ := digests[name].(string)
return s
}
// Assets is the names of a release's files (nil: no such release, or GitHub does not answer).
func (c *Client) Assets(repo, tag string) []string {
r, _ := c.Release(repo, tag)
if r == nil {
return nil
}
list, _ := r["assets"].([]any)
out := []string{}
for _, a := range list {
if s, is := a.(string); is {
out = append(out, s)
}
}
return out
}
// Fetch downloads a URL into a file; false when it did not come whole.
type Fetch func(url, dest string) bool
// HTTPFetch is Fetch over the network: five minutes at most, redirects followed.
func HTTPFetch(url, dest string) bool {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return false
}
req.Header.Set("User-Agent", "Dawnline")
res, err := (&http.Client{Timeout: 300 * time.Second}).Do(req)
if err != nil {
return false
}
defer res.Body.Close()
if res.StatusCode != 200 {
return false
}
f, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
return false
}
n, err := io.Copy(f, res.Body)
if cerr := f.Close(); err != nil || cerr != nil || n == 0 {
os.Remove(dest)
return false
}
return true
}
// SHA256 is a file's sha256 in hex, or "".
func SHA256(path string) string {
f, err := os.Open(path)
if err != nil {
return ""
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return ""
}
return hex.EncodeToString(h.Sum(nil))
}
var fromGitHub = regexp.MustCompile(`^https://(github\.com|raw\.githubusercontent\.com)/`)
// Download gets a file: GitHub itself first; a mirror only when the sha256 is known (from the API) and
// mirrors are allowed. A file that does not match the sha256 is thrown away and the next source is tried.
// It returns the URL that worked, or "". log is told of mirrors (words in t's language; nil: English).
func Download(fetch Fetch, url, dest, want string, mirrors bool, log func(string), t func(string) string) string {
if t == nil {
t = func(s string) string { return s }
}
say := func(format string, args ...any) {
if log != nil {
log(fmt.Sprintf(t(format), args...))
}
}
want = strings.ToLower(want)
type try struct {
url string
mirror bool
}
tries := []try{{url, false}}
if want != "" && mirrors && fromGitHub.MatchString(url) {
for _, m := range Mirrors {
tries = append(tries, try{m + url, true})
}
}
for i, x := range tries {
host := strings.SplitN(strings.TrimPrefix(x.url, "https://"), "/", 2)[0]
if x.mirror {
say("Trying mirror %s", host)
}
if fetch(x.url, dest) {
if st, err := os.Stat(dest); err == nil && st.Size() > 0 {
if want == "" || SHA256(dest) == want {
return x.url
}
say("Warning: %s sent a file that does not match GitHub's sha256, discarded", host)
}
}
os.Remove(dest)
if i == 0 && len(tries) > 1 {
say("GitHub did not answer, trying mirrors")
}
}
return ""
}
+84
View File
@@ -0,0 +1,84 @@
package github
import (
"reflect"
"strings"
"testing"
"time"
)
func TestPoints(t *testing.T) {
for msg, want := range map[string][]string{
"Dawnline 0.3.1\n\n- one thing\n- another,\n wrapped onto a line\n\n* a third\ntrailing words": {"one thing", "another, wrapped onto a line", "a third"},
"Fix the portal's port": {"Fix the portal's port"},
"Merge branch 'main' into x\n\n- a": {},
"v0.4.0": {},
"Dawnline 0.3": {},
"": {},
"A title\n\nwords, no points": {"A title"},
} {
if got := Points(msg); !reflect.DeepEqual(got, want) {
t.Errorf("%q: %q", msg, got)
}
}
}
func TestReleasesAndTheCache(t *testing.T) {
now := time.Unix(1_800_000_000, 0)
asked := 0
answers := map[string]struct {
status int
body string
}{
"https://api.github.com/repos/o/r/releases?per_page=5&page=1": {200, `[{"tag_name":"v2","name":null,"prerelease":true,"html_url":"u","body":"notes","published_at":"2027-01-01T00:00:00Z",
"assets":[{"name":"a.apk","id":77,"digest":"sha256:` + strings.Repeat("ab", 32) + `"},{"name":"b.txt","id":78,"digest":"md5:x"}]},{"tag_name":"draft","draft":true},{"tag_name":"v1"}]`},
"https://api.github.com/repos/o/r/releases/tags/v2": {200, `{"tag_name":"v2","body":"` + strings.Repeat("x", 13000) + `"}`},
"https://api.github.com/repos/o/r/releases/tags/v9": {404, `{"message":"Not Found"}`},
"https://api.github.com/repos/o/r/releases/tags/limited": {403, `{"message":"API rate limit exceeded"}`},
"https://api.github.com/repos/o/r/releases/tags/broken": {500, `{"message":"Server Error"}`},
"https://api.github.com/repos/o/r/compare/v1...v2": {200, `{"commits":[{"commit":{"message":"older"}},{"commit":{"message":"Dawnline 2\n\n- newer a\n- newer b"}}]}`},
"https://api.github.com/repos/o/r/commits/v2": {200, `{"commit":{"message":"Dawnline 2\n\n- its own"}}`},
}
c := &Client{RunDir: t.TempDir(), Now: func() time.Time { return now }, Get: func(url string) (int, []byte) {
asked++
a := answers[url]
return a.status, []byte(a.body)
}}
rel, err := c.Releases("o/r", 1)
if err != nil || len(rel) != 2 {
t.Fatalf("releases: %v %v", rel, err)
}
first := rel[0].(map[string]any)
if first["tag"] != "v2" || first["name"] != "v2" || first["prerelease"] != true || !reflect.DeepEqual(first["assets"], []any{"a.apk", "b.txt"}) ||
!reflect.DeepEqual(first["digests"], map[string]any{"a.apk": strings.Repeat("ab", 32)}) || len(first["ids"].(map[string]any)) != 2 {
t.Fatalf("a release: %v", first)
}
// kept ten minutes, then asked again; with no answer the second time, the old one stands
c.Releases("o/r", 1)
if asked != 1 {
t.Fatal("asked again within ten minutes")
}
now = now.Add(11 * time.Minute)
delete(answers, "https://api.github.com/repos/o/r/releases?per_page=5&page=1")
if rel, _ := c.Releases("o/r", 1); asked != 2 || len(rel) != 2 {
t.Fatalf("with GitHub out of reach: asked %d, %d releases", asked, len(rel))
}
if r, _ := c.Release("o/r", "v2"); len(r["body"].(string)) != 12000 {
t.Fatal("the notes are not cut")
}
if r, err := c.Release("o/r", "v9"); r != nil || err != nil {
t.Fatalf("no such release: %v %v", r, err)
}
if _, err := c.Release("o/r", "limited"); err == nil || err.Error() != "GitHub API rate limit reached: try again in an hour." {
t.Fatalf("a rate limit: %v", err)
}
if _, err := c.Release("o/r", "broken"); err == nil || err.Error() != "GitHub: Server Error" {
t.Fatalf("an API error: %v", err)
}
if ch, _ := c.Changes("o/r", "v1", "v2"); !reflect.DeepEqual(ch, []any{"newer a", "newer b", "older"}) {
t.Fatalf("changes between tags: %v", ch)
}
if ch, _ := c.Changes("o/r", "", "v2"); !reflect.DeepEqual(ch, []any{"its own"}) {
t.Fatalf("a release's own changes: %v", ch)
}
}
+3
View File
@@ -0,0 +1,3 @@
module github.com/tickcount/luci-app-dawnline/backend
go 1.24
+74
View File
@@ -0,0 +1,74 @@
package handout
import (
"os"
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// A device that sends only some networks into the tunnel is given what is behind the routers it reaches;
// one that sends everything needs no word of them, and a router is never given its own.
func TestNetworksBehindRoutersInAConfiguration(t *testing.T) {
text, err := os.ReadFile("../access/testdata/access.uci")
if err != nil {
t.Fatal(err)
}
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
home := []string{"192.168.77.0/24"}
allowed := func(id string) string {
m := model.Load(c, nil)
cfg := ClientConfig(m, m.Devices[id], home)
if cfg == nil {
t.Fatalf("%s has no configuration", id)
}
return strings.Join(cfg.Amnezia.AllowedIPs, ", ")
}
print := func(id string) string {
m := model.Load(c, nil)
return Fingerprint(m, m.Devices[id], home)
}
// Anna's laptop keeps its own local network outside; her phone sends everything; Fedor is in another circle
before := map[string]string{}
for _, id := range []string{"laptop", "phone", "bphone", "fphone", "dphone", "lphone"} {
before[id] = print(id)
}
c.Set("bphone", "kind", "router")
c.SetList("bphone", "behind_net", []string{"192.168.2.0/24"})
c.Set("kphone", "kind", "router")
c.SetList("kphone", "behind_net", []string{"192.168.3.0/24"})
c.Set("fphone", "lan_bypass", "1")
c.Set("lphone", "lan_bypass", "1")
if got := allowed("laptop"); !strings.Contains(got, "192.168.2.0/24") || strings.Contains(got, "192.168.3.0/24") {
t.Errorf("Anna's laptop, in Boris's circle: %s", got)
}
if got := allowed("phone"); got != "0.0.0.0/0, ::/0" {
t.Errorf("Anna's phone sends everything: %s", got)
}
if got := allowed("fphone"); strings.Contains(got, "192.168.2.0/24") {
t.Errorf("Fedor's phone, in another circle: %s", got)
}
// where everyone reaches everyone: the other server's network, and only that server's
if got := allowed("lphone"); !strings.Contains(got, "192.168.3.0/24") || strings.Contains(got, "192.168.2.0/24") {
t.Errorf("Lev's phone: %s", got)
}
// a router is not told to send its own network into the tunnel
c.Set("bphone", "lan_bypass", "1")
if got := allowed("bphone"); strings.Contains(got, "192.168.2.0/24") {
t.Errorf("Boris's router: %s", got)
}
// who was given a new network must import again; who was not keeps what they hold
if print("laptop") == before["laptop"] {
t.Error("Anna's laptop holds a configuration without the network, and is not told")
}
for _, id := range []string{"phone", "dphone"} {
if print(id) != before[id] {
t.Errorf("%s is told to import again for a network that is not in its configuration", id)
}
}
}
+349
View File
@@ -0,0 +1,349 @@
// Package handout is what a device is given: its configuration (the .conf and what the AmneziaVPN key is
// made of) and the fingerprint that tells whether the one it holds is still the one it would get. As
// dawnline.awg client_config, allowed_ips and config_fingerprint make them, character for character: a
// fingerprint that differed between the two backends would tell every device to import again.
//
// Pure: the settings and the home networks in, text out. The home networks are the router's own (which
// interfaces are home, and what they reach): the caller reads them and hands them in.
package handout
import (
"fmt"
"strconv"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
"github.com/tickcount/luci-app-dawnline/backend/model"
)
// ObfKeys is the disguise's settings in the order a configuration lists them (dawnline.core OBF_KEYS).
var ObfKeys = []string{"jc", "jmin", "jmax", "s1", "s2", "s3", "s4", "h1", "h2", "h3", "h4", "i1", "i2", "i3", "i4", "i5",
"header_protection_key", "content_padding_addition", "rekey_after_time", "rekey_timeout", "reject_after_time",
"keepalive_timeout", "max_handshake_attempts", "random_trailers", "disable_cookies"}
// What each is called in a .conf (dawnline.awg OBF_CONF).
var obfConf = map[string]string{
"jc": "Jc", "jmin": "Jmin", "jmax": "Jmax", "s1": "S1", "s2": "S2", "s3": "S3", "s4": "S4",
"h1": "H1", "h2": "H2", "h3": "H3", "h4": "H4", "i1": "I1", "i2": "I2", "i3": "I3", "i4": "I4", "i5": "I5",
"header_protection_key": "HeaderProtectionKey", "content_padding_addition": "ContentPaddingAddition",
"rekey_after_time": "RekeyAfterTime", "rekey_timeout": "RekeyTimeout", "reject_after_time": "RejectAfterTime",
"keepalive_timeout": "KeepaliveTimeout", "max_handshake_attempts": "MaxHandshakeAttempts",
"random_trailers": "RandomTrailers", "disable_cookies": "DisableCookies",
}
// Bypass4 is every IPv4 address but the private ranges, and Bypass6 every IPv6 one outside fc00::/7,
// fe80::/10 and ff00::/8: what a device with its own local network kept outside sends into the tunnel
// (tests/ucode/awg.test.uc proves each is exactly that complement).
var Bypass4 = []string{
"0.0.0.0/5", "8.0.0.0/7", "11.0.0.0/8", "12.0.0.0/6", "16.0.0.0/4", "32.0.0.0/3", "64.0.0.0/3", "96.0.0.0/6",
"100.0.0.0/10", "100.128.0.0/9", "101.0.0.0/8", "102.0.0.0/7", "104.0.0.0/5", "112.0.0.0/4", "128.0.0.0/3", "160.0.0.0/5",
"168.0.0.0/8", "169.0.0.0/9", "169.128.0.0/10", "169.192.0.0/11", "169.224.0.0/12", "169.240.0.0/13", "169.248.0.0/14",
"169.252.0.0/15", "169.255.0.0/16", "170.0.0.0/7", "172.0.0.0/12", "172.32.0.0/11", "172.64.0.0/10", "172.128.0.0/9",
"173.0.0.0/8", "174.0.0.0/7", "176.0.0.0/4", "192.0.0.0/9", "192.128.0.0/11", "192.160.0.0/13", "192.169.0.0/16",
"192.170.0.0/15", "192.172.0.0/14", "192.176.0.0/12", "192.192.0.0/10", "193.0.0.0/8", "194.0.0.0/7", "196.0.0.0/6",
"200.0.0.0/5", "208.0.0.0/4",
}
var Bypass6 = []string{"::/1", "8000::/2", "c000::/3", "e000::/4", "f000::/5", "f800::/6", "fe00::/9", "fec0::/10"}
// AllowedIPs is what a device sends into the tunnel. Home only: the person's home networks, the tunnel
// network or the router's address, and the DNS. Internet: everything, or with the device's own local
// network kept outside everything but that, every DNS server it is given and for home access what Home
// only gets. `home` is the home networks the person reaches; dev may be nil.
//
// What is behind other people's routers is part of what a device reaches: a device that sends only some
// networks into the tunnel gets those too (as the settings have them: which of them the router has taken
// is the router's to know, and a network it has not is dropped there).
func AllowedIPs(m *model.Model, srv *model.Server, p *model.Person, dev *model.Device, home []string) []string {
if p.Access != "lan" && (dev == nil || !dev.LanBypass) {
return []string{"0.0.0.0/0", "::/0"}
}
reach := func() []string {
nets := append([]string{}, home...)
// With devices reaching each other, the whole tunnel network (the router's address is in it);
// otherwise only the router's own address.
if srv.DeviceMesh && srv.Subnet != "" {
nets = append(nets, srv.Subnet)
} else if a := model.ServerAddress(srv); a != "" {
nets = append(nets, a+"/32")
}
id := ""
if dev != nil {
id = dev.ID
}
return append(nets, m.BehindReach(p, id, m.BehindOf(nil))...)
}
dns := m.ClientDNS(srv, p)
if p.Access == "lan" {
nets := reach()
if len(dns) > 0 && dns[0] != "" {
nets = append(nets, dns[0]+"/32")
}
return ipnet.Collapse(nets)
}
// Every DNS server stays in the tunnel: a private one left out would be asked on whatever network
// the device is on (a public one is in the rest).
nets := append([]string{}, Bypass4...)
var nets6 []string
seen6 := map[string]bool{}
for _, a := range dns {
if _, ok := ipnet.IP(a); ok {
nets = append(nets, a+"/32")
} else if ipnet.Private6(a) && !seen6[a] {
seen6[a] = true
nets6 = append(nets6, a+"/128")
}
}
if p.Access == "both" {
nets = append(nets, reach()...)
}
out := append(ipnet.Collapse(nets), Bypass6...)
return append(out, nets6...)
}
// ObfNeutral tells whether the disguise's settings change nothing on the wire (plain WireGuard).
func ObfNeutral(o map[string]string) bool {
for _, k := range ObfKeys {
v := o[k]
if v == "" {
continue
}
want := "0"
switch k {
case "h1":
want = "1"
case "h2":
want = "2"
case "h3":
want = "3"
case "h4":
want = "4"
}
if v != want {
return false
}
}
return true
}
// clientObf is the disguise a device's configuration carries. The junk a device sends is its own: within
// the preset's ranges the count and sizes are shifted a little by the device (the same way every time, so
// the configuration keeps its fingerprint), so two phones of one family do not share one profile. What
// the server must agree on stays the server's.
func clientObf(srv *model.Server, dev *model.Device) map[string]string {
o := map[string]string{}
for k, v := range srv.Obf {
o[k] = v
}
num := func(s string) (int64, bool) {
f, ok := model.Number(s)
return int64(f), ok && s != ""
}
jc, ok := num(o["jc"])
if ObfNeutral(o) || !ok || jc <= 0 {
return o
}
key := srv.ID + "/" + dev.ID + "/" + srv.Obf["h1"]
seed := int64(2166136261)
for i := 0; i < len(key); i++ {
seed = ((seed ^ int64(key[i])) * 16777619) & 0x7fffffff
}
pick := func(n int64) int64 {
seed = (seed*1103515245 + 12345) & 0x7fffffff
return seed % n
}
jmin, _ := num(o["jmin"])
jmax, _ := num(o["jmax"])
o["jc"] = strconv.FormatInt(max(1, min(10, jc+pick(3)-1)), 10)
if jmax > jmin+32 {
lo := jmin + pick(17)
hi := jmax - pick(17)
if hi <= lo {
hi = lo + 32
}
o["jmin"], o["jmax"] = strconv.FormatInt(lo, 10), strconv.FormatInt(hi, 10)
}
return o
}
// Amnezia is what the AmneziaVPN key is made of.
type Amnezia struct {
Host string `json:"host"`
Port int `json:"port"`
MTU string `json:"mtu"`
DNS []string `json:"dns"`
AllowedIPs []string `json:"allowed_ips"`
ClientIP string `json:"client_ip"`
ClientPrivKey string `json:"client_priv_key"`
ClientPubKey string `json:"client_pub_key"`
PSKKey string `json:"psk_key"`
ServerPubKey string `json:"server_pub_key"`
PersistentKeepAlive string `json:"persistent_keep_alive"`
Obfuscation map[string]string `json:"obfuscation"`
}
// Config is a device's configuration.
type Config struct {
Name string `json:"name"`
Person string `json:"person"`
Server string `json:"server"`
Filename string `json:"filename"`
Conf string `json:"conf"`
HasPrivateKey bool `json:"has_private_key"`
Amnezia Amnezia `json:"amnezia"`
}
// ClientConfig is the configuration a device gets now, or nil when it has no server.
func ClientConfig(m *model.Model, dev *model.Device, home []string) *Config {
p := m.People[dev.Person]
if p == nil {
return nil
}
srv := m.Servers[p.Server]
if srv == nil {
return nil
}
host := m.EndpointHost(srv)
ep := host
if strings.Contains(host, ":") && !strings.HasPrefix(host, "[") {
ep = "[" + host + "]"
}
allowed := AllowedIPs(m, srv, p, dev, home)
dns := m.ClientDNS(srv, p)
mtu := model.DeviceMTU(srv, dev)
priv := dev.PrivateKey
if priv == "" {
priv = "<private key stays on your device>"
}
lines := []string{"[Interface]", "PrivateKey = " + priv, "Address = " + dev.Address + "/32"}
if len(dns) > 0 {
lines = append(lines, "DNS = "+strings.Join(dns, ", "))
}
lines = append(lines, fmt.Sprintf("MTU = %d", mtu))
// "Off" is plain WireGuard: neutral values are left out, so any WireGuard app accepts the file.
obf := clientObf(srv, dev)
if !ObfNeutral(obf) {
for _, k := range ObfKeys {
if obf[k] != "" {
lines = append(lines, obfConf[k]+" = "+obf[k])
}
}
}
lines = append(lines, "", "[Peer]", "PublicKey = "+srv.PublicKey)
if dev.PresharedKey != "" {
lines = append(lines, "PresharedKey = "+dev.PresharedKey)
}
lines = append(lines, "AllowedIPs = "+strings.Join(allowed, ", "), fmt.Sprintf("Endpoint = %s:%d", ep, srv.Port))
keepalive := ""
if lo, hi, on := model.KeepaliveRange(srv, dev); on {
keepalive = strconv.Itoa(lo)
if hi != lo {
keepalive = fmt.Sprintf("%d-%d", lo, hi)
}
lines = append(lines, "PersistentKeepalive = "+keepalive)
}
alive := keepalive
if alive == "" {
alive = "0"
}
return &Config{
Name: dev.Name, Person: p.Name, Server: srv.Name,
// The file name is the tunnel's name in WireGuard apps: the server's.
Filename: TunnelName(srv.Name) + ".conf",
Conf: strings.Join(lines, "\n") + "\n",
HasPrivateKey: dev.PrivateKey != "",
Amnezia: Amnezia{
Host: host, Port: srv.Port, MTU: strconv.Itoa(mtu), DNS: dns, AllowedIPs: allowed,
ClientIP: dev.Address, ClientPrivKey: dev.PrivateKey, ClientPubKey: dev.PublicKey,
PSKKey: dev.PresharedKey, ServerPubKey: srv.PublicKey, PersistentKeepAlive: alive, Obfuscation: obf,
},
}
}
// Fingerprint is what a device holds, minus what is its own alone (its private key, its junk's count and
// sizes): it changes when the device must import again. "" for a device with no server.
func Fingerprint(m *model.Model, dev *model.Device, home []string) string {
c := ClientConfig(m, dev, home)
if c == nil {
return ""
}
var keep []string
for _, l := range strings.Split(c.Conf, "\n") {
own := false
for _, k := range []string{"PrivateKey ", "Jc ", "Jmin ", "Jmax "} {
own = own || strings.HasPrefix(l, k)
}
if !own {
keep = append(keep, l)
}
}
h := uint32(2166136261)
for _, b := range []byte(strings.Join(keep, "\n")) {
h = (h ^ uint32(b)) * 16777619
}
return fmt.Sprintf("%08x", h)
}
// Cyrillic letters written in Latin, from а (dawnline.util translit).
var cyrillic = []string{"a", "b", "v", "g", "d", "e", "zh", "z", "i", "y", "k", "l", "m", "n", "o", "p", "r", "s", "t", "u",
"f", "kh", "ts", "ch", "sh", "shch", "", "y", "", "e", "yu", "ya"}
var cyrillicMore = map[rune]string{0x451: "yo", 0x456: "i", 0x457: "yi", 0x454: "ye", 0x491: "g"}
// Translit writes Cyrillic letters in Latin ("Мама" → "mama"); other characters are left alone.
func Translit(s string) string {
var b strings.Builder
for _, r := range s {
switch {
case r >= 0x410 && r <= 0x42f:
r += 0x20
case r == 0x401:
r = 0x451
case r == 0x406:
r = 0x456
case r == 0x407:
r = 0x457
case r == 0x404:
r = 0x454
case r == 0x490:
r = 0x491
}
if r >= 0x430 && int(r-0x430) < len(cyrillic) {
b.WriteString(cyrillic[r-0x430])
} else if t, ok := cyrillicMore[r]; ok {
b.WriteString(t)
} else {
b.WriteRune(r)
}
}
return b.String()
}
// Names Windows keeps for devices: no file may be called that.
var winDevices = map[string]bool{"con": true, "prn": true, "aux": true, "nul": true}
// TunnelName is a server's name as a tunnel's (a file's) name: Latin letters and digits, at most 15.
func TunnelName(name string) string {
var b strings.Builder
gap := false
for _, c := range []byte(Translit(name)) {
if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' {
if gap {
b.WriteByte('-')
}
gap = false
b.WriteByte(c)
} else {
gap = true
}
}
// a gap at either end is trimmed: one before the first letter was written as a dash above
l := strings.Trim(b.String(), "-")
if len(l) > 15 {
l = l[:15]
}
l = strings.TrimRight(l, "-")
low := strings.ToLower(l)
if l == "" || winDevices[low] || (len(low) == 4 && (strings.HasPrefix(low, "com") || strings.HasPrefix(low, "lpt")) && low[3] >= '1' && low[3] <= '9') {
return "dawnline"
}
return l
}
+81
View File
@@ -0,0 +1,81 @@
package handout
import (
"encoding/json"
"os"
"reflect"
"sort"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// Every device of testdata/conf.uci gets the configuration and the fingerprint ucode gives it
// (scripts/ucode-golden.sh writes trace.json from dawnline.awg), from the home networks ucode found.
func TestHeldToUcode(t *testing.T) {
text, err := os.ReadFile("testdata/conf.uci")
if err != nil {
t.Fatal(err)
}
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
m := model.Load(c, nil)
raw, err := os.ReadFile("testdata/trace.json")
if err != nil {
t.Fatal(err)
}
var golden struct {
Names map[string]string `json:"names"`
Devices map[string]struct {
Home []string `json:"home"`
Config any `json:"config"`
FP *string `json:"fp"`
} `json:"devices"`
}
if err := json.Unmarshal(raw, &golden); err != nil {
t.Fatal(err)
}
if len(golden.Devices) != len(m.Devices) || len(m.Devices) < 20 {
t.Fatalf("ucode knows %d devices, Go %d", len(golden.Devices), len(m.Devices))
}
ids := make([]string, 0, len(golden.Devices))
for id := range golden.Devices {
ids = append(ids, id)
}
sort.Strings(ids)
homes := 0
for _, id := range ids {
want := golden.Devices[id]
dev := m.Devices[id]
if dev == nil {
t.Fatalf("%s: not in the Go model", id)
}
homes += len(want.Home)
got := ClientConfig(m, dev, want.Home)
var plain any
if got != nil {
b, _ := json.Marshal(got)
json.Unmarshal(b, &plain)
}
if !reflect.DeepEqual(plain, want.Config) {
g, _ := json.MarshalIndent(plain, "", " ")
w, _ := json.MarshalIndent(want.Config, "", " ")
t.Fatalf("%s: the configuration differs\nGo: %s\nucode: %s", id, g, w)
}
fp := Fingerprint(m, dev, want.Home)
if (want.FP == nil) != (fp == "") || (want.FP != nil && *want.FP != fp) {
t.Fatalf("%s: fingerprint %q, ucode has %v", id, fp, want.FP)
}
}
if homes == 0 {
t.Fatal("no device of the trace reaches a home network: the trace was made without its interfaces")
}
for name, want := range golden.Names {
if got := TunnelName(name); got != want {
t.Errorf("the tunnel's name of %q: %q, ucode has %q", name, got, want)
}
}
}
+311
View File
@@ -0,0 +1,311 @@
# Configurations to hold to ucode: plain WireGuard, the junk-only disguise, the 3.1 one; home-only people with
# and without home names, internet, both; a device with its own local network outside; names in Cyrillic.
config settings 'settings'
option portal_host 'vpn.example.com'
option home_dns_fallback '9.9.9.9'
config server 'plain'
option name 'Дом на море'
option ifname 'awgplain'
option port '51820'
option subnet '10.9.0.0/24'
option public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='
option auto_lan '0'
list lan_device 'dlhome'
list lan_device 'dlguest'
config server 'junk'
option name 'con'
option ifname 'awgjunk'
option port '443'
option subnet '10.10.0.0/29'
option endpoint '2001:db8::7'
option public_key 'AuTb2wZ8C1032Ksn76nFgXzOJgT86OgEVbEE7ClpNy8='
option mesh 'off'
option keepalive '25'
option jc '4'
option jmin '40'
option jmax '200'
option s1 '15'
option s2 '68'
option h1 '1407'
option h2 '2'
option h3 '3'
option h4 '4'
list dns '10.10.0.1'
list dns 'fd00::53'
list dns '2606:4700::1111'
list dns '192.168.50.2'
option auto_lan '0'
list lan_device 'dlhome'
config server 'max'
option name 'A very long server name indeed'
option ifname 'awgmax'
option port '8443'
option subnet '172.20.0.0/16'
option public_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='
option keepalive '30'
option mtu '1420'
option jc '10'
option jmin '64'
option jmax '90'
option s4 '24'
option h1 '100-200'
option h2 '300-400'
option h3 '500-600'
option h4 '700-800'
option i1 '<b 0x0102><r 16>'
option header_protection_key 'yvVKA41To0000000000000000000IUdcl4Zi0000000='
option rekey_after_time '100-140'
option auto_lan '0'
config person 'p_net'
option server 'plain'
option name 'Internet'
option access 'wan'
config person 'p_both'
option server 'plain'
option name 'Both'
option access 'both'
config person 'p_lan'
option server 'plain'
option name 'Home'
option access 'lan'
config person 'p_lanhome'
option server 'plain'
option name 'Home names'
option access 'lan'
option dns_mode 'home'
list home_nets 'dlguest'
config person 'j_both'
option server 'junk'
option name 'Junk both'
option access 'both'
config person 'j_lan'
option server 'junk'
option name 'Junk home'
option access 'lan'
option dns_mode 'home'
config person 'm_net'
option server 'max'
option name 'Max'
option access 'wan'
config person 'lost'
option server 'gone'
option name 'Lost'
config device 'd_p_net_0'
option person 'p_net'
option name 'Device 0'
option kind 'phone'
option address '10.9.0.4'
option public_key 'k00'
config device 'd_p_net_1'
option person 'p_net'
option name 'Device 1'
option kind 'computer'
option address '10.9.0.5'
option public_key 'k01'
option lan_bypass '1'
option private_key 'cHJpdmF0ZXByaXZhdGVwcml2YXRlcHJpdmF0ZXByaXY='
option preshared_key 'cHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHM='
config device 'd_p_net_2'
option person 'p_net'
option name 'Device 2'
option kind 'router'
option address '10.9.0.6'
option public_key 'k02'
option mtu '1300'
option keepalive '0'
config device 'd_p_both_0'
option person 'p_both'
option name 'Device 0'
option kind 'phone'
option address '10.9.0.7'
option public_key 'k10'
config device 'd_p_both_1'
option person 'p_both'
option name 'Device 1'
option kind 'computer'
option address '10.9.0.8'
option public_key 'k11'
option lan_bypass '1'
option private_key 'cHJpdmF0ZXByaXZhdGVwcml2YXRlcHJpdmF0ZXByaXY='
option preshared_key 'cHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHM='
config device 'd_p_both_2'
option person 'p_both'
option name 'Device 2'
option kind 'router'
option address '10.9.0.9'
option public_key 'k12'
option mtu '1300'
option keepalive '0'
config device 'd_p_lan_0'
option person 'p_lan'
option name 'Device 0'
option kind 'phone'
option address '10.9.0.10'
option public_key 'k20'
config device 'd_p_lan_1'
option person 'p_lan'
option name 'Device 1'
option kind 'computer'
option address '10.9.0.11'
option public_key 'k21'
option lan_bypass '1'
option private_key 'cHJpdmF0ZXByaXZhdGVwcml2YXRlcHJpdmF0ZXByaXY='
option preshared_key 'cHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHM='
config device 'd_p_lan_2'
option person 'p_lan'
option name 'Device 2'
option kind 'router'
option address '10.9.0.12'
option public_key 'k22'
option mtu '1300'
option keepalive '0'
config device 'd_p_lanhome_0'
option person 'p_lanhome'
option name 'Device 0'
option kind 'phone'
option address '10.9.0.13'
option public_key 'k30'
config device 'd_p_lanhome_1'
option person 'p_lanhome'
option name 'Device 1'
option kind 'computer'
option address '10.9.0.14'
option public_key 'k31'
option lan_bypass '1'
option private_key 'cHJpdmF0ZXByaXZhdGVwcml2YXRlcHJpdmF0ZXByaXY='
option preshared_key 'cHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHM='
config device 'd_p_lanhome_2'
option person 'p_lanhome'
option name 'Device 2'
option kind 'router'
option address '10.9.0.15'
option public_key 'k32'
option mtu '1300'
option keepalive '0'
config device 'd_j_both_0'
option person 'j_both'
option name 'Device 0'
option kind 'phone'
option address '10.10.0.16'
option public_key 'k40'
config device 'd_j_both_1'
option person 'j_both'
option name 'Device 1'
option kind 'computer'
option address '10.10.0.17'
option public_key 'k41'
option lan_bypass '1'
option private_key 'cHJpdmF0ZXByaXZhdGVwcml2YXRlcHJpdmF0ZXByaXY='
option preshared_key 'cHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHM='
config device 'd_j_both_2'
option person 'j_both'
option name 'Device 2'
option kind 'router'
option address '10.10.0.18'
option public_key 'k42'
option mtu '1300'
option keepalive '0'
config device 'd_j_lan_0'
option person 'j_lan'
option name 'Device 0'
option kind 'phone'
option address '10.10.0.19'
option public_key 'k50'
config device 'd_j_lan_1'
option person 'j_lan'
option name 'Device 1'
option kind 'computer'
option address '10.10.0.20'
option public_key 'k51'
option lan_bypass '1'
option private_key 'cHJpdmF0ZXByaXZhdGVwcml2YXRlcHJpdmF0ZXByaXY='
option preshared_key 'cHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHM='
config device 'd_j_lan_2'
option person 'j_lan'
option name 'Device 2'
option kind 'router'
option address '10.10.0.21'
option public_key 'k52'
option mtu '1300'
option keepalive '0'
config device 'd_m_net_0'
option person 'm_net'
option name 'Device 0'
option kind 'phone'
option address '172.20.0.22'
option public_key 'k60'
config device 'd_m_net_1'
option person 'm_net'
option name 'Device 1'
option kind 'computer'
option address '172.20.0.23'
option public_key 'k61'
option lan_bypass '1'
option private_key 'cHJpdmF0ZXByaXZhdGVwcml2YXRlcHJpdmF0ZXByaXY='
option preshared_key 'cHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHM='
config device 'd_m_net_2'
option person 'm_net'
option name 'Device 2'
option kind 'router'
option address '172.20.0.24'
option public_key 'k62'
option mtu '1300'
option keepalive '0'
config device 'd_lost_0'
option person 'lost'
option name 'Device 0'
option kind 'phone'
option address '10.99.0.25'
option public_key 'k70'
config device 'd_lost_1'
option person 'lost'
option name 'Device 1'
option kind 'computer'
option address '10.99.0.26'
option public_key 'k71'
option lan_bypass '1'
option private_key 'cHJpdmF0ZXByaXZhdGVwcml2YXRlcHJpdmF0ZXByaXY='
option preshared_key 'cHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHNrcHM='
config device 'd_lost_2'
option person 'lost'
option name 'Device 2'
option kind 'router'
option address '10.99.0.27'
option public_key 'k72'
option mtu '1300'
option keepalive '0'
File diff suppressed because it is too large. Load diff
+334
View File
@@ -0,0 +1,334 @@
package health
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/disguise"
"github.com/tickcount/luci-app-dawnline/backend/endpoint"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// ModelErrors is what is wrong with people and devices, as sentences in t's language (dawnline.core
// model_errors): what the sync would have to leave out, or what points at something that does not exist.
// Empty when the settings are consistent.
func ModelErrors(m *model.Model, t *i18n.Catalog) []string {
errs := []string{}
codes := map[string]string{}
for _, id := range m.PersonIDs() {
p := m.People[id]
if p.Server != "" && m.Servers[p.Server] == nil {
errs = append(errs, fmt.Sprintf(t.T("Person %s: server %s does not exist."), p.Name, p.Server))
}
if p.Circle != "" && m.Circles[p.Circle] == nil {
errs = append(errs, fmt.Sprintf(t.T("Person %s: circle %s does not exist."), p.Name, p.Circle))
}
for _, sid := range p.Services {
if m.Services[sid] == nil {
errs = append(errs, fmt.Sprintf(t.T("Person %s: home service %s does not exist."), p.Name, sid))
}
}
if p.Code != "" {
if other, twice := codes[p.Code]; twice {
errs = append(errs, fmt.Sprintf(t.T("Person %s: has the same personal link as %s."), p.Name, other))
}
codes[p.Code] = p.Name
}
}
type holder struct{ device, person string }
addrs := map[string]holder{}
for _, id := range m.DeviceIDs() {
d := m.Devices[id]
p := m.People[d.Person]
if p == nil {
errs = append(errs, fmt.Sprintf(t.T("Device %s: person %s does not exist."), d.Name, d.Person))
continue
}
say := func(msg string, args ...any) {
errs = append(errs, fmt.Sprintf(t.T(msg), append([]any{d.Name, p.Name}, args...)...))
}
if !awg.ValidKey(d.PublicKey) {
say("Device %s of %s: the public key is not a key.")
}
if d.PrivateKey != "" && !awg.ValidKey(d.PrivateKey) {
say("Device %s of %s: the private key is not a key.")
}
if d.PresharedKey != "" && !awg.ValidKey(d.PresharedKey) {
say("Device %s of %s: the preshared key is not a key.")
}
// its own link values (0 / nil: the server's); one out of range is never used
if d.MTU != 0 && (d.MTU < 1280 || d.MTU > 1500) {
say("Device %s of %s: MTU %d is not between 1280 and 1500.", d.MTU)
}
if d.Keepalive != nil && (*d.Keepalive < 0 || *d.Keepalive > 600) {
say("Device %s of %s: keep-alive %d is not between 0 and 600 seconds.", *d.Keepalive)
}
n, ok := ipnet.IP(d.Address)
if !ok {
if d.Address != "" {
say("Device %s of %s: address %s is not a plain IPv4 address.", d.Address)
} else {
say("Device %s of %s: address (none) is not a plain IPv4 address.")
}
continue
}
srv := m.Servers[p.Server]
// AmneziaWG ignores a peer with the interface's own key
if srv != nil && srv.PublicKey != "" && d.PublicKey == srv.PublicKey {
say("Device %s of %s: the public key is the server's own.")
}
if srv != nil {
if net, ok := ipnet.Subnet(srv.Subnet); ok {
last := uint64(net.Base) + net.Size() - 2
if n < net.First() || uint64(n) > last {
say("Device %s of %s: address %s is outside %s's tunnel %s.", d.Address, srv.Name, srv.Subnet)
} else if n == net.First() {
say("Device %s of %s: address %s is the router's own address on %s.", d.Address, srv.Name)
}
}
}
key := p.Server + " " + d.Address
if o, taken := addrs[key]; taken {
say("Device %s of %s: address %s is also used by device %s of %s.", d.Address, o.device, o.person)
}
addrs[key] = holder{d.Name, p.Name}
}
for _, id := range m.CircleIDs() {
if c := m.Circles[id]; m.Servers[c.Server] == nil {
errs = append(errs, fmt.Sprintf(t.T("Circle %s: server %s does not exist."), c.Name, c.Server))
}
}
return errs
}
// TunnelDevices is the devices the sync loads into a server's tunnel, in the settings' order
// (dawnline.awg tunnel_devices): those of people who are active or held only by a limit the portal
// explains, that are on, with a key and an address, and not the server's own key. states is each person's
// state (package state).
func TunnelDevices(m *model.Model, sid string, states map[string]string) []*model.Device {
out := []*model.Device{}
own := ""
if srv := m.Servers[sid]; srv != nil {
own = srv.PublicKey
}
for _, id := range m.DeviceIDs() {
d := m.Devices[id]
p := m.People[d.Person]
// turned off, or a router its owner asked for that the admin has not taken
if p == nil || p.Server != sid || d.Disabled || d.Waiting {
continue
}
if s := states[p.ID]; s != "active" && s != "quota" {
continue
}
if _, ok := ipnet.IP(d.Address); !ok || !awg.ValidKey(d.PublicKey) || (own != "" && d.PublicKey == own) {
continue
}
out = append(out, d)
}
return out
}
// PeerDiff is how the peers read on a tunnel differ from the devices that belong there, counted by key.
func PeerDiff(devs []*model.Device, peers map[string]awg.Peer, ifname string) Peers {
want := map[string]bool{}
for _, d := range devs {
want[d.PublicKey] = true
}
p := Peers{Expected: len(devs)}
found := 0
for k, peer := range peers {
if peer.Ifname != ifname {
continue
}
p.Loaded++
if want[k] {
found++
} else {
p.Extra++
}
}
p.Missing = len(want) - found
return p
}
// Divert is a policy rule that sends marked traffic into a table with `local default`, the way transparent
// proxies capture the router's own traffic.
type Divert struct {
Pref int
Mark, Mask uint32
Table string
}
const markAll = 0xffffffff
var (
markRule = regexp.MustCompile(`^(\d+):.*fwmark 0x([0-9a-f]+)(/0x([0-9a-f]+))?.* lookup (\S+)`)
localDefault = regexp.MustCompile(`(^|\n)local default`)
)
// ProxyDiverts finds the diverts, one a mark and mask (dawnline.fw proxy_diverts). rules is `ip <family>
// rule show` and table `ip <family> route show table <name>`, for "-4" and "-6".
func ProxyDiverts(rules func(family string) string, table func(family, name string) string) []Divert {
out := []Divert{}
seen := map[string]bool{}
for _, fam := range []string{"-4", "-6"} {
for _, line := range strings.Split(rules(fam), "\n") {
m := markRule.FindStringSubmatch(line)
if m == nil {
continue
}
pref, _ := strconv.Atoi(m[1])
if pref >= 32766 {
continue
}
mark, _ := strconv.ParseUint(m[2], 16, 32)
mask := uint64(markAll)
if m[4] != "" {
mask, _ = strconv.ParseUint(m[4], 16, 32)
}
key := fmt.Sprintf("%d/%d", mark, mask)
if seen[key] || !localDefault.MatchString(table(fam, m[5])) {
continue
}
seen[key] = true
out = append(out, Divert{pref, uint32(mark), uint32(mask), m[5]})
}
}
return out
}
// String is a divert as `ip rule` shows it, for people: "pref 1000 fwmark 0x1 → table 100".
func (d Divert) String() string {
mask := ""
if d.Mask != markAll {
mask = fmt.Sprintf("/0x%x", d.Mask)
}
return fmt.Sprintf("pref %d fwmark 0x%x%s → table %s", d.Pref, d.Mark, mask, d.Table)
}
// ReplyScript is the table that keeps answers from Dawnline's ports out of the diverts, as dawnline.fw
// writes it, or "" when there is nothing to keep out of them.
func ReplyScript(m *model.Model, diverts []Divert) string {
var tcp, udp []int
add := func(list []int, n int) []int {
for _, x := range list {
if x == n {
return list
}
}
if n < 1 || n > 65535 {
return list
}
return append(list, n)
}
if s := m.Settings; s.PortalEnabled {
tcp = add(tcp, s.PortalPort)
for _, h := range append([]string{s.PortalHost}, s.PortalAlt...) {
if _, ip := ipnet.IP(h); ip {
tcp = add(tcp, s.PortalPort+1)
break
}
}
}
for _, id := range m.ServerIDs() {
if srv := m.Servers[id]; srv.PrivateKey != "" && srv.Enabled {
udp = add(udp, srv.Port)
}
}
if len(diverts) == 0 || (len(tcp) == 0 && len(udp) == 0) {
return ""
}
t := []string{"table inet dawnline_reply {", "\tchain out {", "\t\ttype route hook output priority filter + 10; policy accept;"}
for _, proto := range []struct {
name string
ports []int
}{{"tcp", tcp}, {"udp", udp}} {
if len(proto.ports) == 0 {
continue
}
var ports []string
for _, p := range proto.ports {
ports = append(ports, strconv.Itoa(p))
}
for _, d := range diverts {
// only the divert's bits: other marks on the packet (multi-WAN) stay
rule := fmt.Sprintf("meta mark 0x%x meta mark set 0x0", d.Mark)
if d.Mask != markAll {
rule = fmt.Sprintf("meta mark & 0x%x == 0x%x meta mark set meta mark & 0x%x", d.Mask, d.Mark, ^d.Mask)
}
t = append(t, "\t\t"+proto.name+" sport { "+strings.Join(ports, ", ")+" } "+rule)
}
}
return strings.Join(append(t, "\t}", "}"), "\n") + "\n"
}
// ServerErrors is what is wrong with a server's definition against the rest of the router
// (dawnline.core server_errors): its interface name, port, tunnel network against `others`, public address.
func ServerErrors(srv *model.Server, others []string, t *i18n.Catalog) []string {
errs := []string{}
if !model.ValidIfname(srv.Ifname) {
errs = append(errs, t.T("Interface name: 1-12 lowercase letters, digits or _."))
}
if srv.Port < 1 || srv.Port > 65535 {
errs = append(errs, t.T("Port must be 1-65535."))
}
if c, ok := ipnet.Subnet(srv.Subnet); !ok {
errs = append(errs, t.T("Subnet must look like 10.77.0.0/24."))
} else {
for _, o := range others {
if oc, ok := ipnet.Subnet(o); ok && overlap(c, oc) {
errs = append(errs, fmt.Sprintf(t.T("Subnet overlaps %s."), o))
}
}
}
if srv.Endpoint != "" && !endpoint.ValidHost(srv.Endpoint) {
errs = append(errs, t.T("Endpoint must be a hostname or IP address."))
}
return errs
}
// ConfigErrors is what is wrong with settings about to replace the current ones (a restore, an import):
// people and devices consistent (ModelErrors), and each server with a valid interface name that is its
// own and not one of the router's (`network`: the router's network configuration), a tunnel network that
// overlaps neither the router's (`foreign`) nor an earlier server's, a port of its own and disguise values
// AmneziaWG takes. Empty when they can be used. dawnline.backup config_errors.
func ConfigErrors(m *model.Model, t *i18n.Catalog, network *uci.Config, foreign []string) []string {
errs := ModelErrors(m, t)
ifnames, ports := map[string]string{}, map[int]string{}
var subnets []string
for _, id := range m.ServerIDs() {
srv := m.Servers[id]
for _, e := range append(ServerErrors(srv, append(append([]string{}, foreign...), subnets...), t), disguise.Unsafe(srv.Obf)...) {
errs = append(errs, fmt.Sprintf(t.T("Server %s: %s"), srv.Name, e))
}
if other, used := ifnames[srv.Ifname]; used {
errs = append(errs, fmt.Sprintf(t.T("Server %s: interface %s is also used by %s."), srv.Name, srv.Ifname, other))
}
ifnames[srv.Ifname] = srv.Name
own := srv.Ifname == "dlx0" || srv.Ifname == "dll0"
if network != nil {
if s := network.Section(srv.Ifname); s != nil {
_, ours := s.Values["_dawnline"]
own = own || s.Type != "interface" || !ours
}
}
if own {
errs = append(errs, fmt.Sprintf(t.T("Server %s: %s is one of the router's own network interfaces."), srv.Name, srv.Ifname))
}
if other, used := ports[srv.Port]; used {
errs = append(errs, fmt.Sprintf(t.T("Server %s: UDP %d is also used by %s."), srv.Name, srv.Port, other))
}
ports[srv.Port] = srv.Name
if srv.Subnet != "" {
subnets = append(subnets, srv.Subnet)
}
}
return errs
}
+132
View File
@@ -0,0 +1,132 @@
package health
import (
"encoding/json"
"os"
"reflect"
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/state"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/usage"
)
// What is wrong with testdata/broken.uci, who is loaded into each tunnel and how made-up tunnels stand
// against that, as ucode says (testdata/trace.uc, scripts/ucode-golden.sh).
func TestBrokenSettingsAsUcode(t *testing.T) {
raw, err := os.ReadFile("testdata/trace.json")
if err != nil {
t.Fatal(err)
}
var trace struct {
TS int64 `json:"ts"`
Errors []string `json:"errors"`
States map[string]string `json:"states"`
Tunnels map[string][]string `json:"tunnels"`
Diffs []struct {
Server string `json:"server"`
Loaded []string `json:"loaded"`
Diff struct{ Loaded, Missing, Extra int }
} `json:"diffs"`
}
if err := json.Unmarshal(raw, &trace); err != nil {
t.Fatal(err)
}
text, _ := os.ReadFile("testdata/broken.uci")
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
m := model.Load(c, nil)
if got := ModelErrors(m, nil); !reflect.DeepEqual(got, trace.Errors) || len(got) < 12 {
t.Errorf("what is wrong\n go:\n %s\n ucode:\n %s", strings.Join(got, "\n "), strings.Join(trace.Errors, "\n "))
}
states := map[string]string{}
for id, p := range m.People {
states[id] = state.Of(m, p, usage.Empty(), trace.TS)
}
if !reflect.DeepEqual(states, trace.States) {
t.Fatalf("states %v, ucode %v", states, trace.States)
}
loaded := 0
for sid, want := range trace.Tunnels {
got := []string{}
for _, d := range TunnelDevices(m, sid, states) {
got = append(got, d.ID)
}
loaded += len(got)
if !reflect.DeepEqual(got, want) {
t.Errorf("tunnel %s holds %v, ucode %v", sid, got, want)
}
}
if loaded == 0 || len(trace.Diffs) < 8 {
t.Fatal("the golden loads nobody")
}
for i, d := range trace.Diffs {
srv := m.Servers[d.Server]
peers := map[string]awg.Peer{"YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYA=": {Ifname: "another"}}
for _, k := range d.Loaded {
peers[k] = awg.Peer{Ifname: srv.Ifname}
}
got := PeerDiff(TunnelDevices(m, d.Server, states), peers, srv.Ifname)
if got.Loaded != d.Diff.Loaded || got.Missing != d.Diff.Missing || got.Extra != d.Diff.Extra {
t.Errorf("diff %d: %+v, ucode %+v", i, got, d.Diff)
}
}
}
func TestProxiesAndTheReplyTable(t *testing.T) {
rules := map[string]string{
"-4": "0:\tfrom all lookup local\n1000:\tfrom all fwmark 0x1 lookup 100\n1001:\tfrom all fwmark 0x1 lookup 100\n2000:\tfrom all fwmark 0x200/0xff00 lookup 200\n3000:\tfrom all fwmark 0x7 lookup 300\n32766:\tfrom all fwmark 0x9 lookup main\n",
"-6": "1000:\tfrom all fwmark 0x1 lookup 100\n",
}
tables := map[string]string{"100": "local default dev lo scope host\n", "200": "10.0.0.0/8 dev x\nlocal default dev lo\n", "300": "default via 10.0.0.1 dev wan\n"}
d := ProxyDiverts(func(f string) string { return rules[f] }, func(_, name string) string { return tables[name] })
var said []string
for _, x := range d {
said = append(said, x.String())
}
if strings.Join(said, "; ") != "pref 1000 fwmark 0x1 → table 100; pref 2000 fwmark 0x200/0xff00 → table 200" {
t.Fatalf("diverts: %v", said)
}
c, _ := uci.Parse("config settings 'settings'\n\toption portal_enabled '1'\n\toption portal_port '4500'\n\tlist portal_alt '203.0.113.7'\n" +
"config server 'home'\n\toption port '4500'\n\toption private_key 'k'\nconfig server 'cabin'\n\toption port '4600'\n\toption private_key 'k'\n\toption enabled '0'\n")
m := model.Load(c, nil)
want := "table inet dawnline_reply {\n\tchain out {\n\t\ttype route hook output priority filter + 10; policy accept;\n" +
"\t\ttcp sport { 4500, 4501 } meta mark 0x1 meta mark set 0x0\n\t\ttcp sport { 4500, 4501 } meta mark & 0xff00 == 0x200 meta mark set meta mark & 0xffff00ff\n" +
"\t\tudp sport { 4500 } meta mark 0x1 meta mark set 0x0\n\t\tudp sport { 4500 } meta mark & 0xff00 == 0x200 meta mark set meta mark & 0xffff00ff\n\t}\n}\n"
if got := ReplyScript(m, d); got != want {
t.Fatalf("the reply table:\n%s", got)
}
if ReplyScript(m, nil) != "" {
t.Fatal("a reply table with no proxy to keep out of")
}
}
// Settings whose servers are wrong in every way: what a restore says of them, as ucode says it.
func TestConfigErrorsAsUcode(t *testing.T) {
raw, _ := os.ReadFile("testdata/trace.json")
var trace struct {
Network string `json:"network"`
Errors []string `json:"config_errors"`
}
if err := json.Unmarshal(raw, &trace); err != nil {
t.Fatal(err)
}
text, _ := os.ReadFile("testdata/servers.uci")
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
network, err := uci.Parse(trace.Network)
if err != nil {
t.Fatal(err)
}
got := ConfigErrors(model.Load(c, nil), nil, network, nil)
if !reflect.DeepEqual(got, trace.Errors) || len(got) < 12 {
t.Errorf("what a restore says\n go:\n %s\n ucode:\n %s", strings.Join(got, "\n "), strings.Join(trace.Errors, "\n "))
}
}
+576
View File
@@ -0,0 +1,576 @@
// Package health is Health's checks, as dawnline.health health() makes them: each named for the healthy
// state ("Clock is set"), a failed one also with the problem in words for notices, in the reader's
// language. Pure: Inputs is everything read on the router, gathered by the caller, so every check can be
// made up in a test. A check whose input is not given yet (nil) is left out.
package health
import (
"fmt"
"strconv"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/endpoint"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// Check is one result. Fix names an action (apply, sync, restart, netrestart, ifup:<name>, install:<feature>,
// updates) or is nil; Problem is the failed check in words, nil for one that passed.
type Check struct {
ID string `json:"id"`
Title string `json:"title"`
Status string `json:"status"`
Detail string `json:"detail"`
Fix *string `json:"fix"`
Problem *string `json:"problem"`
}
func check(id, title, status, detail, fix, problem string) Check {
c := Check{ID: id, Title: title, Status: status, Detail: detail}
if fix != "" {
c.Fix = &fix
}
if status == "warn" || status == "error" {
if problem == "" {
problem = title
}
c.Problem = &problem
}
return c
}
// Iface is what netifd says of an interface.
type Iface struct {
Exists, Up bool
// Errors is its error codes (INVALID_PROTO).
Errors []string
}
// Peers is how a tunnel's loaded devices stand against those it should hold.
type Peers struct {
Expected, Missing, Extra, Loaded int
}
// Replies: whether answers from our ports go straight out, and the proxies' diverts that would swallow them.
type Replies struct {
OK bool
Diverts []string
}
// Subnet is an IPv4 network of a netifd interface.
type Subnet struct {
Iface, CIDR string
}
// Portal is what was read for the portal's checks.
type Portal struct {
Owners []string
Listening bool
// IPs is where its address points; WAN the router's.
IPs []string
WAN string
// Cert is the certificate in use; Valid: for more than 14 days; Names: the DNS names it covers.
Cert string
SelfSigned bool
Valid bool
Names []string
}
// Inputs is everything the checks are made from.
type Inputs struct {
M *model.Model
T *i18n.Catalog
// AmneziaWG: its state after trying to load the module (missing packages, loaded, handler), the firmware
// the installed build was for when that is not this one, why the module does not load, an update that
// waits for a reboot.
Missing []string
Loaded, Handler bool
KernelWas, KernelIs string
WhyNotLoaded string
WaitsForReboot bool
Year int
AgentRunning bool
// FreeKB is the overlay's free space (nil: unknown).
FreeKB *int64
NftLoaded bool
PolicyRouting []string
// ModelErrors is what is wrong with people and devices (nil: not checked yet).
ModelErrors func() []string
Firewall, Network *uci.Config
Iface func(ifname string) Iface
// Unread tells a tunnel that could not be read; Peers how its devices stand (nil: not checked yet).
Unread func(ifname string) bool
Peers func(srv *model.Server) Peers
Replies *Replies
// Subnets is the router's networks; HomeNets a server's home networks; Endpoint what its public address reads as.
Subnets []Subnet
HomeNets func(srv *model.Server) []string
Endpoint func(srv *model.Server, host string) endpoint.Reads
// LikeHome is the home-like path's state (dawnline.loop status); Needs the packages each feature misses.
LikeHome map[string]any
Needs map[string][]string
// The resolver for Home-only devices: its configuration's age in seconds (nil: none), whether it runs.
ResolverConfAge *int64
ResolverRunning bool
Portal func() Portal
// The networks behind people's routers: Behind, those in effect on this router by device (nil: not
// checked); Own, the router's own networks, to name what a network shares addresses with; RouteError,
// what the last sync said of a route that would not go in ("": none).
Behind map[string][]string
Own []string
RouteError string
}
// The /24s home routers come with out of the box: a home network on one of them is very likely the same as
// the network a person connects from.
var commonHome = []string{"192.168.0.0/24", "192.168.1.0/24", "192.168.2.0/24", "192.168.3.0/24", "192.168.8.0/24", "192.168.10.0/24", "192.168.31.0/24",
"192.168.50.0/24", "192.168.88.0/24", "192.168.100.0/24", "192.168.178.0/24", "10.0.0.0/24", "10.1.1.0/24"}
func overlap(a, b ipnet.Net) bool {
return uint64(a.Base) < uint64(b.Base)+b.Size() && uint64(b.Base) < uint64(a.Base)+a.Size()
}
// CommonHome is the home networks that overlap a common out-of-the-box subnet.
func CommonHome(nets []string) []string {
out := []string{}
for _, n := range nets {
a, ok := ipnet.Subnet(n)
if !ok {
continue
}
for _, c := range commonHome {
if b, _ := ipnet.Subnet(c); overlap(a, b) {
out = append(out, n)
break
}
}
}
return out
}
func has(list []string, v string) bool {
for _, x := range list {
if x == v {
return true
}
}
return false
}
func (in *Inputs) noProto() string {
return in.T.T("netifd doesn't know the amneziawg protocol (AmneziaWG missing, or the network wasn't restarted after installing it).")
}
func (in *Inputs) awg() Check {
t := in.T
title := t.T("AmneziaWG installed")
if n := len(in.Missing); n > 0 {
// the module was built for the kernel of the firmware it was installed on
if in.KernelWas != "" && has(in.Missing, "kmod-amneziawg") {
return check("awg", title, "error",
fmt.Sprintf(t.T("The firmware changed (kernel %s → %s): the AmneziaWG build installed for it is gone. Install the build for this firmware."), in.KernelWas, in.KernelIs),
"install:awg", t.T("AmneziaWG needs a build for the new firmware"))
}
return check("awg", title, "error", fmt.Sprintf(t.N(int64(n), "%s is missing.", "%s are missing."), strings.Join(in.Missing, ", ")), "install:awg", t.T("AmneziaWG isn’t installed"))
}
if !in.Loaded {
detail := t.T("The kernel module doesn't load. Reinstall a build for this kernel in Components.")
if in.WhyNotLoaded != "" {
detail = fmt.Sprintf(t.T("The kernel module doesn't load: %s Reinstall a build for this kernel in Components."), in.WhyNotLoaded)
}
return check("awg", title, "error", detail, "updates", t.T("AmneziaWG’s kernel module doesn’t load"))
}
if !in.Handler {
return check("awg", title, "error", in.noProto(), "netrestart", t.T("The network doesn’t know AmneziaWG yet"))
}
if in.WaitsForReboot {
return check("awg", title, "warn", t.T("The AmneziaWG update is installed and starts after a reboot."), "", t.T("The AmneziaWG update waits for a reboot"))
}
return check("awg", title, "ok", t.T("Kernel module, awg tool and the netifd protocol are present."), "", "")
}
func (in *Inputs) reply(id, title, problem, proto string, port int) (Check, bool) {
r := in.Replies
if r == nil {
return Check{}, false
}
t := in.T
via, from := strings.Join(r.Diverts, ", "), strings.ToUpper(proto)+" "+strconv.Itoa(port)
switch {
case !r.OK:
return check(id, title, "error", fmt.Sprintf(t.T("Answers from %s are swallowed by a proxy on the router (%s): clients get no reply."), from, via), "apply", problem), true
case len(r.Diverts) > 0:
return check(id, title, "ok", fmt.Sprintf(t.T("Answers from %s go straight out. A proxy on the router diverts its own traffic (%s); these answers skip it."), from, via), "", problem), true
}
return check(id, title, "ok", fmt.Sprintf(t.T("Answers from %s go straight out."), from), "", problem), true
}
func sectionIs(c *uci.Config, name, kind string) bool {
if c == nil {
return false
}
s := c.Section(name)
return s != nil && s.Type == kind
}
// server is the checks of one server's tunnel.
func (in *Inputs) server(id string, srv *model.Server, label string) []Check {
t, m := in.T, in.M
out := []Check{}
st := in.Iface(srv.Ifname)
running, down := t.T("Tunnel running")+label, t.T("AmneziaWG is down")+label
switch {
case !st.Up && has(st.Errors, "INVALID_PROTO"):
out = append(out, check("up_"+id, running, "error", in.noProto(), "netrestart", down))
case st.Up:
out = append(out, check("up_"+id, running, "ok", fmt.Sprintf(t.T("%s is up, listening on UDP %d."), srv.Ifname, srv.Port), "", down))
case !st.Exists:
out = append(out, check("up_"+id, running, "error", fmt.Sprintf(t.T("%s does not exist."), srv.Ifname), "apply", down))
case len(st.Errors) > 0:
out = append(out, check("up_"+id, running, "error", fmt.Sprintf(t.T("%s is down: %s."), srv.Ifname, strings.Join(st.Errors, ", ")), "ifup:"+srv.Ifname, down))
default:
out = append(out, check("up_"+id, running, "error", fmt.Sprintf(t.T("%s is down."), srv.Ifname), "ifup:"+srv.Ifname, down))
}
// the devices the sync loads, by the same choice the agent compares with every minute
loaded := t.T("Devices loaded") + label
if st.Up && in.Unread != nil && in.Unread(srv.Ifname) {
out = append(out, check("peers_"+id, loaded, "warn", t.T("The tunnel could not be read, so its devices could not be checked."), "",
t.T("The devices in AmneziaWG could not be checked")+label))
} else if st.Up && in.Peers != nil {
p := in.Peers(srv)
bad := t.T("Not every device is loaded into AmneziaWG") + label
switch {
case p.Missing == 0 && p.Extra == 0:
out = append(out, check("peers_"+id, loaded, "ok", fmt.Sprintf(t.N(int64(p.Loaded), "%d device is loaded in the tunnel.", "%d devices are loaded in the tunnel."), p.Loaded), "", bad))
case p.Extra != 0:
out = append(out, check("peers_"+id, loaded, "warn", fmt.Sprintf(t.N(int64(p.Expected), "%d of %d device missing, %d loaded that should not be.",
"%d of %d devices missing, %d loaded that should not be."), p.Missing, p.Expected, p.Extra), "sync", bad))
default:
out = append(out, check("peers_"+id, loaded, "warn", fmt.Sprintf(t.N(int64(p.Expected), "%d of %d device missing.", "%d of %d devices missing."), p.Missing, p.Expected), "sync", bad))
}
}
rules := sectionIs(in.Firewall, "dawnline_"+id+"_port", "rule") && sectionIs(in.Firewall, "dawnline_"+id+"_zone", "zone")
if rules {
out = append(out, check("fw_"+id, t.T("Firewall rules")+label, "ok", fmt.Sprintf(t.T("Zone %s and the WAN rule for UDP %d exist."), srv.Ifname, srv.Port), "", ""))
} else {
out = append(out, check("fw_"+id, t.T("Firewall rules")+label, "error", t.T("Firewall zone or WAN rule is missing."), "apply", t.T("AmneziaWG firewall rules are missing")+label))
}
if c, ok := in.reply("reply_"+id, t.T("Answers reach devices")+label, t.T("AmneziaWG answers may not reach devices")+label, "udp", srv.Port); ok {
out = append(out, c)
}
// port collisions with other tunnels
var clash []string
if in.Network != nil {
for _, s := range in.Network.OfType("interface") {
proto, _ := s.Get("proto")
lp, _ := s.Get("listen_port")
if n, _ := strconv.Atoi(strings.TrimSpace(lp)); s.Name != srv.Ifname && (proto == "amneziawg" || proto == "wireguard") && n == srv.Port {
clash = append(clash, s.Name)
}
}
}
if len(clash) > 0 {
out = append(out, check("port_"+id, t.T("Port is free")+label, "error", fmt.Sprintf(t.T("UDP %d is also used by %s."), srv.Port, strings.Join(clash, ", ")), "", t.T("AmneziaWG port is taken")+label))
}
var overlaps []string
if sc, ok := ipnet.Subnet(srv.Subnet); ok {
for _, o := range in.Subnets {
// every network of the router but this server's own tunnel's
if o.Iface == srv.Ifname {
continue
}
if oc, ok := ipnet.Subnet(o.CIDR); ok && overlap(sc, oc) {
overlaps = append(overlaps, o.Iface+" ("+o.CIDR+")")
}
}
}
if len(overlaps) > 0 {
out = append(out, check("subnet_"+id, t.T("Subnet is unique")+label, "error", fmt.Sprintf(t.T("%s overlaps %s."), srv.Subnet, strings.Join(overlaps, ", ")), "", t.T("AmneziaWG subnet overlaps another network")+label))
} else {
out = append(out, check("subnet_"+id, t.T("Subnet is unique")+label, "ok", fmt.Sprintf(t.T("%s does not overlap other networks."), srv.Subnet), "", ""))
}
// whether the home networks are on a common subnet, while anyone on the server has home access
reach := false
for _, p := range m.People {
reach = reach || (p.Server == id && (p.Access == "lan" || p.Access == "both"))
}
if nets := in.HomeNets(srv); reach && len(nets) > 0 {
c := check("homenet_"+id, t.T("Home network addresses")+label, "ok",
fmt.Sprintf(t.T("%s is not a common home subnet: the networks people connect from rarely use the same addresses."), strings.Join(nets, ", ")), "", "")
if bad := CommonHome(nets); len(bad) > 0 {
c.Status = "warn"
c.Detail = fmt.Sprintf(t.T("%s is one of the most common home subnets. On a network with the same addresses, people with home access reach your home instead of their own: their printer or TV there is out of reach while connected, with LAN Bypass too. A rarer subnet for your home network avoids it, for example 10.37.12.0/24."), strings.Join(bad, ", "))
}
// advice, not a fault: no notice elsewhere
out = append(out, c)
}
if c, has := in.behind(id, label); has {
out = append(out, c)
}
return out
}
// behind is the check of the networks behind a server's routers, when any router has one written: each is
// in effect (a peer of the tunnel, a route, a place in the access table), or the check says which is not
// and what it shares addresses with; a route the router would not take is said in its own words.
func (in *Inputs) behind(id, label string) (Check, bool) {
m, t := in.M, in.T
if in.Behind == nil {
return Check{}, false
}
title := t.T("Networks behind routers") + label
var bad []string
written, routed := 0, 0
for _, did := range m.DeviceIDs() {
d := m.Devices[did]
p := m.People[d.Person]
if p == nil || p.Server != id || d.Kind != "router" || d.Waiting || len(d.Behind) == 0 {
continue
}
written += len(d.Behind)
routed += len(in.Behind[did])
who := p.Name + " · " + d.Name
for _, s := range d.Behind {
n, why := model.BehindNet(s)
switch {
case why != "":
bad = append(bad, fmt.Sprintf(t.T("%s behind %s is not a network a router may have behind it."), s, who))
case has(in.Behind[did], n.String()):
default:
c := m.BehindClash(n, did, in.Own)
switch {
case c == nil:
bad = append(bad, fmt.Sprintf(t.T("%s behind %s is written twice, or is one more than a router may have."), n.String(), who))
case c.Server != "":
bad = append(bad, fmt.Sprintf(t.T("%s behind %s shares addresses with the tunnel network of %s (%s)."), n.String(), who, m.Servers[c.Server].Name, c.Net))
case c.Device != "":
bad = append(bad, fmt.Sprintf(t.T("%s behind %s shares addresses with %s behind %s."), n.String(), who, c.Net, m.Devices[c.Device].Name))
default:
bad = append(bad, fmt.Sprintf(t.T("%s behind %s shares addresses with %s, which the router has itself."), n.String(), who, c.Net))
}
}
}
}
switch {
case written == 0:
return Check{}, false
case len(bad) > 0:
return check("sites_"+id, title, "warn", strings.Join(bad, " ")+" "+t.T("Such a network is not in effect: change it in the server's Network settings."), "",
t.T("A network behind a router isn’t in effect")+label), true
case in.RouteError != "":
return check("sites_"+id, title, "error", fmt.Sprintf(t.T("A route to a network behind a router couldn't be set: %s."), in.RouteError), "sync",
t.T("A network behind a router has no route")+label), true
}
return check("sites_"+id, title, "ok",
fmt.Sprintf(t.N(int64(routed), "%d network behind routers is carried by the tunnel.", "%d networks behind routers are carried by the tunnel."), routed), "", ""), true
}
func (in *Inputs) portal() []Check {
t, s := in.T, in.M.Settings
portal, bad := t.T("Personal portal"), t.T("Personal portal isn’t working")
if need := in.Needs["https"]; len(need) > 0 {
return []Check{check("portal", portal, "error", fmt.Sprintf(t.T("HTTPS isn't available on this router: install %s."), strings.Join(need, ", ")), "install:https", bad)}
}
p := in.Portal()
out := []Check{}
switch {
case len(p.Owners) > 0:
out = append(out, check("portal", portal, "error", fmt.Sprintf(t.T("TCP %d is already used by %s. Choose another port in Settings."), s.PortalPort, strings.Join(p.Owners, ", ")), "", bad))
case p.Listening:
out = append(out, check("portal", portal, "ok", fmt.Sprintf(t.T("Listening on TCP %d."), s.PortalPort), "", bad))
default:
out = append(out, check("portal", portal, "error", fmt.Sprintf(t.T("Nothing listens on TCP %d."), s.PortalPort), "restart", bad))
}
if h := s.PortalHost; h == "" {
out = append(out, check("portal_host", t.T("Portal address"), "error", t.T("Set the public hostname in Settings so links can be generated."), "", t.T("Portal address isn’t set")))
} else {
cf := false
for _, ip := range p.IPs {
cf = cf || endpoint.Cloudflare(ip)
}
ips := strings.Join(p.IPs, ", ")
title, problem := t.T("Portal address points here"), t.T("Portal address doesn’t point here")
switch {
case len(p.IPs) == 0:
out = append(out, check("portal_dns", title, "error", fmt.Sprintf(t.T("%s does not resolve."), h), "", problem))
case cf:
out = append(out, check("portal_dns", title, "ok", fmt.Sprintf(t.T("%s goes through Cloudflare; in Russia such connections often stall after ~16 KB."), h), "", problem))
case p.WAN != "" && !has(p.IPs, p.WAN):
out = append(out, check("portal_dns", title, "warn", fmt.Sprintf(t.T("%s points at %s, not this router (%s)."), h, ips, p.WAN), "", problem))
case p.WAN != "":
out = append(out, check("portal_dns", title, "ok", fmt.Sprintf(t.T("%s → %s (this router)."), h, ips), "", problem))
default:
out = append(out, check("portal_dns", title, "ok", h+" → "+ips+".", "", problem))
}
}
if c, ok := in.reply("portal_reply", t.T("Portal answers reach visitors"), t.T("Portal answers may not reach visitors"), "tcp", s.PortalPort); ok {
out = append(out, c)
}
// IP addresses have no certificate: the portal also answers plain HTTP for them
var plain []string
hosts := append([]string{s.PortalHost}, s.PortalAlt...)
for i, h := range hosts {
if _, ip := ipnet.IP(h); ip && h != "" && !has(hosts[:i], h) {
plain = append(plain, h)
}
}
if len(plain) > 0 {
out = append(out, check("portal_http", t.T("Plain HTTP links"), "warn",
fmt.Sprintf(t.T("Links on %s use plain HTTP: the link and the VPN keys in configurations travel unencrypted."), strings.Join(plain, ", ")), "", t.T("Portal links use plain HTTP")))
}
title := t.T("Portal certificate")
if _, ip := ipnet.IP(s.PortalHost); ip {
return append(out, check("cert", title, "skip", t.T("The main address is an IP: its links use plain HTTP."), "", ""))
}
covers := s.PortalHost == "" || p.SelfSigned
for _, n := range p.Names {
covers = covers || endpoint.Covers(n, s.PortalHost)
}
problem := t.T("Portal certificate needs attention")
switch {
case p.SelfSigned && len(in.Needs["acme"]) > 0:
out = append(out, check("cert", title, "warn", t.T("Using a self-signed certificate: browsers will show a warning. ACME can get a free Let’s Encrypt one."), "", problem))
case p.SelfSigned:
out = append(out, check("cert", title, "warn", t.T("Using a self-signed certificate: browsers will show a warning."), "", problem))
case !covers:
out = append(out, check("cert", title, "warn", fmt.Sprintf(t.T("%s does not cover %s (it has %s): browsers will warn."), p.Cert, s.PortalHost, strings.Join(p.Names, ", ")), "", problem))
case p.Valid:
out = append(out, check("cert", title, "ok", fmt.Sprintf(t.T("%s covers %s and is valid for more than 14 days."), p.Cert, s.PortalHost), "", problem))
default:
out = append(out, check("cert", title, "warn", fmt.Sprintf(t.T("%s expires within 14 days."), p.Cert), "", problem))
}
return out
}
// Run makes every check.
func Run(in Inputs) []Check {
t, m := in.T, in.M
out := []Check{}
ids := m.ServerIDs()
if len(ids) > 0 {
out = append(out, in.awg())
}
if in.Year >= 2025 {
out = append(out, check("clock", t.T("Clock is set"), "ok", t.T("End dates and quotas use the router clock."), "", ""))
} else {
out = append(out, check("clock", t.T("Clock is set"), "error", t.T("The router clock looks wrong; end dates will misbehave. Check NTP."), "", t.T("Clock isn’t set")))
}
fix := "restart"
status := "error"
if in.AgentRunning {
fix, status = "", "ok"
}
out = append(out, check("agent", t.T("Background agent running"), status, t.T("Counts traffic and enforces end dates and quotas."), fix, t.T("Background agent isn’t running")))
// a full overlay fails every config write on the router
if in.FreeKB != nil {
free := *in.FreeKB
problem := t.T("The router is running out of flash")
switch {
case free < 2048:
status := "warn"
if free < 512 {
status = "error"
}
mb := strconv.FormatFloat(float64(int64(float64(free)/1024*10))/10, 'g', -1, 64)
out = append(out, check("flash", t.T("Free space on the router"), status,
fmt.Sprintf(t.T("%s MB free: settings may stop saving. Remove old backups (Settings → Backups) or shorten the history."), mb), "", problem))
default:
out = append(out, check("flash", t.T("Free space on the router"), "ok", fmt.Sprintf(t.T("%d MB free on the overlay."), free/1024), "", problem))
}
}
if len(ids) == 0 {
return append(out, check("server", t.T("Server configured"), "warn", t.T("Set a server up in Settings."), "", t.T("No server yet")))
}
if in.NftLoaded {
out = append(out, check("nft", t.T("Access rules loaded"), "ok", t.T("Table inet dawnline enforces who may reach LAN and the internet."), "", ""))
} else {
out = append(out, check("nft", t.T("Access rules loaded"), "error", t.T("Table inet dawnline is missing: no device is loaded into the tunnels until it is back."), "apply", t.T("Access rules aren’t loaded")))
}
likeHome := false
for _, id := range ids {
likeHome = likeHome || (m.Servers[id].LanLike && m.Servers[id].Enabled)
}
if likeHome {
if pr := in.PolicyRouting; len(pr) > 0 {
out = append(out, check("policy", t.T("Internet like a home device not bypassed"), "warn",
fmt.Sprintf(t.T("Policy routing on the router (%s) sends marked traffic straight out, past the home-device path: tunnel traffic it matches skips your LAN's proxies and filters."), strings.Join(pr, "; ")),
"", t.T("Other policy routing bypasses the home-device path")))
} else {
out = append(out, check("policy", t.T("Internet like a home device not bypassed"), "ok", t.T("No other policy routing ahead of the tunnel rules."), "", ""))
}
}
// a device the sync has to leave out: its sentences, how many more, then what it means
if in.ModelErrors != nil {
if data := in.ModelErrors(); len(data) > 0 {
parts := []string{strings.Join(data[:min(len(data), 4)], " ")}
if len(data) > 4 {
parts = append(parts, fmt.Sprintf(t.T("(%d more)"), len(data)-4))
}
parts = append(parts, t.T("Such devices are left out of the tunnels: fix or remove them."))
out = append(out, check("data", t.T("People and devices consistent"), "error", strings.Join(parts, " "), "", t.T("People and devices are inconsistent")))
} else {
out = append(out, check("data", t.T("People and devices consistent"), "ok", t.T("Every device has a key and an address of its own inside its server."), "", ""))
}
}
keyed := false
for _, id := range ids {
srv := m.Servers[id]
keyed = keyed || (srv.Enabled && srv.PrivateKey != "")
label := ""
if len(ids) > 1 {
label = " (" + srv.Name + ")"
}
if !srv.Enabled {
out = append(out, check("srv_"+id, t.T("Server")+label, "skip", t.T("Server is disabled."), "", ""))
continue
}
out = append(out, in.server(id, srv, label)...)
host := m.EndpointHost(srv)
status, text := endpoint.Words(endpoint.Of(host, in.Endpoint(srv, host)), "UDP "+strconv.Itoa(srv.Port), t)
out = append(out, check("ep_"+id, t.T("Public address")+label, status, text, "", t.T("Public address needs attention")+label))
if srv.LanLike {
ls := in.LikeHome
active, bridged := ls["active"] == true, ls["bridged"] == true
title, problem := t.T("Internet like a home device")+label, t.T("Internet like a home device isn’t working")+label
nh := in.Needs["home"]
switch {
case active && bridged:
out = append(out, check("home_"+id, title, "ok", fmt.Sprintf(t.T("Traffic enters %s as %s, so it is handled like any device on your LAN."), ls["dev"], ls["address"]), "", problem))
case len(nh) > 0:
out = append(out, check("home_"+id, title, "warn", fmt.Sprintf(t.T("Needs %s. Internet traffic leaves through WAN directly until then."), strings.Join(nh, ", ")), "install:home", problem))
case active:
out = append(out, check("home_"+id, title, "warn", fmt.Sprintf(t.T("The link is up but not attached to %s."), ls["dev"]), "apply", problem))
default:
// the path's own sentence, then what it means
why, _ := ls["error"].(string)
if ls["error"] == nil {
why = t.T("Not set up.")
}
out = append(out, check("home_"+id, title, "warn", why+" "+t.T("Internet traffic leaves through WAN directly."), "apply", problem))
}
}
}
// while every server is off the resolver only idles on loopback: nothing to check
if keyed {
running := in.ResolverConfAge != nil && in.ResolverRunning
// a new config (a server turned on, the home link up) restarts it: give it a few seconds
restarting := !running && in.ResolverConfAge != nil && *in.ResolverConfAge < 10
title, problem := t.T("DNS for Home-only devices"), t.T("DNS for Home-only devices isn’t working")
switch {
case restarting:
out = append(out, check("resolver", title, "ok", t.T("Restarting with the new settings."), "", problem))
case running && in.LikeHome["active"] == true && in.LikeHome["transit"] != nil && in.LikeHome["transit"] != "":
out = append(out, check("resolver", title, "ok", fmt.Sprintf(t.T("Plain resolver running next to the LAN (%s)."), in.LikeHome["transit"]), "", problem))
case running:
out = append(out, check("resolver", title, "ok", fmt.Sprintf(t.T("Plain resolver on port %d."), 5354), "", problem))
default:
out = append(out, check("resolver", title, "warn", t.T("The resolver is not running; Home-only devices cannot resolve names."), "apply", problem))
}
}
if m.Settings.PortalEnabled {
out = append(out, in.portal()...)
}
return out
}
+282
View File
@@ -0,0 +1,282 @@
package health
import (
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/endpoint"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
const settings = `
config settings 'settings'
option portal_enabled '1'
option portal_port '4500'
option portal_host 'vpn.example.org'
list portal_alt '203.0.113.7'
config server 'home'
option name 'Home'
option ifname 'dawn0'
option port '4500'
option subnet '10.77.0.0/24'
option private_key 'k'
option endpoint 'vpn.example.org'
config server 'cabin'
option name 'Cabin'
option ifname 'dawn1'
option port '4600'
option subnet '10.78.0.0/24'
option enabled '0'
config person 'anna'
option server 'home'
option access 'both'
`
// a router where everything is as it should be
func healthy(t *testing.T) Inputs {
c, err := uci.Parse(settings)
if err != nil {
t.Fatal(err)
}
fw, _ := uci.Parse("config rule 'dawnline_home_port'\nconfig zone 'dawnline_home_zone'\n")
network, _ := uci.Parse("config interface 'dawn0'\n\toption proto 'amneziawg'\n\toption listen_port '4500'\n")
free, age := int64(40960), int64(600)
return Inputs{
M: model.Load(c, nil), Loaded: true, Handler: true, Year: 2027, AgentRunning: true, FreeKB: &free, NftLoaded: true,
ModelErrors: func() []string { return nil }, Firewall: fw, Network: network,
Iface: func(string) Iface { return Iface{Exists: true, Up: true} },
Unread: func(string) bool { return false },
Peers: func(*model.Server) Peers { return Peers{Expected: 2, Loaded: 2} },
Replies: &Replies{OK: true}, Subnets: []Subnet{{"lan", "192.168.77.1/24"}, {"dawn0", "10.77.0.1/24"}},
HomeNets: func(*model.Server) []string { return []string{"192.168.77.0/24"} },
Endpoint: func(*model.Server, string) endpoint.Reads {
return endpoint.Reads{IPs: []string{"203.0.113.7"}, WAN: "203.0.113.7"}
},
LikeHome: map[string]any{"active": true, "bridged": true, "dev": "br-lan", "address": "192.168.77.9", "transit": "169.254.7.2"},
Needs: map[string][]string{}, ResolverConfAge: &age, ResolverRunning: true,
Portal: func() Portal {
return Portal{Listening: true, IPs: []string{"203.0.113.7"}, WAN: "203.0.113.7", Cert: "/etc/ssl/acme/x.crt", Valid: true, Names: []string{"*.example.org"}}
},
}
}
func byID(list []Check) map[string]Check {
out := map[string]Check{}
for _, c := range list {
out[c.ID] = c
}
return out
}
func TestAHealthyRouter(t *testing.T) {
list := Run(healthy(t))
var ids []string
for _, c := range list {
ids = append(ids, c.ID)
// the plain HTTP links are a standing warning, the server turned off is skipped
want := "ok"
switch c.ID {
case "portal_http":
want = "warn"
case "srv_cabin":
want = "skip"
}
if c.Status != want || (c.Status == "ok") != (c.Problem == nil && c.Fix == nil) && c.ID != "srv_cabin" {
t.Errorf("%s: %s %q fix %v problem %v", c.ID, c.Status, c.Detail, c.Fix, c.Problem)
}
}
want := "awg clock agent flash nft policy data up_home peers_home fw_home reply_home subnet_home homenet_home ep_home home_home srv_cabin resolver portal portal_dns portal_reply portal_http cert"
if got := strings.Join(ids, " "); got != want {
t.Fatalf("the checks and their order:\n %s\n %s", got, want)
}
// two servers: each check of a server carries its name
if c := byID(list)["up_home"]; c.Title != "Tunnel running (Home)" || c.Detail != "dawn0 is up, listening on UDP 4500." {
t.Fatalf("a server's check: %+v", c)
}
}
func TestWhatGoesWrong(t *testing.T) {
for name, c := range map[string]struct {
break_ func(in *Inputs)
id string
status string
fix string
detail string
}{
"a package is missing": {func(in *Inputs) { in.Missing = []string{"kmod-amneziawg"} }, "awg", "error", "install:awg", "kmod-amneziawg is missing."},
"the firmware changed": {func(in *Inputs) {
in.Missing, in.KernelWas, in.KernelIs = []string{"kmod-amneziawg"}, "6.6.1", "6.12.9"
}, "awg", "error", "install:awg", "kernel 6.6.1 → 6.12.9"},
"the module won't load": {func(in *Inputs) { in.Loaded, in.WhyNotLoaded = false, "Exec format error." }, "awg", "error", "updates", "doesn't load: Exec format error. Reinstall"},
"netifd has no protocol": {func(in *Inputs) { in.Handler = false }, "awg", "error", "netrestart", "netifd doesn't know"},
"an update waits": {func(in *Inputs) { in.WaitsForReboot = true }, "awg", "warn", "", "after a reboot"},
"the clock is wrong": {func(in *Inputs) { in.Year = 1970 }, "clock", "error", "", "Check NTP"},
"the agent is gone": {func(in *Inputs) { in.AgentRunning = false }, "agent", "error", "restart", ""},
"little flash": {func(in *Inputs) { kb := int64(1536); in.FreeKB = &kb }, "flash", "warn", "", "1.5 MB free"},
"no flash": {func(in *Inputs) { kb := int64(100); in.FreeKB = &kb }, "flash", "error", "", "0 MB free"},
"no access rules": {func(in *Inputs) { in.NftLoaded = false }, "nft", "error", "apply", "is missing"},
"other policy routing": {func(in *Inputs) { in.PolicyRouting = []string{"pref 1000 fwmark 0x1 → table 100"} }, "policy", "warn", "", "(pref 1000 fwmark 0x1 → table 100)"},
"broken devices": {func(in *Inputs) {
in.ModelErrors = func() []string { return []string{"a.", "b.", "c.", "d.", "e.", "f."} }
}, "data", "error", "",
"a. b. c. d. (2 more) Such devices"},
"the tunnel is down": {func(in *Inputs) { in.Iface = func(string) Iface { return Iface{Exists: true} } }, "up_home", "error", "ifup:dawn0", "dawn0 is down."},
"the tunnel has an error": {func(in *Inputs) {
in.Iface = func(string) Iface { return Iface{Exists: true, Errors: []string{"NO_DEVICE"}} }
}, "up_home", "error", "ifup:dawn0", "is down: NO_DEVICE."},
"the tunnel is not there": {func(in *Inputs) { in.Iface = func(string) Iface { return Iface{} } }, "up_home", "error", "apply", "does not exist"},
"the tunnel's protocol": {func(in *Inputs) {
in.Iface = func(string) Iface { return Iface{Exists: true, Errors: []string{"INVALID_PROTO"}} }
}, "up_home", "error", "netrestart", "netifd doesn't know"},
"the tunnel is unreadable": {func(in *Inputs) { in.Unread = func(string) bool { return true } }, "peers_home", "warn", "", "could not be read"},
"a device is missing": {func(in *Inputs) {
in.Peers = func(*model.Server) Peers { return Peers{Expected: 3, Missing: 1, Loaded: 2} }
}, "peers_home", "warn", "sync", "1 of 3 devices missing."},
"a device too many": {func(in *Inputs) {
in.Peers = func(*model.Server) Peers { return Peers{Expected: 1, Extra: 2, Loaded: 3} }
}, "peers_home", "warn", "sync", "0 of 1 device missing, 2 loaded"},
"no firewall rule": {func(in *Inputs) { in.Firewall, _ = uci.Parse("config zone 'dawnline_home_zone'\n") }, "fw_home", "error", "apply", "is missing"},
"answers are swallowed": {func(in *Inputs) { in.Replies = &Replies{Diverts: []string{"pref 9 → table 100"}} }, "reply_home", "error", "apply", "Answers from UDP 4500 are swallowed"},
"a proxy that is passed": {func(in *Inputs) { in.Replies = &Replies{OK: true, Diverts: []string{"pref 9 → table 100"}} }, "portal_reply", "ok", "", "Answers from TCP 4500 go straight out. A proxy"},
"the port is another's": {func(in *Inputs) {
in.Network, _ = uci.Parse("config interface 'wg0'\n\toption proto 'wireguard'\n\toption listen_port '4500'\n")
}, "port_home", "error", "", "UDP 4500 is also used by wg0."},
"the subnet is another's": {func(in *Inputs) { in.Subnets = append(in.Subnets, Subnet{"guest", "10.77.0.129/25"}) }, "subnet_home", "error", "", "10.77.0.0/24 overlaps guest (10.77.0.129/25)."},
"a common home network": {func(in *Inputs) { in.HomeNets = func(*model.Server) []string { return []string{"192.168.1.0/24"} } }, "homenet_home", "warn", "", "192.168.1.0/24 is one of the most common"},
"the address is elsewhere": {func(in *Inputs) {
in.Endpoint = func(*model.Server, string) endpoint.Reads {
return endpoint.Reads{IPs: []string{"198.51.100.4"}, WAN: "203.0.113.7"}
}
}, "ep_home", "warn", "", "not this router"},
"the home path needs a package": {func(in *Inputs) {
in.LikeHome, in.Needs = map[string]any{"active": false}, map[string][]string{"home": {"kmod-veth"}}
}, "home_home", "warn", "install:home", "Needs kmod-veth."},
"the home path is unattached": {func(in *Inputs) { in.LikeHome = map[string]any{"active": true, "bridged": false, "dev": "br-lan"} }, "home_home", "warn", "apply", "not attached to br-lan"},
"the home path was refused": {func(in *Inputs) { in.LikeHome = map[string]any{"active": false, "error": "No LAN detected."} }, "home_home", "warn", "apply", "No LAN detected. Internet traffic leaves"},
"the home path is not set up": {func(in *Inputs) { in.LikeHome = map[string]any{"active": false, "error": nil} }, "home_home", "warn", "apply", "Not set up. Internet traffic"},
"the resolver stopped": {func(in *Inputs) { in.ResolverRunning = false }, "resolver", "warn", "apply", "is not running"},
"the resolver restarts": {func(in *Inputs) { age := int64(3); in.ResolverRunning, in.ResolverConfAge = false, &age }, "resolver", "ok", "", "Restarting"},
"the resolver beside the tunnel": {func(in *Inputs) { in.LikeHome = map[string]any{"active": false} }, "resolver", "ok", "", "on port 5354"},
"no HTTPS on the router": {func(in *Inputs) { in.Needs = map[string][]string{"https": {"openssl-util"}} }, "portal", "error", "install:https", "install openssl-util"},
"the portal's port is taken": {func(in *Inputs) {
in.Portal = func() Portal { return Portal{Owners: []string{"nginx"}, Listening: true} }
}, "portal", "error", "", "already used by nginx"},
"the portal is silent": {func(in *Inputs) { in.Portal = func() Portal { return Portal{} } }, "portal", "error", "restart", "Nothing listens on TCP 4500."},
"the portal's name is elsewhere": {func(in *Inputs) {
in.Portal = func() Portal {
return Portal{Listening: true, IPs: []string{"198.51.100.4"}, WAN: "203.0.113.7", Valid: true, Names: []string{"vpn.example.org"}}
}
},
"portal_dns", "warn", "", "points at 198.51.100.4, not this router (203.0.113.7)."},
"the portal through Cloudflare": {func(in *Inputs) {
in.Portal = func() Portal { return Portal{Listening: true, IPs: []string{"104.16.1.1"}, WAN: "203.0.113.7"} }
}, "portal_dns", "ok", "", "Cloudflare"},
"a self-signed certificate": {func(in *Inputs) {
in.Portal = func() Portal { return Portal{Listening: true, SelfSigned: true} }
in.Needs = map[string][]string{"acme": {"acme"}}
}, "cert", "warn", "", "ACME can get"},
"a certificate for another name": {func(in *Inputs) {
in.Portal = func() Portal {
return Portal{Listening: true, Cert: "/c.crt", Valid: true, Names: []string{"other.example.org"}}
}
}, "cert", "warn", "", "/c.crt does not cover vpn.example.org (it has other.example.org)"},
"a certificate about to end": {func(in *Inputs) {
in.Portal = func() Portal { return Portal{Listening: true, Cert: "/c.crt", Names: []string{"vpn.example.org"}} }
}, "cert", "warn", "", "expires within 14 days"},
} {
in := healthy(t)
c.break_(&in)
got, has := byID(Run(in))[c.id]
fix := ""
if got.Fix != nil {
fix = *got.Fix
}
// a common home network is advice, not a fault: it warns with no problem to put in a notice
problem := c.status != "ok" && c.id != "homenet_home"
if !has || got.Status != c.status || fix != c.fix || !strings.Contains(got.Detail, c.detail) || problem != (got.Problem != nil) {
t.Errorf("%s: %s %s fix %q problem %v\n %s", name, c.id, got.Status, fix, got.Problem != nil, got.Detail)
}
}
// no server yet: only what the router itself needs, and the advice to set one up
in := healthy(t)
c, _ := uci.Parse("config settings 'settings'\n")
in.M = model.Load(c, nil)
var ids []string
for _, x := range Run(in) {
ids = append(ids, x.ID)
}
if strings.Join(ids, " ") != "clock agent flash server" {
t.Fatalf("with no server: %v", ids)
}
if got := CommonHome([]string{"192.168.1.0/24", "10.37.12.0/24", "192.168.0.0/16", "odd"}); strings.Join(got, " ") != "192.168.1.0/24 192.168.0.0/16" {
t.Fatalf("common home networks: %v", got)
}
}
// The networks behind a server's routers: no check while none is written, then each in effect, or which is
// not and what it shares addresses with, or the route the router would not take.
func TestNetworksBehindRoutersAreChecked(t *testing.T) {
c, err := uci.Parse(settings + `
config person 'boris'
option name 'Boris'
option server 'home'
option access 'both'
config device 'r1'
option person 'boris'
option name 'Dacha'
option kind 'router'
option address '10.77.0.2'
config device 'r2'
option person 'boris'
option name 'Garage'
option kind 'router'
option address '10.77.0.3'
`)
if err != nil {
t.Fatal(err)
}
own := []string{"192.168.50.0/24"}
run := func() (Check, bool) {
m := model.Load(c, nil)
in := Inputs{M: m, Behind: m.BehindOf(own), Own: own}
return in.behind("home", "")
}
if _, has := run(); has {
t.Error("a check with no network written")
}
c.SetList("r1", "behind_net", []string{"192.168.2.0/24", "172.20.0.0/16"})
if got, has := run(); !has || got.ID != "sites_home" || got.Status != "ok" || got.Detail != "2 networks behind routers are carried by the tunnel." {
t.Errorf("all in effect: %+v", got)
}
// each way a written network is not in effect, in its words
c.SetList("r1", "behind_net", []string{"192.168.2.0/24", "10.77.0.128/25", "192.168.50.0/25", "the garage"})
c.SetList("r2", "behind_net", []string{"192.168.2.128/25"})
got, _ := run()
for _, want := range []string{
"10.77.0.128/25 behind Boris · Dacha shares addresses with the tunnel network of Home (10.77.0.0/24).",
"192.168.50.0/25 behind Boris · Dacha shares addresses with 192.168.50.0/24, which the router has itself.",
"the garage behind Boris · Dacha is not a network a router may have behind it.",
"192.168.2.128/25 behind Boris · Garage shares addresses with 192.168.2.0/24 behind Dacha.",
"Such a network is not in effect: change it in the server's Network settings.",
} {
if !strings.Contains(got.Detail, want) {
t.Errorf("the check lacks %q:\n%s", want, got.Detail)
}
}
if got.Status != "warn" || got.Problem == nil || *got.Problem != "A network behind a router isn’t in effect" {
t.Errorf("networks not in effect: %+v", got)
}
// a router that waits for the admin's answer is not checked: nothing of it is in effect by design
c.Set("r2", "waiting", "1")
c.SetList("r1", "behind_net", []string{"192.168.2.0/24"})
if got, _ := run(); got.Status != "ok" || got.Detail != "1 network behind routers is carried by the tunnel." {
t.Errorf("with a waiting router: %+v", got)
}
// a route the router would not take
m := model.Load(c, nil)
in := Inputs{M: m, Behind: m.BehindOf(own), Own: own, RouteError: "192.168.2.0/24 dev dawn0: RTNETLINK answers: a made-up failure"}
if got, _ := in.behind("home", ""); got.Status != "error" || got.Fix == nil || *got.Fix != "sync" || !strings.Contains(got.Detail, "a made-up failure") {
t.Errorf("a route refused: %+v", got)
}
}
+136
View File
@@ -0,0 +1,136 @@
config settings 'settings'
config server 'home'
option name 'Home'
option ifname 'dawn0'
option port '4500'
option subnet '10.77.0.0/24'
option private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='
option public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='
config server 'cabin'
option name 'Cabin'
option ifname 'dawn1'
option port '4600'
option subnet '10.78.0.0/24'
option private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='
config circle 'family'
option server 'home'
option name 'Family'
config circle 'lost'
option server 'gone'
option name 'Lost'
config service 'nas'
option name 'NAS'
option address '192.168.1.5'
config person 'anna'
option server 'home'
option name 'Anna'
option code 'abcdef-ghijkl-mnopqr'
option circle 'family'
list services 'nas'
config person 'boris'
option server 'gone'
option name 'Boris'
option code 'abcdef-ghijkl-mnopqr'
option circle 'nowhere'
list services 'nas'
list services 'printer'
config person 'clara'
option server 'cabin'
option name 'Clara'
option paused '1'
config person 'dmitri'
option server 'home'
option name 'Dmitri'
option expires '2020-01-01'
config device 'good'
option person 'anna'
option name 'Phone'
option address '10.77.0.2'
option public_key 'HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw='
option preshared_key 'TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0='
config device 'same'
option person 'anna'
option name 'Tablet'
option address '10.77.0.2'
option public_key 'TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0='
config device 'off'
option person 'anna'
option name 'Old laptop'
option address '10.77.0.9'
option public_key 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA='
option disabled '1'
config device 'badkey'
option person 'anna'
option name 'Laptop'
option address '10.77.0.3'
option public_key 'not a key'
option private_key 'neither'
option preshared_key 'nor this'
option mtu '9000'
option keepalive '9999'
config device 'noaddr'
option person 'anna'
option name 'Watch'
option public_key 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBA='
config device 'oddaddr'
option person 'anna'
option name 'TV'
option address '10.77.0.300'
option public_key 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCA='
config device 'outside'
option person 'anna'
option name 'Console'
option address '10.99.0.4'
option public_key 'DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDA='
config device 'routers'
option person 'anna'
option name 'Camera'
option address '10.77.0.1'
option public_key 'EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEA='
config device 'ownkey'
option person 'anna'
option name 'Mirror'
option address '10.77.0.7'
option public_key 'xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg='
config device 'orphan'
option person 'nobody'
option name 'Orphan'
option address '10.77.0.8'
option public_key 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA='
config device 'noserver'
option person 'boris'
option name 'Boris phone'
option address '10.77.0.2'
option public_key 'GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGA='
config device 'paused'
option person 'clara'
option name 'Clara phone'
option address '10.78.0.2'
option public_key 'HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHA='
config device 'expired'
option person 'dmitri'
option name 'Dmitri phone'
option address '10.77.0.20'
option public_key 'IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIA='
+47
View File
@@ -0,0 +1,47 @@
config settings 'settings'
config server 'home'
option name 'Home'
option ifname 'dawn0'
option port '4500'
option subnet '10.77.0.0/24'
option private_key 'yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk='
config server 'twin'
option name 'Twin'
option ifname 'dawn0'
option port '4500'
option subnet '10.77.0.128/25'
option endpoint 'bad host'
config server 'lanname'
option name 'Lan name'
option ifname 'lan'
option port '4600'
option subnet '10.80.0.0/24'
config server 'ours'
option name 'Ours'
option ifname 'dawn5'
option port '4700'
option subnet '10.81.0.0/24'
config server 'internal'
option name 'Internal'
option ifname 'dlx0'
option port '0'
option subnet 'ten'
config server 'odd'
option name 'Odd'
option ifname 'Bad-Name-Too-Long'
option port '70000'
option subnet '10.82.0.0/24'
option jmin '90'
option jmax '50'
option h1 '5'
option h2 '5'
config person 'anna'
option server 'gone'
option name 'Anna'
+170
View File
@@ -0,0 +1,170 @@
{
"ts": 1800000000,
"errors": [
"Person Boris: server gone does not exist.",
"Person Boris: circle nowhere does not exist.",
"Person Boris: has the same personal link as Anna.",
"Device Tablet of Anna: address 10.77.0.2 is also used by device Phone of Anna.",
"Device Laptop of Anna: the public key is not a key.",
"Device Laptop of Anna: the private key is not a key.",
"Device Laptop of Anna: the preshared key is not a key.",
"Device Laptop of Anna: MTU 9000 is not between 1280 and 1500.",
"Device Laptop of Anna: keep-alive 9999 is not between 0 and 600 seconds.",
"Device Watch of Anna: address (none) is not a plain IPv4 address.",
"Device TV of Anna: address 10.77.0.300 is not a plain IPv4 address.",
"Device Console of Anna: address 10.99.0.4 is outside Home's tunnel 10.77.0.0/24.",
"Device Camera of Anna: address 10.77.0.1 is the router's own address on Home.",
"Device Mirror of Anna: the public key is the server's own.",
"Device Orphan: person nobody does not exist.",
"Circle Lost: server gone does not exist."
],
"states": {
"anna": "active",
"boris": "no_server",
"clara": "paused",
"dmitri": "expired"
},
"tunnels": {
"home": [
"good",
"same",
"outside",
"routers"
],
"cabin": [
]
},
"diffs": [
{
"server": "home",
"loaded": [
"HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw=",
"TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0=",
"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDA=",
"EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEA="
],
"diff": {
"loaded": 4,
"missing": 0,
"extra": 0
}
},
{
"server": "home",
"loaded": [
"TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0=",
"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDA=",
"EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEA="
],
"diff": {
"loaded": 3,
"missing": 1,
"extra": 0
}
},
{
"server": "home",
"loaded": [
"HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw=",
"TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0=",
"DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDA=",
"EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEA=",
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZA="
],
"diff": {
"loaded": 5,
"missing": 0,
"extra": 1
}
},
{
"server": "home",
"loaded": [
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZA="
],
"diff": {
"loaded": 1,
"missing": 4,
"extra": 1
}
},
{
"server": "home",
"loaded": [
],
"diff": {
"loaded": 0,
"missing": 4,
"extra": 0
}
},
{
"server": "cabin",
"loaded": [
],
"diff": {
"loaded": 0,
"missing": 0,
"extra": 0
}
},
{
"server": "cabin",
"loaded": [
],
"diff": {
"loaded": 0,
"missing": 0,
"extra": 0
}
},
{
"server": "cabin",
"loaded": [
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZA="
],
"diff": {
"loaded": 1,
"missing": 0,
"extra": 1
}
},
{
"server": "cabin",
"loaded": [
"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZA="
],
"diff": {
"loaded": 1,
"missing": 0,
"extra": 1
}
},
{
"server": "cabin",
"loaded": [
],
"diff": {
"loaded": 0,
"missing": 0,
"extra": 0
}
}
],
"network": "config interface 'lan'\n\toption proto 'static'\n\nconfig device 'odd'\n\nconfig interface 'dawn5'\n\toption proto 'amneziawg'\n\toption _dawnline '1'\n",
"config_errors": [
"Person Anna: server gone does not exist.",
"Server Twin: Subnet overlaps 10.77.0.0/24.",
"Server Twin: Endpoint must be a hostname or IP address.",
"Server Twin: interface dawn0 is also used by Home.",
"Server Twin: UDP 4500 is also used by Home.",
"Server Lan name: lan is one of the router's own network interfaces.",
"Server Internal: Port must be 1-65535.",
"Server Internal: Subnet must look like 10.77.0.0/24.",
"Server Internal: dlx0 is one of the router's own network interfaces.",
"Server Odd: Interface name: 1-12 lowercase letters, digits or _.",
"Server Odd: Port must be 1-65535.",
"Server Odd: Jmin must not be larger than Jmax.",
"Server Odd: H1 and H2 overlap: each packet type needs values of its own."
]
}
+251
View File
@@ -0,0 +1,251 @@
// Package hub is the live state the backend shows its pages: named documents
// (topics) and the ordered stream of their changes.
//
// A producer puts a whole document; the hub works out what changed and
// publishes only that (a JSON Merge Patch), numbered. A page takes a snapshot
// once, then follows the stream; one that fell behind (a tab that slept, a
// connection that dropped) asks with the number it last saw and gets what it
// missed, or a new snapshot when that is no longer kept. Nothing is published
// for a document that did not change, so a producer may put as often as it
// likes.
//
// The hub knows no transport: api serves it over ubus, portal over HTTP. It
// owns no data of its own either: what a topic holds is its producer's.
package hub
import (
"context"
"crypto/rand"
"encoding/hex"
"sync"
)
// The operations of an event.
const (
OpPut = "put" // data is the whole document
OpPatch = "patch" // data is a merge patch to the document
OpAppend = "append" // data is { items, keep }: new entries of a list, and how many it holds at most
OpDrop = "drop" // the topic is gone
)
// Event is one change of one topic. Seq grows by one with every event of the
// hub, whatever its topic: a reader that sees a number skipped has missed one.
type Event struct {
Seq uint64 `json:"seq"`
Topic string `json:"topic"`
Op string `json:"op"`
Data any `json:"data,omitempty"`
}
// Answer is what a reader gets when it asks where the hub is: the documents
// (Docs, a snapshot) or the events since the number it gave (Events), and the
// number it is at after applying them. Epoch names this run of the hub: a
// number from another run means nothing.
type Answer struct {
Epoch string `json:"epoch"`
Seq uint64 `json:"seq"`
Docs map[string]any `json:"docs,omitempty"`
Events []Event `json:"events,omitempty"`
}
// journalSize is how many events are kept for readers that fell behind. A
// reader further back gets a snapshot, which is always right, only larger.
const journalSize = 512
// Hub holds the documents and publishes their changes.
type Hub struct {
mu sync.Mutex
epoch string
seq uint64
docs map[string]any
journal []Event
wake chan struct{}
subs map[*Sub]struct{}
}
// New makes an empty hub with an epoch of its own.
func New() *Hub {
var b [6]byte
rand.Read(b[:])
return &Hub{
epoch: hex.EncodeToString(b[:]),
docs: map[string]any{},
wake: make(chan struct{}),
subs: map[*Sub]struct{}{},
}
}
// Epoch names this run of the hub.
func (h *Hub) Epoch() string { return h.epoch }
// Put sets a topic's document and publishes what changed in it, if anything.
// The value goes through encoding/json: a struct is published as its JSON.
func (h *Hub) Put(topic string, doc any) error {
next, err := plain(doc)
if err != nil {
return err
}
h.mu.Lock()
defer h.mu.Unlock()
prev, had := h.docs[topic]
if !had {
h.docs[topic] = next
h.publish(Event{Topic: topic, Op: OpPut, Data: next})
return nil
}
patch, changed := Diff(prev, next)
if !changed {
return nil
}
h.docs[topic] = next
// Only an object can be patched; anything else is sent whole.
if _, object := next.(map[string]any); object {
if _, was := prev.(map[string]any); was {
h.publish(Event{Topic: topic, Op: OpPatch, Data: patch})
return nil
}
}
h.publish(Event{Topic: topic, Op: OpPut, Data: next})
return nil
}
// Append adds entries to a topic that is a list (a log, the points of a
// meter) and publishes them; the list keeps its newest `keep` entries.
func (h *Hub) Append(topic string, keep int, items ...any) error {
if len(items) == 0 {
return nil
}
added, err := plain(items)
if err != nil {
return err
}
h.mu.Lock()
defer h.mu.Unlock()
old, _ := h.docs[topic].([]any)
// A new slice every time: a snapshot handed out earlier is never written to.
list := make([]any, 0, len(old)+len(items))
list = append(append(list, old...), added.([]any)...)
if keep > 0 && len(list) > keep {
list = list[len(list)-keep:]
}
h.docs[topic] = list
h.publish(Event{Topic: topic, Op: OpAppend, Data: map[string]any{"items": added, "keep": float64(keep)}})
return nil
}
// Drop removes a topic and tells its readers.
func (h *Hub) Drop(topic string) {
h.mu.Lock()
defer h.mu.Unlock()
if _, had := h.docs[topic]; !had {
return
}
delete(h.docs, topic)
h.publish(Event{Topic: topic, Op: OpDrop})
}
// publish numbers an event, keeps it and hands it to whoever follows. Called
// with the lock held.
func (h *Hub) publish(e Event) {
h.seq++
e.Seq = h.seq
h.journal = append(h.journal, e)
if len(h.journal) > journalSize {
h.journal = append([]Event(nil), h.journal[len(h.journal)-journalSize:]...)
}
close(h.wake)
h.wake = make(chan struct{})
for s := range h.subs {
select {
case s.c <- e:
default:
// A follower that does not keep up is let go; it asks again with Sync.
delete(h.subs, s)
close(s.c)
}
}
}
// Sync answers a reader that is at (epoch, seq) and wants the topics `want`
// accepts (nil: all): the events it missed when they are still kept, a
// snapshot otherwise (another epoch, too far behind, or its first time: no epoch).
func (h *Hub) Sync(epoch string, seq uint64, want func(topic string) bool) Answer {
h.mu.Lock()
defer h.mu.Unlock()
return h.sync(epoch, seq, want)
}
func (h *Hub) sync(epoch string, seq uint64, want func(string) bool) Answer {
a := Answer{Epoch: h.epoch, Seq: h.seq}
// A reader of this run that is not ahead of it, and whose next event is
// still kept. Zero is a number like any other: a reader that took its
// snapshot before anything was published follows from the first event.
if epoch == h.epoch && seq <= h.seq && (seq == h.seq || (len(h.journal) > 0 && h.journal[0].Seq <= seq+1)) {
a.Events = []Event{}
for _, e := range h.journal {
if e.Seq > seq && (want == nil || want(e.Topic)) {
a.Events = append(a.Events, e)
}
}
return a
}
a.Docs = make(map[string]any, len(h.docs))
for topic, doc := range h.docs {
if want == nil || want(topic) {
a.Docs[topic] = doc
}
}
return a
}
// Wait is Sync that does not answer "nothing new": it waits until there is an
// event the reader wants, or ctx ends (then the answer has no events, and the
// reader asks again). A reader that needs a snapshot gets it at once.
func (h *Hub) Wait(ctx context.Context, epoch string, seq uint64, want func(topic string) bool) Answer {
for {
h.mu.Lock()
a := h.sync(epoch, seq, want)
wake := h.wake
h.mu.Unlock()
if a.Docs != nil || len(a.Events) > 0 {
return a
}
// Events of other topics moved the number: the reader is told, so its
// next question is not answered from the start of the journal.
seq = a.Seq
select {
case <-ctx.Done():
return a
case <-wake:
}
}
}
// Sub follows the hub: every event from the moment it subscribed, in order.
type Sub struct {
h *Hub
c chan Event
}
// Follow subscribes. The channel is closed when the follower did not keep up
// (its buffer of 256 events filled) or called Close.
func (h *Hub) Follow() *Sub {
s := &Sub{h: h, c: make(chan Event, 256)}
h.mu.Lock()
h.subs[s] = struct{}{}
h.mu.Unlock()
return s
}
// Events is the follower's channel.
func (s *Sub) Events() <-chan Event { return s.c }
// Close ends the subscription.
func (s *Sub) Close() {
s.h.mu.Lock()
if _, on := s.h.subs[s]; on {
delete(s.h.subs, s)
close(s.c)
}
s.h.mu.Unlock()
}
+177
View File
@@ -0,0 +1,177 @@
package hub
import (
"context"
"encoding/json"
"reflect"
"testing"
"time"
)
func doc(s string) any {
var v any
if err := json.Unmarshal([]byte(s), &v); err != nil {
panic(err)
}
return v
}
// A patch holds only what changed, and applying it gives the new document.
func TestDiffAndMerge(t *testing.T) {
cases := []struct{ a, b, patch string }{
{`{"a":1,"b":2}`, `{"a":1,"b":3}`, `{"b":3}`},
{`{"a":1,"b":2}`, `{"a":1}`, `{"b":null}`},
{`{"a":1}`, `{"a":1,"c":{"d":true}}`, `{"c":{"d":true}}`},
{`{"p":{"d1":{"online":false,"rx":1},"d2":{"online":true}}}`, `{"p":{"d1":{"online":true,"rx":1},"d2":{"online":true}}}`, `{"p":{"d1":{"online":true}}}`},
{`{"l":[1,2,3]}`, `{"l":[1,2]}`, `{"l":[1,2]}`},
{`{"a":{"b":1}}`, `{"a":"text"}`, `{"a":"text"}`},
{`{"a":"text"}`, `{"a":{"b":1}}`, `{"a":{"b":1}}`},
}
for _, c := range cases {
patch, changed := Diff(doc(c.a), doc(c.b))
if !changed {
t.Fatalf("%s → %s: no change seen", c.a, c.b)
}
if !reflect.DeepEqual(patch, doc(c.patch)) {
got, _ := json.Marshal(patch)
t.Fatalf("%s → %s: patch %s, want %s", c.a, c.b, got, c.patch)
}
if got := Merge(doc(c.a), patch); !reflect.DeepEqual(got, doc(c.b)) {
t.Fatalf("%s + %s did not give %s", c.a, c.patch, c.b)
}
}
if _, changed := Diff(doc(`{"a":[1,{"b":2}]}`), doc(`{"a":[1,{"b":2}]}`)); changed {
t.Fatal("equal documents differ")
}
}
// Nulls never reach a document: a patch could not tell one from a removal.
func TestPlainDropsNulls(t *testing.T) {
got, err := plain(map[string]any{"a": nil, "b": map[string]any{"c": nil, "d": 1}, "l": []any{nil, 1}})
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, doc(`{"b":{"d":1},"l":[null,1]}`)) {
t.Fatalf("got %v", got)
}
}
func TestPutPublishesOnlyChanges(t *testing.T) {
h := New()
sub := h.Follow()
defer sub.Close()
h.Put("people", doc(`{"p1":{"name":"Myata","online":false}}`))
h.Put("people", doc(`{"p1":{"name":"Myata","online":false}}`)) // the same: nothing
h.Put("people", doc(`{"p1":{"name":"Myata","online":true}}`))
first, second := <-sub.Events(), <-sub.Events()
if first.Seq != 1 || first.Op != OpPut || second.Seq != 2 || second.Op != OpPatch {
t.Fatalf("events: %+v %+v", first, second)
}
if !reflect.DeepEqual(second.Data, doc(`{"p1":{"online":true}}`)) {
t.Fatalf("patch: %v", second.Data)
}
select {
case e := <-sub.Events():
t.Fatalf("an event for nothing: %+v", e)
default:
}
}
// A reader gets a snapshot first, then what it missed; from another run or
// too far behind, a snapshot again.
func TestSync(t *testing.T) {
h := New()
h.Put("a", doc(`{"x":1}`))
h.Put("b", doc(`{"y":1}`))
snap := h.Sync("", 0, nil)
if snap.Seq != 2 || len(snap.Docs) != 2 || snap.Events != nil {
t.Fatalf("snapshot: %+v", snap)
}
h.Put("a", doc(`{"x":2}`))
h.Put("b", doc(`{"y":2}`))
only := func(topic string) bool { return topic == "a" }
got := h.Sync(snap.Epoch, snap.Seq, only)
if got.Docs != nil || len(got.Events) != 1 || got.Events[0].Topic != "a" || got.Seq != 4 {
t.Fatalf("catch-up: %+v", got)
}
// A snapshot taken before anything was published is followed from the first event.
if early := h.Sync(snap.Epoch, 0, nil); early.Docs != nil || len(early.Events) != 4 {
t.Fatalf("from zero: %+v", early)
}
if up := h.Sync(snap.Epoch, 4, nil); up.Docs != nil || len(up.Events) != 0 {
t.Fatalf("up to date: %+v", up)
}
if other := h.Sync("another-run", 3, nil); other.Docs == nil {
t.Fatal("a number of another run was taken for ours")
}
if ahead := h.Sync(snap.Epoch, 99, nil); ahead.Docs == nil {
t.Fatal("a number ahead of the hub was accepted")
}
for i := 0; i < journalSize+10; i++ {
h.Put("a", map[string]any{"x": i + 10})
}
if far := h.Sync(snap.Epoch, 4, nil); far.Docs == nil {
t.Fatal("events no longer kept were promised")
}
}
func TestAppendKeepsNewest(t *testing.T) {
h := New()
h.Append("meter", 3, 1, 2)
h.Append("meter", 3, 3, 4)
snap := h.Sync("", 0, nil)
if !reflect.DeepEqual(snap.Docs["meter"], doc(`[2,3,4]`)) {
t.Fatalf("list: %v", snap.Docs["meter"])
}
ev := h.Sync(snap.Epoch, 1, nil).Events
if len(ev) != 1 || ev[0].Op != OpAppend || !reflect.DeepEqual(ev[0].Data, doc(`{"items":[3,4],"keep":3}`)) {
t.Fatalf("event: %+v", ev)
}
}
// Wait holds until something the reader wants happens, and gives up with its context.
func TestWait(t *testing.T) {
h := New()
h.Put("a", doc(`{"x":1}`))
at := h.Sync("", 0, nil)
only := func(topic string) bool { return topic == "a" }
got := make(chan Answer, 1)
go func() { got <- h.Wait(context.Background(), at.Epoch, at.Seq, only) }()
h.Put("b", doc(`{"y":1}`)) // not wanted: the wait goes on
select {
case a := <-got:
t.Fatalf("answered for another topic: %+v", a)
case <-time.After(50 * time.Millisecond):
}
h.Put("a", doc(`{"x":2}`))
select {
case a := <-got:
if len(a.Events) != 1 || a.Seq != 3 {
t.Fatalf("answer: %+v", a)
}
case <-time.After(time.Second):
t.Fatal("never answered")
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond)
defer cancel()
if a := h.Wait(ctx, at.Epoch, 3, only); len(a.Events) != 0 || a.Docs != nil || a.Seq != 3 {
t.Fatalf("timed out with: %+v", a)
}
}
// A follower that does not read is let go, not waited for.
func TestSlowFollowerIsDropped(t *testing.T) {
h := New()
sub := h.Follow()
for i := 0; i < 300; i++ {
h.Put("a", map[string]any{"x": i})
}
n := 0
for range sub.Events() {
n++
}
if n != 256 {
t.Fatalf("read %d events before the channel closed", n)
}
sub.Close() // closing again is harmless
}
+101
View File
@@ -0,0 +1,101 @@
// The difference between two versions of a document as a JSON Merge Patch
// (RFC 7396): an object that holds only what changed, null where a field went.
// Documents are what encoding/json makes of JSON (map[string]any, []any,
// float64, string, bool); they hold no null, which a merge patch could not
// tell from "remove this" (plain() drops them on the way in).
//
// Pure: no state, no clock. hub.go keeps the versions.
package hub
import (
"encoding/json"
"reflect"
)
// plain turns any value into a document: through encoding/json, so a struct
// is seen the way a client will see it, and without nulls.
func plain(v any) (any, error) {
raw, err := json.Marshal(v)
if err != nil {
return nil, err
}
var out any
if err := json.Unmarshal(raw, &out); err != nil {
return nil, err
}
return dropNulls(out), nil
}
// dropNulls removes null fields of objects, at any depth. A null inside an
// array stays: an array is replaced whole by a patch, never merged.
func dropNulls(v any) any {
switch x := v.(type) {
case map[string]any:
for k, e := range x {
if e == nil {
delete(x, k)
} else {
x[k] = dropNulls(e)
}
}
case []any:
for i, e := range x {
x[i] = dropNulls(e)
}
}
return v
}
// Diff returns the merge patch that turns document a into b, and whether they
// differ at all. Objects are compared field by field; anything else (an
// array, a number, a string) is replaced whole when it changed. The patch
// shares b's values: neither may be changed afterwards.
func Diff(a, b any) (any, bool) {
am, aok := a.(map[string]any)
bm, bok := b.(map[string]any)
if !aok || !bok {
if reflect.DeepEqual(a, b) {
return nil, false
}
return b, true
}
patch := map[string]any{}
for k := range am {
if _, still := bm[k]; !still {
patch[k] = nil
}
}
for k, bv := range bm {
av, had := am[k]
if !had {
patch[k] = bv
continue
}
if p, changed := Diff(av, bv); changed {
patch[k] = p
}
}
return patch, len(patch) > 0
}
// Merge applies a merge patch to a document and returns the result. The
// document is changed in place where it is an object; what a client does with
// a patch, kept here so a test can hold Diff to it.
func Merge(doc, patch any) any {
pm, ok := patch.(map[string]any)
if !ok {
return patch
}
dm, ok := doc.(map[string]any)
if !ok {
dm = map[string]any{}
}
for k, pv := range pm {
if pv == nil {
delete(dm, k)
continue
}
dm[k] = Merge(dm[k], pv)
}
return dm
}
+317
View File
@@ -0,0 +1,317 @@
// Package i18n puts the backend's messages into the admin's language, as dawnline.i18n does through LuCI's
// catalog reader: text is English in the code and looked up in the luci-i18n-dawnline-<lang> catalog
// (/usr/lib/lua/luci/i18n/dawnline.<lang>.lmo), placeholders filled after the lookup.
//
// A catalog: the translations, then an index entry per string sorted by key (key hash, number of forms,
// offset, length: big-endian uint32s), then the index's offset. A key is the msgid, "ctx\x01" before it and
// "\x02<n>" after it for plural form n; its hash is Paul Hsieh's SuperFastHash seeded with the length. The
// Plural-Forms formula sits under key 0. Held to LuCI's own reader on testdata (scripts/ucode-golden.sh).
package i18n
import (
"encoding/binary"
"os"
"regexp"
"sort"
"strconv"
"strings"
)
// Dir is where LuCI keeps its catalogs.
const Dir = "/usr/lib/lua/luci/i18n"
// A Catalog is one language's translations. A nil one speaks English.
type Catalog struct {
data []byte
index []byte
// plural is the Plural-Forms expression ("" when the catalog has none)
plural string
}
var tag = regexp.MustCompile(`^[a-z][a-z][a-z]?([_-][A-Za-z][A-Za-z][A-Za-z]?[A-Za-z]?)?$`)
// Use is the catalog for a language as a page names it ("ru", "pt_BR"), or nil for English, a language
// with no Dawnline catalog, or anything that is no language.
func Use(dir, lang string) *Catalog {
if lang == "en" || !tag.MatchString(lang) {
return nil
}
c, _ := Load(dir + "/dawnline." + strings.ToLower(strings.ReplaceAll(lang, "_", "-")) + ".lmo")
return c
}
// Load reads a catalog file.
func Load(path string) (*Catalog, error) {
raw, err := os.ReadFile(path)
if err != nil {
return nil, err
}
if len(raw) < 4 {
return nil, os.ErrInvalid
}
at := int(binary.BigEndian.Uint32(raw[len(raw)-4:]))
if at > len(raw)-4 || (len(raw)-4-at)%16 != 0 {
return nil, os.ErrInvalid
}
c := &Catalog{data: raw[:at], index: raw[at : len(raw)-4]}
if formula, ok := c.find(0); ok {
if i := strings.Index(formula, "plural="); i >= 0 {
c.plural = strings.TrimSpace(strings.SplitN(formula[i+len("plural="):], ";", 2)[0])
}
}
return c, nil
}
func (c *Catalog) find(key uint32) (string, bool) {
n := len(c.index) / 16
i := sort.Search(n, func(i int) bool { return binary.BigEndian.Uint32(c.index[i*16:]) >= key })
if i == n || binary.BigEndian.Uint32(c.index[i*16:]) != key {
return "", false
}
off, size := int(binary.BigEndian.Uint32(c.index[i*16+8:])), int(binary.BigEndian.Uint32(c.index[i*16+12:]))
if off+size > len(c.data) {
return "", false
}
return string(c.data[off : off+size]), true
}
// canon is a key as the reader looks it up: runs of white space are one space, none at either end.
func canon(s string) string {
return strings.Join(strings.FieldsFunc(s, func(r rune) bool {
return r == ' ' || r == '\t' || r == '\n' || r == '\v' || r == '\f' || r == '\r'
}), " ")
}
// T is msg in the catalog's language: the English when it has no translation.
func (c *Catalog) T(msg string) string {
if c != nil {
if k := canon(msg); k != "" {
if s, ok := c.find(hash([]byte(k))); ok {
return s
}
}
}
return msg
}
// N is one or many for n, by the language's plural rules (Russian has three forms).
func (c *Catalog) N(n int64, one, many string) string {
if c != nil && c.plural != "" {
if form, ok := eval(c.plural, n); ok {
if s, found := c.find(hash([]byte(canon(one) + "\x02" + strconv.FormatInt(form, 10)))); found {
return s
}
}
}
if n == 1 {
return one
}
return many
}
// hash is SuperFastHash of the bytes, seeded with their length (lmo.c sfh_hash).
func hash(data []byte) uint32 {
if len(data) == 0 {
return 0
}
get16 := func(i int) uint32 { return uint32(data[i]) | uint32(data[i+1])<<8 }
s8 := func(i int) uint32 { return uint32(int32(int8(data[i]))) }
h := uint32(len(data))
i := 0
for n := len(data) >> 2; n > 0; n, i = n-1, i+4 {
h += get16(i)
tmp := get16(i+2)<<11 ^ h
h = h<<16 ^ tmp
h += h >> 11
}
switch len(data) & 3 {
case 3:
h += get16(i)
h ^= h << 16
h ^= s8(i+2) << 18
h += h >> 11
case 2:
h += get16(i)
h ^= h << 11
h += h >> 17
case 1:
h += s8(i)
h ^= h << 10
h += h >> 1
}
h ^= h << 3
h += h >> 5
h ^= h << 4
h += h >> 17
h ^= h << 25
h += h >> 6
return h
}
// eval works a Plural-Forms expression out for n: C's operators on whole numbers.
func eval(expr string, n int64) (v int64, ok bool) {
p := &parser{s: expr, n: n}
defer func() {
if recover() != nil {
v, ok = 0, false
}
}()
v = p.ternary()
p.space()
return v, p.i == len(p.s)
}
type parser struct {
s string
i int
n int64
}
func (p *parser) space() {
for p.i < len(p.s) && (p.s[p.i] == ' ' || p.s[p.i] == '\t') {
p.i++
}
}
// take eats op when it is next (and not the start of a longer operator).
func (p *parser) take(op string) bool {
p.space()
if !strings.HasPrefix(p.s[p.i:], op) {
return false
}
if rest := p.s[p.i+len(op):]; len(op) == 1 && rest != "" && strings.ContainsRune("<>!=", rune(op[0])) && rest[0] == '=' {
return false
}
p.i += len(op)
return true
}
func truth(b bool) int64 {
if b {
return 1
}
return 0
}
func (p *parser) ternary() int64 {
c := p.or()
if !p.take("?") {
return c
}
a := p.ternary()
if !p.take(":") {
panic("no :")
}
b := p.ternary()
if c != 0 {
return a
}
return b
}
func (p *parser) or() int64 {
v := p.and()
for p.take("||") {
r := p.and()
v = truth(v != 0 || r != 0)
}
return v
}
func (p *parser) and() int64 {
v := p.equal()
for p.take("&&") {
r := p.equal()
v = truth(v != 0 && r != 0)
}
return v
}
func (p *parser) equal() int64 {
v := p.compare()
for {
switch {
case p.take("=="):
v = truth(v == p.compare())
case p.take("!="):
v = truth(v != p.compare())
default:
return v
}
}
}
func (p *parser) compare() int64 {
v := p.sum()
for {
switch {
case p.take("<="):
v = truth(v <= p.sum())
case p.take(">="):
v = truth(v >= p.sum())
case p.take("<"):
v = truth(v < p.sum())
case p.take(">"):
v = truth(v > p.sum())
default:
return v
}
}
}
func (p *parser) sum() int64 {
v := p.product()
for {
switch {
case p.take("+"):
v += p.product()
case p.take("-"):
v -= p.product()
default:
return v
}
}
}
func (p *parser) product() int64 {
v := p.unary()
for {
switch {
case p.take("*"):
v *= p.unary()
case p.take("/"):
v /= p.unary()
case p.take("%"):
v %= p.unary()
default:
return v
}
}
}
func (p *parser) unary() int64 {
if p.take("!") {
return truth(p.unary() == 0)
}
if p.take("(") {
v := p.ternary()
if !p.take(")") {
panic("no )")
}
return v
}
if p.take("n") {
return p.n
}
p.space()
j := p.i
for j < len(p.s) && p.s[j] >= '0' && p.s[j] <= '9' {
j++
}
v, err := strconv.ParseInt(p.s[p.i:j], 10, 64)
if err != nil {
panic(err)
}
p.i = j
return v
}
Loaded 100 of 858 files, more files were not shown because too many files have changed in this diff. Show more