Files
dawnline/backend/apply/keep.go
T
Aemeath ac90575b0b
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
Dawnline 0.20.0
- **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

202 lines
6.4 KiB
Go

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) })
}