Files
dawnline/backend/methods/ask.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

270 lines
8.9 KiB
Go

package methods
import (
"errors"
"strconv"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/journal"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// What a person asks of the admin about a router, and the admin's answer.
//
// On their own page a person may add a router and name the networks behind it. Where the server's rule
// says "ask", neither takes effect: the router waits (`waiting`: no device of the tunnel, no
// configuration) and the networks are only asked for (`ask_net`), until the admin takes them or turns
// them down with a reason the person then reads (`declined`, and `declined_net` for networks). Taking a
// network away is the person's own to do, at once.
// asksNothing is the refusal for an answer to a device that asks for nothing.
var asksNothing = errors.New("This device asks for nothing.")
// applied puts a change of the settings into effect by what it moved: the routes (everything is applied),
// who is in a tunnel (the sync), or nothing on the router. The pages are told either way.
func applied(d Deps, lang string, routes, loads bool) (warning any) {
switch {
case routes:
return rerouted(d, lang)
case loads && d.Sync != nil:
warning = d.Sync(lang)
}
if d.Told != nil {
d.Told()
}
return warning
}
// netsGiven reads networks a page names again (ones the router has or was asked for), each as kept.
func netsGiven(given any) ([]string, error) {
list, _ := given.([]any)
out := []string{}
for _, v := range list {
s := trimmed(v)
if s == "" {
continue
}
n, why := model.BehindNet(s)
if why != "" {
return nil, said{"Network %s is not written like 192.168.2.0/24.", []any{s}}
}
out = append(out, n.String())
}
return out, nil
}
// deviceAnswer is the admin's answer to what a router's owner asked for on their own page:
//
// accept the router is a device of the tunnel from now on, and the networks asked for are its own
// (checked now against everything they must not share addresses with: refused in a sentence
// when they cannot be taken as they are)
// decline neither; `reason` is one of model.Declined, which the owner reads
// reopen the question is put back (the undo of either answer): `router` the router waits again,
// `networks` are asked for again and not its own
//
// `networks` with accept or decline names the ones answered, of a router that does not wait (each asked
// network has its own two answers on the page); without it the answer is to all that is asked.
func deviceAnswer(d Deps, a map[string]any) (map[string]any, error) {
id, answer := text(a["id"]), text(a["answer"])
reason := text(a["reason"])
known := false
for _, r := range model.Declined {
known = known || r == reason
}
if !known {
reason = "plain"
}
again, err := netsGiven(a["networks"])
if err != nil {
return nil, err
}
var was model.Device
var detail []string
routes, loads := false, false
err = write(d, func(c *uci.Config, m *model.Model) error {
dev, err := admin.own(m, id)
if err != nil {
return err
}
was = *dev
if dev.Kind != "router" {
return asksNothing
}
// the router itself is asked for while it waits and is not turned down yet
router := dev.Waiting && dev.Declined == ""
// the networks this answer is about: all that is asked, or the ones named
asked := dev.Ask
if len(again) > 0 && !dev.Waiting && answer != "reopen" {
asked = without(dev.Ask, without(dev.Ask, again))
if len(asked) == 0 {
return asksNothing
}
}
left := without(dev.Ask, asked)
switch answer {
case "accept":
// a router turned down asks for nothing until the question is put back, its networks neither
if (!router && len(asked) == 0) || (dev.Waiting && !router) {
return asksNothing
}
if len(asked) > 0 {
all := []any{}
for _, n := range append(append([]string{}, dev.Behind...), asked...) {
all = append(all, n)
}
nets, err := behindNets(m, id, all, ownNets(d, m), admin, false)
if err != nil {
return err
}
setBehind(c, id, nets)
detail = append(detail, "accepted="+strings.Join(asked, " "))
routes = true
}
if router {
c.Delete(id, "waiting")
detail = append([]string{"accepted=router"}, detail...)
loads = true
}
setList(c, id, "ask_net", left)
if len(left) == 0 {
c.Delete(id, "asked")
}
// what was turned down before is answered anew for the networks taken now
if rest := without(dev.DeclinedNets, asked); len(rest) != len(dev.DeclinedNets) {
setList(c, id, "declined_net", rest)
if len(rest) == 0 && !dev.Waiting {
c.Delete(id, "declined")
}
}
case "decline":
switch {
case router:
// it stays as it is, waiting, for its owner to read why and remove
c.Set(id, "declined", reason)
c.Delete(id, "declined_net")
detail = append(detail, "declined=router", "reason="+reason)
case len(asked) > 0 && !dev.Waiting:
setList(c, id, "declined_net", append(without(dev.DeclinedNets, asked), asked...))
c.Set(id, "declined", reason)
setList(c, id, "ask_net", left)
if len(left) == 0 {
c.Delete(id, "asked")
}
detail = append(detail, "declined="+strings.Join(asked, " "), "reason="+reason)
default:
return asksNothing
}
case "reopen":
back := a["router"] == true
if !back && len(again) == 0 {
return asksNothing
}
if back {
if !dev.Waiting {
c.Set(id, "waiting", "1")
loads = true
}
detail = append(detail, "reopened=router")
}
if len(again) > 0 {
if rest := without(dev.Behind, again); len(rest) != len(dev.Behind) {
setBehind(c, id, rest)
routes = !dev.Waiting
}
setList(c, id, "ask_net", append(without(dev.Ask, again), again...))
detail = append(detail, "reopened="+strings.Join(again, " "))
}
rest := without(dev.DeclinedNets, again)
setList(c, id, "declined_net", rest)
if len(rest) == 0 {
c.Delete(id, "declined")
}
c.Set(id, "asked", strconv.FormatInt(clock(), 10))
default:
return errors.New("Unknown answer.")
}
return nil
})
if err != nil {
return nil, err
}
warning := applied(d, text(a["lang"]), routes, loads)
logged(d, journal.Event{"type": "device_changed", "person": was.Person, "device": id, "detail": strings.Join(detail, ", "), "by": admin.By})
return map[string]any{"warning": warning}, nil
}
// DeviceWishAs is the networks a router's owner wants behind it, all of them at once, from their own page.
// One the router has and the list lacks goes at once: that is the owner's own to do. One it lacks is its
// own at once where the server's rule says so, else asked of the admin (a router that still waits asks for
// all of them). `cleared` names turned-down networks the owner has read and puts away; one asked for
// again is no longer turned down either.
func DeviceWishAs(d Deps, a map[string]any, who Actor) (map[string]any, error) {
id := text(a["id"])
cleared, err := netsGiven(a["cleared"])
if err != nil {
return nil, err
}
var was model.Device
var detail []string
routes := false
err = write(d, func(c *uci.Config, m *model.Model) error {
dev, err := who.own(m, id)
if err != nil {
return err
}
was = *dev
if dev.Kind != "router" {
return errors.New("Only a router has networks behind it.")
}
p := m.People[dev.Person]
if p == nil || m.Servers[p.Server] == nil {
return errors.New("The person has no server.")
}
asking := who.By != "admin" && (dev.Waiting || m.Servers[p.Server].PortalNets == "ask")
wanted, err := behindNets(m, id, a["networks"], ownNets(d, m), who, asking)
if err != nil {
return err
}
// what it has and keeps, in the order it has them; what is new, in the order asked
keep := without(dev.Behind, without(dev.Behind, wanted))
added := without(wanted, dev.Behind)
behind, ask := keep, added
if !asking {
behind, ask = append(keep, added...), nil
}
if !sameList(behind, dev.Behind) {
setBehind(c, id, behind)
detail = append(detail, "networks="+behindSaid(behind))
routes = !dev.Waiting
}
if !sameList(ask, dev.Ask) {
setList(c, id, "ask_net", ask)
if fresh := without(ask, dev.Ask); len(fresh) > 0 {
c.Set(id, "asked", strconv.FormatInt(clock(), 10))
detail = append(detail, "asked="+strings.Join(fresh, " "))
} else if len(ask) == 0 && !dev.Waiting {
c.Delete(id, "asked")
}
if gone := without(dev.Ask, ask); len(gone) > 0 {
detail = append(detail, "withdrew="+strings.Join(gone, " "))
}
}
if rest := without(without(dev.DeclinedNets, cleared), wanted); len(rest) != len(dev.DeclinedNets) {
setList(c, id, "declined_net", rest)
if len(rest) == 0 && !dev.Waiting {
c.Delete(id, "declined")
}
}
return nil
})
if err != nil {
return nil, err
}
warning := applied(d, text(a["lang"]), routes, false)
if len(detail) > 0 {
logged(d, journal.Event{"type": "device_changed", "person": was.Person, "device": id, "detail": strings.Join(detail, ", "), "by": who.By})
}
return map[string]any{"warning": warning}, nil
}