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.
This commit is contained in:
Aemeath committed 2026-11-13 09:43:50 +03:00
1 parent f8130e39a8
commit ac90575b0b
788 files changed
+139244 -41720

No files matched your search

+132
View File
@@ -0,0 +1,132 @@
package methods
import (
"errors"
"regexp"
"strings"
"time"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/view"
)
var ifupFix = regexp.MustCompile(`^ifup:([a-z][a-z0-9_]{0,11})$`)
// healthFix runs a fix a check named and answers Health again with what the fix came to
// (dawnline.health fix): apply and sync are still the ucode backend's, the rest the service's own.
func healthFix(w *view.World, t *i18n.Catalog, d Deps, id, lang string) (map[string]any, error) {
var result any
switch {
case id == "apply":
if d.Apply != nil {
result = d.Apply(lang)
}
case id == "sync":
var warning any
if d.Sync != nil {
warning = d.Sync(lang)
}
result = map[string]any{"warning": warning}
case id == "restart":
if d.Background != nil {
d.Background("sleep 1; /etc/init.d/dawnline restart")
}
result = map[string]any{"code": 0}
// netifd learns protocol handlers (amneziawg) only when it starts; the answer goes out before the network blinks
case id == "netrestart":
if d.Background != nil {
d.Background("sleep 1; /etc/init.d/network restart")
}
result = map[string]any{"ok": true, "background": true}
case ifupFix.MatchString(id):
name := ifupFix.FindStringSubmatch(id)[1]
// only Dawnline's own tunnels: never another interface of the router
mine := false
for _, s := range w.M.Servers {
mine = mine || (s.Ifname == name && s.PrivateKey != "")
}
if !mine {
return nil, said{"%s is not one of Dawnline's tunnels.", []any{name}}
}
if d.Ifup != nil {
d.Ifup(name)
}
result = map[string]any{"ok": true}
default:
return nil, errors.New("Unknown fix.")
}
out := serviceLog(w, t, d.RunDir, d.DataDir)
out["checks"] = healthChecks(w, t, d)
out["result"] = result
return out, nil
}
// check[:app|amneziawg|packages] | app[@tag|:reinstall|:rollback|:lang:<code>|:nolang]
// | amneziawg[:auto|slava|grey[:ru]|:rollback] | pkgs:<name,…> (only packages Dawnline manages)
var updateTarget = regexp.MustCompile(`^(check(:(app|amneziawg|packages))?|app(@v?[0-9][0-9A-Za-z.+~-]{0,40}|:reinstall|:rollback|:lang:[a-z]{2,3}(_[A-Za-z]{2,4})?|:nolang)?|amneziawg(:rollback|:(auto|slava|grey)(:ru)?)?|pkgs:[a-z0-9][a-z0-9.+,-]*)$`)
// The packages Dawnline manages besides its own and AmneziaWG's.
var managedPackages = []string{"ip-full", "kmod-veth", "libustream-openssl", "openssl-util", "acme", "acme-acmesh-dnsapi", "luci-app-acme"}
// updatesRun starts an update job: the updater does the work in the background. It answers the new job
// once its worker has written it (a quick check may be over by then), else none (the page follows
// updates_status); never an older job. It waits two seconds at most.
func updatesRun(d Deps, what, lang string) (map[string]any, error) {
if what == "" {
what = "check"
}
if len(what) > 400 || !updateTarget.MatchString(what) {
return nil, errors.New("Unknown update target.")
}
// checked here too, so a wrong name never becomes a failed job in the history
if names, isPkgs := strings.CutPrefix(what, "pkgs:"); isPkgs {
for _, n := range strings.Split(names, ",") {
known := false
for _, p := range managedPackages {
known = known || p == n
}
if n == "" {
return nil, errors.New("Unknown update target.")
}
if !known {
return nil, said{"%s is not a package Dawnline manages", []any{n}}
}
}
}
if updating(d.RunDir) {
return nil, errors.New("Another component action is already running")
}
type job struct {
ID any `json:"id"`
Started float64 `json:"started"`
}
var prev job
files.ReadJSON(d.RunDir+"/update-job.json", &prev)
began := clock()
args := []string{"install", what}
if rest, isCheck := strings.CutPrefix(what, "check"); isCheck {
args = []string{"check"}
if rest != "" {
args = append(args, strings.TrimPrefix(rest, ":"))
}
}
// the job speaks the admin's language, when its catalog is installed
speak := ""
if i18n.Use(d.Catalogs, lang) != nil {
speak = strings.ToLower(strings.ReplaceAll(lang, "_", "-"))
}
if d.Updater != nil {
d.Updater(speak, args)
}
for i := 0; i < 20; i++ {
var j job
var whole map[string]any
if files.ReadJSON(d.RunDir+"/update-job.json", &j) && j.ID != prev.ID && j.Started >= float64(began) {
files.ReadJSON(d.RunDir+"/update-job.json", &whole)
return map[string]any{"started": true, "job": whole}, nil
}
time.Sleep(100 * time.Millisecond)
}
return map[string]any{"started": true, "job": nil}, nil
}
+269
View File
@@ -0,0 +1,269 @@
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
}
+378
View File
@@ -0,0 +1,378 @@
package methods
import (
"encoding/json"
"os"
"reflect"
"strings"
"testing"
"time"
"github.com/tickcount/luci-app-dawnline/backend/health"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/usage"
"github.com/tickcount/luci-app-dawnline/backend/view"
)
// What a person asks for about a router on their own page, and the admin's answers: Lev, on the server
// where everyone reaches everyone, by each of the server's rules.
func TestRequestsFromThePortal(t *testing.T) {
d, st, dir, ap, sy := sitesStand(t)
lev := Actor{By: "portal", Person: "lev"}
dev := func(id string) *model.Device {
t.Helper()
_, m, err := st.Read()
if err != nil {
t.Fatal(err)
}
if m.Devices[id] == nil {
t.Fatalf("no device %s", id)
}
return m.Devices[id]
}
loaded := func(id string) bool {
_, m, _ := st.Read()
states := map[string]string{}
for _, p := range m.PersonIDs() {
states[p] = "active"
}
for _, x := range health.TunnelDevices(m, "open", states) {
if x.ID == id {
return true
}
}
return false
}
nets := func(list ...string) []any {
out := []any{}
for _, n := range list {
out = append(out, n)
}
return out
}
wish := func(id string, a map[string]any) string {
a["id"] = id
if _, err := DeviceWishAs(d, a, lev); err != nil {
return err.Error()
}
return ""
}
answer := func(id, how string, more map[string]any) any {
a := map[string]any{"id": id, "answer": how}
for k, v := range more {
a[k] = v
}
return ask(t, d, "device_answer", a)["error"]
}
counts := func(what string, applied, synced int) {
t.Helper()
if *ap != applied || *sy != synced {
t.Errorf("%s: applied %d, synced %d, want %d and %d", what, *ap, *sy, applied, synced)
}
}
// Kira's router has a network, the admin's doing
if res := ask(t, d, "device_networks", map[string]any{"id": "kphone", "networks": nets("192.168.3.0/24")}); res["error"] != nil {
t.Fatal(res["error"])
}
counts("the admin's own", 1, 0)
// asked of the admin (the rule a server starts with): the router waits, its networks are only asked for
if _, err := DeviceCreateAs(d, map[string]any{"person": "lev", "name": "Dacha", "kind": "router", "networks": nets("10.79.0.0/24")}, lev); err == nil ||
err.Error() != "Network 10.79.0.0/24 is taken: 10.79.0.0/24 is the tunnel network of Open. The two ends of a tunnel need different addresses." {
t.Errorf("the tunnel's own network: %v", err)
}
res, err := DeviceCreateAs(d, map[string]any{"person": "lev", "name": "Dacha", "kind": "router", "networks": nets("192.168.88.1/24", "192.168.3.128/25")}, lev)
if err != nil {
t.Fatal(err)
}
dacha, _ := res["id"].(string)
if x := dev(dacha); res["waiting"] != true || res["config"] != nil || !x.Waiting || x.Behind != nil || x.Asked == 0 || x.CreatedBy != "portal" ||
!reflect.DeepEqual(x.Ask, []string{"192.168.88.0/24", "192.168.3.128/25"}) {
t.Fatalf("a router asked for: %v, %+v", res, x)
}
counts("a router that waits", 1, 0)
if loaded(dacha) || !loaded("lphone") {
t.Error("a router that waits is in the tunnel, or Lev's phone is not")
}
_, m, _ := st.Read()
if cfg := configOf(d, m, dacha); cfg != nil {
t.Errorf("a router that waits has a configuration: %v", cfg)
}
if _, err := DeviceRotateAs(d, map[string]any{"id": dacha}, lev); err == nil || !strings.Contains(err.Error(), "waits for an answer") {
t.Errorf("new keys for a router that waits: %v", err)
}
// taken as it is, it would share addresses with Kira's: the admin is told whose, and nothing is written
if got := answer(dacha, "accept", nil); got != "Network 192.168.3.128/25 is taken: 192.168.3.0/24 is behind Kphone (Kira). Two sites need different addresses." {
t.Errorf("accepting a clash: %v", got)
}
if !dev(dacha).Waiting {
t.Error("a refused answer took the router")
}
// Lev takes that one back; then the admin takes the router with what is left
if got := wish(dacha, map[string]any{"networks": nets("192.168.88.0/24")}); got != "" || !reflect.DeepEqual(dev(dacha).Ask, []string{"192.168.88.0/24"}) {
t.Errorf("a wish changed while it waits: %q, %v", got, dev(dacha).Ask)
}
if got := answer(dacha, "accept", nil); got != nil {
t.Fatalf("accept: %v", got)
}
if x := dev(dacha); x.Waiting || x.Ask != nil || x.Asked != 0 || !reflect.DeepEqual(x.Behind, []string{"192.168.88.0/24"}) || !loaded(dacha) {
t.Errorf("after the answer: %+v", x)
}
counts("accepted with a network", 2, 0)
if got := answer(dacha, "accept", nil); got != "This device asks for nothing." {
t.Errorf("an answer to nothing: %v", got)
}
if got := answer("lphone", "decline", nil); got != "This device asks for nothing." {
t.Errorf("an answer to a phone: %v", got)
}
// one more network: asked for, turned down with a reason, put back, turned down, and put away by Lev
if got := wish(dacha, map[string]any{"networks": nets("192.168.88.0/24", "192.168.89.0/24")}); got != "" {
t.Fatal(got)
}
if x := dev(dacha); !reflect.DeepEqual(x.Ask, []string{"192.168.89.0/24"}) || !reflect.DeepEqual(x.Behind, []string{"192.168.88.0/24"}) || x.Asked == 0 {
t.Errorf("one more asked for: %+v", x)
}
counts("a network asked for", 2, 0)
if got := answer(dacha, "decline", map[string]any{"reason": "overlap"}); got != nil {
t.Fatal(got)
}
if x := dev(dacha); x.Declined != "overlap" || !reflect.DeepEqual(x.DeclinedNets, []string{"192.168.89.0/24"}) || x.Ask != nil || x.Asked != 0 {
t.Errorf("turned down: %+v", x)
}
if got := answer(dacha, "reopen", map[string]any{"networks": nets("192.168.89.0/24")}); got != nil {
t.Fatal(got)
}
if x := dev(dacha); x.Declined != "" || x.DeclinedNets != nil || !reflect.DeepEqual(x.Ask, []string{"192.168.89.0/24"}) {
t.Errorf("the question put back: %+v", x)
}
if got := answer(dacha, "decline", map[string]any{"reason": "because"}); got != nil || dev(dacha).Declined != "plain" {
t.Errorf("a reason nobody knows: %v, %q", got, dev(dacha).Declined)
}
if got := wish(dacha, map[string]any{"networks": nets("192.168.88.0/24"), "cleared": nets("192.168.89.0/24")}); got != "" || dev(dacha).Declined != "" || dev(dacha).DeclinedNets != nil {
t.Errorf("put away: %q, %+v", got, dev(dacha))
}
counts("answers that move no route", 2, 0)
// accepted and undone: the network is asked for again, and its route goes
wish(dacha, map[string]any{"networks": nets("192.168.88.0/24", "192.168.89.0/24")})
if got := answer(dacha, "accept", nil); got != nil || !reflect.DeepEqual(dev(dacha).Behind, []string{"192.168.88.0/24", "192.168.89.0/24"}) {
t.Fatalf("a network accepted: %v, %v", got, dev(dacha).Behind)
}
if got := answer(dacha, "reopen", map[string]any{"networks": nets("192.168.89.0/24")}); got != nil {
t.Fatal(got)
}
if x := dev(dacha); !reflect.DeepEqual(x.Behind, []string{"192.168.88.0/24"}) || !reflect.DeepEqual(x.Ask, []string{"192.168.89.0/24"}) {
t.Errorf("an accept undone: %+v", x)
}
counts("accepted and undone", 4, 0)
// two asked for, each with its own answer: one taken, the other turned down, neither touching the other
wish(dacha, map[string]any{"networks": nets("192.168.88.0/24", "192.168.89.0/24", "192.168.94.0/24")})
if got := answer(dacha, "accept", map[string]any{"networks": nets("192.168.94.0/24")}); got != nil {
t.Fatal(got)
}
if x := dev(dacha); !reflect.DeepEqual(x.Behind, []string{"192.168.88.0/24", "192.168.94.0/24"}) || !reflect.DeepEqual(x.Ask, []string{"192.168.89.0/24"}) || x.Asked == 0 {
t.Errorf("one of two taken: %+v", x)
}
if got := answer(dacha, "accept", map[string]any{"networks": nets("192.168.99.0/24")}); got != "This device asks for nothing." {
t.Errorf("an answer to a network nobody asked for: %v", got)
}
if got := answer(dacha, "decline", map[string]any{"networks": nets("192.168.89.0/24"), "reason": "unneeded"}); got != nil {
t.Fatal(got)
}
if x := dev(dacha); x.Ask != nil || x.Declined != "unneeded" || !reflect.DeepEqual(x.DeclinedNets, []string{"192.168.89.0/24"}) {
t.Errorf("the other turned down: %+v", x)
}
counts("each with its own answer", 5, 0)
// taking a network away is Lev's own to do, at once; what was asked for goes with the list
if got := wish(dacha, map[string]any{"networks": nets()}); got != "" || dev(dacha).Behind != nil || dev(dacha).Ask != nil {
t.Errorf("all taken away: %q, %+v", got, dev(dacha))
}
counts("taken away", 6, 0)
// networks at once: the server's owner said so
if res := ask(t, d, "server_update", map[string]any{"id": "open", "values": map[string]any{"portal_nets": "free"}}); res["error"] != nil {
t.Fatal(res["error"])
}
if got := wish(dacha, map[string]any{"networks": nets("192.168.90.0/24")}); got != "" || !reflect.DeepEqual(dev(dacha).Behind, []string{"192.168.90.0/24"}) || dev(dacha).Ask != nil {
t.Errorf("at once: %q, %+v", got, dev(dacha))
}
counts("a network at once", 8, 0)
// then everything is held against it, and Lev is not told whose the addresses are
if got := wish(dacha, map[string]any{"networks": nets("192.168.90.0/24", "192.168.3.0/25")}); got != "Network 192.168.3.0/25 is taken on this server. Ask the admin which addresses are free." {
t.Errorf("another person's network: %q", got)
}
// a router still waits for the admin, with its network asked for all the same
res, err = DeviceCreateAs(d, map[string]any{"person": "lev", "name": "Cabin", "kind": "router", "networks": nets("192.168.92.0/24")}, lev)
cabin, _ := res["id"].(string)
if err != nil || !dev(cabin).Waiting || !reflect.DeepEqual(dev(cabin).Ask, []string{"192.168.92.0/24"}) {
t.Fatalf("a router asked for where networks are free: %v", err)
}
// turned down, it stays for Lev to read why; the answer undone, it is asked for again; then taken
if got := answer(cabin, "decline", map[string]any{"reason": "talk"}); got != nil || !dev(cabin).Waiting || dev(cabin).Declined != "talk" || loaded(cabin) {
t.Errorf("a router turned down: %v, %+v", got, dev(cabin))
}
if got := answer(cabin, "accept", nil); got != "This device asks for nothing." {
t.Errorf("accepting what was turned down: %v", got)
}
if got := answer(cabin, "reopen", map[string]any{"router": true}); got != nil || dev(cabin).Declined != "" || !dev(cabin).Waiting {
t.Errorf("the router asked for again: %v, %+v", got, dev(cabin))
}
counts("a router turned down and back", 8, 0)
if got := answer(cabin, "accept", nil); got != nil || dev(cabin).Waiting || !loaded(cabin) || !reflect.DeepEqual(dev(cabin).Behind, []string{"192.168.92.0/24"}) {
t.Errorf("the router taken: %v, %+v", got, dev(cabin))
}
counts("a router taken with its network", 9, 0)
// that undone: it waits again, out of the tunnel (its network stays written and is in effect nowhere)
if got := answer(cabin, "reopen", map[string]any{"router": true}); got != nil || !dev(cabin).Waiting || loaded(cabin) {
t.Errorf("a taken router put back: %v, %+v", got, dev(cabin))
}
counts("put back", 9, 1)
if _, m, _ := st.Read(); m.BehindOf(nil)[cabin] != nil {
t.Errorf("a waiting router's network is in effect: %v", m.BehindOf(nil)[cabin])
}
// routers at once, and no routers at all
ask(t, d, "server_update", map[string]any{"id": "open", "values": map[string]any{"portal_routers": "free"}})
res, err = DeviceCreateAs(d, map[string]any{"person": "lev", "name": "Garage", "kind": "router", "networks": nets("192.168.93.0/24")}, lev)
garage, _ := res["id"].(string)
if err != nil || res["config"] == nil || dev(garage).Waiting || !reflect.DeepEqual(dev(garage).Behind, []string{"192.168.93.0/24"}) || !loaded(garage) {
t.Errorf("a router at once: %v, %v", err, res)
}
counts("a router at once", 11, 1)
if _, err := DeviceRotateAs(d, map[string]any{"id": "lphone", "kind": "router"}, lev); err != nil {
t.Errorf("a phone made a router where routers are free: %v", err)
}
ask(t, d, "server_update", map[string]any{"id": "open", "values": map[string]any{"portal_routers": "off"}})
if _, err := DeviceCreateAs(d, map[string]any{"person": "lev", "name": "Shed", "kind": "router"}, lev); err == nil ||
err.Error() != "The admin adds routers on this server. Ask them for one." {
t.Errorf("a router where the owner adds them: %v", err)
}
// on Boris's server the rule is the one a server starts with: no device is made a router there
if _, err := DeviceRotateAs(d, map[string]any{"id": "phone", "kind": "router"}, Actor{By: "portal", Person: "anna"}); err == nil ||
err.Error() != "Add the router as a new device: a device cannot be made one here." {
t.Errorf("a phone made a router: %v", err)
}
if res := ask(t, d, "server_update", map[string]any{"id": "open", "values": map[string]any{"portal_nets": "sometimes"}}); res["error"] != "Unknown choice for routers on people's pages." {
t.Errorf("a rule nobody knows: %v", res["error"])
}
// "ask" is what an unset rule means: back to it, the options leave the file
ask(t, d, "server_update", map[string]any{"id": "open", "values": map[string]any{"portal_routers": "ask", "portal_nets": "ask"}})
if _, m, _ := st.Read(); m.Servers["open"].PortalRouters != "ask" || m.Servers["open"].PortalNets != "ask" {
t.Errorf("the rules back: %q, %q", m.Servers["open"].PortalRouters, m.Servers["open"].PortalNets)
}
// the log has what was asked and answered, in order, each by who did it
var said []string
for _, e := range journalOf(t, dir) {
if e["type"] == "device_changed" && (e["device"] == dacha || e["device"] == cabin) {
said = append(said, e["by"].(string)+": "+e["detail"].(string))
}
}
want := []string{
"portal: asked=router, asked=192.168.88.0/24 192.168.3.128/25",
"portal: withdrew=192.168.3.128/25",
"admin: accepted=router, accepted=192.168.88.0/24",
"portal: asked=192.168.89.0/24",
"admin: declined=192.168.89.0/24, reason=overlap",
"admin: reopened=192.168.89.0/24",
"admin: declined=192.168.89.0/24, reason=plain",
"portal: asked=192.168.89.0/24",
"admin: accepted=192.168.89.0/24",
"admin: reopened=192.168.89.0/24",
"portal: asked=192.168.94.0/24",
"admin: accepted=192.168.94.0/24",
"admin: declined=192.168.89.0/24, reason=unneeded",
"portal: networks=none",
"portal: networks=192.168.90.0/24",
"portal: asked=router, asked=192.168.92.0/24",
"admin: declined=router, reason=talk",
"admin: reopened=router",
"admin: accepted=router, accepted=192.168.92.0/24",
"admin: reopened=router",
}
if !reflect.DeepEqual(said, want) {
t.Errorf("logged:\n%s\nwant:\n%s", strings.Join(said, "\n"), strings.Join(want, "\n"))
}
}
// The same through a person's own link: what the page is told about routers, a router asked for, its
// networks changed while it waits, and no configuration until the admin answers.
func TestARouterThroughThePersonsLink(t *testing.T) {
d, st, dir, _, _ := sitesStand(t)
c, _, _ := st.Read()
const code = "kamiro-telosu-bavine"
c.Set("lev", "code", code)
c.Set("settings", "portal_enabled", "1")
os.WriteFile(dir+"/dawnline", []byte(c.Format()), 0o600)
d.World = func() *view.World {
_, m, _ := st.Read()
return &view.World{M: m, Usage: usage.Empty(), Now: time.Now().Unix()}
}
call := func(method, path, body string) (int, map[string]any) {
q := PortalRequest{Method: method, Path: "/" + code + path, Host: "vpn.example.org:4500", HTTPS: true, Remote: "198.51.100.9"}
if method == "POST" {
q.ContentType, q.Origin, q.Body, q.ContentLength = "application/json", "https://"+q.Host, strings.NewReader(body), int64(len(body))
}
a := Portal(d, q)
var out map[string]any
json.Unmarshal(a.Body, &out)
return a.Status, out
}
rules := func() (routers, nets any, router bool) {
status, state := call("GET", "/api/state", "")
if status != 200 {
t.Fatalf("the state: %d", status)
}
for _, k := range state["kinds"].([]any) {
router = router || k == "router"
}
server := state["server"].(map[string]any)
return server["routers"], server["nets"], router
}
if routers, nets, router := rules(); routers != "ask" || nets != "ask" || !router {
t.Errorf("the rules a server starts with: %v, %v, router offered: %v", routers, nets, router)
}
status, res := call("POST", "/api/devices", `{"name":"Dacha","kind":"router","networks":["192.168.88.9/24"]}`)
id, _ := res["id"].(string)
if status != 200 || res["waiting"] != true || res["config"] != nil || id == "" {
t.Fatalf("a router asked for: %d %v", status, res)
}
// the page's state has it as it stands: waiting, with what is asked for
var mine map[string]any
for _, x := range res["state"].(map[string]any)["person"].(map[string]any)["devices"].([]any) {
if dv := x.(map[string]any); dv["id"] == id {
mine = dv
}
}
if mine == nil || mine["waiting"] != true || !reflect.DeepEqual(mine["ask"], []any{"192.168.88.0/24"}) || mine["behind"] != nil {
t.Errorf("the device as the page has it: %v", mine)
}
if status, res = call("GET", "/api/devices/"+id+"/config", ""); status != 409 || res["error"] != "This router waits for an answer to its request: it has no configuration yet." {
t.Errorf("its configuration: %d %v", status, res)
}
if status, res = call("POST", "/api/devices/"+id, `{"action":"reconnect"}`); status != 400 || res["error"] != "This router waits for an answer to its request: it has no configuration yet." {
t.Errorf("set up again while it waits: %d %v", status, res)
}
if status, res = call("POST", "/api/devices/"+id, `{"action":"networks","networks":["192.168.88.0/24","192.168.89.0/24"]}`); status != 200 {
t.Fatalf("its networks changed: %d %v", status, res)
}
if _, m, _ := st.Read(); !reflect.DeepEqual(m.Devices[id].Ask, []string{"192.168.88.0/24", "192.168.89.0/24"}) || m.Devices[id].Behind != nil {
t.Errorf("what it asks for now: %+v", m.Devices[id])
}
if status, res = call("POST", "/api/devices/"+id, `{"action":"networks","networks":["8.8.8.0/24"]}`); status != 400 ||
res["error"] != "Network 8.8.8.0/24 is not private (10.x, 172.16-31.x, 192.168.x)." {
t.Errorf("a public network: %d %v", status, res)
}
if status, res = call("POST", "/api/devices/lphone", `{"action":"networks","networks":["192.168.95.0/24"]}`); status != 400 || res["error"] != "Only a router has networks behind it." {
t.Errorf("a network behind a phone: %d %v", status, res)
}
// where the owner adds routers, the page does not offer one and the router takes none
c, _, _ = st.Read()
c.Set("open", "portal_routers", "off")
os.WriteFile(dir+"/dawnline", []byte(c.Format()), 0o600)
if routers, _, router := rules(); routers != "off" || router {
t.Errorf("routers off: %v, router offered: %v", routers, router)
}
if status, res = call("POST", "/api/devices", `{"name":"Shed","kind":"router"}`); status != 400 || res["error"] != "The admin adds routers on this server. Ask them for one." {
t.Errorf("a router where the owner adds them: %d %v", status, res)
}
}
+365
View File
@@ -0,0 +1,365 @@
package methods
import (
"archive/tar"
"bytes"
"compress/gzip"
"encoding/base64"
"errors"
"io"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
"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/journal"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/zone"
)
// How many backups of each kind stay (the newest): the nightly ones must not push out what the admin made
// by hand or what a removal left behind. A kind not named keeps three.
var backupKeep = map[string]int{"manual": 10, "auto": 7, "pre-delete": 5, "pre-remove": 3, "pre-update": 3, "pre-restore": 3, "pre-import": 3}
// Kinds that hold the settings alone: made often, and never to bring history back.
var configOnly = map[string]bool{"auto": true, "pre-delete": true, "pre-remove": true, "pre-update": true}
var backupKind = regexp.MustCompile(`^[a-z0-9-]{1,24}$`)
// backupCreate makes a backup now and returns its name (dawnline.backup backup_create): the settings, and
// for the kinds that protect data about to be replaced the traffic history and the activity log too, as a
// tar.gz with the paths ucode's restore unpacks at /. Written whole or not at all. Of each kind the oldest
// beyond its number are removed.
func backupCreate(d Deps, reason string, keep ...string) (string, error) {
kind := "manual"
if backupKind.MatchString(reason) {
kind = reason
}
// the live counters and log first, so the backup has them as they are now
if !configOnly[kind] {
if raw, err := os.ReadFile(d.RunDir + "/usage.json"); err == nil {
files.Write(d.DataDir+"/usage.json", raw, 0o600)
}
if d.Store != nil {
if _, m, err := d.Store.Read(); err == nil {
journal.Log{Run: d.RunDir, Data: d.DataDir}.Persist(journal.PolicyOf(m.Settings.Retention), clock())
}
}
}
dir := d.DataDir + "/backups"
os.MkdirAll(dir, 0o700)
name := "dawnline-" + time.Unix(clock(), 0).In(zone.Local()).Format("20060102-150405")
if kind != "manual" {
name += "-" + kind
}
name += ".tar.gz"
want := [][2]string{{"etc/config/dawnline", d.Config}}
if !configOnly[kind] {
want = append(want, [2]string{"etc/dawnline/usage.json", d.DataDir + "/usage.json"}, [2]string{"etc/dawnline/events.json", d.DataDir + "/events.json"})
}
// not a backup's name until it is complete
tmp := dir + "/." + name + ".tmp"
err := func() error {
f, err := os.OpenFile(tmp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
if err != nil {
return err
}
defer f.Close()
zw := gzip.NewWriter(f)
tw := tar.NewWriter(zw)
for _, w := range want {
raw, err := os.ReadFile(w[1])
if err != nil {
continue
}
st, _ := os.Stat(w[1])
if err := tw.WriteHeader(&tar.Header{Name: w[0], Mode: 0o600, Size: int64(len(raw)), ModTime: st.ModTime(), Typeflag: tar.TypeReg, Uname: "root", Gname: "root"}); err != nil {
return err
}
if _, err := tw.Write(raw); err != nil {
return err
}
}
if err := tw.Close(); err != nil {
return err
}
if err := zw.Close(); err != nil {
return err
}
return f.Sync()
}()
if err == nil {
err = os.Rename(tmp, dir+"/"+name)
}
if err != nil {
os.Remove(tmp)
return "", errors.New("Backup failed: the archive could not be written (is the flash full?)")
}
os.Chmod(dir+"/"+name, 0o600)
seen := map[string]int{}
for _, b := range backups(dir) {
// the new one, and the backup being restored: it must outlive the backup made just before restoring it
if b["name"] == name || (len(keep) > 0 && b["name"] == keep[0]) {
continue
}
k := b["reason"].(string)
keep, known := backupKeep[k]
if !known {
keep = 3
}
if seen[k]++; seen[k] > keep {
os.Remove(dir + "/" + b["name"].(string))
}
}
return name, nil
}
// what a backup may hold, by its path in the archive
var backupFiles = []string{"etc/config/dawnline", "etc/dawnline/usage.json", "etc/dawnline/events.json"}
// The unpacked size a backup may have (an upload itself is limited to 4 MB).
const maxUnpacked = 16 * 1024 * 1024
// unpack reads a backup archive after checking it as dawnline.backup check_archive does: only Dawnline's own
// files, all of them regular files (no links, devices or directories), within a sane unpacked size, the
// settings among them.
func unpack(raw []byte) (map[string][]byte, error) {
invalid := errors.New("Not a valid backup archive.")
zr, err := gzip.NewReader(bytes.NewReader(raw))
if err != nil {
return nil, invalid
}
tr := tar.NewReader(zr)
out := map[string][]byte{}
var total int64
for {
h, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, invalid
}
name := strings.TrimPrefix(strings.TrimPrefix(h.Name, "."), "/")
if h.Typeflag != tar.TypeReg {
return nil, said{"Unexpected entry in backup: %s", []any{name}}
}
known := false
for _, f := range backupFiles {
known = known || f == name
}
if !known {
return nil, said{"Unexpected file in backup: %s", []any{name}}
}
if total += h.Size; total > maxUnpacked {
return nil, errors.New("Backup is too large.")
}
data, err := io.ReadAll(io.LimitReader(tr, maxUnpacked+1))
if err != nil {
return nil, invalid
}
out[name] = data
}
if out["etc/config/dawnline"] == nil {
return nil, errors.New("Backup does not contain /etc/config/dawnline.")
}
return out, nil
}
// usable refuses settings that cannot replace the current ones, listing what is wrong with them.
func usable(d Deps, t *i18n.Catalog, text string) error {
c, err := uci.Parse(text)
if err != nil || len(c.Sections) == 0 {
return errors.New("The configuration in this file cannot be read.")
}
// the router's networks that are not Dawnline's own tunnels' (the current settings say which those are)
var foreign []string
if d.Addresses != nil && d.Store != nil {
ours := map[string]bool{}
if _, now, err := d.Store.Read(); err == nil {
for _, s := range now.Servers {
ours[s.Ifname] = true
}
}
for _, a := range d.Addresses() {
if !ours[a.Iface] {
foreign = append(foreign, a.CIDR)
}
}
}
if errs := health.ConfigErrors(model.Load(c, nil), t, parseFile("/etc/config/network"), foreign); len(errs) > 0 {
return said{"This configuration can't be used: %s", []any{strings.Join(errs, " ")}}
}
return nil
}
// restore puts a backup's files in place: checked first, a backup of what is there made before, the
// settings written first under the config lock (when they cannot be, nothing else is touched), then the
// counters and the log, which the watcher takes up from flash; then ucode's full apply.
func restore(d Deps, lang string, raw []byte, name string) (map[string]any, error) {
parts, err := unpack(raw)
if err != nil {
return nil, err
}
if err := usable(d, i18n.Use(d.Catalogs, lang), string(parts["etc/config/dawnline"])); err != nil {
return nil, err
}
if _, err := backupCreate(d, "pre-restore", name); err != nil {
return nil, err
}
os.MkdirAll(d.DataDir, 0o700)
err = func() error {
defer files.Lock(d.RunDir, "config")()
for _, f := range [][2]string{{"etc/config/dawnline", d.Config}, {"etc/dawnline/usage.json", d.DataDir + "/usage.json"}, {"etc/dawnline/events.json", d.DataDir + "/events.json"}} {
if data, has := parts[f[0]]; has {
if files.Write(f[1], data, 0o600) != nil {
return said{"Couldn't write %s. Is the flash full?", []any{"/" + f[0]}}
}
}
}
return nil
}()
if err != nil {
return nil, err
}
// the live counters and log must follow the restored files: the watcher loads them from flash anew
os.Remove(d.RunDir + "/usage.json")
os.Remove(d.RunDir + "/events.json")
files.Write(d.RunDir+"/usage.reload", []byte(strconv.FormatInt(clock(), 10)), 0o600)
applied := map[string]any{}
if d.Apply != nil {
applied = d.Apply(lang)
}
if d.Told != nil {
d.Told()
}
return applied, nil
}
// backupRestore brings a kept backup back.
func backupRestore(d Deps, name, lang string) (map[string]any, error) {
if !backupFile.MatchString(name) {
return nil, errors.New("Invalid backup name.")
}
raw, err := os.ReadFile(d.DataDir + "/backups/" + name)
if err != nil {
return nil, errors.New("Backup not found.")
}
out, err := restore(d, lang, raw, name)
if err == nil && d.Log != nil {
d.Log.Admin("dawnline", "restored", "backup", name, "")
}
return out, err
}
// backupUpload brings back a backup the admin sends (base64, 4 MB at most).
func backupUpload(d Deps, data, lang string) (map[string]any, error) {
raw, err := base64.StdEncoding.DecodeString(strings.Trim(data, blank))
if err != nil || len(raw) == 0 || len(raw) > 4*1024*1024 {
return nil, errors.New("Upload is empty or too large.")
}
out, err := restore(d, lang, raw, "")
if err == nil && d.Log != nil {
d.Log.Admin("dawnline", "restored", "backup", "from a file", "")
}
return out, err
}
var (
sectionName = regexp.MustCompile(`^[A-Za-z0-9_]{1,64}$`)
optionName = regexp.MustCompile(`^[a-z0-9_]{1,32}$`)
)
// importAll replaces the settings with an export (dawnline.backup import_json): only Dawnline's kinds of
// section, options that are strings or lists of them; checked as a restore's, a backup made before. A
// section's options are written in the order of their names (the bus hands them over as a map).
func importAll(d Deps, data any, lang string) (map[string]any, error) {
o, _ := data.(map[string]any)
sections, isList := o["sections"].([]any)
if o == nil || o["format"] != "dawnline" || !isList {
return nil, errors.New("Not a Dawnline export.")
}
kinds := map[string]bool{"settings": true, "server": true, "person": true, "device": true, "service": true, "circle": true}
c := &uci.Config{}
for _, x := range sections {
s, _ := x.(map[string]any)
name, _ := s[".name"].(string)
kind, _ := s[".type"].(string)
if s == nil || !kinds[kind] || !sectionName.MatchString(name) {
return nil, said{"Invalid section %s.", []any{name}}
}
c.Add(kind, name)
keys := make([]string, 0, len(s))
for k := range s {
if !strings.HasPrefix(k, ".") {
keys = append(keys, k)
}
}
sort.Strings(keys)
for _, k := range keys {
if !optionName.MatchString(k) {
return nil, said{"Invalid option %s.", []any{k}}
}
switch v := s[k].(type) {
case string:
// an empty value is written as it is: a cursor would drop it, a file keeps it
sec := c.Section(name)
sec.Values[k] = uci.Value{Str: v}
sec.Order = append(sec.Order, k)
case []any:
var list []string
for _, e := range v {
str, isString := e.(string)
if !isString {
return nil, said{"Invalid option %s.", []any{k}}
}
list = append(list, str)
}
if len(list) > 0 {
sec := c.Section(name)
sec.Values[k] = uci.Value{IsList: true, List: list}
sec.Order = append(sec.Order, k)
}
default:
return nil, said{"Invalid option %s.", []any{k}}
}
}
}
text := c.Format()
if err := usable(d, i18n.Use(d.Catalogs, lang), text); err != nil {
return nil, err
}
if _, err := backupCreate(d, "pre-import"); err != nil {
return nil, err
}
err := func() error {
defer files.Lock(d.RunDir, "config")()
if files.Write(d.Config, []byte(text), 0o600) != nil {
return said{"Couldn't write %s. Is the flash full?", []any{"/etc/config/dawnline"}}
}
return nil
}()
if err != nil {
return nil, err
}
applied := map[string]any{}
if d.Apply != nil {
applied = d.Apply(lang)
}
if d.Log != nil {
d.Log.Admin("dawnline", "imported", "", "settings from a file", "")
}
if d.Told != nil {
d.Told()
}
return applied, nil
}
// BackupCreate saves a backup of a kind ("auto": the nightly one) for who keeps the router on its own.
func BackupCreate(d Deps, kind string) (string, error) { return backupCreate(d, kind) }
+186
View File
@@ -0,0 +1,186 @@
package methods
import (
"encoding/json"
"os"
"reflect"
"strings"
"testing"
"unicode"
"unicode/utf8"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/store"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/view"
)
// sitesStand is access's household on the golden's router (which has 192.168.50.0/24 and 192.168.77.0/24
// itself) with Boris's and Kira's devices made routers, and how often the full apply and the sync were
// asked for.
func sitesStand(t *testing.T) (d Deps, st *store.Store, dir string, applied, synced *int) {
t.Helper()
dir = t.TempDir()
text, _ := os.ReadFile("../access/testdata/access.uci")
c, err := uci.Parse(string(text))
if err != nil {
t.Fatal(err)
}
c.Set("bphone", "kind", "router")
c.Set("kphone", "kind", "router")
c.Set("lev", "device_limit", "10")
os.WriteFile(dir+"/dawnline", []byte(c.Format()), 0o600)
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)
}
applied, synced = new(int), new(int)
st = &store.Store{Config: dir + "/dawnline", RunDir: dir}
d = Deps{World: func() *view.World { return &view.World{} }, Store: st, Config: dir + "/dawnline", RunDir: dir, DataDir: dir + "/data",
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 }})
},
AWG: func() map[string]any { return map[string]any{"tools": true, "kmod": true} },
Apply: func(string) map[string]any { *applied++; return map[string]any{"warning": nil} },
Sync: func(string) any { *synced++; return nil }}
// keys and ids as a row of made-up ones, each its own
keys, hex := newKeys, randHex
t.Cleanup(func() { newKeys, randHex = keys, hex })
n := 0
newKeys = func() Keys {
n++
k := func(c byte) string { return strings.Repeat(string(c), 41) + string(rune('A'+n)) + "A=" }
return Keys{Private: k('P'), Public: k('Q'), Preshared: k('R')}
}
made := 0
randHex = func(int) string { made++; return "0a0b0c0" + string(rune('0'+made)) }
return d, st, dir, applied, synced
}
// The networks behind a router, as the admin sets them: access's household on the golden's router (which
// has 192.168.50.0/24 and 192.168.77.0/24 itself), every refusal in its words, and the full apply asked
// for only when something changed.
func TestNetworksBehindARouter(t *testing.T) {
d, st, dir, ap, sy := sitesStand(t)
behind := func(id string) []string {
_, m, err := st.Read()
if err != nil {
t.Fatal(err)
}
return m.Devices[id].Behind
}
set := func(id string, nets ...any) map[string]any {
return ask(t, d, "device_networks", map[string]any{"id": id, "networks": nets})
}
// two networks, each told from its first address, the one written twice once
res := set("bphone", " 192.168.2.9/24 ", "172.20.0.0/16", "192.168.2.0/24", "")
if res["error"] != nil || !reflect.DeepEqual(behind("bphone"), []string{"192.168.2.0/24", "172.20.0.0/16"}) || *ap != 1 || *sy != 0 {
t.Fatalf("set: %v, %v, applied %d, synced %d", res, behind("bphone"), *ap, *sy)
}
if !reflect.DeepEqual(res["networks"], []string{"192.168.2.0/24", "172.20.0.0/16"}) {
t.Errorf("answered %v", res["networks"])
}
// the same again: nothing written, nothing applied
if res = set("bphone", "192.168.2.0/24", "172.20.0.0/16"); res["error"] != nil || *ap != 1 {
t.Errorf("the same list: %v, applied %d", res, *ap)
}
for _, r := range []struct {
id string
nets []any
want string
}{
{"bphone", []any{"192.168.2.0"}, "Network 192.168.2.0 is not written like 192.168.2.0/24."},
{"bphone", []any{"10.0.0.0/8"}, "Network 10.0.0.0/8 is too wide: /16 at the most."},
{"bphone", []any{"8.8.8.0/24"}, "Network 8.8.8.0/24 is not private (10.x, 172.16-31.x, 192.168.x)."},
{"bphone", []any{"192.168.4.0/24", "192.168.4.128/25"}, "Networks 192.168.4.0/24 and 192.168.4.128/25 share addresses."},
{"bphone", []any{"10.77.0.0/16"}, "Network 10.77.0.0/16 is taken: 10.77.0.0/24 is the tunnel network of Home. The two ends of a tunnel need different addresses."},
{"bphone", []any{"192.168.50.0/25"}, "Network 192.168.50.0/25 is taken: the router has 192.168.50.0/24 itself. The two ends of a tunnel need different addresses."},
{"kphone", []any{"172.20.5.0/24"}, "Network 172.20.5.0/24 is taken: 172.20.0.0/16 is behind Bphone (Boris). Two sites need different addresses."},
{"phone", []any{"192.168.9.0/24"}, "Only a router has networks behind it."},
{"nobody", []any{"192.168.9.0/24"}, "Unknown device."},
{"bphone", []any{"192.168.10.0/24", "192.168.11.0/24", "192.168.12.0/24", "192.168.13.0/24", "192.168.14.0/24", "192.168.15.0/24",
"192.168.16.0/24", "192.168.17.0/24", "192.168.18.0/24"}, "A router has 8 networks behind it at the most."},
} {
if res := set(r.id, r.nets...); res["error"] != r.want {
t.Errorf("%s %v: %v, want %q", r.id, r.nets, res["error"], r.want)
}
// the page shows a refusal as it came only when it begins as a sentence does (web/src/admin/failure.ts):
// one that began with the network's address would be read as a program's error
if first, _ := utf8.DecodeRuneInString(r.want); !unicode.IsUpper(first) {
t.Errorf("%q does not begin with a capital letter", r.want)
}
}
if *ap != 1 || !reflect.DeepEqual(behind("bphone"), []string{"192.168.2.0/24", "172.20.0.0/16"}) {
t.Errorf("a refusal wrote or applied: %d, %v", *ap, behind("bphone"))
}
// the names in a refusal are the settings' own
_, m, _ := st.Read()
if m.Servers["home"].Name != "Home" || m.Devices["bphone"].Name != "Bphone" || m.People["boris"].Name != "Boris" {
t.Fatalf("the household's names moved: %q, %q, %q", m.Servers["home"].Name, m.Devices["bphone"].Name, m.People["boris"].Name)
}
// none: the option goes, and the routes with it
if res = set("bphone"); res["error"] != nil || behind("bphone") != nil || *ap != 2 {
t.Errorf("none: %v, %v, applied %d", res, behind("bphone"), *ap)
}
if file, _ := os.ReadFile(dir + "/dawnline"); strings.Contains(string(file), "behind_net") {
t.Error("the option is still in the file")
}
// a router added with its networks is applied in full; another device is loaded by the sync, as before
res = ask(t, d, "device_create", map[string]any{"person": "lev", "name": "Dacha", "kind": "router", "networks": []any{"192.168.88.1/24"}})
if res["error"] != nil || res["id"] != "d_0a0b0c01" || !reflect.DeepEqual(behind("d_0a0b0c01"), []string{"192.168.88.0/24"}) || *ap != 3 || *sy != 0 {
t.Fatalf("a router with its network: %v, applied %d, synced %d", res["error"], *ap, *sy)
}
if res = ask(t, d, "device_create", map[string]any{"person": "lev", "name": "Tablet", "kind": "tablet", "networks": []any{"192.168.89.0/24"}}); res["error"] != "Only a router has networks behind it." {
t.Errorf("a tablet with a network: %v", res["error"])
}
if res = ask(t, d, "device_create", map[string]any{"person": "lev", "name": "Second", "kind": "router", "networks": []any{"192.168.88.128/25"}}); res["error"] !=
"Network 192.168.88.128/25 is taken: 192.168.88.0/24 is behind Dacha (Lev). Two sites need different addresses." {
t.Errorf("a second router on the first one's addresses: %v", res["error"])
}
if *ap != 3 || *sy != 0 {
t.Errorf("refusals applied: %d, %d", *ap, *sy)
}
// set up again as a phone: what is no router has nothing behind it
if res = ask(t, d, "device_rotate", map[string]any{"id": "d_0a0b0c01", "kind": "phone"}); res["error"] != nil || behind("d_0a0b0c01") != nil || *ap != 4 {
t.Errorf("a router made a phone: %v, %v, applied %d", res["error"], behind("d_0a0b0c01"), *ap)
}
// and the log has each change in its words
var details []string
for _, e := range journalOf(t, dir) {
if e["type"] == "device_changed" {
details = append(details, e["detail"].(string))
}
}
want := []string{"networks=192.168.2.0/24 172.20.0.0/16", "networks=none", "networks=192.168.88.0/24", "kind=phone, networks=none"}
if !reflect.DeepEqual(details, want) {
t.Errorf("logged %v, want %v", details, want)
}
}
// journalOf is the activity log a test's methods wrote, the oldest first.
func journalOf(t *testing.T, dir string) []map[string]any {
t.Helper()
var list []map[string]any
raw, err := os.ReadFile(dir + "/events.json")
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(raw, &list); err != nil {
t.Fatal(err)
}
if len(list) > 1 && list[0]["ts"].(float64) > list[len(list)-1]["ts"].(float64) {
for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 {
list[i], list[j] = list[j], list[i]
}
}
return list
}
+147
View File
@@ -0,0 +1,147 @@
package methods
import (
"errors"
"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/view"
)
var olderDays = regexp.MustCompile(`^[1-9][0-9]{0,3}$`)
// logsClear removes activity by person, device, category and/or age; nothing given clears it all
// (dawnline.rpc.status logs_clear). What went is written in the service log.
func logsClear(w *view.World, d Deps, person, device, category, older string) (map[string]any, error) {
f := journal.Filter{Device: device}
if person != "" {
if w.M.People[person] == nil {
return nil, errors.New("Unknown person.")
}
f.Person = person
}
if category != "" {
known := false
for _, c := range journal.Categories {
known = known || c == category
}
if !known {
return nil, errors.New("Unknown category.")
}
f.Category = category
}
if older != "" {
if !olderDays.MatchString(older) {
return nil, errors.New("Older than must be a number of days.")
}
days, _ := strconv.ParseInt(older, 10, 64)
f.Before = time.Now().Unix() - days*86400
}
removed := journal.Log{Run: d.RunDir, Data: d.DataDir}.Clear(f)
if removed > 0 && d.Log != nil {
note := []string{strconv.Itoa(removed) + " event"}
if removed != 1 {
note[0] += "s"
}
if f.Category != "" {
note = append(note, f.Category)
}
if f.Person != "" {
note = append(note, w.M.People[f.Person].Name)
}
if older != "" {
note = append(note, "older than "+older+" days")
}
d.Log.Admin("dawnline", "cleared", "", "the activity log", strings.Join(note, " · "))
}
return map[string]any{"ok": true, "removed": removed}, nil
}
// serviceLogClear removes the service log's entries, all of them or those older than a number of days.
func serviceLogClear(d Deps, older string) map[string]any {
// as ucode reads the number: anything that is not one clears it all
days, _ := strconv.ParseInt(strings.TrimSpace(older), 10, 64)
if days < 0 {
days = 0
}
removed := 0
if d.Log != nil {
removed = d.Log.Clear(d.DataDir, days)
if removed > 0 {
note := strconv.Itoa(removed) + " entries"
if removed == 1 {
note = "1 entry"
}
if days > 0 {
note += " older than " + older + " days"
}
d.Log.Admin("dawnline", "cleared", "", "the service log", note)
}
}
return map[string]any{"removed": removed}
}
// usageClear forgets traffic history: everyone's, a person's (with their devices and their limit), a
// device's, or a person's limit month alone (dawnline.rpc.status usage_clear).
func usageClear(w *view.World, d Deps, scope, id string) (map[string]any, error) {
m := w.M
who := "for everyone"
switch scope {
case "all":
case "person", "limit":
if m.People[id] == nil {
return nil, errors.New("Unknown person.")
}
who = "for " + m.People[id].Name
case "device":
dev := m.Devices[id]
if dev == nil {
return nil, errors.New("Unknown device.")
}
owner := ""
if p := m.People[dev.Person]; p != nil {
owner = p.Name
}
who = "for " + dev.Name + " (" + owner + ")"
default:
return nil, errors.New("Unknown scope.")
}
if d.ResetUsage == nil || !d.ResetUsage(scope, id) {
return nil, errors.New("The router is still starting. Try again in a moment.")
}
if d.Log != nil {
msg := "reset traffic history"
if scope == "limit" {
msg = "reset the data limit's usage"
}
d.Log.Admin("dawnline", msg, "", who, "")
}
return map[string]any{"ok": true}, nil
}
// geoClear forgets the places found for addresses, in the live copy and on flash.
func geoClear(d Deps) map[string]any {
empty := map[string]any{"ips": map[string]any{}, "home": nil}
os.MkdirAll(d.RunDir, 0o755)
files.WriteJSON(d.RunDir+"/geo.json", empty, 0o600)
os.MkdirAll(d.DataDir, 0o700)
files.WriteJSON(d.DataDir+"/geo.json", empty, 0o600)
return map[string]any{"ok": true}
}
// backupDelete removes a backup by its name.
func backupDelete(dir, name string) (map[string]any, error) {
if !backupFile.MatchString(name) {
return nil, errors.New("Invalid backup name.")
}
if !exists(dir + "/" + name) {
return nil, errors.New("Backup not found.")
}
os.Remove(dir + "/" + name)
return map[string]any{"ok": true}, nil
}
+846
View File
@@ -0,0 +1,846 @@
package methods
import (
"crypto/ecdh"
crand "crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"os"
"regexp"
"strconv"
"strings"
"unicode/utf8"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/handout"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
"github.com/tickcount/luci-app-dawnline/backend/journal"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// DeviceNameMax is the longest name a device takes, in characters.
const DeviceNameMax = 20
// Past ASCII, letters of any language are welcome in a device's name; these blocks are not: symbols,
// punctuation, arrows, maths, emoji and invisibles (both ends in).
var nameSymbols = [][2]rune{{0x80, 0xbf}, {0xd7, 0xd7}, {0xf7, 0xf7}, {0x2000, 0x2bff}, {0x3000, 0x303f}, {0xd800, 0xf8ff}, {0xfe00, 0xfe0f},
{0xfe30, 0xfe4f}, {0xfeff, 0xfeff}, {0xfff0, 0xffff}, {0x1f000, 0x1fbff}, {0xe0000, 0xe007f}}
// ValidDeviceName tells a device's name (trimmed by the caller): 1 to DeviceNameMax characters, not blank,
// each an ASCII letter, digit, space or one of - _ . or, past ASCII, none of nameSymbols. Broken UTF-8 is refused.
func ValidDeviceName(s string) bool {
if strings.Trim(s, blank) == "" || !utf8.ValidString(s) || utf8.RuneCountInString(s) > DeviceNameMax {
return false
}
for _, r := range s {
if r < 0x80 {
if !(r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == ' ' || r == '.' || r == '_' || r == '-') {
return false
}
continue
}
for _, b := range nameSymbols {
if r >= b[0] && r <= b[1] {
return false
}
}
}
return true
}
var linkNumber = regexp.MustCompile(`^[0-9]{1,4}$`)
// Actor is who changes a device (dawnline.devices opts): the admin, or the portal for one person. The
// portal is held to that person's own devices and their device limit, cannot touch a device the admin
// turned off, and may bring the device's public key from the browser. Defer: the page came through the
// device being set up again, so its new keys are loaded a moment after the answer is out.
type Actor struct {
By, Person string
PublicKey string
Defer bool
}
var admin = Actor{By: "admin"}
// own is the device an actor may touch, or why not.
func (who Actor) own(m *model.Model, id string) (*model.Device, error) {
dev := m.Devices[id]
if dev == nil || (who.Person != "" && dev.Person != who.Person) {
if who.Person != "" {
return nil, errors.New("Device not found.")
}
return nil, errors.New("Unknown device.")
}
if who.By == "portal" && dev.Disabled {
return nil, errors.New("The admin has turned this device off.")
}
return dev, nil
}
// deviceUpdate renames a device, turns it off or on, or changes how it connects (its own MTU and
// keep-alive, "" the server's again; LAN Bypass): dawnline.devices update(), as the admin.
func deviceUpdate(d Deps, a map[string]any) (map[string]any, error) {
return DeviceUpdateAs(d, a, admin)
}
// DeviceUpdateAs is deviceUpdate for an actor: only the admin turns devices off or sets the MTU and
// keep-alive; the portal may rename and switch LAN Bypass.
func DeviceUpdateAs(d Deps, a map[string]any, who Actor) (map[string]any, error) {
id := text(a["id"])
name, renames := a["name"].(string)
_, switches := a["disabled"].(bool)
off := truthy(a["disabled"])
if switches && who.By != "admin" {
return nil, errors.New("Only the admin can turn devices off.")
}
if (a["mtu"] != nil || a["keepalive"] != nil) && who.By != "admin" {
return nil, errors.New("Only the admin can change how a device connects.")
}
var was model.Device
var linked, said_ []string
fn := func(c *uci.Config, m *model.Model) error {
dev, err := who.own(m, id)
if err != nil {
return err
}
was = *dev
if renames && name != dev.Name {
name = strings.Trim(name, blank)
if !ValidDeviceName(name) {
return said{"Enter a device name: up to %d letters, digits, spaces and - _ .", []any{DeviceNameMax}}
}
if p := m.People[dev.Person]; p != nil {
for _, o := range p.Devices {
if o != id && lower(m.Devices[o].Name) == lower(name) {
return said{`"%s" already exists.`, []any{name}}
}
}
}
c.Set(id, "name", name)
}
if switches {
if off {
c.Set(id, "disabled", "1")
} else {
c.Delete(id, "disabled")
}
}
// its link values: only those given change, each told with what it became
put := func(k string, v, old *int, shown string) {
if (v == nil) == (old == nil) && (v == nil || *v == *old) {
return
}
if v == nil {
c.Delete(id, k)
} else {
c.Set(id, k, strconv.Itoa(*v))
}
linked = append(linked, k)
said_ = append(said_, k+"="+shown)
}
number := func(v any, lo, hi int, msg string) (*int, string, error) {
s := trimmed(v)
if s == "" {
return nil, "auto", nil
}
n, _ := strconv.Atoi(s)
if !linkNumber.MatchString(s) || n < lo || n > hi {
return nil, "", errors.New(msg)
}
return &n, s, nil
}
if a["mtu"] != nil {
v, shown, err := number(a["mtu"], 1280, 1500, "MTU must be 1280-1500.")
if err != nil {
return err
}
var old *int
if dev.MTU != 0 {
old = &dev.MTU
}
put("mtu", v, old, shown)
}
if a["keepalive"] != nil {
v, shown, err := number(a["keepalive"], 0, 600, "Keep-alive must be 0-600 seconds.")
if err != nil {
return err
}
put("keepalive", v, dev.Keepalive, shown)
}
if on, given := a["lan_bypass"].(bool); given {
one := 1
var v, old *int
shown := "off"
if on {
v, shown = &one, "on"
}
if dev.LanBypass {
old = &one
}
put("lan_bypass", v, old, shown)
}
return nil
}
var warning any
var err error
// a change of the link values alone is in the device's configuration: nothing on the router changes
if renames || switches {
warning, err = change(d, text(a["lang"]), fn)
} else {
err = write(d, fn)
}
if err != nil {
return nil, err
}
event := func(detail string) {
logged(d, journal.Event{"type": "device_changed", "person": was.Person, "device": id, "detail": detail, "by": who.By})
}
if switches && off != was.Disabled {
if off {
event("turned off")
} else {
event("turned on")
}
} else if renames && a["name"] != was.Name {
event("renamed from " + was.Name)
}
if len(linked) > 0 {
event(strings.Join(said_, ", "))
}
if d.Told != nil {
d.Told()
}
return map[string]any{"warning": warning}, nil
}
// rerouted has everything put into effect after a change of the settings that moves routes (the networks
// behind a router): the home-like path keeps its own rules for them, which only the full apply brings up
// to date. The warning is that apply's.
func rerouted(d Deps, lang string) (warning any) {
if d.Apply != nil {
warning = d.Apply(lang)["warning"]
}
if d.Told != nil {
d.Told()
}
return warning
}
// ownNets is the router's own networks, which none behind a device may share addresses with (nil where
// the router cannot be looked at).
func ownNets(d Deps, m *model.Model) []string {
if d.Snapshot == nil {
return nil
}
if snap := d.Snapshot(); snap != nil {
return snap.Own(m)
}
return nil
}
// behindNets reads the networks behind a device as a page gives them (a list; blanks around each and
// empty entries dropped), each told from its first address, or says what is wrong with the first that
// will not do: no private network of a site's size, too many, two that share addresses, or one that
// something else has already (a tunnel, the router itself, another device).
//
// asking: the list is only asked of the admin, who answers with everything in view; then what another
// router has is not held against it here. The portal is never told whose a network is unless it is the
// person's own.
func behindNets(m *model.Model, device string, given any, own []string, who Actor, asking bool) ([]string, error) {
list, _ := given.([]any)
var nets []ipnet.Net
out := []string{}
for _, v := range list {
s := trimmed(v)
if s == "" {
continue
}
n, why := model.BehindNet(s)
switch why {
case "form":
return nil, said{"Network %s is not written like 192.168.2.0/24.", []any{s}}
case "wide":
return nil, said{"Network %s is too wide: /%d at the most.", []any{s, model.BehindWidest}}
case "public":
return nil, said{"Network %s is not private (10.x, 172.16-31.x, 192.168.x).", []any{s}}
}
// the same one written twice is one
twice := false
for _, o := range nets {
if o.Base == n.Base && o.Prefix == n.Prefix {
twice = true
} else if o.Overlaps(n) {
return nil, said{"Networks %s and %s share addresses.", []any{o.String(), n.String()}}
}
}
if twice {
continue
}
if c := m.BehindClash(n, device, own); c != nil {
switch {
case c.Server != "":
return nil, said{"Network %s is taken: %s is the tunnel network of %s. The two ends of a tunnel need different addresses.", []any{n.String(), c.Net, m.Servers[c.Server].Name}}
case c.Device == "":
return nil, said{"Network %s is taken: the router has %s itself. The two ends of a tunnel need different addresses.", []any{n.String(), c.Net}}
}
o := m.Devices[c.Device]
switch {
case who.Person != "" && o.Person == who.Person:
return nil, said{"Network %s is taken: %s is behind your %s. Two sites need different addresses.", []any{n.String(), c.Net, o.Name}}
case who.Person != "" && !asking:
return nil, said{"Network %s is taken on this server. Ask the admin which addresses are free.", []any{n.String()}}
case who.Person == "":
owner := o.Person
if p := m.People[o.Person]; p != nil {
owner = p.Name
}
return nil, said{"Network %s is taken: %s is behind %s (%s). Two sites need different addresses.", []any{n.String(), c.Net, o.Name, owner}}
}
}
nets = append(nets, n)
out = append(out, n.String())
}
if len(out) > model.BehindMost {
return nil, said{"A router has %d networks behind it at the most.", []any{model.BehindMost}}
}
return out, nil
}
func sameList(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// deviceNetworks sets the networks behind a router, all of them at once (none: it is a device like any
// other again). The tunnel then carries them both ways: a host there has its owner's access, and whoever
// reaches the router reaches it. A list that is the one there changes nothing and applies nothing.
func deviceNetworks(d Deps, a map[string]any) (map[string]any, error) {
id := text(a["id"])
var was model.Device
var nets []string
same := 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 nets, err = behindNets(m, id, a["networks"], ownNets(d, m), admin, false); err != nil {
return err
}
if len(nets) > 0 && dev.Kind != "router" {
return errors.New("Only a router has networks behind it.")
}
if same = sameList(nets, dev.Behind); same {
return nil
}
setBehind(c, id, nets)
// what its owner asked for and now has is asked no more
if rest := without(dev.Ask, nets); len(rest) != len(dev.Ask) {
setList(c, id, "ask_net", rest)
if len(rest) == 0 && !dev.Waiting {
c.Delete(id, "asked")
}
}
return nil
})
if err != nil {
return nil, err
}
if same {
return map[string]any{"networks": nets, "warning": nil}, nil
}
warning := rerouted(d, text(a["lang"]))
logged(d, journal.Event{"type": "device_changed", "person": was.Person, "device": id, "detail": "networks=" + behindSaid(nets), "by": admin.By})
return map[string]any{"networks": nets, "warning": warning}, nil
}
// setList writes a list option, or removes it when the list is empty.
func setList(c *uci.Config, id, option string, list []string) {
if len(list) == 0 {
c.Delete(id, option)
return
}
c.SetList(id, option, list)
}
// setBehind writes a device's networks: the list, or no option at all when there are none.
func setBehind(c *uci.Config, id string, nets []string) { setList(c, id, "behind_net", nets) }
// without is a list minus what another has, in its own order.
func without(list, drop []string) []string {
out := []string{}
for _, x := range list {
gone := false
for _, y := range drop {
gone = gone || x == y
}
if !gone {
out = append(out, x)
}
}
return out
}
// behindSaid is a device's networks in a line of the activity log.
func behindSaid(nets []string) string {
if len(nets) == 0 {
return "none"
}
return strings.Join(nets, " ")
}
// deviceDelete removes a device. The event carries its name and kind: the log reads well once it is gone.
func deviceDelete(d Deps, id, lang string) (map[string]any, error) {
return DeviceDeleteAs(d, id, lang, admin)
}
// DeviceDeleteAs is deviceDelete for an actor.
func DeviceDeleteAs(d Deps, id, lang string, who Actor) (map[string]any, error) {
var was model.Device
warning, err := change(d, lang, func(c *uci.Config, m *model.Model) error {
dev, err := who.own(m, id)
if err != nil {
return err
}
was = *dev
c.DeleteSection(id)
return nil
})
if err != nil {
return nil, err
}
logged(d, journal.Event{"type": "device_removed", "person": was.Person, "device": id, "detail": was.Name, "dname": was.Name, "dkind": was.Kind, "by": who.By})
if d.Told != nil {
d.Told()
}
return map[string]any{"warning": warning}, nil
}
// Keys is a device's new keys.
type Keys struct {
Private, Public, Preshared string
}
// newKeys makes a device's keys as `awg genkey`, `awg pubkey` and `awg genpsk` do: a Curve25519 private
// key (32 random bytes, clamped), its public key, and 32 random bytes to share. A test puts its own in its place.
var newKeys = func() Keys {
priv := make([]byte, 32)
crand.Read(priv)
priv[0] &= 248
priv[31] = priv[31]&127 | 64
psk := make([]byte, 32)
crand.Read(psk)
k := Keys{Private: base64.StdEncoding.EncodeToString(priv), Preshared: base64.StdEncoding.EncodeToString(psk)}
if key, err := ecdh.X25519().NewPrivateKey(priv); err == nil {
k.Public = base64.StdEncoding.EncodeToString(key.PublicKey().Bytes())
}
return k
}
var deviceKinds = []string{"computer", "phone", "tablet", "router", "other"}
// needAWG refuses, in the admin's words, while AmneziaWG is not on the router: no device's keys can be loaded.
func needAWG(d Deps) error {
if d.AWG == nil {
return nil
}
state := d.AWG()
if state["tools"] == true && state["kmod"] == true {
return nil
}
var miss []string
list, _ := state["missing"].([]any)
for _, x := range list {
miss = append(miss, text(x))
}
if len(miss) > 0 {
return said{"AmneziaWG is not installed (%s): install it in Dawnline → Components.", []any{strings.Join(miss, ", ")}}
}
return errors.New("AmneziaWG is not installed: install it in Dawnline → Components.")
}
// homeOf is the home networks a device's person reaches, for its configuration.
func homeOf(d Deps, m *model.Model, dev *model.Device) []string {
p := m.People[dev.Person]
if p == nil || d.Home == nil {
return nil
}
srv := m.Servers[p.Server]
if srv == nil {
return nil
}
return d.Home(srv, p)
}
// markIssued remembers that a device was just given its current configuration, and when
// (dawnline.awg mark_issued): what it holds, as a fingerprint, in issued.json.
func markIssued(d Deps, m *model.Model, id string) {
dev := m.Devices[id]
if dev == nil || dev.Waiting {
return
}
fp := handout.Fingerprint(m, dev, homeOf(d, m, dev))
if fp == "" {
return
}
defer files.Lock(d.RunDir, "issued")()
path := d.DataDir + "/issued.json"
var raw map[string]json.RawMessage
files.ReadJSON(path, &raw)
all := map[string]any{}
for k, v := range raw {
if m.Devices[k] == nil {
continue
}
// an entry from before moments were kept is the fingerprint alone
var old string
if json.Unmarshal(v, &old) == nil {
if old != "" {
all[k] = map[string]any{"fp": old, "ts": 0}
}
continue
}
all[k] = v
}
all[id] = map[string]any{"fp": fp, "ts": clock()}
os.MkdirAll(d.DataDir, 0o700)
files.WriteJSON(path, all, 0o600)
}
// markReconnect remembers that a device is set up again: handshakes up to now are its old configuration's.
func markReconnect(d Deps, m *model.Model, id string, late int64) {
defer files.Lock(d.RunDir, "reconnect")()
path := d.DataDir + "/reconnect.json"
all := map[string]any{}
files.ReadJSON(path, &all)
all[id] = clock() + late
for k := range all {
if m.Devices[k] == nil {
delete(all, k)
}
}
os.MkdirAll(d.DataDir, 0o700)
files.WriteJSON(path, all, 0o600)
}
// configOf is a device's configuration as a method answers it.
func configOf(d Deps, m *model.Model, id string) map[string]any {
dev := m.Devices[id]
// a router that waits for the admin's answer has none yet
if dev == nil || dev.Waiting {
return nil
}
cfg := handout.ClientConfig(m, dev, homeOf(d, m, dev))
if cfg == nil {
return nil
}
raw, _ := json.Marshal(cfg)
var out map[string]any
json.Unmarshal(raw, &out)
return out
}
// deviceCreate adds a device for a person with fresh keys: { id, config } (dawnline.devices add(), as the
// admin, who is not held to the person's device limit).
func deviceCreate(d Deps, a map[string]any) (map[string]any, error) {
return DeviceCreateAs(d, a, admin)
}
// DeviceCreateAs is deviceCreate for an actor: the portal is held to the person's device limit, and may
// bring the device's public key (its private key then never leaves the browser).
func DeviceCreateAs(d Deps, a map[string]any, who Actor) (map[string]any, error) {
if err := needAWG(d); err != nil {
return nil, err
}
person, kind := text(a["person"]), text(a["kind"])
name := trimmed(a["name"])
bypass, _ := a["lan_bypass"].(bool)
id := ""
var nets []string
// waits: a router asked of the admin; asks: its networks are asked too, not its own yet
waits, asks := false, false
err := write(d, func(c *uci.Config, m *model.Model) error {
p := m.People[person]
if p == nil {
return errors.New("Unknown person.")
}
srv := m.Servers[p.Server]
if srv == nil {
return errors.New("The person has no server.")
}
if !ValidDeviceName(name) {
return said{"Enter a device name: up to %d letters, digits, spaces and - _ .", []any{DeviceNameMax}}
}
if who.By == "portal" && len(p.Devices) >= p.DeviceLimit {
return said{"Device limit reached (%d).", []any{p.DeviceLimit}}
}
known := false
for _, k := range deviceKinds {
known = known || k == kind
}
if !known {
kind = "other"
}
for _, o := range p.Devices {
if lower(m.Devices[o].Name) == lower(name) {
return said{`"%s" already exists.`, []any{name}}
}
}
// A router from someone's own page, by the server's rule: not taken there at all, asked of the admin
// (it waits: no device of the tunnel, no configuration), or added at once. Its networks likewise:
// asked with it, or its own from the start.
if kind == "router" && who.By == "portal" {
switch srv.PortalRouters {
case "off":
return errors.New("The admin adds routers on this server. Ask them for one.")
case "ask":
waits = true
}
asks = waits || srv.PortalNets == "ask"
}
if a["networks"] != nil {
var err error
if nets, err = behindNets(m, "", a["networks"], ownNets(d, m), who, asks); err != nil {
return err
}
if len(nets) > 0 && kind != "router" {
return errors.New("Only a router has networks behind it.")
}
}
addr := allocAddress(m, srv, func(q *model.Person) string { return q.Server }, func(x *model.Device) string { return x.Address })
if addr == "" {
return errors.New("No free addresses left in the server subnet.")
}
k := newKeys()
if who.PublicKey != "" {
if !awg.ValidKey(who.PublicKey) {
return errors.New("Invalid public key.")
}
k.Public, k.Private = who.PublicKey, ""
}
if !awg.ValidKey(k.Public) {
return errors.New("Key generation failed (is amneziawg-tools installed?).")
}
for _, o := range m.Devices {
if o.PublicKey == k.Public {
return errors.New("This key is already in use.")
}
}
// a server's own key is in every configuration: the kernel would silently ignore such a peer
for _, s := range m.Servers {
if s.PublicKey == k.Public {
return errors.New("This key is already in use.")
}
}
id = "d_" + randHex(4)
c.Add("device", id)
c.Set(id, "person", person)
c.Set(id, "name", name)
c.Set(id, "kind", kind)
c.Set(id, "address", addr)
c.Set(id, "public_key", k.Public)
if k.Private != "" {
c.Set(id, "private_key", k.Private)
}
c.Set(id, "preshared_key", k.Preshared)
c.Set(id, "created", strconv.FormatInt(clock(), 10))
c.Set(id, "created_by", who.By)
if bypass {
c.Set(id, "lan_bypass", "1")
}
if waits {
c.Set(id, "waiting", "1")
}
if asks {
setList(c, id, "ask_net", nets)
} else {
setBehind(c, id, nets)
}
if waits || (asks && len(nets) > 0) {
c.Set(id, "asked", strconv.FormatInt(clock(), 10))
}
return nil
})
if err != nil {
return nil, err
}
// With networks of its own the routes move: everything is applied. A router that waits changes
// nothing on the router. Else the sync loads the device.
var warning any
switch {
case len(nets) > 0 && !asks:
warning = rerouted(d, text(a["lang"]))
case waits:
if d.Told != nil {
d.Told()
}
default:
if d.Sync != nil {
warning = d.Sync(text(a["lang"]))
}
if d.Told != nil {
d.Told()
}
}
_, m, _ := d.Store.Read()
logged(d, journal.Event{"type": "device_added", "person": person, "device": id, "detail": name, "by": who.By})
var detail []string
if waits {
detail = append(detail, "asked=router")
}
if len(nets) > 0 && asks {
detail = append(detail, "asked="+behindSaid(nets))
} else if len(nets) > 0 {
detail = append(detail, "networks="+behindSaid(nets))
}
if len(detail) > 0 {
logged(d, journal.Event{"type": "device_changed", "person": person, "device": id, "detail": strings.Join(detail, ", "), "by": who.By})
}
if waits {
return map[string]any{"id": id, "config": nil, "waiting": true, "warning": warning}, nil
}
markIssued(d, m, id)
if d.Told != nil {
d.Told()
}
return map[string]any{"id": id, "config": configOf(d, m, id), "warning": warning}, nil
}
// deviceRotate sets a device up from scratch (the admin's New keys): new keys, so the configuration it
// holds stops working with the sync, and it counts as not set up until it connects with the new one. Its
// kind and LAN Bypass change with the keys when given. It answers the new configuration.
func deviceRotate(d Deps, a map[string]any) (map[string]any, error) {
return DeviceRotateAs(d, a, admin)
}
// DeviceRotateAs is deviceRotate for an actor. With who.Defer the new keys are loaded a moment after the
// answer: the device the page came through would lose its tunnel before the answer gets out.
func DeviceRotateAs(d Deps, a map[string]any, who Actor) (map[string]any, error) {
id := text(a["id"])
kind := text(a["kind"])
bypass, bypassGiven := a["lan_bypass"].(bool)
var changed []string
person := ""
// unrouted: the device was a router with networks behind it, which went with its kind
unrouted := false
put := func(fn func(c *uci.Config, m *model.Model) error) (any, error) {
if err := write(d, fn); err != nil {
return nil, err
}
if unrouted {
return rerouted(d, text(a["lang"])), nil
}
var warning any
if d.Sync != nil {
warning = d.Sync(text(a["lang"]))
}
if d.Told != nil {
d.Told()
}
return warning, nil
}
if who.Defer {
put = func(fn func(c *uci.Config, m *model.Model) error) (any, error) {
if err := write(d, fn); err != nil {
return nil, err
}
if d.Background != nil {
d.Background("sleep 3; /usr/bin/dawnline sync")
}
return nil, nil
}
}
warning, err := put(func(c *uci.Config, m *model.Model) error {
dev, err := who.own(m, id)
if err != nil {
return err
}
person = dev.Person
if dev.Waiting {
return errors.New("This router waits for an answer to its request: it has no configuration yet.")
}
if p := m.People[dev.Person]; who.By == "portal" && kind == "router" && dev.Kind != "router" && p != nil {
if srv := m.Servers[p.Server]; srv == nil || srv.PortalRouters != "free" {
return errors.New("Add the router as a new device: a device cannot be made one here.")
}
}
if kind != "" {
known := false
for _, k := range deviceKinds {
known = known || k == kind
}
if !known {
return errors.New("Unknown device type.")
}
if kind != dev.Kind {
c.Set(id, "kind", kind)
changed = append(changed, "kind="+kind)
// what is no router any more has nothing behind it, and asks for nothing
if len(dev.Behind) > 0 {
c.Delete(id, "behind_net")
changed = append(changed, "networks=none")
unrouted = true
}
for _, k := range []string{"ask_net", "asked", "declined", "declined_net"} {
c.Delete(id, k)
}
}
}
if bypassGiven && bypass != dev.LanBypass {
if bypass {
c.Set(id, "lan_bypass", "1")
changed = append(changed, "lan_bypass=on")
} else {
c.Delete(id, "lan_bypass")
changed = append(changed, "lan_bypass=off")
}
}
k := newKeys()
if !awg.ValidKey(k.Public) || k.Private == "" {
return errors.New("Key generation failed.")
}
c.Set(id, "private_key", k.Private)
c.Set(id, "public_key", k.Public)
if k.Preshared != "" {
c.Set(id, "preshared_key", k.Preshared)
}
return nil
})
if err != nil {
return nil, err
}
if len(changed) > 0 {
logged(d, journal.Event{"type": "device_changed", "person": person, "device": id, "detail": strings.Join(changed, ", "), "by": who.By})
}
_, m, _ := d.Store.Read()
logged(d, journal.Event{"type": "device_keys", "person": person, "device": id, "detail": m.Devices[id].Name, "by": who.By})
markIssued(d, m, id)
// a deferred sync lets the old keys live a few seconds more
late := int64(0)
if who.Defer {
late = 5
}
markReconnect(d, m, id, late)
if d.Told != nil {
d.Told()
}
cfg := configOf(d, m, id)
if cfg != nil && warning != nil && warning != "" {
cfg["warning"] = warning
}
if cfg == nil {
return map[string]any{"result": nil}, nil
}
return cfg, nil
}
View File
Whitespace-only changes.
+360
View File
@@ -0,0 +1,360 @@
package methods
import (
"fmt"
"os"
"os/exec"
"regexp"
"sort"
"strings"
"time"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/endpoint"
"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/roster"
"github.com/tickcount/luci-app-dawnline/backend/state"
"github.com/tickcount/luci-app-dawnline/backend/view"
"github.com/tickcount/luci-app-dawnline/backend/zone"
)
var (
dfFree = regexp.MustCompile(`\n\S+\s+[0-9]+\s+[0-9]+\s+([0-9]+)\s`)
markRule = regexp.MustCompile(`^(\d+):.*fwmark 0x([0-9a-f]+)(/0x[0-9a-f]+)?.* lookup (\S+)`)
defRoute = regexp.MustCompile(`(^|\n)default [^\n]*`)
)
func run(name string, args ...string) (string, bool) {
out, err := exec.Command(name, args...).Output()
return string(out), err == nil
}
// policyRouting is policy routing by another package (mwan3, pbr): an fwmark rule ahead of Dawnline's
// (pref < 5205) whose table has a default route of its own sends marked tunnel traffic straight out.
func policyRouting(rules string, table func(name string) string) []string {
out := []string{}
for _, line := range strings.Split(rules, "\n") {
m := markRule.FindStringSubmatch(line)
if m == nil {
continue
}
var pref int
fmt.Sscan(m[1], &pref)
if pref >= 5205 {
continue
}
def := defRoute.FindString(table(m[4]))
if def == "" || strings.Contains(def, "local default") || strings.Contains(def, "dev dlx0") {
continue
}
out = append(out, "pref "+m[1]+" fwmark 0x"+m[2]+" → table "+m[4])
}
return out
}
// tcpListening tells whether anything listens on a TCP port, by /proc/net/tcp and tcp6 (state 0A).
func tcpListening(port int, tables ...string) bool {
hex := fmt.Sprintf(":%04X ", port)
for _, t := range tables {
for _, line := range strings.Split(t, "\n") {
if strings.Index(line, hex) > 0 && strings.Contains(line, " 0A ") {
return true
}
}
}
return false
}
// healthChecks gathers what Health's checks are made from and makes them.
func healthChecks(w *view.World, t *i18n.Catalog, d Deps) []health.Check {
m := w.M
in := health.Inputs{M: m, T: t, Needs: Needs(), Firewall: parseFile("/etc/config/firewall"), Network: parseFile("/etc/config/network")}
in.ModelErrors = func() []string { return health.ModelErrors(m, t) }
in.Unread = func(ifname string) bool { return w.Reads[ifname] == awg.Failed }
states := map[string]string{}
for id, p := range m.People {
states[id] = state.Of(m, p, w.Usage, w.Now)
}
in.Peers = func(srv *model.Server) health.Peers {
return health.PeerDiff(health.TunnelDevices(m, srv.ID, states), w.Peers, srv.Ifname)
}
// answers from our ports, and the proxies that would swallow them
diverts := health.ProxyDiverts(func(fam string) string {
out, _ := run("ip", fam, "rule", "show")
return out
}, func(fam, name string) string {
out, _ := run("ip", fam, "route", "show", "table", name)
return out
})
rep := &health.Replies{Diverts: []string{}}
for _, x := range diverts {
rep.Diverts = append(rep.Diverts, x.String())
}
if script := health.ReplyScript(m, diverts); script == "" {
rep.OK = true
} else {
kept, _ := os.ReadFile(d.RunDir + "/reply.nft")
_, loaded := run("nft", "list", "table", "inet", "dawnline_reply")
rep.OK = string(kept) == script && loaded
}
in.Replies = rep
// AmneziaWG, after loading an installed module that is not loaded yet
state := roster.AwgState(d.Handler)
if state["kmod"] == true && state["loaded"] == false {
exec.Command("modprobe", "amneziawg").Run()
state = roster.AwgState(d.Handler)
}
for _, p := range state["missing"].([]any) {
in.Missing = append(in.Missing, p.(string))
}
in.Loaded, in.Handler = state["loaded"] == true, state["handler"] == true
rel, _ := os.ReadFile("/proc/sys/kernel/osrelease")
var rec struct {
OSRelease string `json:"osrelease"`
}
if files.ReadJSON(d.DataDir+"/awg.json", &rec); rec.OSRelease != "" && rec.OSRelease != strings.TrimSpace(string(rel)) {
in.KernelWas, in.KernelIs = rec.OSRelease, strings.TrimSpace(string(rel))
}
if len(in.Missing) == 0 && !in.Loaded {
out, _ := exec.Command("sh", "-c", "modprobe amneziawg 2>&1; dmesg | grep -i amneziawg | tail -n 3").Output()
in.WhyNotLoaded = strings.Trim(string(out), " \t\r\n\v\f")
}
in.WaitsForReboot = awg.SystemCaps(d.DataDir).Pending
in.Year = time.Unix(w.Now, 0).In(zone.Local()).Year()
// what counts traffic and enforces end dates and limits is this backend: it answers, so it runs
in.AgentRunning = true
df, ok := run("df", "-Pk", "/overlay")
if !ok {
df, _ = run("df", "-Pk", "/")
}
if f := dfFree.FindStringSubmatch(df); f != nil {
var kb int64
fmt.Sscan(f[1], &kb)
in.FreeKB = &kb
}
_, in.NftLoaded = run("nft", "list", "table", "inet", "dawnline")
rules, _ := run("ip", "-4", "rule", "show")
in.PolicyRouting = policyRouting(rules, func(name string) string {
out, _ := run("ip", "-4", "route", "show", "table", name)
return out
})
in.Iface = func(ifname string) health.Iface {
st := map[string]any(nil)
if d.Bus != nil {
st = d.Bus("network.interface."+ifname, "status", nil)
}
if st == nil {
return health.Iface{}
}
f := health.Iface{Exists: true, Up: st["up"] == true}
errs, _ := st["errors"].([]any)
for _, e := range errs {
if em, ok := e.(map[string]any); ok {
f.Errors = append(f.Errors, text(em["code"]))
}
}
return f
}
if d.Addresses != nil {
for _, a := range d.Addresses() {
in.Subnets = append(in.Subnets, health.Subnet{Iface: a.Iface, CIDR: a.CIDR})
}
}
snap := d.Snapshot()
// the networks behind people's routers: which are in effect here, and what the last sync said of their routes
in.Behind, in.Own = snap.Behind(m), snap.Own(m)
if said, err := os.ReadFile(d.RunDir + "/routes.err"); err == nil {
in.RouteError = strings.TrimSpace(string(said))
}
in.HomeNets = func(srv *model.Server) []string {
_, _, nets, _ := snap.ServerNet(srv)
return nets
}
in.Endpoint = func(srv *model.Server, host string) endpoint.Reads { return endpointReads(d, srv, host) }
in.LikeHome, _ = roster.LikeHome(d.RunDir)
if st, err := os.Stat("/var/etc/dawnline-dns.conf"); err == nil {
age := time.Now().Unix() - st.ModTime().Unix()
in.ResolverConfAge = &age
pid, _ := os.ReadFile("/var/run/dawnline-dns.pid")
p := strings.TrimSpace(string(pid))
in.ResolverRunning = p != "" && strings.Trim(p, "0123456789") == "" && exists("/proc/"+p)
}
in.Portal = func() health.Portal {
s := m.Settings
p := health.Portal{}
for _, h := range listening("tcp") {
if h.port == s.PortalPort {
p.Owners = append(p.Owners, h.name)
}
}
tcp, _ := os.ReadFile("/proc/net/tcp")
tcp6, _ := os.ReadFile("/proc/net/tcp6")
p.Listening = tcpListening(s.PortalPort, string(tcp), string(tcp6))
if s.PortalHost != "" && d.Resolve != nil {
p.IPs = d.Resolve(s.PortalHost)
}
if d.RouterWAN != nil {
p.WAN = d.RouterWAN(m)
}
p.Cert, p.SelfSigned = PortalCert(s, d.DataDir)
if c := certOf(p.Cert); c != nil {
p.Valid = c.NotAfter.After(time.Now().Add(14 * 24 * time.Hour))
if !p.SelfSigned {
p.Names = c.DNSNames
}
}
return p
}
return health.Run(in)
}
// acmeCert is an ACME certificate on the router and the names it covers.
type acmeCert struct {
names []string
}
func acmeCerts() []acmeCert {
var out []acmeCert
entries, _ := os.ReadDir("/etc/ssl/acme")
for _, e := range entries {
base, is := strings.CutSuffix(e.Name(), ".fullchain.crt")
if !is || base == "" || !exists("/etc/ssl/acme/"+base+".key") {
continue
}
c := acmeCert{}
if x := certOf("/etc/ssl/acme/" + e.Name()); x != nil {
c.names = x.DNSNames
}
out = append(out, c)
}
return out
}
// portalCandidates is the addresses the portal could use in links, found on the router: names in ACME
// certificates, server endpoints, DDNS names, the current ones, and the WAN IP; each with where it points
// and whether a certificate covers it, the best first (dawnline.portalcert portal_candidates).
func portalCandidates(m *model.Model, d Deps) []map[string]any {
wan := ""
if d.RouterWAN != nil {
wan = d.RouterWAN(m)
}
var names []string
add := func(h string) {
if _, ip := ipnet.IP(h); h == "" || ip || !endpoint.ValidHost(h) || strings.HasPrefix(h, "*.") {
return
}
for _, n := range names {
if n == h {
return
}
}
names = append(names, h)
}
certs := acmeCerts()
for _, c := range certs {
for _, n := range c.names {
add(n)
}
}
for _, id := range m.ServerIDs() {
add(m.Servers[id].Endpoint)
}
if ddns := parseFile("/etc/config/ddns"); ddns != nil {
for _, s := range ddns.OfType("service") {
if on, _ := s.Get("enabled"); on != "0" {
h, _ := s.Get("lookup_host")
add(h)
h, _ = s.Get("domain")
add(h)
}
}
}
hosts := view.PortalHosts(m)
for _, h := range hosts {
add(h)
}
type cand struct {
m map[string]any
score int
}
var list []cand
put := func(host, kind string, ips []string, here, cf, cert bool) {
score := 0
if here {
score += 4
}
if cert {
score += 2
}
if kind == "name" {
score++
}
if cf {
score -= 8
}
list = append(list, cand{map[string]any{"host": host, "kind": kind, "ips": ips, "here": here, "cloudflare": cf, "cert": cert}, score})
}
// every name asked at once: each lookup may wait two seconds
found := make([][]string, len(names))
done := make(chan int)
for i, h := range names {
go func() {
if d.Resolve != nil {
found[i] = d.Resolve(h)
}
done <- i
}()
}
for range names {
<-done
}
for i, h := range names {
ips := found[i]
if ips == nil {
ips = []string{}
}
here, cf, covered := false, false, false
for _, ip := range ips {
here = here || (wan != "" && ip == wan)
cf = cf || endpoint.Cloudflare(ip)
}
for _, c := range certs {
for _, n := range c.names {
covered = covered || endpoint.Covers(n, h)
}
}
put(h, "name", ips, here, cf, covered)
}
if wan != "" {
put(wan, "ip", []string{wan}, true, false, false)
}
for _, h := range hosts {
if _, ip := ipnet.IP(h); ip && h != wan {
put(h, "ip", []string{h}, false, false, false)
}
}
// best first: here with a certificate, then here, then the rest
sort.SliceStable(list, func(i, j int) bool { return list[i].score > list[j].score })
out := []map[string]any{}
for _, c := range list {
out = append(out, c.m)
}
return out
}
// portalInfo is the slow part of the portal's settings (DNS lookups and Health's checks of the portal),
// fetched after the page shows.
func portalInfo(w *view.World, t *i18n.Catalog, d Deps) map[string]any {
checks := []health.Check{}
for _, c := range healthChecks(w, t, d) {
switch c.ID {
case "portal", "portal_dns", "portal_reply", "portal_http", "cert":
checks = append(checks, c)
}
}
return map[string]any{"candidates": portalCandidates(w.M, d), "checks": checks}
}
+147
View File
@@ -0,0 +1,147 @@
package methods
import (
"errors"
"os"
"sort"
"strings"
"time"
backend "github.com/tickcount/luci-app-dawnline/backend"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
"github.com/tickcount/luci-app-dawnline/backend/view"
)
// homeHosts is the devices at home for the Home services picker: DHCP leases and static hosts in a
// server's home networks, with the services they announce (Bonjour, when umdns runs). A server not named
// is the first one. Each device's services are in the order of their type, then their name.
func homeHosts(w *view.World, d Deps, id, leases, dhcp string) (map[string]any, error) {
srv := w.M.Servers[id]
if ids := w.M.ServerIDs(); srv == nil && len(ids) > 0 {
srv = w.M.Servers[ids[0]]
}
if srv == nil || d.Snapshot == nil {
return nil, errors.New("No server yet.")
}
_, _, cidrs, _ := d.Snapshot().ServerNet(srv)
var nets []ipnet.Net
for _, c := range cidrs {
if n, ok := ipnet.Any(c); ok && strings.Contains(c, "/") {
nets = append(nets, n)
}
}
inside := func(ip string) bool {
n, ok := ipnet.IP(ip)
if !ok {
return false
}
for _, r := range nets {
if r.Holds(n) {
return true
}
}
return false
}
type host struct {
Address string `json:"address"`
Name string `json:"name"`
MAC string `json:"mac"`
Services []map[string]any `json:"services"`
}
hosts := map[string]*host{}
raw, _ := os.ReadFile(leases)
for _, line := range strings.Split(string(raw), "\n") {
if f := strings.Split(line, " "); len(f) >= 4 && inside(f[2]) {
name := f[3]
if name == "*" {
name = ""
}
hosts[f[2]] = &host{Address: f[2], Name: name, MAC: f[1], Services: []map[string]any{}}
}
}
if c := parseFile(dhcp); c != nil {
for _, h := range c.OfType("host") {
ip, _ := h.Get("ip")
if ip == "" || !inside(ip) || hosts[ip] != nil {
continue
}
name, _ := h.Get("name")
mac := h.Values["mac"]
if mac.IsList && len(mac.List) > 0 {
mac.Str = mac.List[0]
}
hosts[ip] = &host{Address: ip, Name: name, MAC: mac.Str, Services: []map[string]any{}}
}
}
// what is announced on the home networks, from OpenWrt's mDNS daemon while it answers
var browse map[string]any
if d.Bus != nil {
browse = d.Bus("umdns", "browse", map[string]any{"address": true, "array": true})
}
types := make([]string, 0, len(browse))
for t := range browse {
types = append(types, t)
}
sort.Strings(types)
for _, t := range types {
insts, _ := browse[t].(map[string]any)
names := make([]string, 0, len(insts))
for n := range insts {
names = append(names, n)
}
sort.Strings(names)
for _, name := range names {
i, _ := insts[name].(map[string]any)
addr := text(i["ipv4"])
if list, ok := i["ipv4"].([]any); ok && len(list) > 0 {
addr = text(list[0])
}
if addr == "" {
continue
}
port := i["port"]
if port == nil {
port = 0
}
svc := map[string]any{"type": t, "name": name, "port": port}
if h := hosts[addr]; h != nil {
h.Services = append(h.Services, svc)
} else if inside(addr) {
hn := strings.TrimSuffix(strings.TrimSuffix(text(i["host"]), "."), ".local")
hosts[addr] = &host{Address: addr, Name: hn, Services: []map[string]any{svc}}
}
}
}
list := make([]*host, 0, len(hosts))
for _, h := range hosts {
list = append(list, h)
}
sort.Slice(list, func(i, j int) bool {
a, _ := ipnet.IP(list[i].Address)
b, _ := ipnet.IP(list[j].Address)
return a < b
})
return map[string]any{"hosts": list, "mdns": browse != nil}, nil
}
// export is every section of the settings as written, for the admin to keep (it holds private keys).
func export(d Deps) (map[string]any, error) {
sections := []map[string]any{}
if c := parseFile(d.Config); c != nil {
for _, s := range c.Sections {
o := map[string]any{".type": s.Type, ".name": s.Name}
for k, v := range s.Values {
if v.IsList {
o[k] = v.List
} else {
o[k] = v.Str
}
}
sections = append(sections, o)
}
}
if d.Log != nil {
d.Log.Admin("dawnline", "exported", "", "all settings", "the file holds private keys")
}
return map[string]any{"format": "dawnline", "version": backend.Version, "exported": time.Now().Unix(), "sections": sections}, nil
}
View File
Whitespace-only changes.
+855
View File
@@ -0,0 +1,855 @@
// Package methods is the admin's questions the Go backend answers itself: methods of the ubus object
// `dawnline` that were luci.dawnline's (dawnline.rpc.*), with the same arguments and the same answers, a
// failure as { error } in the page's language. hello names them, and a page asks them here instead.
//
// events { limit, person, device, since } the newest of the activity log
// person_usage { id, days } a person's days of traffic, and each of their devices'
// person_links { id } the personal code and links (admins only: the access list)
// device_config { id } a device's configuration, to show (admins only)
// service_log the watchdog's state and the newest of the service log
// backup_list the backups kept on the router
// backup_read { name } one of them, in base64 (admins only: it holds keys)
// settings_get the Settings page's data
// server_ports { id } the ports and networks a server must avoid
// interfaces { id } a server's interfaces: what exists, what is detected, what it uses
// updates_status the Components tab's data
// setup_defaults what a new server starts from
// health Health's checks, the watchdog's state and the service log
// home_hosts { id } the devices at home, for the Home services picker
// export every section of the settings (admins only: it holds keys)
// access_check { from, to } where a connection between two sides stops, in words (admins only)
//
// and the first that change something, none of them the settings or the tunnels (admins only):
//
// logs_clear { person, device, category, older_days } remove activity; nothing given clears it all
// service_log_clear { older_days } remove the service log's entries
// usage_clear { scope, id } forget traffic history: all | person | device | limit
// geo_clear forget the places found for addresses
// backup_delete { name } remove a backup
//
// and those that change the settings (package store), each put into effect by the ucode backend's sync
// until apply is ported:
//
// service_set { id, name, address, ports } add or change a home service
// service_delete { id } remove one, and take it from everyone who had it
// circle_set { id, server, name, color, members } add or change a circle; members replaces who is in it
// circle_delete { id } remove one, and take everyone out of it
// person_pause { id, paused } pause a person, or let them on again
// person_delete { id } remove a person and their devices
// person_update { id, …fields } an immediate edit from a person's page
// people_update { list } several people at once, all or nothing
// person_create { name, slug, server, access, device_limit, expires, quota_gb, quota_period }
// person_regen { id, remove } a new personal link, the old one's devices taken away when asked
// device_update { id, name, disabled, mtu, keepalive, lan_bypass } rename, turn off or on, how it connects
// device_delete { id } remove a device
// device_create { person, name, kind, lan_bypass, networks } a new device with keys made here: its id and configuration
// device_networks { id, networks } the networks behind a router, all at once; applied in full
// device_answer { id, answer, reason, router, networks } what a router's owner asked for: accept, decline, reopen
// device_rotate { id, kind, lan_bypass } new keys: the device is set up again, its new configuration
// settings_update { values } save settings, "" back to the default; the portal checked as it will be
// server_update { id, values } a server's settings, applied at once (ucode's full apply for now)
// server_interfaces { id, auto_wan, wan_device, auto_lan, lan_devices, lan_like, device_mesh, mesh }
// server_delete { id } a server with its people, their devices and its circles; a backup first
// server_obfuscation { id, preset } a new disguise for a server: off, balanced or max
// health_fix { id } run a fix a check named, and Health again
// updates_run { what } start an update job (the updater works in the background)
// apply put everything into effect (ucode's full apply for now)
// server_create { id, name, ifname, port, subnet, endpoint, mtu, preset, lan_zone, wan_zone, lan_like,
// portal_enabled, portal_port } set a server up, with keys made here
// backup_create a backup now: its name
// backup_restore { name } bring a kept backup back (checked first, a backup made before)
// backup_upload { data } the same for a backup the admin sends, in base64
// import { data } replace the settings with an export
//
// portal_info the addresses the portal could use and Health's checks of it
// endpoint_probe { host, id, port } where a public address points, as the admin types it
// updates_releases { page } Dawnline's releases, five a page
// updates_notes { component, tag, build } a release's notes
// updates_changes { from, to } what changed between the installed version and a release
package methods
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"sort"
"strings"
"time"
"github.com/tickcount/luci-app-dawnline/backend/access"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/endpoint"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/github"
"github.com/tickcount/luci-app-dawnline/backend/handout"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/roster"
"github.com/tickcount/luci-app-dawnline/backend/servicelog"
"github.com/tickcount/luci-app-dawnline/backend/state"
"github.com/tickcount/luci-app-dawnline/backend/store"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/ubus"
"github.com/tickcount/luci-app-dawnline/backend/usage"
"github.com/tickcount/luci-app-dawnline/backend/view"
)
// Deps is what the methods read.
type Deps struct {
// World is the settings, the counters and the tunnels as the watcher last saw them (nil before its first look).
World func() *view.World
// Events is the whole activity log, the oldest first, home names with the connections that have them.
Events func() []map[string]any
// Catalogs is where the translations are (i18n.Dir).
Catalogs string
// Home is the home networks a person reaches on their server; HostIP, where a name points ("" nowhere).
Home func(srv *model.Server, p *model.Person) []string
HostIP func(host string) string
// RunDir and DataDir are the state's (/var/run/dawnline, /etc/dawnline); Config the settings file.
RunDir, DataDir, Config string
// Addresses is every IPv4 address of netifd's interfaces.
Addresses func() []Address
// Snapshot is a fresh look at the router's interfaces.
Snapshot func() *netview.Snapshot
// Board is `ubus call system board`'s answer.
Board func() map[string]any
// Handler tells whether netifd lists the amneziawg protocol.
Handler func() bool
// GitHub asks GitHub (nil: the methods that need it are not offered).
GitHub *github.Client
// Resolve is a name's addresses; WANOf the WAN's IPv4 for a server (nil: the default WAN), "" when none.
Resolve func(host string) []string
WANOf func(srv *model.Server) string
// Bus asks another ubus object (nil answer: it did not answer); RouterWAN is the router's WAN address.
Bus func(object, method string, args map[string]any) map[string]any
RouterWAN func(m *model.Model) string
// Log writes the service log (nil: nothing is written).
Log *servicelog.Log
// ResetUsage forgets traffic history in the watcher's counters (false: it has not looked yet).
ResetUsage func(scope, id string) bool
// Store changes the settings; Sync puts them into effect and returns a warning for the admin (nil: none).
Store *store.Store
Sync func(lang string) any
// Told has the pages told of a change at once: the settings read anew and the topics put again.
Told func()
// AWG is what of AmneziaWG is on the router (nil: not asked).
AWG func() map[string]any
// Reload has the service take new portal settings (its own restart among them): after the answer is out.
Reload func()
// Apply has the ucode backend put everything into effect (its full apply) and returns what that came to.
Apply func(lang string) map[string]any
// Caps is what the installed AmneziaWG can do (nil: read on the router).
Caps func() awg.Caps
// Restart turns the service on and restarts it after a server is made (the network first when netifd
// does not know AmneziaWG yet), in the background: this very process restarts with it.
Restart func(network bool)
// Background runs a shell script detached, after the answer is out; Ifup brings a netifd interface up;
// Updater starts the updater with these arguments, speaking a language ("" English).
Background func(script string)
Ifup func(name string)
Updater func(lang string, args []string)
// SyncPlan is what Go's own sync would load, with nothing loaded (nil: not there).
SyncPlan func() any
// Teardown removes everything Dawnline made on the router, from the settings as they were (nil: not there);
// Stop then stops the service and keeps it from starting, once the answer is out.
Teardown func(m *model.Model)
Stop func()
// ApplyPlan is what Go's own full apply would change, with nothing changed (nil: not there).
ApplyPlan func() any
}
// num is a number as the bus hands it over.
func num(v any) float64 {
switch n := v.(type) {
case int:
return float64(n)
case int32:
return float64(n)
case int64:
return float64(n)
case uint32:
return float64(n)
case uint64:
return float64(n)
case float64:
return n
}
return 0
}
func text(v any) string {
s, _ := v.(string)
return s
}
// days is how much history a caller may ask for: 1 to 400, 30 when not said.
func days(v any) int {
n := num(v)
if v == nil || n < 1 {
return 30
}
if n > 400 {
return 400
}
return int(n)
}
// a method's own work: its answer, or the failure to tell the page (said in English, translated by wrap)
type call func(w *view.World, args map[string]any) (map[string]any, error)
// unworded puts an error that has no sentence for a person into the system log: the page is told it went wrong.
func (d Deps) unworded(line string) {
if d.Log != nil {
d.Log.Say("error", "dawnline", line)
}
}
// said is a failure whose sentence has values: translated first, filled after.
type said struct {
msg string
args []any
}
func (s said) Error() string { return fmt.Sprintf(s.msg, s.args...) }
// List is the methods, to add to the object.
func List(d Deps) []ubus.Method {
wrap := func(fn call) ubus.Handler {
return func(_ context.Context, args map[string]any) (map[string]any, error) {
t := i18n.Use(d.Catalogs, text(args["lang"]))
w := d.World()
if w == nil {
return map[string]any{"error": t.T("The router is still starting. Try again in a moment.")}, nil
}
res, err := fn(w, args)
if s, refused := err.(access.Said); refused {
return map[string]any{"error": fmt.Sprintf(t.T(s.Msg), s.Args...)}, nil
}
if f, isGitHub := err.(github.Failure); isGitHub {
return map[string]any{"error": fmt.Sprintf(t.T(f.Msg), f.Args...)}, nil
}
if s, filled := err.(said); filled {
return map[string]any{"error": fmt.Sprintf(t.T(s.msg), s.args...)}, nil
}
if err != nil {
return map[string]any{"error": wordsOf(t, err, d.unworded)}, nil
}
return res, nil
}
}
// the same, for an answer that has words of its own in the page's language
worded := func(fn func(w *view.World, t *i18n.Catalog) map[string]any) ubus.Handler {
return func(_ context.Context, args map[string]any) (map[string]any, error) {
t := i18n.Use(d.Catalogs, text(args["lang"]))
w := d.World()
if w == nil {
return map[string]any{"error": t.T("The router is still starting. Try again in a moment.")}, nil
}
return fn(w, t), nil
}
}
list := []ubus.Method{}
if d.GitHub != nil {
list = append(list,
ubus.Method{Name: "updates_releases", Params: map[string]ubus.Kind{"page": ubus.Int, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return releases(d.GitHub, a["page"]) })},
ubus.Method{Name: "updates_notes", Params: map[string]ubus.Kind{"component": ubus.String, "tag": ubus.String, "build": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return notes(d.GitHub, text(a["component"]), text(a["tag"]), text(a["build"]))
})},
ubus.Method{Name: "updates_changes", Params: map[string]ubus.Kind{"from": ubus.String, "to": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return changes(d.GitHub, text(a["from"]), text(a["to"]))
})},
)
}
return append(list, []ubus.Method{
{Name: "device_config", Params: map[string]ubus.Kind{"id": ubus.String, "lang": ubus.String},
Handle: wrap(func(w *view.World, a map[string]any) (map[string]any, error) {
return deviceConfig(w, d, text(a["id"]))
})},
{Name: "service_log", Params: map[string]ubus.Kind{"lang": ubus.String},
Handle: worded(func(w *view.World, t *i18n.Catalog) map[string]any { return serviceLog(w, t, d.RunDir, d.DataDir) })},
{Name: "settings_get", Params: map[string]ubus.Kind{"lang": ubus.String},
Handle: worded(func(w *view.World, t *i18n.Catalog) map[string]any { return settingsGet(w, t, d) })},
{Name: "server_ports", Params: map[string]ubus.Kind{"id": ubus.String, "lang": ubus.String},
Handle: func(_ context.Context, a map[string]any) (map[string]any, error) {
return worded(func(w *view.World, t *i18n.Catalog) map[string]any { return serverPorts(w, t, d, text(a["id"])) })(nil, a)
}},
{Name: "interfaces", Params: map[string]ubus.Kind{"id": ubus.String, "lang": ubus.String},
Handle: wrap(func(w *view.World, a map[string]any) (map[string]any, error) { return interfaces(w, d, text(a["id"])) })},
{Name: "updates_status", Params: map[string]ubus.Kind{"lang": ubus.String},
Handle: worded(func(w *view.World, t *i18n.Catalog) map[string]any { return updatesStatus(w, t, d) })},
{Name: "setup_defaults", Params: map[string]ubus.Kind{"lang": ubus.String},
Handle: worded(func(w *view.World, t *i18n.Catalog) map[string]any { return setupDefaults(w, t, d) })},
{Name: "endpoint_probe", Params: map[string]ubus.Kind{"host": ubus.String, "id": ubus.String, "port": ubus.Int, "lang": ubus.String},
Handle: func(_ context.Context, a map[string]any) (map[string]any, error) {
return worded(func(w *view.World, t *i18n.Catalog) map[string]any { return endpointProbe(w, t, d, a) })(nil, a)
}},
{Name: "home_hosts", Params: map[string]ubus.Kind{"id": ubus.String, "lang": ubus.String},
Handle: wrap(func(w *view.World, a map[string]any) (map[string]any, error) {
return homeHosts(w, d, text(a["id"]), "/tmp/dhcp.leases", "/etc/config/dhcp")
})},
{Name: "export", Params: map[string]ubus.Kind{"lang": ubus.String},
Handle: wrap(func(*view.World, map[string]any) (map[string]any, error) { return export(d) })},
{Name: "access_check", Params: map[string]ubus.Kind{"from": ubus.String, "to": ubus.String, "lang": ubus.String},
Handle: func(_ context.Context, a map[string]any) (map[string]any, error) {
return wrap(func(w *view.World, a map[string]any) (map[string]any, error) {
return accessCheck(w, i18n.Use(d.Catalogs, text(a["lang"])), d, text(a["from"]), text(a["to"]))
})(nil, a)
}},
{Name: "logs_clear", Params: map[string]ubus.Kind{"person": ubus.String, "device": ubus.String, "category": ubus.String, "older_days": ubus.String, "lang": ubus.String},
Handle: wrap(func(w *view.World, a map[string]any) (map[string]any, error) {
return logsClear(w, d, text(a["person"]), text(a["device"]), text(a["category"]), text(a["older_days"]))
})},
{Name: "service_log_clear", Params: map[string]ubus.Kind{"older_days": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return serviceLogClear(d, text(a["older_days"])), nil
})},
{Name: "usage_clear", Params: map[string]ubus.Kind{"scope": ubus.String, "id": ubus.String, "lang": ubus.String},
Handle: wrap(func(w *view.World, a map[string]any) (map[string]any, error) {
return usageClear(w, d, text(a["scope"]), text(a["id"]))
})},
{Name: "geo_clear", Params: map[string]ubus.Kind{"lang": ubus.String},
Handle: wrap(func(*view.World, map[string]any) (map[string]any, error) { return geoClear(d), nil })},
{Name: "backup_delete", Params: map[string]ubus.Kind{"name": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return backupDelete(d.DataDir+"/backups", text(a["name"]))
})},
{Name: "service_set", Params: map[string]ubus.Kind{"id": ubus.String, "name": ubus.String, "address": ubus.String, "ports": ubus.Array, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return serviceSet(d, a) })},
{Name: "service_delete", Params: map[string]ubus.Kind{"id": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return serviceDelete(d, text(a["id"]), text(a["lang"]))
})},
{Name: "circle_set", Params: map[string]ubus.Kind{"id": ubus.String, "server": ubus.String, "name": ubus.String, "color": ubus.Int, "members": ubus.Array, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return circleSet(d, a) })},
{Name: "circle_delete", Params: map[string]ubus.Kind{"id": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return circleDelete(d, text(a["id"]), text(a["lang"]))
})},
{Name: "person_pause", Params: map[string]ubus.Kind{"id": ubus.String, "paused": ubus.Bool, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return personPause(d, text(a["id"]), truthy(a["paused"]), text(a["lang"]))
})},
{Name: "person_delete", Params: map[string]ubus.Kind{"id": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return personDelete(d, text(a["id"]), text(a["lang"]))
})},
{Name: "person_update", Params: map[string]ubus.Kind{"id": ubus.String, "name": ubus.String, "access": ubus.String, "device_limit": ubus.String, "expires": ubus.String,
"quota_gb": ubus.String, "quota_period": ubus.String, "bonus_gb": ubus.String, "note": ubus.String, "dns_mode": ubus.String, "paused": ubus.Bool,
"services": ubus.Array, "circle": ubus.String, "home_nets": ubus.Array, "server": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return personUpdate(d, a) })},
{Name: "people_update", Params: map[string]ubus.Kind{"list": ubus.Array, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return peopleUpdate(d, a) })},
{Name: "person_create", Params: map[string]ubus.Kind{"name": ubus.String, "slug": ubus.String, "server": ubus.String, "access": ubus.String, "device_limit": ubus.String,
"expires": ubus.String, "quota_gb": ubus.String, "quota_period": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return personCreate(d, a) })},
{Name: "person_regen", Params: map[string]ubus.Kind{"id": ubus.String, "remove": ubus.Array, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return personRegen(d, a) })},
{Name: "device_update", Params: map[string]ubus.Kind{"id": ubus.String, "name": ubus.String, "disabled": ubus.Bool, "mtu": ubus.String, "keepalive": ubus.String,
"lan_bypass": ubus.Bool, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return deviceUpdate(d, a) })},
{Name: "device_delete", Params: map[string]ubus.Kind{"id": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return deviceDelete(d, text(a["id"]), text(a["lang"]))
})},
{Name: "device_create", Params: map[string]ubus.Kind{"person": ubus.String, "name": ubus.String, "kind": ubus.String, "lan_bypass": ubus.Bool,
"networks": ubus.Array, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return deviceCreate(d, a) })},
{Name: "device_networks", Waits: true, Params: map[string]ubus.Kind{"id": ubus.String, "networks": ubus.Array, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return deviceNetworks(d, a) })},
{Name: "device_answer", Waits: true, Params: map[string]ubus.Kind{"id": ubus.String, "answer": ubus.String, "reason": ubus.String, "router": ubus.Bool,
"networks": ubus.Array, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return deviceAnswer(d, a) })},
{Name: "device_rotate", Params: map[string]ubus.Kind{"id": ubus.String, "kind": ubus.String, "lan_bypass": ubus.Bool, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return deviceRotate(d, a) })},
{Name: "settings_update", Params: map[string]ubus.Kind{"values": ubus.Table, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return settingsUpdate(d, a) })},
{Name: "server_update", Waits: true, Params: map[string]ubus.Kind{"id": ubus.String, "values": ubus.Table, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return serverUpdate(d, a) })},
{Name: "server_interfaces", Waits: true, Params: map[string]ubus.Kind{"id": ubus.String, "auto_wan": ubus.Bool, "wan_device": ubus.String, "auto_lan": ubus.Bool,
"lan_devices": ubus.Array, "lan_like": ubus.Bool, "device_mesh": ubus.Bool, "mesh": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return serverInterfaces(d, a) })},
{Name: "server_delete", Waits: true, Params: map[string]ubus.Kind{"id": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return serverDelete(d, text(a["id"]), text(a["lang"]))
})},
{Name: "backup_create", Params: map[string]ubus.Kind{"lang": ubus.String},
Handle: wrap(func(*view.World, map[string]any) (map[string]any, error) {
name, err := backupCreate(d, "")
if err != nil {
return nil, err
}
return map[string]any{"name": name}, nil
})},
{Name: "server_obfuscation", Waits: true, Params: map[string]ubus.Kind{"id": ubus.String, "preset": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return serverObfuscation(d, text(a["id"]), asString(a["preset"]), text(a["lang"]))
})},
{Name: "server_create", Waits: true, Params: map[string]ubus.Kind{"id": ubus.String, "name": ubus.String, "ifname": ubus.String, "port": ubus.Int, "subnet": ubus.String,
"endpoint": ubus.String, "mtu": ubus.Int, "preset": ubus.String, "lan_zone": ubus.String, "wan_zone": ubus.String, "lan_like": ubus.Bool,
"portal_enabled": ubus.Bool, "portal_port": ubus.Int, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) { return serverCreate(d, a) })},
{Name: "health_fix", Waits: true, Params: map[string]ubus.Kind{"id": ubus.String, "lang": ubus.String},
Handle: func(_ context.Context, a map[string]any) (map[string]any, error) {
return wrap(func(w *view.World, a map[string]any) (map[string]any, error) {
return healthFix(w, i18n.Use(d.Catalogs, text(a["lang"])), d, text(a["id"]), text(a["lang"]))
})(nil, a)
}},
{Name: "updates_run", Params: map[string]ubus.Kind{"what": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return updatesRun(d, text(a["what"]), text(a["lang"]))
})},
{Name: "apply", Waits: true, Params: map[string]ubus.Kind{"lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
out := map[string]any{}
if d.Apply != nil {
out = d.Apply(text(a["lang"]))
}
if d.Told != nil {
d.Told()
}
return out, nil
})},
{Name: "uninstall", Waits: true, Params: map[string]ubus.Kind{"confirm": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return uninstall(d, text(a["confirm"]))
})},
{Name: "backup_restore", Waits: true, Params: map[string]ubus.Kind{"name": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return backupRestore(d, text(a["name"]), text(a["lang"]))
})},
{Name: "backup_upload", Waits: true, Params: map[string]ubus.Kind{"data": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return backupUpload(d, text(a["data"]), text(a["lang"]))
})},
{Name: "import", Waits: true, Params: map[string]ubus.Kind{"data": ubus.Table, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return importAll(d, a["data"], text(a["lang"]))
})},
// What Go's sync would load: for root on the router, to hold it to what runs there. No access list names it.
{Name: "sync_plan", Handle: func(context.Context, map[string]any) (map[string]any, error) {
if d.SyncPlan == nil {
return map[string]any{}, nil
}
raw, _ := json.Marshal(d.SyncPlan())
var out map[string]any
json.Unmarshal(raw, &out)
return out, nil
}},
// What Go's full apply would do: for root on the router, likewise.
{Name: "apply_plan", Waits: true, Handle: func(context.Context, map[string]any) (map[string]any, error) {
if d.ApplyPlan == nil {
return map[string]any{}, nil
}
raw, _ := json.Marshal(d.ApplyPlan())
var out map[string]any
json.Unmarshal(raw, &out)
return out, nil
}},
{Name: "portal_info", Params: map[string]ubus.Kind{"lang": ubus.String},
Handle: worded(func(w *view.World, t *i18n.Catalog) map[string]any { return portalInfo(w, t, d) })},
// Health: the checks, with the watchdog's state and the service log beside them.
{Name: "health", Params: map[string]ubus.Kind{"lang": ubus.String},
Handle: worded(func(w *view.World, t *i18n.Catalog) map[string]any {
out := serviceLog(w, t, d.RunDir, d.DataDir)
out["checks"] = healthChecks(w, t, d)
return out
})},
{Name: "backup_read", Params: map[string]ubus.Kind{"name": ubus.String, "lang": ubus.String},
Handle: wrap(func(_ *view.World, a map[string]any) (map[string]any, error) {
return backupRead(d.DataDir+"/backups", text(a["name"]))
})},
{Name: "backup_list", Params: map[string]ubus.Kind{"lang": ubus.String},
Handle: wrap(func(*view.World, map[string]any) (map[string]any, error) {
return map[string]any{"backups": backups(d.DataDir + "/backups")}, nil
})},
{Name: "events", Params: map[string]ubus.Kind{"limit": ubus.Int, "person": ubus.String, "device": ubus.String, "since": ubus.Int, "lang": ubus.String},
Handle: wrap(func(w *view.World, a map[string]any) (map[string]any, error) {
return events(w, d.Events(), text(a["person"]), text(a["device"]), num(a["since"]), int(num(a["limit"]))), nil
})},
{Name: "person_usage", Params: map[string]ubus.Kind{"id": ubus.String, "days": ubus.Int, "lang": ubus.String},
Handle: wrap(func(w *view.World, a map[string]any) (map[string]any, error) {
return personUsage(w, text(a["id"]), days(a["days"]))
})},
{Name: "person_links", Params: map[string]ubus.Kind{"id": ubus.String, "lang": ubus.String},
Handle: wrap(func(w *view.World, a map[string]any) (map[string]any, error) { return personLinks(w, text(a["id"])) })},
}...)
}
// events is the newest `limit` entries of the log that pass every filter given, how many passed before the
// limit, and what the log keeps (dawnline.rpc.status events_list).
func events(w *view.World, log []map[string]any, person, device string, since float64, limit int) map[string]any {
kept := []map[string]any{}
for _, e := range log {
// the tunnel down/up events of older versions are left out: the service log tells it better
if t := text(e["type"]); t == "server_down" || t == "server_up" {
continue
}
if (person != "" && text(e["person"]) != person) || (device != "" && text(e["device"]) != device) || (since != 0 && num(e["ts"]) <= since) {
continue
}
kept = append(kept, e)
}
total := len(kept)
if limit <= 0 {
limit = 300
}
if len(kept) > limit {
kept = kept[len(kept)-limit:]
}
return map[string]any{"events": kept, "total": total, "policy": policy(w)}
}
func personUsage(w *view.World, id string, days int) (map[string]any, error) {
p := w.M.People[id]
if p == nil {
return nil, errors.New("Unknown person.")
}
devs := map[string]any{}
for _, d := range p.Devices {
devs[d] = view.Series([]*usage.Record{w.Usage.Devices[d]}, days, w.Now)
}
return map[string]any{"person": view.Series([]*usage.Record{w.Usage.People[id]}, days, w.Now), "devices": devs}, nil
}
func personLinks(w *view.World, id string) (map[string]any, error) {
p := w.M.People[id]
if p == nil {
return nil, errors.New("Unknown person.")
}
hosts := view.PortalHosts(w.M)
var link any
links := []string{}
for i, h := range hosts {
l := view.PortalLink(w.M, p, h)
if l == "" {
continue
}
if i == 0 {
link = l
}
links = append(links, l)
}
return map[string]any{"code": p.Code, "link": link, "links": links}, nil
}
// deviceConfig is a device's configuration, for the admin to show (QR code, file, text). Showing it marks
// nothing: a configuration counts as given out when it is made or fetched on the portal.
func deviceConfig(w *view.World, d Deps, id string) (map[string]any, error) {
dev := w.M.Devices[id]
if dev == nil {
return nil, errors.New("Unknown device.")
}
if dev.Waiting {
return nil, errors.New("This router waits for an answer to its request: it has no configuration yet.")
}
var home []string
p := w.M.People[dev.Person]
var srv *model.Server
if p != nil {
srv = w.M.Servers[p.Server]
}
if srv != nil && d.Home != nil {
home = d.Home(srv, p)
}
cfg := handout.ClientConfig(w.M, dev, home)
if cfg == nil {
return map[string]any{"result": nil}, nil
}
raw, _ := json.Marshal(cfg)
var out map[string]any
json.Unmarshal(raw, &out)
// V2RayTun on Android takes the server by its IP: only where it can connect at all, and AmneziaWG 3.1
// (Maximum) is beyond it.
if dev.Kind != "router" && dev.Kind != "computer" && srv.Preset != "max" && d.HostIP != nil {
if am, ok := out["amnezia"].(map[string]any); ok {
am["host_ip"] = nil
if ip := d.HostIP(text(am["host"])); ip != "" {
am["host_ip"] = ip
}
}
}
return out, nil
}
// ordered is a JSON object's keys in the order the file has them.
func ordered(raw json.RawMessage) []string {
dec := json.NewDecoder(bytes.NewReader(raw))
if t, err := dec.Token(); err != nil || t != json.Delim('{') {
return nil
}
var keys []string
for dec.More() {
k, err := dec.Token()
if err != nil {
return keys
}
keys = append(keys, k.(string))
var skip json.RawMessage
if dec.Decode(&skip) != nil {
return keys
}
}
return keys
}
// serviceLog is the watchdog's state and the newest two hundred entries of the service log, newest first
// (dawnline.watchdog view): target and repair names in the page's language, the log as it was written.
func serviceLog(w *view.World, t *i18n.Catalog, run, data string) map[string]any {
var st struct {
Last any `json:"last"`
Targets json.RawMessage `json:"targets"`
Repairs []map[string]any `json:"repairs"`
}
files.ReadJSON(run+"/watchdog.json", &st)
var beat float64
files.ReadJSON(run+"/agent.beat", &beat)
names := map[string]string{"agent": t.T("Agent"), "nft": t.T("Access rules"), "fw": t.T("Firewall"), "portal": t.T("Portal"),
"resolver": t.T("Resolver"), "rpc": t.T("LuCI link")}
chip := func(id string, stored any) any {
if n, ok := names[id]; ok {
return n
}
if srv := w.M.Servers[strings.TrimPrefix(id, "cfg_")]; srv != nil && strings.HasPrefix(id, "cfg_") {
return fmt.Sprintf(t.T("%s · interface settings"), srv.Name)
}
return stored
}
now := time.Now().Unix()
targets := []any{map[string]any{"id": "agent", "name": names["agent"], "ok": beat > 0 && float64(now)-beat < 180, "tries": 0}}
var byID map[string]map[string]any
if json.Unmarshal(st.Targets, &byID) == nil {
for _, id := range ordered(st.Targets) {
x := byID[id]
tries := x["tries"]
if tries == nil {
tries = 0
}
targets = append(targets, map[string]any{"id": id, "name": chip(id, x["name"]), "ok": truthy(x["ok"]), "tries": tries})
}
}
repairs := []any{}
for _, r := range st.Repairs {
r["name"] = chip(text(r["id"]), r["name"])
repairs = append(repairs, r)
}
var last, beatAt any = st.Last, nil
if beat != 0 {
beatAt = beat
}
raw, err := os.ReadFile(run + "/service.log")
if err != nil {
raw, _ = os.ReadFile(data + "/service.log")
}
lines := strings.Split(string(raw), "\n")
log := []any{}
for i := len(lines) - 1; i >= 0 && len(log) < 200; i-- {
var e map[string]any
if lines[i] != "" && json.Unmarshal([]byte(lines[i]), &e) == nil && e != nil && truthy(e["ts"]) {
log = append(log, e)
}
}
return map[string]any{
"watchdog": map[string]any{"enabled": w.M.Settings.Watchdog, "last": last, "beat": beatAt, "now": now, "targets": targets, "repairs": repairs},
"log": log,
}
}
// truthy is a JSON value as ucode's !! reads it.
func truthy(v any) bool {
switch x := v.(type) {
case nil:
return false
case bool:
return x
case float64:
return x != 0
case string:
return x != ""
}
return true
}
var backupName = regexp.MustCompile(`^dawnline-([0-9]{8}-[0-9]{6})(-([a-z0-9-]+))?\.tar\.gz$`)
// backups is the backups kept in dir, the newest first: why each was made is in its name.
func backups(dir string) []map[string]any {
out := []map[string]any{}
entries, _ := os.ReadDir(dir)
for _, e := range entries {
m := backupName.FindStringSubmatch(e.Name())
if m == nil {
continue
}
reason := m[3]
if reason == "" {
reason = "manual"
}
var created, size int64
if st, err := os.Stat(dir + "/" + e.Name()); err == nil {
created, size = st.ModTime().Unix(), st.Size()
}
out = append(out, map[string]any{"name": e.Name(), "created": created, "size": size, "reason": reason})
}
sort.SliceStable(out, func(i, j int) bool { return out[i]["created"].(int64) > out[j]["created"].(int64) })
return out
}
// orNull is a name, or null when there is none.
func orNull(s string) any {
if s == "" {
return nil
}
return s
}
// interfaces is a server's interfaces for Settings: what exists, what is detected, what the server uses
// (dawnline.rpc.servers interfaces_get). A server not named is the first one.
func interfaces(w *view.World, d Deps, id string) (map[string]any, error) {
srv := w.M.Servers[id]
if ids := w.M.ServerIDs(); srv == nil && len(ids) > 0 {
srv = w.M.Servers[ids[0]]
}
if srv == nil || d.Snapshot == nil {
return nil, errors.New("No server yet.")
}
snap := d.Snapshot()
wan, home, nets, zones := snap.ServerNet(srv)
byDev := map[string]netview.HomeOf{}
homes := []any{}
for _, h := range home {
byDev[h.Dev] = h
homes = append(homes, map[string]any{"dev": h.Dev, "auto": h.Auto, "nets": h.Nets, "zone": orNull(h.Zone), "missing": h.Missing})
}
devs := []any{}
for _, name := range snap.Names() {
x := snap.Devices[name]
h, isHome := byDev[name]
devs = append(devs, map[string]any{
"name": x.Name, "category": x.Category, "kind": x.Kind, "up": x.Up, "master": orNull(x.Master), "members": x.Members, "nets": x.Nets,
"addrs": x.Addrs, "logical": orNull(x.Logical), "proto": orNull(x.Proto), "zone": orNull(x.Zone),
"own": name == srv.Ifname, "home": isHome, "auto": isHome && h.Auto,
})
}
// saved names that no longer exist still show, so they can be removed
for _, h := range home {
if h.Missing {
devs = append(devs, map[string]any{"name": h.Dev, "category": "other", "kind": "other", "missing": true, "nets": []string{}, "addrs": []string{},
"members": []string{}, "home": true, "auto": h.Auto})
}
}
lanOnly := 0
for _, p := range w.M.People {
if p.Server == srv.ID && p.Access == "lan" {
lanOnly++
}
}
lans := srv.LanDevices
if lans == nil {
lans = []string{}
}
likeHome, _ := roster.LikeHome(d.RunDir)
return map[string]any{
"server": srv.ID, "ifname": srv.Ifname,
"detected": map[string]any{"wan": orNull(snap.WAN), "lan": orNull(snap.LAN)},
"saved": map[string]any{"wan": srv.DetectedWan, "lan": srv.DetectedLan},
"config": map[string]any{"auto_wan": srv.AutoWan, "wan_device": srv.WanDevice, "auto_lan": srv.AutoLan, "lan_devices": lans, "lan_like": srv.LanLike,
"device_mesh": srv.DeviceMesh, "mesh": srv.Mesh},
"like_home": likeHome, "needs": Needs()["home"],
"effective": map[string]any{"wan": map[string]any{"dev": orNull(wan.Dev), "auto": wan.Auto, "zone": wan.Zone, "missing": wan.Missing},
"home": homes, "nets": nets, "zones": zones},
"zones": snap.Zones, "devices": devs, "lan_only": lanOnly,
}, nil
}
// endpointProbe is where a public address points, as the admin types it: the facts Health uses, in words.
// `id` names the server whose WAN to compare with (a new one: the default WAN).
func endpointProbe(w *view.World, t *i18n.Catalog, d Deps, a map[string]any) map[string]any {
host := strings.Trim(text(a["host"]), " \t\r\n\v\f")
if host != "" && !endpoint.ValidHost(host) {
return map[string]any{"error": t.T("Public address must be a hostname or IP address.")}
}
srv := w.M.Servers[strings.Trim(text(a["id"]), " \t\r\n\v\f")]
f := endpoint.Of(host, endpointReads(d, srv, host))
port := 4500
if srv != nil {
port = srv.Port
} else if p := int(num(a["port"])); p != 0 {
port = p
}
status, words := endpoint.Words(f, fmt.Sprintf("UDP %d", port), t)
raw, _ := json.Marshal(f)
var out map[string]any
json.Unmarshal(raw, &out)
out["status"], out["text"] = status, words
return out
}
// endpointReads is what a public address's facts are made from, read on the router.
func endpointReads(d Deps, srv *model.Server, host string) endpoint.Reads {
r := endpoint.Reads{}
if host != "" && d.Resolve != nil {
r.IPs = d.Resolve(host)
}
if d.WANOf != nil {
r.WAN = d.WANOf(srv)
}
// the router's IPv6 only when the name has some to compare with
for _, ip := range r.IPs {
if strings.Contains(ip, ":") {
r.Router6, r.Known6 = router6()
break
}
}
return r
}
// router6 is the router's public IPv6 addresses, and whether `ip` could tell.
func router6() ([]string, bool) {
out, err := exec.Command("ip", "-6", "-j", "addr", "show", "scope", "global").Output()
if err != nil {
return nil, false
}
if len(bytes.TrimSpace(out)) == 0 {
return []string{}, true
}
var links []struct {
Info []struct {
Family string `json:"family"`
Local string `json:"local"`
DADFailed bool `json:"dadfailed"`
} `json:"addr_info"`
}
if json.Unmarshal(out, &links) != nil {
return nil, false
}
return endpoint.Own6(links), true
}
// accessCheck is "Check access" between two sides, each a device's id or an IPv4 address.
func accessCheck(w *view.World, t *i18n.Catalog, d Deps, from, to string) (map[string]any, error) {
states := map[string]string{}
for id, p := range w.M.People {
states[id] = state.Of(w.M, p, w.Usage, w.Now)
}
ctx := access.Ctx{States: states, Peers: w.Peers, Snap: d.Snapshot(), T: t, Online: func(dev *model.Device, peer awg.Peer) bool {
// the watcher's word; without one, a recent handshake
if w.Online != nil {
return w.Online[dev.ID]
}
return peer.Handshake > 0 && w.Now-peer.Handshake < 190
}}
ok, steps, err := access.Check(w.M, from, to, ctx)
if err != nil {
return nil, err
}
return map[string]any{"ok": ok, "steps": steps}, nil
}
+800
View File
@@ -0,0 +1,800 @@
package methods
import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"reflect"
"strings"
"testing"
"time"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/disguise"
"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/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/zone"
)
func world() *view.World {
m := &model.Model{
People: map[string]*model.Person{"anna": {ID: "anna", Name: "Anna", Server: "home", Code: "abcdef-ghijkl-mnopqr", Devices: []string{"phone"}}},
Servers: map[string]*model.Server{"home": {ID: "home"}},
Devices: map[string]*model.Device{"phone": {ID: "phone", Person: "anna"}},
}
m.Settings.PortalEnabled, m.Settings.PortalPort, m.Settings.PortalHost, m.Settings.PortalAlt = true, 4500, "vpn.example.org", []string{"203.0.113.7", "vpn.example.org"}
return &view.World{M: m, Usage: usage.Empty(), Now: 1_800_000_000}
}
func ask(t *testing.T, d Deps, name string, args map[string]any) map[string]any {
t.Helper()
for _, m := range List(d) {
if m.Name == name {
res, err := m.Handle(context.Background(), args)
if err != nil {
t.Fatal(err)
}
return res
}
}
t.Fatalf("no method %s", name)
return nil
}
func TestEvents(t *testing.T) {
log := []map[string]any{
{"ts": 10.0, "type": "connect", "person": "anna", "device": "phone"},
{"ts": 20.0, "type": "server_down"},
{"ts": 30.0, "type": "disconnect", "person": "anna", "device": "phone"},
{"ts": 40.0, "type": "connect", "person": "boris", "device": "tv"},
{"ts": 50.0, "type": "roam", "person": "anna", "device": "laptop"},
}
d := Deps{World: world, Events: func() []map[string]any { return log }}
ts := func(res map[string]any) (out []float64) {
for _, e := range res["events"].([]map[string]any) {
out = append(out, e["ts"].(float64))
}
return
}
for name, c := range map[string]struct {
args map[string]any
want []float64
total int
}{
"all but the old tunnel events": {map[string]any{}, []float64{10, 30, 40, 50}, 4},
"a person's": {map[string]any{"person": "anna"}, []float64{10, 30, 50}, 3},
"a device's, the newest one": {map[string]any{"device": "phone", "limit": int32(1)}, []float64{30}, 2},
"after a moment": {map[string]any{"since": int64(30)}, []float64{40, 50}, 2},
"nothing passes": {map[string]any{"person": "nobody"}, nil, 0},
} {
res := ask(t, d, "events", c.args)
if got := ts(res); !reflect.DeepEqual(got, c.want) || res["total"] != c.total {
t.Errorf("%s: %v of %v", name, got, res["total"])
}
}
pol := ask(t, d, "events", nil)["policy"].(map[string]any)
if pol["keep_days"] != 30 || pol["max"] != 2000 || pol["persist"] != "hourly" || pol["usage_days"] != 62 || pol["usage_months"] != 24 || len(pol["categories"].([]string)) != 3 {
t.Fatalf("the policy: %v", pol)
}
}
func TestPersonQuestions(t *testing.T) {
d := Deps{World: world, Events: func() []map[string]any { return nil }}
links := ask(t, d, "person_links", map[string]any{"id": "anna"})
want := map[string]any{"code": "abcdef-ghijkl-mnopqr", "link": "https://vpn.example.org:4500/abcdef-ghijkl-mnopqr",
"links": []string{"https://vpn.example.org:4500/abcdef-ghijkl-mnopqr", "http://203.0.113.7:4501/abcdef-ghijkl-mnopqr"}}
if !reflect.DeepEqual(links, want) {
t.Fatalf("links: %v", links)
}
u := ask(t, d, "person_usage", map[string]any{"id": "anna", "days": 7})
if len(u["person"].([]view.Day)) != 7 || len(u["devices"].(map[string]any)["phone"].([]view.Day)) != 7 {
t.Fatalf("usage: %v", u)
}
if u := ask(t, d, "person_usage", map[string]any{"id": "anna"}); len(u["person"].([]view.Day)) != 30 {
t.Fatal("days not said are thirty")
}
for _, name := range []string{"person_links", "person_usage"} {
if res := ask(t, d, name, map[string]any{"id": "nobody"}); res["error"] != "Unknown person." {
t.Errorf("%s for nobody: %v", name, res)
}
}
// before the watcher's first look there is nothing to answer from
starting := Deps{World: func() *view.World { return nil }}
if res := ask(t, starting, "events", nil); res["error"] == nil {
t.Fatal("answered with no world")
}
var _ []ubus.Method = List(d)
}
func TestServiceLogAndBackups(t *testing.T) {
dir := t.TempDir()
os.MkdirAll(dir+"/data/backups", 0o755)
os.WriteFile(dir+"/watchdog.json", []byte(`{"last":1799999990,"targets":{"nft":{"ok":true,"name":"Access rules","tries":0},"cfg_home":{"ok":false,"name":"old","tries":2},"awg_home":{"ok":1,"name":"Home · AmneziaWG"}},"repairs":[{"id":"fw","name":"Firewall","ts":5}]}`), 0o600)
os.WriteFile(dir+"/service.log", []byte("{\"ts\":1,\"msg\":\"a\"}\nnot json\n\n{\"msg\":\"no time\"}\n{\"ts\":3,\"msg\":\"c\"}\n"), 0o600)
for i, name := range []string{"dawnline-20270101-000000.tar.gz", "dawnline-20270102-000000-pre-update.tar.gz", "notes.txt"} {
os.WriteFile(dir+"/data/backups/"+name, []byte("x"), 0o600)
at := time.Unix(1_800_000_000+int64(i)*60, 0)
os.Chtimes(dir+"/data/backups/"+name, at, at)
}
w := world()
w.M.Servers["home"].Name = "Home"
d := Deps{World: func() *view.World { return w }, RunDir: dir, DataDir: dir + "/data"}
res := ask(t, d, "service_log", nil)
wd := res["watchdog"].(map[string]any)
var got []string
for _, x := range wd["targets"].([]any) {
m := x.(map[string]any)
got = append(got, fmt.Sprint(m["id"], "=", m["name"], "/", m["ok"], "/", m["tries"]))
}
want := []string{"agent=Agent/false/0", "nft=Access rules/true/0", "cfg_home=Home · interface settings/false/2", "awg_home=Home · AmneziaWG/true/0"}
if !reflect.DeepEqual(got, want) || wd["beat"] != nil || wd["last"] != 1799999990.0 || len(wd["repairs"].([]any)) != 1 {
t.Fatalf("the watchdog: %v %v", got, wd)
}
if log := res["log"].([]any); len(log) != 2 || log[0].(map[string]any)["msg"] != "c" {
t.Fatalf("the log: %v", log)
}
b := ask(t, d, "backup_list", nil)["backups"].([]map[string]any)
if len(b) != 2 || b[0]["reason"] != "pre-update" || b[1]["reason"] != "manual" || b[1]["size"] != int64(1) {
t.Fatalf("backups: %v", b)
}
}
func TestDeviceConfig(t *testing.T) {
d := Deps{World: world, HostIP: func(string) string { return "203.0.113.7" }}
if res := ask(t, d, "device_config", map[string]any{"id": "nothing"}); res["error"] != "Unknown device." {
t.Fatalf("an unknown device: %v", res)
}
res := ask(t, d, "device_config", map[string]any{"id": "phone"})
am, _ := res["amnezia"].(map[string]any)
if res["conf"] == nil || am == nil || am["host_ip"] != "203.0.113.7" {
t.Fatalf("a phone's configuration: %v", res)
}
w := world()
w.M.Devices["phone"].Kind = "computer"
res = ask(t, Deps{World: func() *view.World { return w }, HostIP: d.HostIP}, "device_config", map[string]any{"id": "phone"})
if _, has := res["amnezia"].(map[string]any)["host_ip"]; has {
t.Fatal("a computer was given the server's IP")
}
}
func TestTakenPorts(t *testing.T) {
netstat := `Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1899/uhttpd
tcp 0 0 0.0.0.0:4500 0.0.0.0:* LISTEN 2001/dawnlined
tcp 0 0 192.168.1.1:8443 0.0.0.0:* LISTEN 2100/nginx
tcp 0 0 :::8443 :::* LISTEN 2100/nginx
tcp 0 0 :::9000 :::* LISTEN -
udp 0 0 0.0.0.0:53 0.0.0.0:* 900/dnsmasq
`
progs := programs(netstat, "tcp", func(pid string) string {
if pid == "2001" {
return "/usr/libexec/dawnline/dawnlined\x00-portal-port\x004500"
}
return "/usr/sbin/other"
})
if !reflect.DeepEqual(progs, []held{{80, "uhttpd"}, {8443, "nginx"}}) {
t.Fatalf("programs: %v", progs)
}
fw, _ := uci.Parse(`
config redirect 'web'
option name 'Web server'
option src 'wan'
option src_dport '8080-8090 9443'
option proto 'tcp'
config redirect 'game'
option src 'wan'
option src_dport '27015'
option proto 'udp'
config redirect 'both'
option src 'wan'
option src_dport '5000:5010'
config redirect 'off'
option src 'wan'
option src_dport '6000'
option enabled '0'
config redirect 'dawnline_x'
option src 'wan'
option src_dport '7000'
config redirect 'lan'
option src 'lan'
option src_dport '7100'
`)
w := world()
got := tcpTaken(w.M, nil, progs, fw)
var lines []string
for _, x := range got {
lines = append(lines, fmt.Sprint(x["from"], "-", x["to"], " ", x["who"]))
}
want := []string{"22-22 the router itself", "53-53 the router itself", "80-80 the router itself", "443-443 the router itself", "8443-8443 nginx on this router",
"8080-8090 port forward “Web server”", "9443-9443 port forward “Web server”", "5000-5010 port forward “both”"}
if !reflect.DeepEqual(lines, want) {
t.Fatalf("taken:\n%v\n%v", lines, want)
}
// UDP: the other server, a tunnel of the router's own, what is bound, a forward
conf, _ := uci.Parse("config settings 'settings'\nconfig server 'home'\n\toption name 'Home'\n\toption port '4500'\n\toption ifname 'dawn0'\n\toption subnet '10.77.0.0/24'\n" +
"config server 'cabin'\n\toption name 'Cabin'\n\toption port '4600'\n\toption ifname 'dawn1'\n\toption subnet '10.78.0.9/24'\n")
w.M = model.Load(conf, nil)
network, _ := uci.Parse("config interface 'wg0'\n\toption listen_port '51820'\nconfig interface 'dawn0'\n\toption listen_port '4500'\nconfig interface 'lan'\n")
proc := " sl local_address rem_address st\n 12: 00000000:0035 00000000:0000 07 x\n 13: 00000000:1194 00000000:0000 07 x\n 14: 0A000001:D431 08080808:0035 01 x\n 15: 00000000:CA6C 00000000:0000 07 x\n 16: 00000000:0043 00000000:0000 07 x\n"
udp := programs(netstat, "udp", func(string) string { return "" })
lines = nil
for _, x := range udpTaken(w.M, w.M.Servers["home"], nil, network, fw, udpBound(proc, proc), udp) {
lines = append(lines, fmt.Sprint(x["from"], "-", x["to"], " ", x["who"]))
}
want = []string{"4600-4600 server “Cabin”", "51820-51820 interface wg0", "53-53 dnsmasq on this router", "67-67 another program on this router",
"27015-27015 port forward “game”", "5000-5010 port forward “both”"}
if !reflect.DeepEqual(lines, want) {
t.Fatalf("taken UDP:\n%v\n%v", lines, want)
}
nets := takenNetworks(w.M, []Address{{"lan", "192.168.1.1/24"}, {"guest", "192.168.1.7/24"}, {"wan", "203.0.113.7/32"}})
if fmt.Sprint(nets) != "[map[cidr:192.168.1.0/24 name:lan] map[cidr:10.77.0.0/24 name:Home] map[cidr:10.78.0.0/24 name:Cabin]]" {
t.Fatalf("networks: %v", nets)
}
}
func TestSettingsAndBackupRead(t *testing.T) {
dir := t.TempDir()
os.MkdirAll(dir+"/data/backups", 0o755)
os.WriteFile(dir+"/config", []byte("config settings 'settings'\n\toption portal_port '4500'\n\tlist portal_alt 'a.example'\n\tlist portal_alt 'b.example'\n"), 0o600)
os.WriteFile(dir+"/events.json", []byte(`[{"ts":5,"type":"connect"},{"ts":9,"type":"person_added"}]`), 0o600)
os.WriteFile(dir+"/geo.json", []byte(`{"ips":{"203.0.113.7":{},"198.51.100.4":{}}}`), 0o600)
os.WriteFile(dir+"/data/backups/dawnline-20270101-000000.tar.gz", []byte("archive"), 0o600)
w := world()
w.M.Settings.PortalEnabled = false
w.Usage.People["anna"] = &usage.Record{Days: map[string]usage.Pair{"2027-01-02": {1, 2}, "2026-12-30": {1, 2}}}
d := Deps{World: func() *view.World { return w }, RunDir: dir, DataDir: dir + "/data", Config: dir + "/config"}
res := ask(t, d, "settings_get", nil)
set := res["settings"].(map[string]any)
if set["portal_port"] != "4500" || !reflect.DeepEqual(set["portal_alt"], []string{"a.example", "b.example"}) || set["domain"] != "" || len(set) != len(Settings)+1 || res["cert"] != nil || res["geo_cached"] != 2 {
t.Fatalf("settings: %v %v %v", set, res["cert"], res["geo_cached"])
}
logs := res["logs"].(map[string]any)
if logs["events"] != 2 || logs["oldest"] != 5.0 || logs["usage_since"] != "2026-12-30" || logs["usage_days"] != 2 || logs["log_ram"] != int64(58) ||
!reflect.DeepEqual(logs["by_category"], map[string]int{"connections": 1, "people": 1, "problems": 0}) {
t.Fatalf("logs: %v", logs)
}
if got := ask(t, d, "backup_read", map[string]any{"name": "dawnline-20270101-000000.tar.gz"}); got["data"] != "YXJjaGl2ZQ==" {
t.Fatalf("a backup: %v", got)
}
for name, want := range map[string]string{"../config": "Invalid backup name.", "dawnline-20270102-000000.tar.gz": "Backup not found."} {
if got := ask(t, d, "backup_read", map[string]any{"name": name}); got["error"] != want {
t.Errorf("%s: %v", name, got)
}
}
if !covers("*.example.org", "vpn.example.org") || covers("*.example.org", "a.vpn.example.org") || covers("*.example.org", "example.org") || !covers("vpn.example.org", "vpn.example.org") {
t.Fatal("which names a certificate covers")
}
}
func TestUpdatesStatus(t *testing.T) {
dir := t.TempDir()
os.MkdirAll(dir+"/data/keep/app", 0o755)
os.MkdirAll(dir+"/i18n", 0o755)
os.WriteFile(dir+"/i18n/dawnline.ru.lmo", nil, 0o644)
os.WriteFile(dir+"/i18n/base.ru.lmo", nil, 0o644)
os.WriteFile(dir+"/update-job.json", []byte(`{"id":"j1","state":"running","started":5}`), 0o600)
os.WriteFile(dir+"/data/keep/app/pkg.apk", []byte("x"), 0o600)
os.WriteFile(dir+"/data/keep/app/meta.json", []byte(`{"version":"0.17.0","files":["`+dir+`/data/keep/app/pkg.apk"],"saved":7}`), 0o600)
hist := "["
for i := 0; i < 15; i++ {
hist += fmt.Sprintf(`{"ts":%d},`, i)
}
os.WriteFile(dir+"/data/update-history.json", []byte(hist[:len(hist)-1]+"]"), 0o600)
d := Deps{World: world, RunDir: dir, DataDir: dir + "/data", Catalogs: dir + "/i18n"}
res := ask(t, d, "updates_status", nil)
job := res["job"].(map[string]any)
// nobody holds the lock: the job that says it runs has stopped
if job["state"] != "failed" || job["error"] != "The update stopped unexpectedly." || job["finished"] == nil {
t.Fatalf("a job whose worker is gone: %v", job)
}
kept := res["kept"].(map[string]any)
if kept["amneziawg"] != nil || kept["app"].(map[string]any)["version"] != "0.17.0" || len(res["history"].([]any)) != 12 ||
!reflect.DeepEqual(res["translations"], []string{"ru"}) || res["check"] != nil || res["repo"] != Repo {
t.Fatalf("status: %v", res)
}
os.Remove(dir + "/data/keep/app/pkg.apk")
if res := ask(t, d, "updates_status", nil); res["kept"].(map[string]any)["app"] != nil {
t.Fatal("a kept version whose file is gone is still offered")
}
}
func TestWhatANewServerStartsFrom(t *testing.T) {
held := func(ranges ...[2]int) (out []map[string]any) {
for _, r := range ranges {
out = append(out, map[string]any{"from": r[0], "to": r[1]})
}
return
}
if freePort(nil) != 4500 {
t.Fatal("the usual port is free and not taken")
}
for i := 0; i < 50; i++ {
if p := freePort(held([2]int{4500, 4500}, [2]int{40000, 45000})); p <= 45000 || p > 60000 {
t.Fatalf("a port that is taken: %d", p)
}
n, ok := ipnet.Subnet(freeSubnet([]string{"10.0.0.0/9", "192.168.1.0/24", "not a network"}))
if !ok || n.Prefix != 24 || ipnet.String(n.Base)[:3] != "10." || n.Base < 10<<24|128<<16 {
t.Fatalf("a network inside one taken: %v", n)
}
}
if n := freeSubnet([]string{"10.0.0.0/8"}); n[:4] != "172." {
t.Fatalf("with all of 10/8 taken: %s", n)
}
if freePortalPort(4500, 0, held([2]int{4501, 4501}, [2]int{8443, 8443})) != 9443 || freePortalPort(4500, 0, nil) != 4500 || freePortalPort(4500, 7000, held([2]int{4500, 4500})) != 7000 {
t.Fatal("the portal's port")
}
conf, _ := uci.Parse("config settings 'settings'\nconfig server 'home'\n\toption ifname 'dawn0'\nconfig person 'server2'\n\toption server 'home'\n")
m := model.Load(conf, nil)
network, _ := uci.Parse("config interface 'dawn1'\nconfig interface 'lan'\n")
if freeServerID(m) != "server3" || freeIfname(m, network) != "dawn2" || idTaken(m, "nobody") || !idTaken(m, "api") || !idTaken(m, "main") {
t.Fatalf("free names: %s %s", freeServerID(m), freeIfname(m, network))
}
for wan, want := range map[string]string{"": "none", "203.0.113.7": "public", "100.70.1.1": "cgnat", "192.168.1.2": "private", "10.1.1.1": "private"} {
if got := wanKind(wan); got != want {
t.Errorf("%q is %s", wan, got)
}
}
}
func TestHomeHostsAndExport(t *testing.T) {
dir := t.TempDir()
os.WriteFile(dir+"/leases", []byte("1800000000 aa:bb:cc:00:00:01 192.168.77.20 nas *\n1800000000 aa:bb:cc:00:00:02 192.168.77.5 * *\n1800000000 aa:bb:cc:00:00:03 10.9.9.9 elsewhere *\n"), 0o644)
os.WriteFile(dir+"/dhcp", []byte("config host\n\toption name 'printer'\n\toption ip '192.168.77.9'\n\tlist mac 'aa:bb:cc:00:00:04'\n\tlist mac 'aa:bb:cc:00:00:05'\nconfig host\n\toption name 'again'\n\toption ip '192.168.77.20'\n"), 0o644)
os.WriteFile(dir+"/config", []byte("config settings 'settings'\n\toption portal_port '4500'\n\tlist portal_alt 'a.example'\nconfig server 'home'\n\toption private_key 'secret'\n"), 0o600)
conf, _ := uci.Parse("config settings 'settings'\nconfig server 'home'\n\toption ifname 'dawn0'\n\toption lan_device 'br-lan'\n\toption auto_lan '0'\n")
w := &view.World{M: model.Load(conf, nil), Usage: usage.Empty()}
var logged []string
d := Deps{World: func() *view.World { return w }, Config: dir + "/config", RunDir: dir,
Snapshot: func() *netview.Snapshot {
return &netview.Snapshot{Devices: map[string]*netview.Device{"br-lan": {Name: "br-lan", Nets: []string{"192.168.77.0/24"}}}}
},
Bus: func(object, method string, _ map[string]any) map[string]any {
if object != "umdns" {
return nil
}
return map[string]any{
"_ipp._tcp": map[string]any{"Printer": map[string]any{"ipv4": []any{"192.168.77.9"}, "port": 631, "host": "printer.local"}},
"_airplay._tcp": map[string]any{"TV": map[string]any{"ipv4": "192.168.77.30", "port": 7000, "host": "tv.local."}, "Far": map[string]any{"ipv4": "10.1.1.1"}, "None": map[string]any{}},
}
},
Log: &servicelog.Log{RunDir: dir, Syslog: func(_, line string) { logged = append(logged, line) }},
}
res, err := homeHosts(w, d, "", dir+"/leases", dir+"/dhcp")
if err != nil {
t.Fatal(err)
}
raw, _ := json.Marshal(res)
want := `{"hosts":[{"address":"192.168.77.5","name":"","mac":"aa:bb:cc:00:00:02","services":[]},` +
`{"address":"192.168.77.9","name":"printer","mac":"aa:bb:cc:00:00:04","services":[{"name":"Printer","port":631,"type":"_ipp._tcp"}]},` +
`{"address":"192.168.77.20","name":"nas","mac":"aa:bb:cc:00:00:01","services":[]},` +
`{"address":"192.168.77.30","name":"tv","mac":"","services":[{"name":"TV","port":7000,"type":"_airplay._tcp"}]}],"mdns":true}`
if string(raw) != want {
t.Fatalf("the devices at home:\n%s", raw)
}
out := ask(t, d, "export", nil)
sections := out["sections"].([]map[string]any)
if out["format"] != "dawnline" || len(sections) != 2 || sections[0]["portal_port"] != "4500" || !reflect.DeepEqual(sections[0]["portal_alt"], []string{"a.example"}) ||
sections[1][".type"] != "server" || sections[1][".name"] != "home" || sections[1]["private_key"] != "secret" {
t.Fatalf("an export: %v", out)
}
// taking the keys out of the router is written down
if len(logged) != 1 || logged[0] != "[info] [dawnline] admin exported all settings · the file holds private keys" {
t.Fatalf("the log: %v", logged)
}
}
func TestClearingTheLogs(t *testing.T) {
dir := t.TempDir()
os.MkdirAll(dir+"/data", 0o755)
now := time.Now().Unix()
os.WriteFile(dir+"/events.json", []byte(fmt.Sprintf(`[{"ts":%d,"type":"connect","person":"anna","device":"phone"},{"ts":%d,"type":"connect","person":"anna","device":"phone"},{"ts":%d,"type":"paused","person":"boris"}]`,
now-40*86400, now-100, now-50)), 0o600)
os.WriteFile(dir+"/service.log", []byte(fmt.Sprintf("{\"ts\":%d,\"msg\":\"old\"}\n{\"ts\":%d,\"msg\":\"new\"}\n", now-40*86400, now-10)), 0o600)
var said []string
d := Deps{World: world, RunDir: dir, DataDir: dir + "/data", Log: &servicelog.Log{RunDir: dir, Syslog: func(_, line string) { said = append(said, line) }}}
for args, want := range map[string]string{
`{"person":"nobody"}`: "Unknown person.", `{"category":"gossip"}`: "Unknown category.", `{"older_days":"0"}`: "Older than must be a number of days.", `{"older_days":"a week"}`: "Older than must be a number of days.",
} {
var a map[string]any
json.Unmarshal([]byte(args), &a)
if res := ask(t, d, "logs_clear", a); res["error"] != want {
t.Errorf("%s: %v", args, res)
}
}
if res := ask(t, d, "logs_clear", map[string]any{"person": "anna", "older_days": "7"}); res["removed"] != 1 || res["ok"] != true {
t.Fatalf("an old event of a person's: %v", res)
}
if res := ask(t, d, "logs_clear", map[string]any{"device": "tv"}); res["removed"] != 0 {
t.Fatalf("nothing to remove: %v", res)
}
if res := ask(t, d, "service_log_clear", map[string]any{"older_days": "7"}); res["removed"] != 1 {
t.Fatalf("an old entry of the service log: %v", res)
}
want := []string{"[info] [dawnline] admin cleared the activity log · 1 event · Anna · older than 7 days", "[info] [dawnline] admin cleared the service log · 1 entry older than 7 days"}
if !reflect.DeepEqual(said, want) {
t.Fatalf("what was written down:\n%s", strings.Join(said, "\n"))
}
var left []map[string]any
raw, _ := os.ReadFile(dir + "/events.json")
json.Unmarshal(raw, &left)
if len(left) != 2 {
t.Fatalf("the activity left: %s", raw)
}
// the service log keeps its newer entry, and the two lines of what was cleared
raw, _ = os.ReadFile(dir + "/service.log")
if n := strings.Count(string(raw), "\n"); n != 3 || strings.Contains(string(raw), `"old"`) {
t.Fatalf("the service log left:\n%s", raw)
}
}
func TestForgetting(t *testing.T) {
dir := t.TempDir()
os.MkdirAll(dir+"/data/backups", 0o755)
os.WriteFile(dir+"/geo.json", []byte(`{"ips":{"203.0.113.7":{"city":"X"}},"home":{"lat":1}}`), 0o600)
os.WriteFile(dir+"/data/backups/dawnline-20270101-000000.tar.gz", []byte("x"), 0o600)
var said, reset []string
d := Deps{World: world, RunDir: dir, DataDir: dir + "/data", Log: &servicelog.Log{RunDir: dir, Syslog: func(_, line string) { said = append(said, line) }},
ResetUsage: func(scope, id string) bool { reset = append(reset, scope+" "+id); return true }}
for args, want := range map[string]string{`{"scope":"some"}`: "Unknown scope.", `{"scope":"person","id":"nobody"}`: "Unknown person.", `{"scope":"limit","id":"nobody"}`: "Unknown person.",
`{"scope":"device","id":"nothing"}`: "Unknown device."} {
var a map[string]any
json.Unmarshal([]byte(args), &a)
if res := ask(t, d, "usage_clear", a); res["error"] != want {
t.Errorf("%s: %v", args, res)
}
}
for _, a := range []map[string]any{{"scope": "all"}, {"scope": "person", "id": "anna"}, {"scope": "limit", "id": "anna"}, {"scope": "device", "id": "phone"}} {
if res := ask(t, d, "usage_clear", a); res["ok"] != true {
t.Fatalf("%v: %v", a, res)
}
}
if strings.Join(reset, "; ") != "all ; person anna; limit anna; device phone" {
t.Fatalf("what was reset: %v", reset)
}
want := "[info] [dawnline] admin reset traffic history for everyone\n[info] [dawnline] admin reset traffic history for Anna\n" +
"[info] [dawnline] admin reset the data limit's usage for Anna\n[info] [dawnline] admin reset traffic history for (Anna)"
if got := strings.Join(said, "\n"); got != want {
t.Fatalf("what was written down:\n%s", got)
}
ask(t, d, "geo_clear", nil)
for _, f := range []string{dir + "/geo.json", dir + "/data/geo.json"} {
if raw, _ := os.ReadFile(f); strings.Join(strings.Fields(string(raw)), "") != `{"home":null,"ips":{}}` {
t.Fatalf("%s after forgetting: %s", f, raw)
}
}
for name, want := range map[string]string{"../x": "Invalid backup name.", "dawnline-20270102-000000.tar.gz": "Backup not found."} {
if res := ask(t, d, "backup_delete", map[string]any{"name": name}); res["error"] != want {
t.Errorf("%s: %v", name, res)
}
}
if res := ask(t, d, "backup_delete", map[string]any{"name": "dawnline-20270101-000000.tar.gz"}); res["ok"] != true || exists(dir+"/data/backups/dawnline-20270101-000000.tar.gz") {
t.Fatalf("a backup removed: %v", res)
}
}
func TestBackupCreate(t *testing.T) {
dir := t.TempDir()
os.MkdirAll(dir+"/data", 0o755)
os.WriteFile(dir+"/config", []byte("config settings 'settings'\n"), 0o600)
os.WriteFile(dir+"/usage.json", []byte(`{"live":true}`), 0o600)
os.WriteFile(dir+"/data/events.json", []byte(`[]`), 0o600)
d := Deps{Config: dir + "/config", RunDir: dir, DataDir: dir + "/data"}
now := int64(1_800_000_000)
clock = func() int64 { now += 60; return now }
defer func() { clock = func() int64 { return time.Now().Unix() } }()
read := func(name string) map[string]string {
f, err := os.Open(dir + "/data/backups/" + name)
if err != nil {
t.Fatal(err)
}
defer f.Close()
zr, err := gzip.NewReader(f)
if err != nil {
t.Fatal(err)
}
tr := tar.NewReader(zr)
out := map[string]string{}
for {
h, err := tr.Next()
if err != nil {
break
}
raw, _ := io.ReadAll(tr)
out[h.Name] = string(raw)
}
return out
}
// by hand: everything, the live counters put on flash first
name, err := backupCreate(d, "")
if err != nil || !backupFile.MatchString(name) || strings.Contains(name[len("dawnline-20270115-080100"):], "-") {
t.Fatalf("a backup by hand: %q %v", name, err)
}
if got := read(name); len(got) != 3 || got["etc/config/dawnline"] != "config settings 'settings'\n" || got["etc/dawnline/usage.json"] != `{"live":true}` || got["etc/dawnline/events.json"] != "[]" {
t.Fatalf("what it holds: %v", got)
}
// before a removal: the settings alone, and its kind in its name
name, _ = backupCreate(d, "pre-delete")
if got := read(name); len(got) != 1 || !strings.HasSuffix(name, "-pre-delete.tar.gz") {
t.Fatalf("a backup before a removal: %s %v", name, got)
}
// of each kind only the newest stay: five before removals and the one just made
for i := 0; i < 8; i++ {
backupCreate(d, "pre-delete")
at := time.Unix(now, 0)
os.Chtimes(dir+"/data/backups/"+"dawnline-"+at.In(zone.Local()).Format("20060102-150405")+"-pre-delete.tar.gz", at, at)
}
kinds := map[string]int{}
for _, b := range backups(dir + "/data/backups") {
kinds[b["reason"].(string)]++
}
if kinds["pre-delete"] != 6 || kinds["manual"] != 1 {
t.Fatalf("what stays: %v", kinds)
}
left, _ := os.ReadDir(dir + "/data/backups")
for _, e := range left {
if strings.HasPrefix(e.Name(), ".") {
t.Fatalf("a half-written archive stays: %s", e.Name())
}
}
}
func TestANewDisguise(t *testing.T) {
dir := t.TempDir()
os.WriteFile(dir+"/dawnline", []byte("config settings 'settings'\nconfig server 'home'\n\toption name 'Home'\n\toption jc '3'\n\toption i5 '<r 9>'\n\toption header_protection_key 'old'\n\toption preset 'strong'\n"), 0o600)
full := awg.Caps{V2: true, V3: true}
var said []string
d := Deps{World: world, Store: &store.Store{Config: dir + "/dawnline", RunDir: dir}, Caps: func() awg.Caps { return full },
Log: &servicelog.Log{RunDir: dir, Syslog: func(_, line string) { said = append(said, line) }}}
for _, name := range []string{"balanced", "max", "", "off"} {
if res := ask(t, d, "server_obfuscation", map[string]any{"id": "home", "preset": name}); res["error"] != nil {
t.Fatalf("%s: %v", name, res)
}
_, m, _ := d.Store.Read()
srv := m.Servers["home"]
want := name
if want == "" {
// none named: the strongest this router runs
want = "max"
}
if srv.Preset != want || srv.Obf["i5"] != "" || srv.Obf["header_protection_key"] == "old" || len(disguise.Errors(srv.Obf)) != 0 || len(srv.Obf) != len(disguise.Preset(want)) {
t.Fatalf("%s left %s with %v", name, srv.Preset, srv.Obf)
}
}
if len(said) != 4 || said[1] != "[info] [awg] admin changed server Home · disguise: max" {
t.Fatalf("what was written down: %v", said)
}
// an AmneziaWG that cannot run it: told what to install
full = awg.Caps{V2: true}
if res := ask(t, d, "server_obfuscation", map[string]any{"id": "home", "preset": "max"}); !strings.HasPrefix(text(res["error"]), "Maximum needs AmneziaWG 3.1") {
t.Fatalf("3.1 on a 2.0 router: %v", res)
}
if res := ask(t, d, "server_obfuscation", map[string]any{"id": "home"}); res["error"] != nil {
t.Fatalf("the strongest a 2.0 router runs: %v", res)
}
if _, m, _ := d.Store.Read(); m.Servers["home"].Preset != "balanced" {
t.Fatalf("it is %s", m.Servers["home"].Preset)
}
}
func TestFixesAndUpdates(t *testing.T) {
dir := t.TempDir()
conf, _ := uci.Parse("config settings 'settings'\nconfig server 'home'\n\toption ifname 'dawn0'\n\toption private_key 'k'\n\toption subnet '10.77.0.0/24'\nconfig server 'keyless'\n\toption ifname 'dawn1'\n")
w := &view.World{M: model.Load(conf, nil), Usage: usage.Empty(), Now: 1_800_000_000}
var did []string
d := Deps{World: func() *view.World { return w }, RunDir: dir, DataDir: dir,
Snapshot: func() *netview.Snapshot { return &netview.Snapshot{Devices: map[string]*netview.Device{}} },
Apply: func(string) map[string]any { did = append(did, "apply"); return map[string]any{"applied": true} },
Sync: func(string) any { did = append(did, "sync"); return "a warning" },
Background: func(s string) { did = append(did, "run "+s) },
Ifup: func(n string) { did = append(did, "ifup "+n) },
Updater: func(lang string, args []string) { did = append(did, "updater "+lang+" "+strings.Join(args, " ")) },
}
for id, want := range map[string]string{"apply": "apply", "sync": "sync", "restart": "run sleep 1; /etc/init.d/dawnline restart",
"netrestart": "run sleep 1; /etc/init.d/network restart", "ifup:dawn0": "ifup dawn0"} {
did = nil
res := ask(t, d, "health_fix", map[string]any{"id": id})
if res["error"] != nil || res["result"] == nil || res["checks"] == nil || res["watchdog"] == nil || len(did) != 1 || did[0] != want {
t.Errorf("%s: did %v, answered error %v result %v", id, did, res["error"], res["result"])
}
}
// never another interface of the router, nor a tunnel that has no keys
for id, want := range map[string]string{"ifup:br-lan": "Unknown fix.", "ifup:lan": "lan is not one of Dawnline's tunnels.", "ifup:dawn1": "dawn1 is not one of Dawnline's tunnels.",
"reboot": "Unknown fix.", "": "Unknown fix."} {
did = nil
if res := ask(t, d, "health_fix", map[string]any{"id": id}); res["error"] != want || len(did) != 0 {
t.Errorf("%q: %v, did %v", id, res["error"], did)
}
}
for what, want := range map[string]string{"": "updater check", "check:app": "updater check app", "app@v0.19.0": "updater install app@v0.19.0",
"amneziawg:slava:ru": "updater install amneziawg:slava:ru", "pkgs:ip-full,kmod-veth": "updater install pkgs:ip-full,kmod-veth"} {
did = nil
if res := ask(t, d, "updates_run", map[string]any{"what": what}); res["started"] != true || res["job"] != nil || len(did) != 1 || did[0] != want {
t.Errorf("%q: %v, did %v", what, res, did)
}
}
for what, want := range map[string]string{"everything": "Unknown update target.", "app; reboot": "Unknown update target.", "pkgs:busybox": "busybox is not a package Dawnline manages",
"pkgs:ip-full,": "Unknown update target."} {
did = nil
if res := ask(t, d, "updates_run", map[string]any{"what": what}); res["error"] != want || len(did) != 0 {
t.Errorf("%q: %v, did %v", what, res["error"], did)
}
}
// the job the updater wrote is answered, never an older one
os.WriteFile(dir+"/update-job.json", []byte(`{"id":"old","started":5,"state":"done"}`), 0o600)
d.Updater = func(string, []string) {
os.WriteFile(dir+"/update-job.json", []byte(fmt.Sprintf(`{"id":"new","started":%d,"state":"running"}`, time.Now().Unix()+1)), 0o600)
}
if res := ask(t, d, "updates_run", map[string]any{"what": "check"}); res["job"] == nil || res["job"].(map[string]any)["id"] != "new" {
t.Fatalf("the new job: %v", res)
}
if res := ask(t, d, "apply", nil); res["applied"] != true {
t.Fatalf("apply: %v", res)
}
}
func TestRestoreAndImport(t *testing.T) {
dir := t.TempDir()
os.MkdirAll(dir+"/data", 0o755)
good := "\nconfig settings 'settings'\n\toption portal_port '4500'\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\n"
os.WriteFile(dir+"/config", []byte(good), 0o600)
os.WriteFile(dir+"/usage.json", []byte(`{"devices":{},"people":{},"then":true}`), 0o600)
os.WriteFile(dir+"/data/events.json", []byte(`[{"ts":1,"type":"connect"}]`), 0o600)
var said []string
applied := 0
d := Deps{World: world, Config: dir + "/config", RunDir: dir, DataDir: dir + "/data", Store: &store.Store{Config: dir + "/config", RunDir: dir},
Apply: func(string) map[string]any { applied++; return map[string]any{"ok": true} },
Log: &servicelog.Log{RunDir: dir, Syslog: func(_, line string) { said = append(said, line) }}}
now := int64(1_800_000_000)
clock = func() int64 { now += 60; return now }
defer func() { clock = func() int64 { return time.Now().Unix() } }()
kept, _ := backupCreate(d, "")
// the settings change, the counters move on, then the backup is brought back
os.WriteFile(dir+"/config", []byte(strings.Replace(good, "'Home'", "'Changed'", 1)), 0o600)
os.WriteFile(dir+"/usage.json", []byte(`{"devices":{},"people":{},"later":true}`), 0o600)
os.WriteFile(dir+"/data/usage.json", []byte(`{"devices":{},"people":{},"later":true}`), 0o600)
if res := ask(t, d, "backup_restore", map[string]any{"name": kept}); res["ok"] != true || applied != 1 {
t.Fatalf("a restore: %v, applied %d", res, applied)
}
if raw, _ := os.ReadFile(dir + "/config"); string(raw) != good {
t.Fatalf("the settings after:\n%s", raw)
}
if raw, _ := os.ReadFile(dir + "/data/usage.json"); !strings.Contains(string(raw), "then") {
t.Fatalf("the counters after: %s", raw)
}
// the watcher is told to load the counters anew, and what was there before the restore is kept
if !exists(dir+"/usage.reload") || exists(dir+"/usage.json") {
t.Fatal("the live counters were not handed back to flash")
}
kinds := map[string]int{}
for _, b := range backups(dir + "/data/backups") {
kinds[b["reason"].(string)]++
}
if kinds["pre-restore"] != 1 || kinds["manual"] != 1 || len(said) != 1 || !strings.Contains(said[0], "admin restored backup "+kept) {
t.Fatalf("what stays %v, what was written down %v", kinds, said)
}
// what is no backup, or holds what a backup may not
archive := func(entries ...[3]string) string {
var buf bytes.Buffer
zw := gzip.NewWriter(&buf)
tw := tar.NewWriter(zw)
for _, e := range entries {
h := &tar.Header{Name: e[0], Mode: 0o600, Size: int64(len(e[1])), Typeflag: tar.TypeReg}
if e[2] == "link" {
h.Typeflag, h.Linkname, h.Size = tar.TypeSymlink, e[1], 0
}
tw.WriteHeader(h)
if e[2] != "link" {
tw.Write([]byte(e[1]))
}
}
tw.Close()
zw.Close()
return base64.StdEncoding.EncodeToString(buf.Bytes())
}
broken := strings.Replace(good, "'10.77.0.0/24'", "'not a network'", 1)
for name, c := range map[string][2]string{
"nothing": {"", "Upload is empty or too large."},
"not an archive": {base64.StdEncoding.EncodeToString([]byte("hello")), "Not a valid backup archive."},
"another file": {archive([3]string{"etc/config/dawnline", good, ""}, [3]string{"etc/passwd", "x", ""}), "Unexpected file in backup: etc/passwd"},
"a link": {archive([3]string{"etc/config/dawnline", "/etc/shadow", "link"}), "Unexpected entry in backup: etc/config/dawnline"},
"no settings": {archive([3]string{"etc/dawnline/usage.json", "{}", ""}), "Backup does not contain /etc/config/dawnline."},
"unreadable": {archive([3]string{"etc/config/dawnline", "config 'unclosed", ""}), "The configuration in this file cannot be read."},
"settings that fail": {archive([3]string{"./etc/config/dawnline", broken, ""}), "This configuration can't be used: Server Home: Subnet must look like 10.77.0.0/24."},
} {
before := applied
if res := ask(t, d, "backup_upload", map[string]any{"data": c[0]}); res["error"] != c[1] || applied != before {
t.Errorf("%s: %v", name, res["error"])
}
}
if raw, _ := os.ReadFile(dir + "/config"); string(raw) != good {
t.Fatal("a refused upload touched the settings")
}
if res := ask(t, d, "backup_upload", map[string]any{"data": archive([3]string{"/etc/config/dawnline", strings.Replace(good, "'Home'", "'Sent'", 1), ""})}); res["ok"] != true {
t.Fatalf("a good upload: %v", res)
}
// an export comes back as the settings it was taken from
out := ask(t, d, "export", nil)
raw, _ := json.Marshal(out)
var sent map[string]any
json.Unmarshal(raw, &sent)
os.WriteFile(dir+"/config", []byte(good), 0o600)
if res := ask(t, d, "import", map[string]any{"data": sent}); res["ok"] != true {
t.Fatalf("an import: %v", res)
}
_, m, _ := d.Store.Read()
if m.Servers["home"].Name != "Sent" || m.Servers["home"].Subnet != "10.77.0.0/24" || m.Settings.PortalPort != 4500 {
t.Fatalf("the settings imported: %+v", m.Servers["home"])
}
for name, c := range map[string]struct {
data any
want string
}{
"not an export": {map[string]any{"format": "other", "sections": []any{}}, "Not a Dawnline export."},
"nothing": {nil, "Not a Dawnline export."},
"another kind": {map[string]any{"format": "dawnline", "sections": []any{map[string]any{".type": "interface", ".name": "lan"}}}, "Invalid section lan."},
"a bad name": {map[string]any{"format": "dawnline", "sections": []any{map[string]any{".type": "person", ".name": "a b"}}}, "Invalid section a b."},
"a bad option": {map[string]any{"format": "dawnline", "sections": []any{map[string]any{".type": "person", ".name": "anna", "Name": "x"}}}, "Invalid option Name."},
"a number": {map[string]any{"format": "dawnline", "sections": []any{map[string]any{".type": "person", ".name": "anna", "limit": 5.0}}}, "Invalid option limit."},
"what is broken": {map[string]any{"format": "dawnline", "sections": []any{map[string]any{".type": "settings", ".name": "settings"}, map[string]any{".type": "person", ".name": "anna", "server": "gone", "name": "Anna"}}}, "This configuration can't be used: Person Anna: server gone does not exist."},
} {
if res := ask(t, d, "import", map[string]any{"data": c.data}); res["error"] != c.want {
t.Errorf("%s: %v", name, res["error"])
}
}
}
// A backup the ucode backend made with its own tar is read by Go's restore, file for file.
func TestUcodesBackupIsRead(t *testing.T) {
raw, err := os.ReadFile("testdata/backup.json")
if err != nil {
t.Fatal(err)
}
var golden struct {
Config string `json:"config"`
Archives []struct{ Name, Data string }
}
if err := json.Unmarshal(raw, &golden); err != nil {
t.Fatal(err)
}
if len(golden.Archives) != 2 {
t.Fatalf("%d archives", len(golden.Archives))
}
for i, a := range golden.Archives {
data, _ := base64.StdEncoding.DecodeString(a.Data)
parts, err := unpack(data)
if err != nil || string(parts["etc/config/dawnline"]) != golden.Config || !backupFile.MatchString(a.Name) {
t.Fatalf("%s: %v", a.Name, err)
}
// by hand: the counters and the log with it; before an update: the settings alone
if want := []int{3, 1}[i]; len(parts) != want {
t.Errorf("%s holds %d files, want %d", a.Name, len(parts), want)
}
}
if !strings.Contains(string(mustUnpack(t, golden.Archives[0].Data)["etc/dawnline/usage.json"]), "by ucode") {
t.Fatal("the counters in it are not the ones that were there")
}
}
func mustUnpack(t *testing.T, b64 string) map[string][]byte {
data, _ := base64.StdEncoding.DecodeString(b64)
parts, err := unpack(data)
if err != nil {
t.Fatal(err)
}
return parts
}
+679
View File
@@ -0,0 +1,679 @@
package methods
import (
crand "crypto/rand"
"errors"
"math"
"regexp"
"strconv"
"strings"
"time"
"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/journal"
"github.com/tickcount/luci-app-dawnline/backend/model"
"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/zone"
)
// personFields are what person_update takes as strings ("" clears an optional one), in the order they are
// applied and told; a name and a move are one person at a time.
var personFields = []string{"name", "access", "device_limit", "expires", "quota_gb", "quota_period", "bonus_gb", "note", "dns_mode", "server"}
// fields is a person's changes from a call's arguments: the known ones that are given, and their names in
// the order ucode keeps them (the fields, then paused, services, circle, home_nets).
func fieldsOf(a map[string]any) (f map[string]any, order []string) {
f = map[string]any{}
take := func(k string) {
f[k] = a[k]
order = append(order, k)
}
for _, k := range personFields {
if a[k] != nil {
take(k)
}
}
if a["paused"] != nil {
take("paused")
}
if _, list := a["services"].([]any); list {
take("services")
}
if a["circle"] != nil {
take("circle")
}
if _, list := a["home_nets"].([]any); list {
take("home_nets")
}
return f, order
}
// number reads a value as ucode's unary plus does: a whole number (decimal or 0x…), or one with a
// fraction or an exponent, blanks around it allowed. ok is false for anything else, and for none.
func number(v any) (float64, bool) {
switch x := v.(type) {
case string:
s := strings.Trim(x, blank)
if s == "" {
return 0, false
}
if n, err := strconv.ParseInt(s, 0, 64); err == nil && !strings.HasPrefix(strings.TrimLeft(s, "+-"), "0b") && !strings.HasPrefix(strings.TrimLeft(s, "+-"), "0o") {
return float64(n), true
}
if strings.ContainsAny(s, "nNiIxXpP_") {
return 0, false
}
n, err := strconv.ParseFloat(s, 64)
return n, err == nil
case nil:
return 0, false
}
return num(v), true
}
// str is a value as ucode writes it into a string: a whole number plainly, another as C's %g does.
func str(n float64) string {
if n == math.Trunc(n) && math.Abs(n) < 1e15 {
return strconv.FormatInt(int64(n), 10)
}
return strconv.FormatFloat(n, 'g', -1, 64)
}
// validDate tells YYYY-MM-DD, and a day that exists (2026-02-30 does not).
func validDate(s string) bool {
t, err := time.ParseInLocation("2006-01-02", s, zone.Local())
return err == nil && len(s) == 10 && t.Format("2006-01-02") == s
}
const ifaceChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.@-"
// allocAddress is the first free address in a server's tunnel network (the router has the first), or "".
func allocAddress(m *model.Model, srv *model.Server, serverOf func(*model.Person) string, addressOf func(*model.Device) string) string {
t, ok := ipnet.Subnet(srv.Subnet)
if !ok {
return ""
}
used := map[uint32]bool{t.First(): true}
for _, id := range m.DeviceIDs() {
d := m.Devices[id]
if p := m.People[d.Person]; p != nil && serverOf(p) == srv.ID {
if n, ok := ipnet.IP(addressOf(d)); ok {
used[n] = true
}
}
}
last := uint32(uint64(t.Base) + t.Size() - 2)
for n := t.First() + 1; n <= last; n++ {
if !used[n] {
return ipnet.String(n)
}
}
return ""
}
// updatePerson changes a person in the settings: only the fields given, "" clearing an optional one
// (dawnline.core update_person). A value that is refused leaves everything as it was (the caller's
// transaction writes nothing). t words the one refusal that counts devices.
func updatePerson(c *uci.Config, m *model.Model, id string, f map[string]any, records *usage.Usage, t *i18n.Catalog) error {
p := m.People[id]
if p == nil {
return errors.New("Unknown person.")
}
set := func(k, v string) {
if v == "" {
c.Delete(id, k)
} else {
c.Set(id, k, v)
}
}
given := func(k string) (string, bool) {
v, has := f[k]
if !has || v == nil {
return "", false
}
if s, isString := v.(string); isString {
return s, true
}
return str(num(v)), true
}
if v, ok := given("name"); ok {
if !ValidName(v) {
return errors.New(`Enter a name: up to 48 characters, without < > " or \.`)
}
set("name", strings.Trim(v, blank))
}
if v, ok := given("access"); ok {
if v != "lan" && v != "internet" && v != "both" {
return errors.New("Access must be lan, internet or both.")
}
set("access", v)
}
if _, ok := given("device_limit"); ok {
x, _ := number(f["device_limit"])
n := int(x)
if n < 1 || n > 32 {
return errors.New("Device limit must be between 1 and 32.")
}
if have := len(p.Devices); n < have {
return said{t.N(int64(have), "%s already has %d device. Remove one first to lower the limit to %d.",
"%s already has %d devices. Remove one first to lower the limit to %d."), []any{p.Name, have, n}}
}
set("device_limit", strconv.Itoa(n))
}
if v, ok := given("expires"); ok {
if v != "" && !validDate(v) {
return errors.New("End date must be YYYY-MM-DD.")
}
set("expires", v)
}
if v, ok := given("dns_mode"); ok {
if v != "device" && v != "home" {
return errors.New("Unknown DNS choice.")
}
if v == "device" {
v = ""
}
set("dns_mode", v)
}
if v, ok := given("quota_gb"); ok {
q, isNumber := number(f["quota_gb"])
if !isNumber {
q = -1
}
if v != "" && (q < 0 || q > 100000) {
return errors.New("Data limit must be a positive number of GB.")
}
if q > 0 {
set("quota_gb", str(q))
} else {
set("quota_gb", "")
}
}
if v, ok := given("quota_period"); ok {
if v != "total" {
v = ""
}
set("quota_period", v)
}
if _, ok := given("bonus_gb"); ok {
b, isNumber := number(f["bonus_gb"])
if !isNumber || b < 0 || b > 100000 {
return errors.New("Extra data must be a positive number of GB.")
}
if b > 0 {
set("bonus_gb", str(b))
// it lasts until their limit month ends (none running: a month from now)
now := time.Now().Unix()
from := now
if records != nil {
if lm := usage.LimitMonth(records.People[id], now); lm != nil {
from = int64(lm.Start)
}
}
set("bonus_until", strconv.FormatInt(usage.MonthAfter(from), 10))
} else {
set("bonus_gb", "")
set("bonus_until", "")
}
set("bonus_month", "")
}
if v, ok := given("note"); ok {
note := []rune(strings.Trim(v, blank))
if len(note) > 500 {
note = note[:500]
}
set("note", string(note))
}
if list, ok := f["services"].([]any); ok {
var ids []string
for _, s := range list {
sid := text(s)
if m.Services[sid] == nil {
return said{"Unknown service %s.", []any{sid}}
}
ids = append(ids, sid)
}
if len(ids) > 0 {
c.SetList(id, "service", ids)
} else {
c.Delete(id, "service")
}
}
if v, ok := given("circle"); ok {
if ci := m.Circles[v]; v != "" && (ci == nil || ci.Server != p.Server) {
return errors.New("Unknown circle.")
}
set("circle", v)
}
// another server: every device gets an address in its tunnel (so each needs its configuration again);
// circles and home networks are the old server's and go
if v, ok := given("server"); ok && v != "" && v != p.Server {
srv := m.Servers[v]
if srv == nil {
return errors.New("Unknown server.")
}
set("server", v)
moved := map[string]string{}
serverOf := func(q *model.Person) string {
if q.ID == id {
return v
}
return q.Server
}
addressOf := func(d *model.Device) string {
if a, has := moved[d.ID]; has {
return a
}
return d.Address
}
for _, did := range p.Devices {
addr := allocAddress(m, srv, serverOf, addressOf)
if addr == "" {
return said{"No free address left on %s.", []any{srv.Name}}
}
moved[did] = addr
c.Set(did, "address", addr)
}
c.Delete(id, "circle")
c.Delete(id, "home_net")
}
if list, ok := f["home_nets"].([]any); ok {
var nets []string
seen := map[string]bool{}
for _, x := range list {
n := trimmed(x)
if n == "" || len(n) > 32 || strings.Trim(n, ifaceChars) != "" || seen[n] {
continue
}
seen[n] = true
nets = append(nets, n)
}
if len(nets) > 0 {
c.SetList(id, "home_net", nets)
} else {
c.Delete(id, "home_net")
}
}
if v, has := f["paused"]; has && v != nil {
if truthy(v) {
set("paused", "1")
} else {
set("paused", "")
}
}
return nil
}
// records is the watcher's counters, or nil before it has looked.
func records(d Deps) *usage.Usage {
if w := d.World(); w != nil {
return w.Usage
}
return nil
}
// merged puts an event in the activity log, folded into the last one of the same kind and person when that
// is no older than two minutes, and in syslog (dawnline.live add_event_merged).
func merged(d Deps, e journal.Event) {
_, m, err := d.Store.Read()
if err != nil {
return
}
names := journal.NamesOf(m)
if lvl := journal.Level(e); lvl != "" && d.Log != nil {
line := journal.Event{}
for k, v := range e {
line[k] = v
}
kind, _ := e["type"].(string)
d.Log.Say(lvl, journal.CategoryOf(kind), journal.Line(journal.Named(line, names)))
}
journal.Log{Run: d.RunDir, Data: d.DataDir}.AddMerged(e, 120, journal.PolicyOf(m.Settings.Retention), names, time.Now().Unix())
}
// personUpdate is an immediate edit from a person's page; it returns the person as the page shows them.
func personUpdate(d Deps, a map[string]any) (map[string]any, error) {
id := text(a["id"])
f, order := fieldsOf(a)
t := i18n.Use(d.Catalogs, text(a["lang"]))
var was any
var before *model.Model
warning, err := change(d, text(a["lang"]), func(c *uci.Config, m *model.Model) error {
before = m
if p := m.People[id]; p != nil {
was = p.Access
}
return updatePerson(c, m, id, f, records(d), t)
})
if err != nil {
return nil, err
}
// "moved to Test" says more than "server"
var changes []string
for _, k := range order {
switch {
case k == "note":
case k == "server" && before.Servers[text(f["server"])] != nil:
changes = append(changes, "moved to "+before.Servers[text(f["server"])].Name)
default:
changes = append(changes, k)
}
}
if len(changes) > 0 {
merged(d, journal.Event{"type": "person_changed", "person": id, "detail": strings.Join(changes, ", "), "by": "admin"})
}
if d.Told != nil {
d.Told()
}
var person any
if w := d.World(); w != nil && w.M != nil && w.M.People[id] != nil {
person = w.PersonOf(w.M.People[id], true)
}
return map[string]any{"person": person, "was": was, "warning": warning}, nil
}
// bulkMax is how many people one people_update changes at most.
const bulkMax = 500
// peopleUpdate changes several people at once (People's picks: extend, pause, resume, and their undo):
// `list` is [{ id, patch }] with person_update's fields. All or nothing: one transaction, one apply.
func peopleUpdate(d Deps, a map[string]any) (map[string]any, error) {
list, _ := a["list"].([]any)
if len(list) == 0 {
return nil, errors.New("Nothing to change.")
}
if len(list) > bulkMax {
return nil, said{"At most %d people at once.", []any{bulkMax}}
}
type one struct {
id string
f map[string]any
order []string
}
var todo []one
seen := map[string]bool{}
for _, x := range list {
o, _ := x.(map[string]any)
id, isString := o["id"].(string)
if !isString || id == "" || len(id) > 64 {
return nil, errors.New("Each change needs the id of a person.")
}
if seen[id] {
return nil, said{"%s is in the list twice.", []any{id}}
}
seen[id] = true
patch, _ := o["patch"].(map[string]any)
if len(patch) == 0 {
return nil, said{"Nothing to change for %s.", []any{id}}
}
for k, v := range patch {
if k == "paused" {
if _, isBool := v.(bool); !isBool {
return nil, errors.New("Paused must be true or false.")
}
continue
}
known := false
for _, pf := range personFields {
known = known || pf == k
}
if !known || k == "name" || k == "server" {
return nil, said{"%s cannot be changed for several people at once.", []any{k}}
}
switch v.(type) {
case string, int, int32, int64, float64:
default:
return nil, said{"%s must be a value, not a list.", []any{k}}
}
}
f, order := fieldsOf(patch)
todo = append(todo, one{id, f, order})
}
t := i18n.Use(d.Catalogs, text(a["lang"]))
was := map[string]bool{}
warning, err := change(d, text(a["lang"]), func(c *uci.Config, m *model.Model) error {
for _, x := range todo {
if p := m.People[x.id]; p != nil {
was[x.id] = p.Paused
}
if err := updatePerson(c, m, x.id, x.f, records(d), t); err != nil {
return err
}
}
return nil
})
if err != nil {
return nil, err
}
for _, x := range todo {
if v, has := x.f["paused"]; has && truthy(v) != was[x.id] {
kind := "resumed"
if truthy(v) {
kind = "paused"
}
logged(d, journal.Event{"type": kind, "person": x.id, "by": "admin"})
}
var changes []string
for _, k := range x.order {
if k != "note" && k != "paused" {
changes = append(changes, k)
}
}
if len(changes) > 0 {
merged(d, journal.Event{"type": "person_changed", "person": x.id, "detail": strings.Join(changes, ", "), "by": "admin"})
}
}
if d.Told != nil {
d.Told()
}
return map[string]any{"changed": len(todo), "warning": warning}, nil
}
var _ = view.Person{}
// clock is the moment a change is made at. A test puts its own in its place.
var clock = func() int64 { return time.Now().Unix() }
// NewCode draws a personal code (for the steps that replace older ones).
func NewCode() string { return newCode() }
// newCode is a personal code: three pronounceable words of three syllables each (kamiro-telosu-bavine),
// easy to read out or type; the code is the whole link path. A test puts its own in its place.
var newCode = func() string {
const consonants, vowels = "bdfgklmnprstvz", "aeiou"
pick := func(alphabet string) byte {
n := len(alphabet)
lim := 256 - 256%n
for {
var b [1]byte
crand.Read(b[:])
if int(b[0]) < lim {
return alphabet[int(b[0])%n]
}
}
}
for {
words := make([]string, 3)
for w := range words {
var s []byte
for i := 0; i < 3; i++ {
s = append(s, pick(consonants), pick(vowels))
}
words[w] = string(s)
}
code := strings.Join(words, "-")
bad := false
for _, x := range []string{"suka", "pido", "muda", "zalup", "dura", "gavn"} {
bad = bad || strings.Contains(code, x)
}
if !bad {
return code
}
}
}
var (
notLowerAlnum = regexp.MustCompile(`[^a-z0-9]+`)
personID = regexp.MustCompile(`^[a-z0-9_]{1,32}$`)
)
// slugify turns a display name into an id to start from ("Мама Таня" → "mama-tanya"), "user" for nothing.
func slugify(s string) string {
out := notLowerAlnum.ReplaceAllString(lower(handout.Translit(strings.Trim(s, blank))), "-")
out = strings.Trim(out, "-")
if len(out) > 32 {
out = out[:32]
}
if out == "" {
return "user"
}
return out
}
// link is a person's main link, or nil when the portal would not answer it.
func link(m *model.Model, id string) any {
p := m.People[id]
hosts := view.PortalHosts(m)
if p == nil || len(hosts) == 0 {
return nil
}
return orNull(view.PortalLink(m, p, hosts[0]))
}
// personCreate adds a person (dawnline.core create_person): an id from their name, a personal code.
func personCreate(d Deps, a map[string]any) (map[string]any, error) {
name := trimmed(a["name"])
id := ""
err := write(d, func(c *uci.Config, m *model.Model) error {
if !ValidName(name) {
return errors.New(`Enter a name: up to 48 characters, without < > " or \.`)
}
// the id is a section's name: letters, digits and underscores only; repeated names get _2, _3, …
base := trimmed(a["slug"])
if base == "" {
base = slugify(name)
}
base = strings.ReplaceAll(base, "-", "_")
if !personID.MatchString(base) {
return errors.New("The id may use lowercase letters, digits and underscores.")
}
id = base
for n := 2; taken(m, id); n++ {
if n > 99 {
return said{`"%s" is already taken.`, []any{base}}
}
id = base[:min(len(base), 28)] + "_" + strconv.Itoa(n)
}
server := trimmed(a["server"])
if ids := m.ServerIDs(); server == "" && len(ids) > 0 {
server = ids[0]
}
if m.Servers[server] == nil {
return errors.New("Set up a server first.")
}
access := "internet"
if a["access"] != nil {
access = text(a["access"])
}
if access != "lan" && access != "internet" && access != "both" {
return errors.New("Access must be lan, internet or both.")
}
limit := m.Settings.DefaultDeviceLimit
if v := trimmed(a["device_limit"]); v != "" {
if x, ok := number(v); ok {
limit = int(x)
}
}
if limit < 1 || limit > 32 {
return errors.New("Device limit must be between 1 and 32.")
}
expires := trimmed(a["expires"])
if expires != "" && !validDate(expires) {
return errors.New("End date must be YYYY-MM-DD.")
}
quota, _ := number(trimmed(a["quota_gb"]))
if quota < 0 || quota > 100000 {
return errors.New("Monthly data must be a positive number of GB.")
}
now := strconv.FormatInt(clock(), 10)
c.Add("person", id)
c.Set(id, "server", server)
c.Set(id, "name", name)
c.Set(id, "access", access)
c.Set(id, "device_limit", strconv.Itoa(limit))
c.Set(id, "code", newCode())
c.Set(id, "code_set", now)
c.Set(id, "created", now)
if expires != "" {
c.Set(id, "expires", expires)
}
if quota > 0 {
c.Set(id, "quota_gb", str(quota))
period := "month"
if text(a["quota_period"]) == "total" {
period = "total"
}
c.Set(id, "quota_period", period)
}
return nil
})
if err != nil {
return nil, err
}
logged(d, journal.Event{"type": "person_added", "person": id, "detail": name, "by": "admin"})
if d.Told != nil {
d.Told()
}
_, m, _ := d.Store.Read()
return map[string]any{"id": id, "link": link(m, id)}, nil
}
// personRegen gives a person a new personal link: the old one stops working at once. What it already
// handed out stays unless asked: `remove` lists the person's devices to take away with it.
func personRegen(d Deps, a map[string]any) (map[string]any, error) {
id := text(a["id"])
var gone []*model.Device
err := write(d, func(c *uci.Config, m *model.Model) error {
if m.People[id] == nil {
return errors.New("Unknown person.")
}
c.Set(id, "code", newCode())
c.Set(id, "code_set", strconv.FormatInt(clock(), 10))
list, _ := a["remove"].([]any)
for _, x := range list {
if dev := m.Devices[text(x)]; dev != nil && dev.Person == id {
already := false
for _, g := range gone {
already = already || g.ID == dev.ID
}
if !already {
gone = append(gone, dev)
c.DeleteSection(dev.ID)
}
}
}
return nil
})
if err != nil {
return nil, err
}
logged(d, journal.Event{"type": "link_changed", "person": id, "by": "admin"})
var warning any
if len(gone) > 0 {
for _, dev := range gone {
logged(d, journal.Event{"type": "device_removed", "person": id, "device": dev.ID, "detail": dev.Name, "dname": dev.Name, "dkind": dev.Kind, "by": "admin"})
}
if d.Sync != nil {
warning = d.Sync(text(a["lang"]))
}
}
if d.Told != nil {
d.Told()
}
_, m, _ := d.Store.Read()
return map[string]any{"link": link(m, id), "removed": len(gone), "warning": warning}, nil
}
+752
View File
@@ -0,0 +1,752 @@
package methods
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/netip"
"os"
"regexp"
"sort"
"strconv"
"strings"
"time"
backend "github.com/tickcount/luci-app-dawnline/backend"
"github.com/tickcount/luci-app-dawnline/backend/access"
"github.com/tickcount/luci-app-dawnline/backend/endpoint"
"github.com/tickcount/luci-app-dawnline/backend/files"
"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/journal"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/state"
"github.com/tickcount/luci-app-dawnline/backend/view"
"github.com/tickcount/luci-app-dawnline/backend/zone"
)
// The personal portal's answers (dawnline.portal):
//
// GET /<code> the app shell
// GET /<code>/assets/<file> its script and styles (gzipped when the browser takes it)
// GET /<code>/api/state
// POST /<code>/api/devices { name, kind, public_key?, lan_bypass?, networks? }
// GET /<code>/api/devices/<id>/config
// POST /<code>/api/devices/<id> { action: rename|reconnect|bypass|networks|delete, name?, kind?, lan_bypass?, on?, networks?, cleared? }
//
// The personal link is the key: every request carries its code, there are no sessions. Without a valid
// code every path answers the same bare 404, so nothing tells a scanner there is a portal here. Wrong
// codes are counted per client (a blocked client gets the same 404 for a wrong code; a valid link always
// opens): per IPv4 address or IPv6 /64, behind Cloudflare per visitor, with a higher ceiling for all
// Cloudflare visitors together. POST bodies must be JSON from the same origin. The portal can only manage
// the person's own devices within their limits, and not those the admin turned off (Actor). Changes
// through a link are limited too, and the configurations themselves (keys, the disguise) only go over
// HTTPS: a plain-HTTP link shows the page, but gets them from its HTTPS twin.
const (
portalFailLimit = 8
portalFailWindow = 900
portalSharedLimit = portalFailLimit * 25
portalSharedKey = "cloudflare"
portalRateMax = 2000
portalActionLimit = 40
portalBodyMax = 8192
)
// PortalAssets is where the portal's script and styles are kept (outside any web root).
var PortalAssets = "/usr/share/dawnline/portal/assets"
// PortalRequest is one request to the portal.
type PortalRequest struct {
Method, Path, Host string
HTTPS bool
Remote, CFConnectingIP string
Origin, ContentType string
AcceptLanguage, AcceptEncoding string
ContentLength int64
Body io.Reader
// Live: the page has a stream to follow (the shell tells it).
Live bool
}
// PortalAnswer is what it gets.
type PortalAnswer struct {
Status int
Header [][2]string
Body []byte
}
type portalDone struct{ a PortalAnswer }
type portalCall struct {
d Deps
q PortalRequest
t *i18n.Catalog
w *view.World
person *model.Person
base string
buckets []portalBucket
}
type portalBucket struct {
key string
limit int
}
var (
wordCode = regexp.MustCompile(`^[a-z]{6}-[a-z]{6}-[a-z]{6}$`)
assetName = regexp.MustCompile(`^[a-z0-9-]+\.(js|css|png)$`)
takesGzip = regexp.MustCompile(`(^|[ ,])gzip([ ;,]|$)`)
firstLang = regexp.MustCompile(`^[ \t]*([A-Za-z][A-Za-z][A-Za-z]?)`)
portOfHost = regexp.MustCompile(`:([0-9]+)$`)
jsonType = regexp.MustCompile(`^application/json`)
bareFile = regexp.MustCompile(`^/(luci-static|cgi-bin|ubus)(/|$)`)
assetTypes = map[string]string{"js": "text/javascript", "css": "text/css", "png": "image/png"}
)
func (c *portalCall) answer(status int, extra [][2]string, body []byte) {
h := [][2]string{{"X-Content-Type-Options", "nosniff"}, {"Referrer-Policy", "no-referrer"}, {"X-Frame-Options", "DENY"}, {"Cache-Control", "no-store"}}
for _, kv := range extra {
replaced := false
for i := range h {
if h[i][0] == kv[0] {
h[i][1], replaced = kv[1], true
}
}
if !replaced {
h = append(h, kv)
}
}
// HSTS pins the whole name: only when the visitor is on the standard port
port := "443"
if m := portOfHost.FindStringSubmatch(c.q.Host); m != nil {
port = m[1]
}
if c.q.HTTPS && port == "443" {
h = append(h, [2]string{"Strict-Transport-Security", "max-age=31536000"})
}
panic(portalDone{PortalAnswer{Status: status, Header: h, Body: body}})
}
func (c *portalCall) json(status int, v any) {
raw, _ := json.Marshal(v)
c.answer(status, [][2]string{{"Content-Type", "application/json"}}, raw)
}
func (c *portalCall) fail(status int, msg string) {
c.json(status, map[string]any{"error": msg})
}
// refuse is fail for what a change said: its words in the visitor's language.
func (c *portalCall) refuse(status int, err error) {
switch s := err.(type) {
case access.Said:
c.fail(status, fmt.Sprintf(c.t.T(s.Msg), s.Args...))
case said:
c.fail(status, fmt.Sprintf(c.t.T(s.msg), s.args...))
default:
c.fail(status, wordsOf(c.t, err, nil))
}
}
// notFound is the one answer for anything without a valid code: a bare 404, or the redirect from
// Settings. Files always get the 404: a missing asset must not turn into someone's website.
func (c *portalCall) notFound() {
parts := strings.Split(c.q.Path, "/")
file := bareFile.MatchString(c.q.Path) || strings.Contains(parts[len(parts)-1], ".")
if to := c.w.M.Settings.PortalRedirect; to != "" && !file {
c.answer(302, [][2]string{{"Location", to}, {"Cache-Control", "no-store"}, {"Content-Type", "text/plain"}}, []byte("Moved\n"))
}
c.answer(404, [][2]string{{"Content-Type", "text/plain"}}, []byte("Not found\n"))
}
/* ---------- rate limiting ---------- */
// addrKey is the rate-limit key of an address: an IPv4 address, or the /64 of an IPv6 one ("": neither).
func addrKey(a string) string {
ip, err := netip.ParseAddr(a)
if err != nil {
return ""
}
if ip.Is4() {
return ip.String()
}
// an IPv4 address seen through an IPv6 socket
if ip.Is4In6() {
return ip.Unmap().String()
}
b := ip.As16()
return fmt.Sprintf("%x:%x:%x:%x::/64", uint16(b[0])<<8|uint16(b[1]), uint16(b[2])<<8|uint16(b[3]), uint16(b[4])<<8|uint16(b[5]), uint16(b[6])<<8|uint16(b[7]))
}
// portalBuckets is what a request counts against: its client, and behind Cloudflare (the only proxy whose
// CF-Connecting-IP is believed) also the bucket all Cloudflare visitors share.
func portalBuckets(q PortalRequest) []portalBucket {
if !endpoint.Cloudflare(q.Remote) {
key := addrKey(q.Remote)
if key == "" {
key = q.Remote
}
return []portalBucket{{key, portalFailLimit}}
}
out := []portalBucket{{portalSharedKey, portalSharedLimit}}
if v := addrKey(strings.TrimSpace(q.CFConnectingIP)); v != "" && !endpoint.Cloudflare(v) {
out = append([]portalBucket{{v, portalFailLimit}}, out...)
}
return out
}
type portalRate struct {
First int64 `json:"first"`
Count int `json:"count"`
}
func (c *portalCall) rates() map[string]*portalRate {
st := map[string]*portalRate{}
files.ReadJSON(c.d.RunDir+"/portal-rate.json", &st)
now := time.Now().Unix()
for k, v := range st {
if v == nil || now-v.First > portalFailWindow {
delete(st, k)
}
}
return st
}
func (c *portalCall) blocked() bool {
st := c.rates()
for _, b := range c.buckets {
if v := st[b.key]; v != nil && v.Count >= b.limit {
return true
}
}
return false
}
// wrongCode counts a well-formed guess; a bucket that just filled is told in the activity log.
func (c *portalCall) wrongCode() {
var hit []string
func() {
defer files.Lock(c.d.RunDir, "portal-rate")()
st := c.rates()
now := time.Now().Unix()
for _, b := range c.buckets {
v := st[b.key]
if v == nil {
v = &portalRate{First: now}
st[b.key] = v
}
if v.Count++; v.Count == b.limit {
hit = append(hit, b.key)
}
}
// bounded: an attacker with many addresses cannot grow the file without end
if over := len(st) - portalRateMax; over > 0 {
keys := make([]string, 0, len(st))
for k := range st {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool { return st[keys[i]].First < st[keys[j]].First })
for _, k := range keys[:over] {
delete(st, k)
}
}
os.MkdirAll(c.d.RunDir, 0o755)
files.WriteJSON(c.d.RunDir+"/portal-rate.json", st, 0o600)
}()
for _, k := range hit {
if k == portalSharedKey {
k = "all Cloudflare visitors"
}
logged(c.d, journal.Event{"type": "portal_blocked", "detail": k})
}
}
// action counts one more change through this link; refused beyond the limit a window.
func (c *portalCall) action() {
over := false
func() {
defer files.Lock(c.d.RunDir, "portal-rate")()
st := c.rates()
key := "act:" + c.person.ID
v := st[key]
if v == nil {
v = &portalRate{First: time.Now().Unix()}
st[key] = v
}
v.Count++
over = v.Count > portalActionLimit
os.MkdirAll(c.d.RunDir, 0o755)
files.WriteJSON(c.d.RunDir+"/portal-rate.json", st, 0o600)
}()
if over {
c.fail(429, c.t.T("Too many changes in a short time. Try again in a few minutes."))
}
}
/* ---------- pages ---------- */
var htmlEscape = strings.NewReplacer("&", "&amp;", "<", "&lt;", ">", "&gt;", `"`, "&quot;", "'", "&#39;")
// shell is the app shell; it reads the code from its own URL and sends it along.
func (c *portalCall) shell() {
// the build's time joins the version: every update gives the assets new URLs, so they are kept for good
var built int64
if st, err := os.Stat(PortalAssets + "/portal.js"); err == nil {
built = st.ModTime().Unix()
}
v := fmt.Sprintf("?v=%s.%d", backend.Version, built)
title := "Dawnline"
if srv := c.w.M.Servers[c.person.Server]; srv != nil && srv.Name != "" {
title = srv.Name
}
// the icon goes in the page: a request of its own is one more round trip
favicon := ""
if icon, err := os.ReadFile(PortalAssets + "/icon.svg"); err == nil && len(icon) > 0 {
favicon = `<link rel="icon" href="data:image/svg+xml;base64,` + base64.StdEncoding.EncodeToString(icon) + `" type="image/svg+xml">` + "\n"
}
live := ""
if c.q.Live {
live = ` data-live="1"`
}
page := `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover">
<meta name="color-scheme" content="light dark">
<meta name="robots" content="noindex, nofollow">
<title>` + htmlEscape.Replace(title) + `</title>
` + favicon + `<link rel="stylesheet" href="` + c.base + `/assets/portal.css` + v + `">
</head>
<body>
<div id="app" data-base="` + htmlEscape.Replace(c.base) + `" data-title="` + htmlEscape.Replace(title) + `"` + live + `></div>
<script src="` + c.base + `/assets/portal.js` + v + `" defer></script>
</body>
</html>
`
c.answer(200, [][2]string{{"Content-Type", "text/html; charset=utf-8"},
{"Content-Security-Policy", "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data: blob:; " +
"connect-src 'self'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'"}}, []byte(page))
}
// asset is the script and the styles; the shell's ?v= changes with every build, so they never go stale.
func (c *portalCall) asset(name string) {
m := assetName.FindStringSubmatch(name)
if m == nil {
c.notFound()
}
h := [][2]string{{"Content-Type", assetTypes[m[1]]}, {"Cache-Control", "private, max-age=31536000, immutable"}, {"Vary", "Accept-Encoding"}}
// the build keeps a gzipped copy beside each: a third of the bytes for a browser that takes it
if takesGzip.MatchString(strings.ToLower(c.q.AcceptEncoding)) {
if gz, err := os.ReadFile(PortalAssets + "/" + name + ".gz"); err == nil {
c.answer(200, append(h, [2]string{"Content-Encoding", "gzip"}), gz)
}
}
data, err := os.ReadFile(PortalAssets + "/" + name)
if err != nil {
c.notFound()
}
c.answer(200, h, data)
}
/* ---------- the API ---------- */
// reload is the settings again after a change.
func (c *portalCall) reload() {
c.w = c.d.World()
if c.w == nil {
c.fail(503, c.t.T("The router is still starting. Try again in a moment."))
}
if c.person = c.w.M.People[c.person.ID]; c.person == nil {
c.notFound()
}
}
// personState is the person's state for the page: a stop by the admin (pause, end of access) wins over
// their server being off, so they learn that their access is stopped.
func (c *portalCall) personState() string {
st := state.Of(c.w.M, c.person, c.w.Usage, c.w.Now)
if st == "server_off" {
if own := state.Own(c.person, c.w.Usage, c.w.Now); own == "paused" || own == "expired" {
return own
}
}
return st
}
// needOpen refuses a change (or a configuration) while the person is stopped.
func (c *portalCall) needOpen() {
switch c.personState() {
case "paused":
c.fail(403, c.t.T("Your access is paused."))
case "expired":
c.fail(403, c.t.T("Your access has ended."))
}
}
// httpsURL is the same link over HTTPS (for a plain-HTTP visitor), or "" when this is it.
func (c *portalCall) httpsURL() string {
if c.q.HTTPS {
return ""
}
h := strings.SplitN(c.q.Host, ":", 2)[0]
if strings.HasPrefix(c.q.Host, "[") {
if i := strings.Index(c.q.Host, "]"); i > 0 {
h = c.q.Host[:i+1]
}
}
port := ""
if p := c.w.M.Settings.PortalPort; p != 443 {
port = ":" + strconv.Itoa(p)
}
return "https://" + h + port + c.base
}
// needHTTPS: keys and the disguise never travel in the clear; the answer names the HTTPS link to use.
func (c *portalCall) needHTTPS() {
if u := c.httpsURL(); u != "" {
c.json(403, map[string]any{"error": c.t.T("Open this link over HTTPS to get the connection settings."), "https_url": u})
}
}
func (c *portalCall) notReady() {
st := map[string]any{}
if c.d.AWG != nil {
st = c.d.AWG()
}
if c.d.AWG != nil && !(st["tools"] == true && st["kmod"] == true) {
c.fail(503, c.t.T("Adding devices isn't available right now: the VPN server isn't ready. Ask whoever sent you this link."))
}
}
// PortalState is everything a person's page shows, for a visitor at `peer` (which of the person's
// devices the page is open on), over HTTPS or not, at `host`.
func PortalState(w *view.World, d Deps, person *model.Person, peer, host string, https bool) map[string]any {
c := &portalCall{d: d, w: w, person: person, base: "/" + person.Code, q: PortalRequest{Remote: peer, Host: host, HTTPS: https}}
return c.state()
}
func (c *portalCall) state() map[string]any {
m, p := c.w.M, c.person
v := c.w.PersonOf(p, false)
raw, _ := json.Marshal(v)
var pv map[string]any
json.Unmarshal(raw, &pv)
st := c.personState()
pv["state"] = st
// day by day this month, for the page's chart
pv["month_days"] = view.Spark(c.w.Usage.People[p.ID], 30, c.w.Now)
// the device this page was opened from, through the tunnel or beside it
var here any
peer := strings.TrimPrefix(c.q.Remote, "::ffff:")
if peer != "" {
var near []string
for _, dv := range v.Devices {
if dv.Address == peer {
here, near = dv.ID, nil
break
}
if dv.Online && dv.Endpoint == peer {
near = append(near, dv.ID)
}
}
if here == nil && len(near) == 1 {
here = near[0]
}
}
server := map[string]any{"name": nil, "endpoint": "", "device_mesh": false, "awg_disguise": false, "preset": "off", "home": []string{},
"routers": "off", "nets": "ask"}
kinds := deviceKinds
if srv := m.Servers[p.Server]; srv != nil {
// what the page offers about routers: a router among the kinds, and whether it and its networks are asked
server["routers"], server["nets"] = srv.PortalRouters, srv.PortalNets
if srv.PortalRouters == "off" {
kinds = without(deviceKinds, []string{"router"})
}
neutral := handout.ObfNeutral(srv.Obf)
server["name"], server["endpoint"], server["device_mesh"], server["awg_disguise"] = srv.Name, m.EndpointHost(srv), srv.DeviceMesh, !neutral
// which apps (and versions) connect: off, balanced (2.0), max (3.1); legacy = the old junk-only presets
switch {
case neutral:
case srv.Preset == "balanced" || srv.Preset == "max":
server["preset"] = srv.Preset
default:
server["preset"] = "legacy"
}
// the home networks a device with LAN Bypass still sends into the tunnel
if p.Access == "both" && c.d.Home != nil {
if home := c.d.Home(srv, p); home != nil {
server["home"] = home
}
}
}
now := time.Unix(c.w.Now, 0).In(zone.Local())
var https any
if u := c.httpsURL(); u != "" {
https = u
}
return map[string]any{
"person": pv, "here": here, "server": server,
"can_add": len(p.Devices) < p.DeviceLimit, "locked": st == "paused" || st == "expired",
// over plain HTTP: where the configurations are (keys only travel encrypted)
"https_url": https, "kinds": kinds,
// the router's day and clock: access ends by them, not the browser's
"today": now.Format("2006-01-02"), "now": c.w.Now, "version": backend.Version,
}
}
// body is the JSON object a POST carries ({} without a body).
func (c *portalCall) body() map[string]any {
if c.q.ContentLength <= 0 || c.q.Body == nil {
return map[string]any{}
}
if c.q.ContentLength > portalBodyMax {
c.fail(413, c.t.T("Request too large."))
}
raw, _ := io.ReadAll(io.LimitReader(c.q.Body, c.q.ContentLength))
var v any
if json.Unmarshal(raw, &v) != nil {
c.fail(400, c.t.T("Invalid JSON."))
}
if obj, is := v.(map[string]any); is {
return obj
}
return map[string]any{}
}
// withIP is a configuration as the portal sends it: with the server's address as an IP too, for an app
// that takes no name for AmneziaWG.
func (c *portalCall) withIP(cfg map[string]any) map[string]any {
if am, ok := cfg["amnezia"].(map[string]any); ok && c.d.HostIP != nil {
am["host_ip"] = nil
if ip := c.d.HostIP(text(am["host"])); ip != "" {
am["host_ip"] = ip
}
}
return cfg
}
func (c *portalCall) ownDevice(id string) *model.Device {
dev := c.w.M.Devices[id]
if dev == nil || dev.Person != c.person.ID {
c.fail(404, c.t.T("Device not found."))
}
return dev
}
func (c *portalCall) api(method string, segs []string) {
lang := ""
if m := firstLang.FindStringSubmatch(c.q.AcceptLanguage); m != nil {
lang = m[1]
}
who := Actor{By: "portal", Person: c.person.ID}
switch {
case method == "GET" && len(segs) == 1 && segs[0] == "state":
c.json(200, c.state())
case method == "POST" && len(segs) == 1 && segs[0] == "devices":
c.needOpen()
c.needHTTPS()
b := c.body()
c.notReady()
c.action()
who.PublicKey = text(b["public_key"])
res, err := DeviceCreateAs(c.d, map[string]any{"person": c.person.ID, "name": b["name"], "kind": text(b["kind"]), "lan_bypass": b["lan_bypass"] == true,
"networks": b["networks"], "lang": lang}, who)
if err != nil {
c.refuse(400, err)
}
c.reload()
// a router asked of the admin has no configuration yet: the page says it waits
if res["waiting"] == true {
c.json(200, map[string]any{"id": res["id"], "waiting": true, "state": c.state()})
}
cfg, _ := res["config"].(map[string]any)
c.json(200, map[string]any{"id": res["id"], "config": c.withIP(cfg), "state": c.state()})
case method == "GET" && len(segs) == 3 && segs[0] == "devices" && segs[2] == "config":
dev := c.ownDevice(segs[1])
c.needOpen()
c.needHTTPS()
if dev.Waiting {
c.fail(409, c.t.T("This router waits for an answer to its request: it has no configuration yet."))
}
markIssued(c.d, c.w.M, dev.ID)
c.json(200, c.withIP(configOf(c.d, c.w.M, dev.ID)))
case method == "POST" && len(segs) == 2 && segs[0] == "devices":
dev := c.ownDevice(segs[1])
c.needOpen()
b := c.body()
out := map[string]any{"ok": true}
var err error
switch b["action"] {
case "rename":
c.action()
_, err = DeviceUpdateAs(c.d, map[string]any{"id": dev.ID, "name": b["name"], "lang": lang}, who)
case "reconnect":
// set up from scratch: new keys and the new configuration, which the set-up sheet takes in
c.action()
c.needHTTPS()
c.notReady()
a := map[string]any{"id": dev.ID, "lang": lang}
// what it is and LAN Bypass may change with the keys (the sheet asks again); left out, they stay
if k, is := b["kind"].(string); is {
a["kind"] = k
}
if on, is := b["lan_bypass"].(bool); is {
a["lan_bypass"] = on
}
who.Defer = dev.Address == strings.TrimPrefix(c.q.Remote, "::ffff:")
var cfg map[string]any
if cfg, err = DeviceRotateAs(c.d, a, who); err == nil {
out["config"] = c.withIP(cfg)
}
case "bypass":
// LAN Bypass on or off, keys unchanged: the device takes its updated configuration
c.action()
c.needHTTPS()
on, is := b["on"].(bool)
if !is {
c.fail(400, c.t.T("Unknown action."))
}
_, err = DeviceUpdateAs(c.d, map[string]any{"id": dev.ID, "lan_bypass": on, "lang": lang}, who)
case "networks":
// the networks behind a router as its owner wants them; turned-down ones they put away
c.action()
_, err = DeviceWishAs(c.d, map[string]any{"id": dev.ID, "networks": b["networks"], "cleared": b["cleared"], "lang": lang}, who)
case "delete":
c.action()
_, err = DeviceDeleteAs(c.d, dev.ID, lang, who)
default:
c.fail(400, c.t.T("Unknown action."))
}
if err != nil {
c.refuse(400, err)
}
c.reload()
out["state"] = c.state()
c.json(200, out)
}
c.fail(404, c.t.T("Not found."))
}
// Portal answers one request to the portal.
func Portal(d Deps, q PortalRequest) (out PortalAnswer) {
lang := ""
if m := firstLang.FindStringSubmatch(q.AcceptLanguage); m != nil {
lang = m[1]
}
c := &portalCall{d: d, q: q, t: i18n.Use(d.Catalogs, lang), buckets: portalBuckets(q)}
defer func() {
r := recover()
switch x := r.(type) {
case nil:
case portalDone:
out = x.a
default:
// what broke, the path without its first part (the person's link code is a secret)
if d.Log != nil {
rest := ""
if i := strings.Index(strings.TrimPrefix(q.Path, "/"), "/"); i >= 0 {
rest = strings.TrimPrefix(q.Path, "/")[i:]
}
d.Log.Say("error", "portal", fmt.Sprintf("%s /<link>%s: 500 · %v", q.Method, rest, r))
}
out = PortalAnswer{Status: 500, Header: [][2]string{{"Content-Type", "text/plain"}}, Body: []byte("Error\n")}
}
}()
c.w = d.World()
if c.w == nil {
c.answer(503, [][2]string{{"Content-Type", "text/plain"}}, []byte("Starting\n"))
}
m := c.w.M
var segs []string
for _, s := range strings.Split(q.Path, "/") {
if s != "" {
segs = append(segs, s)
}
}
// Plain HTTP only for an IP address chosen in Settings (no certificate exists for it); names stay HTTPS-only.
if !q.HTTPS {
h := strings.SplitN(q.Host, ":", 2)[0]
if strings.HasPrefix(q.Host, "[") {
h = q.Host
}
_, isIP := ipnet.IP(h)
chosen := false
for _, x := range view.PortalHosts(m) {
chosen = chosen || x == h
}
if !isIP || !chosen {
c.notFound()
}
}
// the link is /<word-code>[/assets/…|/api/…]: the code is the address
if len(segs) < 1 || !wordCode.MatchString(segs[0]) {
c.notFound()
}
for _, id := range m.PersonIDs() {
if p := m.People[id]; p.Code == segs[0] {
c.person = p
}
}
// A wrong code is indistinguishable from a path that does not exist. Only well-formed guesses count
// towards the limit, and a client over it is just no longer counted: a valid link always opens, so
// guesses from a shared address cannot lock its owner out.
if c.person == nil {
if !c.blocked() {
c.wrongCode()
}
c.notFound()
}
c.base = "/" + segs[0]
rest := segs[1:]
if len(rest) == 0 && q.Method == "GET" {
c.shell()
}
if len(rest) == 2 && rest[0] == "assets" && q.Method == "GET" {
c.asset(rest[1])
}
if len(rest) == 0 || rest[0] != "api" {
c.notFound()
}
// CSRF: only same-origin JSON requests (a cross-site page cannot send application/json without CORS,
// and it would need the code anyway)
switch q.Method {
case "POST":
scheme := "http"
if q.HTTPS {
scheme = "https"
}
if !jsonType.MatchString(q.ContentType) || (q.Origin != "" && q.Origin != scheme+"://"+q.Host) {
c.fail(403, c.t.T("Cross-site request refused."))
}
case "GET":
default:
c.fail(405, c.t.T("Method not allowed."))
}
c.api(q.Method, rest[1:])
return out
}
// PortalHandler is Portal as a web server's handler; live: the pages it serves have a stream to follow.
func PortalHandler(d Deps, live bool) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
remote := r.RemoteAddr
if host, _, err := net.SplitHostPort(remote); err == nil {
remote = host
}
a := Portal(d, PortalRequest{Method: r.Method, Path: r.URL.Path, Host: r.Host, HTTPS: r.TLS != nil, Remote: remote,
CFConnectingIP: r.Header.Get("Cf-Connecting-Ip"), Origin: r.Header.Get("Origin"), ContentType: r.Header.Get("Content-Type"),
AcceptLanguage: r.Header.Get("Accept-Language"), AcceptEncoding: r.Header.Get("Accept-Encoding"),
ContentLength: r.ContentLength, Body: r.Body, Live: live})
for _, kv := range a.Header {
w.Header().Set(kv[0], kv[1])
}
w.WriteHeader(a.Status)
w.Write(a.Body)
})
}
+236
View File
@@ -0,0 +1,236 @@
package methods
import (
"encoding/json"
"os"
"strings"
"testing"
"time"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/store"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/usage"
"github.com/tickcount/luci-app-dawnline/backend/view"
)
// What a person does through their link: each change lands in the settings as theirs, within their
// limits, and never on what is not theirs.
func TestPortalChanges(t *testing.T) {
dir := t.TempDir()
text, _ := os.ReadFile("../access/testdata/access.uci")
c, _ := uci.Parse(string(text))
var who, code, mine, theirs string
for _, s := range c.OfType("device") {
p, _ := s.Get("person")
if who == "" {
who, mine = p, s.Name
} else if p != who && theirs == "" {
theirs = s.Name
}
}
code = "kamiro-telosu-bavine"
c.Set(who, "code", code)
c.Set(who, "device_limit", "5")
c.Set("settings", "portal_enabled", "1")
os.WriteFile(dir+"/dawnline", []byte(c.Format()), 0o600)
st := &store.Store{Config: dir + "/dawnline", RunDir: dir}
var later []string
d := Deps{Store: st, Config: dir + "/dawnline", RunDir: dir, DataDir: dir + "/data",
World: func() *view.World {
_, m, _ := st.Read()
return &view.World{M: m, Usage: usage.Empty(), Now: time.Now().Unix()}
},
AWG: func() map[string]any { return map[string]any{"tools": true, "kmod": true} },
Background: func(script string) { later = append(later, script) }}
load := func() *model.Model { _, m, _ := st.Read(); return m }
n := len(load().People[who].Devices)
ask := func(method, path, body, remote string, https bool) (int, map[string]any) {
q := PortalRequest{Method: method, Path: "/" + code + path, Host: "vpn.example.org:4500", HTTPS: https, Remote: remote}
if !https {
q.Host = "203.0.113.7:4501"
}
if method == "POST" {
q.ContentType, q.Origin, q.Body, q.ContentLength = "application/json", "https://"+q.Host, strings.NewReader(body), int64(len(body))
if !https {
q.Origin = "http://" + q.Host
}
}
a := Portal(d, q)
var out map[string]any
json.Unmarshal(a.Body, &out)
return a.Status, out
}
// a new device: theirs, made by the portal, its configuration in the answer and the state after
status, res := ask("POST", "/api/devices", `{"name":"Tablet","kind":"tablet","lan_bypass":true}`, "198.51.100.9", true)
id, _ := res["id"].(string)
dev := load().Devices[id]
if status != 200 || dev == nil || dev.Person != who || dev.CreatedBy != "portal" || !dev.LanBypass || dev.Kind != "tablet" {
t.Fatalf("a new device: %d %v", status, res["error"])
}
cfg, _ := res["config"].(map[string]any)
state, _ := res["state"].(map[string]any)
person, _ := state["person"].(map[string]any)
if !strings.Contains(cfg["conf"].(string), "[Interface]") || len(person["devices"].([]any)) != n+1 {
t.Errorf("the answer to a new device: %v", res)
}
// with the browser's own key the private key never reaches the router
const pub = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbA="
status, res = ask("POST", "/api/devices", `{"name":"Desk","kind":"computer","public_key":"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzA="}`, "198.51.100.9", true)
laptop, _ := res["id"].(string)
if dev := load().Devices[laptop]; status != 200 || dev == nil || dev.PublicKey != "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzA=" || dev.PrivateKey != "" {
t.Fatalf("a device with the browser's key: %d %v", status, res["error"])
}
_ = pub
// the limit holds the portal (the admin is not held to it)
if n != 3 {
t.Fatalf("the household changed: %d devices", n)
}
if status, res = ask("POST", "/api/devices", `{"name":"One more","kind":"phone"}`, "198.51.100.9", true); status != 400 || res["error"] != "Device limit reached (5)." {
t.Errorf("over the limit: %d %v", status, res)
}
if state, _ := ask("GET", "/api/state", "", "198.51.100.9", true); state != 200 {
t.Errorf("the state: %d", state)
}
// a rename, LAN Bypass, and what is somebody else's
if status, _ = ask("POST", "/api/devices/"+id, `{"action":"rename","name":"Big tablet"}`, "198.51.100.9", true); status != 200 || load().Devices[id].Name != "Big tablet" {
t.Errorf("a rename: %d", status)
}
if status, _ = ask("POST", "/api/devices/"+id, `{"action":"bypass","on":false}`, "198.51.100.9", true); status != 200 || load().Devices[id].LanBypass {
t.Errorf("LAN Bypass off: %d", status)
}
if theirs != "" {
if status, res = ask("POST", "/api/devices/"+theirs, `{"action":"delete"}`, "198.51.100.9", true); status != 404 || load().Devices[theirs] == nil {
t.Errorf("another person's device: %d %v", status, res)
}
}
// set up again from the device itself: the keys change now, and are loaded a moment after the answer
was := load().Devices[id].PublicKey
later = nil
status, res = ask("POST", "/api/devices/"+id, `{"action":"reconnect","kind":"phone"}`, load().Devices[id].Address, true)
now := load().Devices[id]
if status != 200 || now.PublicKey == was || now.Kind != "phone" || len(later) != 1 || !strings.Contains(later[0], "dawnline sync") || res["config"] == nil {
t.Errorf("set up again: %d, later %v, %v", status, later, res["error"])
}
// keys never travel in the clear: the answer names the HTTPS link
status, res = ask("GET", "/api/devices/"+id+"/config", "", "198.51.100.9", false)
if status != 403 || res["https_url"] != "https://203.0.113.7:4500/"+code {
t.Errorf("a configuration over plain HTTP: %d %v", status, res)
}
if status, res = ask("GET", "/api/state", "", "198.51.100.9", false); status != 200 || res["https_url"] != "https://203.0.113.7:4500/"+code {
t.Errorf("the state over plain HTTP: %d %v", status, res["https_url"])
}
// a device the admin turned off is not the portal's to change
c2, _, _ := st.Read()
c2.Set(id, "disabled", "1")
os.WriteFile(dir+"/dawnline", []byte(c2.Format()), 0o600)
if status, res = ask("POST", "/api/devices/"+id, `{"action":"rename","name":"Mine again"}`, "198.51.100.9", true); status != 400 || res["error"] != "The admin has turned this device off." {
t.Errorf("a device turned off: %d %v", status, res)
}
// a person the admin paused sees their page and changes nothing
c2.Set(who, "paused", "1")
os.WriteFile(dir+"/dawnline", []byte(c2.Format()), 0o600)
if status, res = ask("POST", "/api/devices/"+laptop, `{"action":"delete"}`, "198.51.100.9", true); status != 403 || res["error"] != "Your access is paused." || load().Devices[laptop] == nil {
t.Errorf("a paused person's change: %d %v", status, res)
}
if status, res = ask("GET", "/api/state", "", "198.51.100.9", true); status != 200 || res["locked"] != true {
t.Errorf("a paused person's page: %d %v", status, res["locked"])
}
c2.Delete(who, "paused")
os.WriteFile(dir+"/dawnline", []byte(c2.Format()), 0o600)
if status, _ = ask("POST", "/api/devices/"+laptop, `{"action":"delete"}`, "198.51.100.9", true); status != 200 || load().Devices[laptop] != nil {
t.Errorf("a delete: %d", status)
}
// the activity log says who did it
raw, _ := os.ReadFile(dir + "/events.json")
if !strings.Contains(string(raw), `"by":"portal"`) || !strings.Contains(string(raw), "device_removed") || strings.Contains(string(raw), `"by":"admin"`) {
t.Errorf("the activity log: %.300s", raw)
}
_ = mine
}
// Wrong codes are counted a client, and a client over the limit is told nothing new; a link is held to
// forty changes a quarter hour; a valid link always opens.
func TestPortalLimits(t *testing.T) {
dir := t.TempDir()
text, _ := os.ReadFile("../access/testdata/access.uci")
c, _ := uci.Parse(string(text))
who := c.OfType("person")[0].Name
c.Set(who, "code", "kamiro-telosu-bavine")
var dev string
for _, s := range c.OfType("device") {
if p, _ := s.Get("person"); p == who && dev == "" {
dev = s.Name
}
}
os.WriteFile(dir+"/dawnline", []byte(c.Format()), 0o600)
st := &store.Store{Config: dir + "/dawnline", RunDir: dir}
d := Deps{Store: st, Config: dir + "/dawnline", RunDir: dir, DataDir: dir + "/data", World: func() *view.World {
_, m, _ := st.Read()
return &view.World{M: m, Usage: usage.Empty(), Now: time.Now().Unix()}
}}
get := func(path, remote, cf string) PortalAnswer {
return Portal(d, PortalRequest{Method: "GET", Path: path, Host: "vpn.example.org:4500", HTTPS: true, Remote: remote, CFConnectingIP: cf})
}
for i := 0; i < 20; i++ {
if a := get("/zzzzzz-zzzzzz-zzzzzz", "2001:db8:1:2:3:4:5:"+string(rune('a'+i%6)), ""); a.Status != 404 || string(a.Body) != "Not found\n" {
t.Fatalf("a wrong code: %d %q", a.Status, a.Body)
}
}
var rates map[string]struct{ Count int }
raw, _ := os.ReadFile(dir + "/portal-rate.json")
json.Unmarshal(raw, &rates)
// one /64 is one client; counted to the limit and no further
if len(rates) != 1 || rates["2001:db8:1:2::/64"].Count != portalFailLimit {
t.Errorf("the count of wrong codes: %s", raw)
}
log, _ := os.ReadFile(dir + "/events.json")
if strings.Count(string(log), "portal_blocked") != 1 || !strings.Contains(string(log), "2001:db8:1:2::/64") {
t.Errorf("a blocked client is told once: %.300s", log)
}
// the link's owner opens it from that very address all the same
if a := get("/kamiro-telosu-bavine", "2001:db8:1:2::9", ""); a.Status != 200 || !strings.Contains(string(a.Body), `data-base="/kamiro-telosu-bavine"`) {
t.Errorf("a valid link from a blocked address: %d", a.Status)
}
// behind Cloudflare the visitor is who it says, and only then
get("/zzzzzz-zzzzzz-zzzzzz", "173.245.48.5", "198.51.100.77")
get("/zzzzzz-zzzzzz-zzzzzz", "198.51.100.200", "198.51.100.78")
raw, _ = os.ReadFile(dir + "/portal-rate.json")
json.Unmarshal(raw, &rates)
if rates["198.51.100.77"].Count != 1 || rates["cloudflare"].Count != 1 || rates["198.51.100.200"].Count != 1 || rates["198.51.100.78"].Count != 0 {
t.Errorf("the buckets: %s", raw)
}
// what is no link at all is not counted, and files never become a redirect
c.Set("settings", "portal_redirect", "https://example.org/")
os.WriteFile(dir+"/dawnline", []byte(c.Format()), 0o600)
if a := get("/", "203.0.113.50", ""); a.Status != 302 {
t.Errorf("the redirect: %d", a.Status)
}
if a := get("/favicon.ico", "203.0.113.50", ""); a.Status != 404 {
t.Errorf("a file: %d", a.Status)
}
raw, _ = os.ReadFile(dir + "/portal-rate.json")
if strings.Contains(string(raw), "203.0.113.50") {
t.Error("a path that is no link was counted")
}
// plain HTTP only for an address chosen in Settings
plain := func(host string) int {
return Portal(d, PortalRequest{Method: "GET", Path: "/kamiro-telosu-bavine", Host: host, Remote: "203.0.113.50"}).Status
}
if plain("203.0.113.7:4501") != 200 || plain("vpn.example.org:4501") != 302 || plain("198.51.100.1:4501") != 302 {
t.Error("plain HTTP is open for more than the chosen address, or closed for it")
}
// forty changes a quarter hour through one link
last := 0
for i := 0; i <= portalActionLimit; i++ {
body := `{"action":"bypass","on":` + map[bool]string{true: "true", false: "false"}[i%2 == 0] + `}`
last = Portal(d, PortalRequest{Method: "POST", Path: "/kamiro-telosu-bavine/api/devices/" + dev, Host: "vpn.example.org:4500", HTTPS: true, Remote: "203.0.113.60",
ContentType: "application/json", Origin: "https://vpn.example.org:4500", Body: strings.NewReader(body), ContentLength: int64(len(body))}).Status
if i < portalActionLimit && last != 200 {
t.Fatalf("change %d: %d", i, last)
}
}
if last != 429 {
t.Errorf("the forty-first change: %d", last)
}
}
View File
Whitespace-only changes.
+478
View File
@@ -0,0 +1,478 @@
package methods
import (
"errors"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/endpoint"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
"github.com/tickcount/luci-app-dawnline/backend/journal"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/view"
)
// spec is one setting of dawnline.schema: its type, its range or choices, and whether the portal must
// restart when it changes.
type spec struct {
kind string
min, max int
values []string
restart bool
}
var schema = map[string]spec{
"portal_enabled": {kind: "bool", restart: true}, "portal_host": {kind: "host", restart: true}, "portal_alt": {kind: "hosts", restart: true},
"portal_port": {kind: "int", min: 1, max: 65535, restart: true}, "portal_redirect": {kind: "url"},
"portal_cert": {kind: "file", restart: true}, "portal_key": {kind: "file", restart: true},
"default_device_limit": {kind: "int", min: 1, max: 32}, "hostnames": {kind: "bool"}, "domain": {kind: "domain"},
"geo_enabled": {kind: "bool"}, "geo_provider": {kind: "enum", values: []string{"ipwho.is", "ipapi.co"}},
"log_connections": {kind: "bool"}, "log_level": {kind: "enum", values: []string{"error", "warn", "info", "debug"}}, "watchdog": {kind: "bool"},
"log_keep_days": {kind: "int", min: 0, max: 9999}, "log_max": {kind: "int", min: 100, max: 20000},
"log_persist": {kind: "enum", values: []string{"10min", "hourly", "off"}},
"usage_keep_days": {kind: "int", min: 7, max: 1830}, "usage_keep_months": {kind: "int", min: 1, max: 120},
"home_dns_fallback": {kind: "ipv4"}, "update_check": {kind: "bool"}, "update_interval": {kind: "enum", values: []string{"6h", "12h", "1d", "7d"}},
"update_auto": {kind: "bool"}, "update_mode": {kind: "enum", values: []string{"immediate", "scheduled"}}, "update_time": {kind: "time"},
"update_targets": {kind: "targets"}, "update_keep": {kind: "bool"}, "update_mirrors": {kind: "bool"},
}
// what the admin is told about a bad value, by setting
var badValue = map[string]string{
"portal_host": "Public address must be a hostname or IP address.",
"portal_alt": "Other addresses must be hostnames or IP addresses.",
"portal_port": "Choose a free port between 1 and 65535 (not 22, 53, 80 or 443).",
"portal_redirect": "Redirect must be a full http:// or https:// address.",
"default_device_limit": "Default device limit must be 1-32.",
"domain": "Domain may use lowercase letters, digits, dashes and dots.",
"geo_provider": "Unknown location service.",
"log_level": "Choose a system log level: errors, warnings, normal or verbose.",
"log_keep_days": "Keep activity for 0 (no age limit) to 9999 days.",
"log_max": "Log size must be 100-20000 events.",
"log_persist": "Save the log every 10min, hourly or off.",
"usage_keep_days": "Daily traffic history must be 7-1830 days.",
"usage_keep_months": "Monthly traffic history must be 1-120 months.",
"home_dns_fallback": "Fallback DNS must be an IPv4 address (or empty for none).",
"update_interval": "Check every 6h, 12h, 1d or 7d.",
"update_mode": "Install updates immediately or scheduled.",
"update_time": "Enter a time as HH:MM, 00:00 to 23:59.",
}
func bad(key string) error {
if msg, ok := badValue[key]; ok {
return errors.New(msg)
}
return said{"Invalid value for %s.", []any{key}}
}
var (
wholeNumber = regexp.MustCompile(`^[0-9]{1,9}$`)
redirectURL = regexp.MustCompile(`^https?://[A-Za-z0-9.-]+(:[0-9]+)?(/[^\s"'<>]*)?$`)
domainName = regexp.MustCompile(`^[a-z0-9-]{1,32}(\.[a-z0-9-]{1,32})*$`)
clockTime = regexp.MustCompile(`^([01]?[0-9]|2[0-3]):[0-5][0-9]$`)
)
// validate checks a value the admin saves for a setting and returns what to store: a string, a list, or
// nil to remove the option (back to the default). dawnline.schema validate().
func validate(key string, val any) (any, error) {
sp, known := schema[key]
if !known {
return nil, said{"Unknown setting %s.", []any{key}}
}
list, _ := val.([]any)
switch sp.kind {
case "hosts":
var out []string
for _, x := range list {
s, isString := x.(string)
h := strings.Trim(s, blank)
// a typo must not vanish on Save: refused, like the main address
if !isString || (h != "" && !endpoint.ValidHost(h)) {
return nil, bad(key)
}
dup := false
for _, o := range out {
dup = dup || o == h
}
if h != "" && !dup {
out = append(out, h)
}
}
if len(out) == 0 {
return nil, nil
}
return out, nil
case "targets":
var out []string
for _, x := range list {
s := text(x)
dup := false
for _, o := range out {
dup = dup || o == s
}
if (s == "app" || s == "amneziawg" || s == "packages") && !dup {
out = append(out, s)
}
}
// an empty list is a choice too ("none"), unlike a missing option (the default)
if len(out) == 0 {
return []string{"none"}, nil
}
return out, nil
}
v := ""
switch x := val.(type) {
case bool:
v = "0"
if x {
v = "1"
}
case string:
v = strings.Trim(x, blank)
case nil:
default:
v = str(num(val))
}
if sp.kind == "ipv4" && v == "" {
return "none", nil
}
if v == "" {
return nil, nil
}
switch sp.kind {
case "bool":
if v != "0" && v != "1" {
return nil, said{"%s is 0 or 1.", []any{key}}
}
case "int":
n, _ := strconv.Atoi(v)
if !wholeNumber.MatchString(v) || n < sp.min || n > sp.max {
return nil, bad(key)
}
v = strconv.Itoa(n)
case "enum":
ok := false
for _, c := range sp.values {
ok = ok || c == v
}
if !ok {
return nil, bad(key)
}
case "host":
if !endpoint.ValidHost(v) {
return nil, bad(key)
}
case "url":
if len(v) > 300 || !redirectURL.MatchString(v) {
return nil, bad(key)
}
case "file":
if !strings.HasPrefix(v, "/") || !exists(v) {
return nil, said{"File %s does not exist.", []any{v}}
}
case "domain":
if !domainName.MatchString(v) {
return nil, bad(key)
}
case "ipv4":
if _, ok := ipnet.IP(v); !ok {
return nil, bad(key)
}
case "time":
if !clockTime.MatchString(v) {
return nil, bad(key)
}
}
return v, nil
}
// flat is a stored or a new value as one string, to tell whether it changed: a list a line an entry.
func flat(v any) string {
switch x := v.(type) {
case string:
return x
case []string:
return strings.Join(x, "\n")
}
return ""
}
// shownValue is a value as the syslog line of a change says it.
func shownValue(v any) string {
switch x := v.(type) {
case string:
if x != "" {
return x
}
case []string:
var kept []string
for _, e := range x {
if e != "" {
kept = append(kept, e)
}
}
if len(kept) == 0 {
return "none"
}
return strings.Join(kept, " ")
}
return "default"
}
func isReserved(port int) bool {
for _, r := range Reserved {
if r == port {
return true
}
}
return false
}
func plainHTTP(s model.Settings) bool {
for _, h := range append([]string{s.PortalHost}, s.PortalAlt...) {
if _, ip := ipnet.IP(h); ip {
return true
}
}
return false
}
// checkPortal refuses a portal whose ports cannot be taken: its HTTPS port and, when links on IP addresses
// use plain HTTP, the next one. Neither may be the router's own, another program's, or forwarded away.
func checkPortal(now, was *model.Model, progs []held, fw *uci.Config) error {
s := now.Settings
if !s.PortalEnabled {
return nil
}
port := s.PortalPort
if port < 1 || port > 65535 || isReserved(port) {
return errors.New("Choose a free port between 1 and 65535 (not 22, 53, 80 or 443).")
}
ports := []int{port}
if plainHTTP(s) {
hp := port + 1
if hp < 1 || hp > 65535 || isReserved(hp) {
return said{"Links on IP addresses use plain HTTP on the next port (%d), which the portal cannot take. Choose another portal port.", []any{hp}}
}
ports = append(ports, hp)
}
// only ports the portal does not hold already
var mine []int
if was.Settings.PortalEnabled {
mine = append(mine, was.Settings.PortalPort)
if plainHTTP(was.Settings) {
mine = append(mine, was.Settings.PortalPort+1)
}
}
for _, p := range ports {
kept := false
for _, h := range mine {
kept = kept || h == p
}
if kept {
continue
}
var owners []string
for _, h := range progs {
if h.port == p {
owners = append(owners, h.name)
}
}
if len(owners) > 0 {
msg := "TCP %d is already used by %s. Choose another portal port."
if p != port {
msg = "TCP %d is already used by %s. Links on IP addresses use plain HTTP on the port after the portal's. Choose another portal port."
}
return said{msg, []any{p, strings.Join(owners, ", ")}}
}
}
// a forwarded port never reaches the router: the portal would be unreachable
zones := []string{"wan"}
for _, id := range now.ServerIDs() {
z, dup := now.Servers[id].WanZone, false
for _, o := range zones {
dup = dup || o == z
}
if !dup {
zones = append(zones, z)
}
}
for _, p := range ports {
for _, z := range zones {
for _, f := range forwards(fw, z, "tcp") {
if f.from <= p && p <= f.to {
return said{"TCP %d is sent on by port forward “%s”. Choose another portal port.", []any{p, f.name}}
}
}
}
}
return nil
}
// settingsUpdate saves settings (`values`: key to value, "" back to the default); the portal is checked as
// it will be. The settings are taken in the schema's order, whatever order they came in.
func settingsUpdate(d Deps, a map[string]any) (map[string]any, error) {
values, isObject := a["values"].(map[string]any)
// no values at all is nothing to change, and no refusal; anything but an object is
if a["values"] == nil {
values, isObject = map[string]any{}, true
}
if !isObject {
return nil, errors.New("Nothing to save.")
}
var unknown []string
for k := range values {
if _, known := schema[k]; !known && k != "log_category" {
unknown = append(unknown, k)
}
}
if len(unknown) > 0 {
sort.Strings(unknown)
return nil, said{"Unknown setting %s.", []any{unknown[0]}}
}
type change_ struct {
key string
from, to any
}
var diff []change_
restart := false
var was, now *model.Model
err := write(d, func(c *uci.Config, m *model.Model) error {
was = m
section := c.Add("settings", "settings")
current := func(k string) any {
v, has := section.Values[k]
if !has {
return nil
}
if v.IsList {
return v.List
}
return v.Str
}
put := func(k string, to any) {
if from := current(k); flat(to) != flat(from) {
diff = append(diff, change_{k, from, to})
}
switch x := to.(type) {
case nil:
c.Delete("settings", k)
case string:
// an empty string is no value to a cursor: the option goes
c.Set("settings", k, x)
case []string:
c.SetList("settings", k, x)
}
}
for _, k := range Settings {
val, given := values[k]
if !given {
continue
}
nv, err := validate(k, val)
if err != nil {
return err
}
if schema[k].restart && flat(nv) != flat(current(k)) {
restart = true
}
put(k, nv)
}
if val, given := values["log_category"]; given {
list, _ := val.([]any)
var cats []string
for _, x := range list {
for _, known := range journal.Categories {
if text(x) == known {
cats = append(cats, known)
}
}
}
if len(cats) == 0 {
cats = []string{""}
}
put("log_category", cats)
// superseded by the category list
c.Delete("settings", "log_connections")
}
var blocked map[string]any
files.ReadJSON(d.RunDir+"/netifd-blocked.json", &blocked)
now = model.Load(c, blocked)
return checkPortal(now, m, listening("tcp"), parseFile("/etc/config/firewall"))
})
if err != nil {
return nil, err
}
// every setting that changed, in syslog
if len(diff) > 0 && d.Log != nil {
var parts []string
for _, x := range diff {
parts = append(parts, x.key+": "+shownValue(x.from)+" → "+shownValue(x.to))
}
d.Log.Say("info", "settings", "admin changed settings · "+strings.Join(parts, ", "))
}
var warning any
if restart {
if d.Reload != nil {
d.Reload()
}
} else if d.Sync != nil {
warning = d.Sync(text(a["lang"]))
}
// the changes worth a line: the portal and the watchdog
if d.Log != nil {
n, w := now.Settings, was.Settings
if n.PortalEnabled != w.PortalEnabled {
if n.PortalEnabled {
d.Log.Admin("portal", "turned on", "", "the personal portal", fmt.Sprintf("port %d", n.PortalPort))
} else {
d.Log.Admin("portal", "turned off", "", "the personal portal", "")
}
} else if n.PortalEnabled {
var what []string
if n.PortalPort != w.PortalPort {
what = append(what, fmt.Sprintf("port %d", n.PortalPort))
}
if n.PortalHost != w.PortalHost {
what = append(what, "address "+n.PortalHost)
}
if strings.Join(n.PortalAlt, " ") != strings.Join(w.PortalAlt, " ") {
what = append(what, "other addresses")
}
if n.PortalCert != w.PortalCert || n.PortalKey != w.PortalKey {
what = append(what, "certificate")
}
if n.PortalRedirect != w.PortalRedirect {
what = append(what, "redirect")
}
if len(what) > 0 {
d.Log.Admin("portal", "changed", "", "the personal portal", strings.Join(what, ", "))
}
}
if n.Watchdog != w.Watchdog {
if n.Watchdog {
d.Log.Admin("dawnline", "turned on", "", "the watchdog", "")
} else {
d.Log.Admin("dawnline", "turned off", "", "the watchdog", "problems are only logged, nothing is repaired")
}
}
}
if d.Told != nil {
d.Told()
}
out := map[string]any{}
if w := d.World(); w != nil && w.M != nil {
out = settingsGet(w, i18n.Use(d.Catalogs, text(a["lang"])), d)
}
if warning != nil && warning != "" {
out["warning"] = warning
}
return out, nil
}
var _ = view.World{}
+874
View File
@@ -0,0 +1,874 @@
package methods
import (
"errors"
"fmt"
"net"
"os"
"os/exec"
"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/files"
"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/servicelog"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/view"
)
var (
meshWords = map[string]string{"off": "devices kept apart", "all": "devices reach each other", "circles": "devices reach each other in circles"}
keepaliveArg = regexp.MustCompile(`^[0-9]{1,3}$`)
mtuArg = regexp.MustCompile(`^[0-9]{1,4}$`)
)
// on tells a switch as a page sends it: true or "1".
func on(v any) bool { return v == true || v == "1" || num(v) == 1 }
// asString is a value as ucode writes it into a string, trimmed.
func asString(v any) string {
switch x := v.(type) {
case string:
return strings.Trim(x, blank)
case nil:
return ""
case bool:
if x {
return "true"
}
return "false"
}
return str(num(v))
}
// dnsList is a list of DNS servers as typed (a list, or "a, b"): each an IPv4 or IPv6 address.
func dnsList(v any) ([]string, error) {
var parts []string
if list, isList := v.([]any); isList {
for _, x := range list {
parts = append(parts, asString(x))
}
} else {
parts = regexp.MustCompile(`[ ,]+`).Split(asString(v), -1)
}
out := []string{}
for _, d := range parts {
if d == "" {
continue
}
if _, v4 := ipnet.IP(d); !v4 && (net.ParseIP(d) == nil || !strings.Contains(d, ":")) {
return nil, said{"%s is not an IP address.", []any{d}}
}
out = append(out, d)
}
return out, nil
}
// privateNet tells a network that lies inside one of the private ranges.
func privateNet(n ipnet.Net) bool {
for _, p := range []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"} {
r, _ := ipnet.Any(p)
if uint64(n.Base) >= uint64(r.Base) && uint64(n.Base)+n.Size() <= uint64(r.Base)+r.Size() {
return true
}
}
return false
}
// udpHeld is the UDP ports a server cannot take, read on the router.
func udpHeld(m *model.Model, srv *model.Server, t *i18n.Catalog) []map[string]any {
udp, _ := os.ReadFile("/proc/net/udp")
udp6, _ := os.ReadFile("/proc/net/udp6")
return udpTaken(m, srv, t, parseFile("/etc/config/network"), parseFile("/etc/config/firewall"), udpBound(string(udp), string(udp6)), listening("udp"))
}
// serverUpdate saves a server's settings from the General and Network tabs and applies them at once
// (dawnline.rpc.servers server_update). It answers what the apply came to.
func serverUpdate(d Deps, a map[string]any) (map[string]any, error) {
id := text(a["id"])
v, _ := a["values"].(map[string]any)
if v == nil {
v = map[string]any{}
}
if v["device_mesh"] != nil && v["mesh"] == nil {
v["mesh"] = "off"
if on(v["device_mesh"]) {
v["mesh"] = "all"
}
}
t := i18n.Use(d.Catalogs, text(a["lang"]))
var was model.Server
var newDNS []string
err := write(d, func(c *uci.Config, m *model.Model) error {
srv := m.Servers[id]
if srv == nil {
return errors.New("Unknown server.")
}
was = *srv
set := func(k, val string) {
if val == "" {
c.Delete(id, k)
} else {
c.Set(id, k, val)
}
}
if v["name"] != nil {
if !ValidName(asString(v["name"])) {
return errors.New(`Server: Enter a name: up to 48 characters, without < > " or \.`)
}
set("name", asString(v["name"]))
}
// empty: the portal's address, as at creation
if v["endpoint"] != nil {
if e := asString(v["endpoint"]); e != "" && !endpoint.ValidHost(e) {
return errors.New("Public address must be a hostname or IP address.")
}
set("endpoint", asString(v["endpoint"]))
}
if v["port"] != nil {
if !ValidPort(asString(v["port"])) {
return errors.New("UDP port must be 1-65535.")
}
n, _ := strconv.Atoi(asString(v["port"]))
if n != srv.Port {
for _, h := range udpHeld(m, srv, t) {
if h["from"].(int) <= n && n <= h["to"].(int) {
return said{"UDP %d is used by %s. Choose another port.", []any{n, h["who"]}}
}
}
}
set("port", strconv.Itoa(n))
}
if v["enabled"] != nil {
if on(v["enabled"]) {
set("enabled", "1")
} else {
set("enabled", "0")
}
}
if v["mesh"] != nil {
mesh := text(v["mesh"])
if _, known := meshWords[mesh]; !known {
return errors.New("Unknown choice for devices reaching each other.")
}
set("mesh", mesh)
set("device_mesh", "")
}
// what people may do about routers on their own pages; "ask" is what an unset one means
for k, choices := range map[string][]string{"portal_routers": model.PortalRouters, "portal_nets": model.PortalNets} {
if v[k] == nil {
continue
}
pick, known := text(v[k]), false
for _, x := range choices {
known = known || x == pick
}
if !known {
return errors.New("Unknown choice for routers on people's pages.")
}
if pick == "ask" {
pick = ""
}
set(k, pick)
}
if v["mtu"] != nil {
s := asString(v["mtu"])
if n, _ := strconv.Atoi(s); s != "" && (!mtuArg.MatchString(s) || n < 1280 || n > 1500) {
return errors.New("MTU must be 1280-1500.")
}
set("mtu", s)
}
if v["keepalive"] != nil {
s := asString(v["keepalive"])
if n, _ := strconv.Atoi(s); s != "" && (!keepaliveArg.MatchString(s) || n > 600) {
return errors.New("Keep-alive must be 0-600 seconds.")
}
set("keepalive", s)
}
if v["dns"] != nil {
list, err := dnsList(v["dns"])
if err != nil {
return err
}
newDNS = list
if len(list) > 0 {
c.SetList(id, "dns", list)
} else {
c.Delete(id, "dns")
}
}
// a new tunnel network: private, free, and every device renumbered into it
if sub := asString(v["subnet"]); v["subnet"] != nil && sub != "" && sub != srv.Subnet {
pc, ok := ipnet.Subnet(sub)
if !ok {
return errors.New("Subnet must look like 10.77.0.0/24.")
}
if !privateNet(pc) {
return errors.New("Use a private range: 10.x.x.x, 172.16–31.x.x or 192.168.x.x.")
}
var addrs []Address
if d.Addresses != nil {
addrs = d.Addresses()
}
for _, n := range takenNetworks(m, addrs) {
if n["cidr"] == srv.Subnet || n["name"] == srv.Name {
continue
}
if o, ok := ipnet.Subnet(n["cidr"].(string)); ok && uint64(pc.Base) < uint64(o.Base)+o.Size() && uint64(o.Base) < uint64(pc.Base)+pc.Size() {
return said{"Subnet overlaps %s.", []any{n["cidr"]}}
}
}
// every device on the server gets an address in it, lowest first
moved := map[string]string{}
here := map[string]bool{}
for _, pid := range m.PersonIDs() {
if p := m.People[pid]; p.Server == id {
for _, did := range p.Devices {
here[did] = true
}
}
}
to := *srv
to.Subnet = pc.String()
for _, pid := range m.PersonIDs() {
p := m.People[pid]
if p.Server != id {
continue
}
for _, did := range p.Devices {
addr := allocAddress(m, &to, func(q *model.Person) string { return q.Server }, func(x *model.Device) string {
if here[x.ID] {
return moved[x.ID]
}
return x.Address
})
if addr == "" {
return said{"%s has no room for every device on %s.", []any{pc.String(), srv.Name}}
}
moved[did] = addr
c.Set(did, "address", addr)
}
}
c.Set(id, "subnet", pc.String())
}
return nil
})
if err != nil {
return nil, err
}
// for the log: what changed, in words; turning on or off is its own line
var what []string
for _, k := range []struct{ key, label string }{{"name", "name"}, {"endpoint", "public address"}, {"port", "AmneziaWG port"}, {"mtu", "MTU"},
{"keepalive", "keep-alive"}, {"dns", "DNS"}, {"subnet", "tunnel network"}} {
if v[k.key] == nil {
continue
}
before, now := "", asString(v[k.key])
switch k.key {
case "name":
before = was.Name
case "endpoint":
before = was.Endpoint
case "port":
before = strconv.Itoa(was.Port)
case "mtu":
if was.MTU != 0 {
before = strconv.Itoa(was.MTU)
}
case "keepalive":
before = strconv.Itoa(was.Keepalive)
case "dns":
before, now = strings.Join(was.DNS, " "), strings.Join(newDNS, " ")
case "subnet":
before = was.Subnet
}
if strings.Trim(before, blank) != strings.Trim(now, blank) {
what = append(what, k.label)
}
}
name := was.Name
if v["name"] != nil {
name = asString(v["name"])
}
var applied map[string]any
if d.Apply != nil {
applied = d.Apply(text(a["lang"]))
}
if d.Log != nil {
line := func(msg, note string) {
d.Log.Write("dawnline", "info", msg, "", name, servicelog.Extra{By: "admin", Kind: "server", Note: note, SID: id})
}
if v["enabled"] != nil && on(v["enabled"]) != was.Enabled {
if on(v["enabled"]) {
line("turned on", "")
} else {
line("turned off", "")
}
}
if len(what) > 0 {
line("changed", strings.Join(what, ", "))
}
if v["mesh"] != nil && text(v["mesh"]) != was.Mesh {
line("changed", meshWords[text(v["mesh"])])
}
if v["portal_routers"] != nil && text(v["portal_routers"]) != was.PortalRouters {
line("changed", "routers on people's pages: "+text(v["portal_routers"]))
}
if v["portal_nets"] != nil && text(v["portal_nets"]) != was.PortalNets {
line("changed", "networks behind routers on people's pages: "+text(v["portal_nets"]))
}
}
if d.Told != nil {
d.Told()
}
if applied == nil {
applied = map[string]any{}
}
return applied, nil
}
// serverDelete removes a server with its people, their devices and its circles; a backup is made first.
func serverDelete(d Deps, id, lang string) (map[string]any, error) {
if d.Store == nil {
return nil, errors.New("The router is still starting. Try again in a moment.")
}
if _, m, err := d.Store.Read(); err != nil || m.Servers[id] == nil {
return nil, errors.New("Unknown server.")
}
if _, err := backupCreate(d, "pre-delete"); err != nil {
return nil, err
}
name, gone := "", 0
err := write(d, func(c *uci.Config, m *model.Model) error {
srv := m.Servers[id]
if srv == nil {
return errors.New("Unknown server.")
}
for _, pid := range m.PersonIDs() {
if p := m.People[pid]; p.Server == id {
for _, dev := range p.Devices {
c.DeleteSection(dev)
}
c.DeleteSection(pid)
gone++
}
}
for _, cid := range m.CircleIDs() {
if m.Circles[cid].Server == id {
c.DeleteSection(cid)
}
}
c.DeleteSection(id)
name = srv.Name
return nil
})
if err != nil {
return nil, err
}
var applied map[string]any
if d.Apply != nil {
applied = d.Apply(lang)
}
if d.Log != nil {
note := ""
if gone == 1 {
note = "with 1 person"
} else if gone > 1 {
note = "with " + strconv.Itoa(gone) + " people"
}
d.Log.Write("awg", "info", "removed", "", name, servicelog.Extra{By: "admin", Kind: "server", Note: note, SID: id})
}
if d.Told != nil {
d.Told()
}
if applied == nil {
applied = map[string]any{}
}
return applied, nil
}
// off tells a switch a page turned off: false or "0".
func off(v any) bool { return v == false || v == "0" }
// serverInterfaces saves which interfaces a server uses (its WAN, its home networks), whether its traffic
// leaves like a home device's and who reaches whom, and applies it (dawnline.rpc.servers server_interfaces).
// It answers the interfaces as the page shows them, and whether the home networks changed.
func serverInterfaces(d Deps, a map[string]any) (map[string]any, error) {
if d.Snapshot == nil {
return nil, errors.New("The router is still starting. Try again in a moment.")
}
id := text(a["id"])
snap := d.Snapshot()
autoWan, autoLan := !off(a["auto_wan"]), !off(a["auto_lan"])
wanDev := asString(a["wan_device"])
var was, now model.Server
mesh := ""
err := write(d, func(c *uci.Config, m *model.Model) error {
srv := m.Servers[id]
if srv == nil {
return errors.New("Unknown server.")
}
was = *srv
if !autoWan {
dev := snap.Devices[wanDev]
if dev == nil {
return errors.New("Choose the interface that leads to the internet.")
}
if dev.Zone == "" {
return said{"%s is not in a firewall zone, so the server port cannot be opened on it.", []any{wanDev}}
}
}
wan := wanDev
if autoWan {
wan = snap.WAN
}
var lans []string
list, _ := a["lan_devices"].([]any)
for _, x := range list {
name := asString(x)
saved := false
for _, o := range srv.LanDevices {
saved = saved || o == name
}
dev := snap.Devices[name]
// unknown names are kept only if they were saved before (an interface down right now)
if name == "" || name == srv.Ifname || (dev == nil && !saved) {
continue
}
if dev != nil && dev.Master != "" {
return said{"%s is part of %s; choose %s instead.", []any{name, dev.Master, dev.Master}}
}
if name == wan {
return said{"%s is the WAN.", []any{name}}
}
dup := false
for _, o := range lans {
dup = dup || o == name
}
if !dup {
lans = append(lans, name)
}
}
if a["lan_like"] != nil {
if off(a["lan_like"]) {
c.Set(id, "lan_like", "0")
} else {
c.Set(id, "lan_like", "1")
}
}
mesh = srv.Mesh
if _, known := meshWords[text(a["mesh"])]; known {
mesh = text(a["mesh"])
} else if a["device_mesh"] != nil {
mesh = "all"
if off(a["device_mesh"]) {
mesh = "off"
}
}
c.Set(id, "mesh", mesh)
c.Delete(id, "device_mesh")
flag := func(k string, v bool) {
if v {
c.Set(id, k, "1")
} else {
c.Set(id, k, "0")
}
}
flag("auto_wan", autoWan)
flag("auto_lan", autoLan)
if autoWan || wanDev == "" {
c.Delete(id, "wan_device")
} else {
c.Set(id, "wan_device", wanDev)
}
if len(lans) > 0 {
c.SetList(id, "lan_device", lans)
} else {
c.Delete(id, "lan_device")
}
// what was detected, as the fallback while an interface is down
if snap.WAN != "" {
c.Set(id, "detected_wan", snap.WAN)
}
if snap.LAN != "" {
c.Set(id, "detected_lan", snap.LAN)
}
// the older zone options kept in step
var blocked map[string]any
files.ReadJSON(d.RunDir+"/netifd-blocked.json", &blocked)
wanNet, _, _, zones := snap.ServerNet(model.Load(c, blocked).Servers[id])
if wanNet.Zone != "" {
c.Set(id, "wan_zone", wanNet.Zone)
}
if len(zones) > 0 {
c.Set(id, "lan_zone", zones[0])
}
now = *model.Load(c, blocked).Servers[id]
return nil
})
if err != nil {
return nil, err
}
sig := func(x model.Server) (string, string) {
wan, _, nets, _ := snap.ServerNet(&x)
return strings.Join(nets, ","), strings.Join(nets, ",") + "|" + wan.Dev + "|" + strconv.FormatBool(x.LanLike)
}
before, sigWas := sig(was)
_, sigNow := sig(now)
var what []string
if sigNow != sigWas {
what = append(what, "home network and internet path")
}
if mesh != was.Mesh {
what = append(what, meshWords[mesh])
}
if len(what) > 0 && d.Log != nil {
d.Log.Write("awg", "info", "changed", "", was.Name, servicelog.Extra{By: "admin", Kind: "server", Note: strings.Join(what, ", "), SID: id})
}
if d.Apply != nil {
d.Apply(text(a["lang"]))
}
if d.Told != nil {
d.Told()
}
w := d.World()
if w == nil || w.M == nil {
_, m, _ := d.Store.Read()
w = &view.World{M: m}
}
out, err := interfaces(w, d, id)
if err != nil {
return nil, err
}
eff, _ := out["effective"].(map[string]any)
nets, _ := eff["nets"].([]string)
out["nets_changed"] = before != strings.Join(nets, ",")
return out, nil
}
// caps is what the installed AmneziaWG can do.
func caps(d Deps) awg.Caps {
if d.Caps != nil {
return d.Caps()
}
return awg.SystemCaps(d.DataDir)
}
// defaultMethod is the disguise a new server gets: the strongest this router can run.
func defaultMethod(d Deps) string {
switch c := caps(d); {
case c.V3:
return "max"
case c.V2:
return "balanced"
}
return "off"
}
// checkMethod refuses a disguise the installed AmneziaWG cannot run, in the admin's words.
func checkMethod(d Deps, name string) error {
if name != "off" && name != "balanced" && name != "max" {
return errors.New("Unknown disguise method.")
}
c := caps(d)
installed := true
if d.AWG != nil {
st := d.AWG()
installed = st["tools"] == true && st["kmod"] == true
}
if name == "max" && !c.V3 {
if installed {
return errors.New("Maximum needs AmneziaWG 3.1 on the router (kmod-amneziawg, amneziawg-tools 3.1): install a 3.1 build in Dawnline → Components.")
}
return errors.New("AmneziaWG is not installed: install it in Dawnline → Components, then choose Maximum.")
}
if name == "balanced" && !c.V2 {
if installed {
return errors.New("Balanced needs AmneziaWG 2.0 or newer on the router: install a current build in Dawnline → Components.")
}
return errors.New("AmneziaWG is not installed: install it in Dawnline → Components, then choose Balanced.")
}
return nil
}
// serverObfuscation gives a server a new disguise of a method (the strongest the router runs when none is
// named): its old values go, the new ones are made here, and it is applied.
func serverObfuscation(d Deps, id, preset, lang string) (map[string]any, error) {
if preset == "" {
preset = defaultMethod(d)
}
if err := checkMethod(d, preset); err != nil {
return nil, err
}
values := disguise.Preset(preset)
// a cheap self-check: new values pass what a restore would be held to
o := map[string]string{}
for _, p := range values {
o[p.Key] = p.Value
}
if errs := disguise.Errors(o); len(errs) > 0 {
return nil, errors.New(strings.Join(errs, " "))
}
name := ""
err := write(d, func(c *uci.Config, m *model.Model) error {
srv := m.Servers[id]
if srv == nil {
return errors.New("Unknown server.")
}
name = srv.Name
for _, k := range model.ObfKeys {
c.Delete(id, k)
}
for _, p := range values {
c.Set(id, p.Key, p.Value)
}
c.Set(id, "preset", preset)
return nil
})
if err != nil {
return nil, err
}
var applied map[string]any
if d.Apply != nil {
applied = d.Apply(lang)
}
if d.Log != nil {
d.Log.Write("awg", "info", "changed", "", name, servicelog.Extra{By: "admin", Kind: "server", Note: "disguise: " + preset, SID: id})
}
if d.Told != nil {
d.Told()
}
if applied == nil {
applied = map[string]any{}
}
return applied, nil
}
// needLoaded: a server is made only where AmneziaWG is installed and its module loads. The golden's test
// turns it off, as the copy of ucode its golden ran on has that one check taken out.
var needLoaded = true
var serverID = regexp.MustCompile(`^[a-z][a-z0-9_]{0,23}$`)
// serverCreate sets a server up (the wizard's last step): its section with keys made here and the disguise
// of its method, the portal as asked, then ucode's full apply and the service turned on and restarted.
func serverCreate(d Deps, a map[string]any) (map[string]any, error) {
if needLoaded {
if err := needAWG(d); err != nil {
return nil, err
}
}
// an installed module that is not loaded yet is loaded: nothing loads it before the first tunnel comes up
if needLoaded && d.AWG != nil && d.AWG()["loaded"] != true {
exec.Command("modprobe", "amneziawg").Run()
if d.AWG()["loaded"] != true {
return nil, errors.New("AmneziaWG's kernel module doesn't load on this firmware: reinstall a build for this kernel in Components.")
}
}
if d.Snapshot == nil {
return nil, errors.New("The router is still starting. Try again in a moment.")
}
snap := d.Snapshot()
var routerNets []string
if d.Addresses != nil {
for _, x := range d.Addresses() {
routerNets = append(routerNets, x.CIDR)
}
}
t := i18n.Use(d.Catalogs, text(a["lang"]))
id, name, port := asString(a["id"]), "", 0
if id == "" {
id = "home"
}
err := write(d, func(c *uci.Config, m *model.Model) error {
// a section's name: a dash would make uci drop it silently
if !serverID.MatchString(id) {
return errors.New("Server ID may use lowercase letters, digits and underscores.")
}
if idTaken(m, id) {
return said{`"%s" is already taken.`, []any{id}}
}
network := parseFile("/etc/config/network")
ifname := asString(a["ifname"])
if ifname == "" {
ifname = freeIfname(m, network)
}
free := (network == nil || network.Section(ifname) == nil) && ifname != "dlx0" && ifname != "dll0"
for _, s := range m.Servers {
free = free && s.Ifname != ifname
}
if !free {
return said{"Network interface %s already exists.", []any{ifname}}
}
wanZone := asString(a["wan_zone"])
if wanZone == "" {
wanZone = "wan"
}
udp := udpHeld(m, &model.Server{WanZone: wanZone}, t)
port = freePort(udp)
if p := asString(a["port"]); a["port"] != nil && p != "" && p != "0" {
if !ValidPort(p) {
return errors.New("UDP port must be 1-65535.")
}
port, _ = strconv.Atoi(p)
}
for _, h := range udp {
if h["from"].(int) <= port && port <= h["to"].(int) {
return said{"UDP %d is used by %s. Choose another port.", []any{port, h["who"]}}
}
}
mtu := asString(a["mtu"])
if mtu == "0" {
mtu = ""
}
if n, _ := strconv.Atoi(mtu); mtu != "" && (!mtuArg.MatchString(mtu) || n < 1280 || n > 1500) {
return errors.New("MTU must be 1280-1500.")
}
preset := asString(a["preset"])
if preset == "" {
preset = defaultMethod(d)
}
if err := checkMethod(d, preset); err != nil {
return err
}
values := disguise.Preset(preset)
subnet := asString(a["subnet"])
pc, isNet := ipnet.Subnet(subnet)
if isNet {
subnet = pc.String()
// public addresses inside the tunnel would hide the real hosts behind them
if !privateNet(pc) {
return errors.New("Use a private range: 10.x.x.x, 172.16–31.x.x or 192.168.x.x.")
}
}
endpointHost := asString(a["endpoint"])
// the definition against the rest of the router, every fault at once
var errs []string
if !model.ValidIfname(ifname) {
errs = append(errs, t.T("Interface name: 1-12 lowercase letters, digits or _."))
}
if port < 1 || port > 65535 {
errs = append(errs, t.T("Port must be 1-65535."))
}
if !isNet {
errs = append(errs, t.T("Subnet must look like 10.77.0.0/24."))
} else {
taken := append([]string{}, routerNets...)
for _, sid := range m.ServerIDs() {
taken = append(taken, m.Servers[sid].Subnet)
}
for _, o := range taken {
if oc, ok := ipnet.Subnet(o); ok && uint64(pc.Base) < uint64(oc.Base)+oc.Size() && uint64(oc.Base) < uint64(pc.Base)+pc.Size() {
errs = append(errs, fmt.Sprintf(t.T("Subnet overlaps %s."), o))
}
}
}
if endpointHost != "" && !endpoint.ValidHost(endpointHost) {
errs = append(errs, t.T("Endpoint must be a hostname or IP address."))
}
o := map[string]string{}
for _, p := range values {
o[p.Key] = p.Value
}
errs = append(errs, disguise.Errors(o)...)
if len(errs) > 0 {
return said{"%s", []any{strings.Join(errs, " ")}}
}
name = asString(a["name"])
if name == "" {
name = "Home"
}
if !ValidName(name) {
return errors.New(`Server: Enter a name: up to 48 characters, without < > " or \.`)
}
keys := newKeys()
if !awg.ValidKey(keys.Public) || keys.Private == "" {
return errors.New("Key generation failed (is amneziawg-tools installed?).")
}
c.Add("settings", "settings")
c.Add("server", id)
set := func(k, v string) { c.Set(id, k, v) }
set("name", name)
set("ifname", ifname)
set("port", strconv.Itoa(port))
set("subnet", subnet)
set("endpoint", endpointHost)
set("mtu", mtu)
set("keepalive", "25")
set("private_key", keys.Private)
set("public_key", keys.Public)
set("preset", preset)
for _, p := range values {
set(p.Key, p.Value)
}
zone := func(k string) {
z := asString(a[k])
if z == "" {
z = strings.TrimSuffix(k, "_zone")
}
set(k, z)
}
zone("lan_zone")
zone("wan_zone")
set("auto_wan", "1")
set("auto_lan", "1")
set("detected_wan", snap.WAN)
set("detected_lan", snap.LAN)
// off unless asked for: the loop through the LAN costs speed
if on(a["lan_like"]) {
set("lan_like", "1")
} else {
set("lan_like", "0")
}
set("enabled", "1")
if a["portal_enabled"] != nil {
if on(a["portal_enabled"]) {
c.Set("settings", "portal_enabled", "1")
} else {
c.Set("settings", "portal_enabled", "0")
}
if p := asString(a["portal_port"]); a["portal_port"] != nil && p != "" && p != "0" {
if !ValidPort(p) {
return errors.New("Portal port must be 1-65535.")
}
n, _ := strconv.Atoi(p)
c.Set("settings", "portal_port", strconv.Itoa(n))
}
}
if host, _ := c.Section("settings").Get("portal_host"); endpointHost != "" && host == "" && endpoint.ValidHost(endpointHost) {
c.Set("settings", "portal_host", endpointHost)
}
// the portal as it will be, whatever this call changed
var blocked map[string]any
files.ReadJSON(d.RunDir+"/netifd-blocked.json", &blocked)
return checkPortal(model.Load(c, blocked), m, listening("tcp"), parseFile("/etc/config/firewall"))
})
if err != nil {
return nil, err
}
var applied map[string]any
if d.Apply != nil {
applied = d.Apply(text(a["lang"]))
}
if d.Log != nil {
d.Log.Write("dawnline", "info", "created", "", name, servicelog.Extra{By: "admin", Kind: "server", Note: "UDP " + strconv.Itoa(port), SID: id})
}
// netifd reads its protocol handlers when it starts: AmneziaWG installed after that needs a network restart
networkRestart := d.AWG != nil && d.AWG()["handler"] != true
if d.Restart != nil {
d.Restart(networkRestart)
}
if d.Told != nil {
d.Told()
}
if applied == nil {
applied = map[string]any{}
}
return map[string]any{"id": id, "applied": applied, "network_restart": networkRestart, "warning": applied["warning"]}, nil
}
+565
View File
@@ -0,0 +1,565 @@
package methods
import (
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/files"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
"github.com/tickcount/luci-app-dawnline/backend/journal"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/usage"
"github.com/tickcount/luci-app-dawnline/backend/view"
)
// Settings is every setting dawnline.schema knows, in its order: what settings_get shows as written.
var Settings = []string{"portal_enabled", "portal_host", "portal_alt", "portal_port", "portal_redirect", "portal_cert", "portal_key",
"default_device_limit", "hostnames", "domain", "geo_enabled", "geo_provider", "log_connections", "log_level", "watchdog", "log_keep_days",
"log_max", "log_persist", "usage_keep_days", "usage_keep_months", "home_dns_fallback", "update_check", "update_interval", "update_auto",
"update_mode", "update_time", "update_targets", "update_keep", "update_mirrors"}
// Reserved are the ports the router itself holds: nothing of Dawnline's may take them.
var Reserved = []int{22, 53, 80, 443}
func exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func size(path string) int64 {
st, err := os.Stat(path)
if err != nil {
return 0
}
return st.Size()
}
// luciACME is what tells luci-app-acme is there: its menu file, which every version names after the package, and
// its view where each version put it (view/acme.js before 25.12, view/acme/acme.js since). Looking for the old view
// alone said «not installed» of an installed one (the owner's screenshot, 2026-11-12).
var luciACME = []string{
"/usr/share/luci/menu.d/luci-app-acme.json",
"/www/luci-static/resources/view/acme/acme.js",
"/www/luci-static/resources/view/acme.js",
}
// anyExists tells whether one of the paths exists.
func anyExists(paths ...string) bool {
for _, p := range paths {
if exists(p) {
return true
}
}
return false
}
// Needs is the packages a feature needs that are missing, by feature (dawnline.health needs()): checked by
// files, so cheap enough for every page load.
func Needs() map[string][]string {
tls := false
for _, d := range []string{"/lib", "/usr/lib"} {
entries, _ := os.ReadDir(d)
for _, e := range entries {
tls = tls || strings.HasPrefix(e.Name(), "libustream-ssl.s")
}
}
rel, _ := os.ReadFile("/proc/sys/kernel/osrelease")
kver := strings.TrimSpace(string(rel))
kmod := func(name string) bool {
return exists("/sys/module/"+name) || exists("/lib/modules/"+kver+"/"+name+".ko")
}
type need struct {
has bool
pkg string
}
miss := func(list ...need) []string {
out := []string{}
for _, n := range list {
if !n.has {
out = append(out, n.pkg)
}
}
return out
}
return map[string][]string{
"https": miss(need{exists("/usr/bin/openssl"), "openssl-util"}, need{tls, "libustream-openssl"}),
"acme": miss(need{exists("/usr/lib/acme/client/acme.sh"), "acme"}, need{exists("/usr/lib/acme/client/dnsapi/dns_cf.sh"), "acme-acmesh-dnsapi"},
need{anyExists(luciACME...), "luci-app-acme"}),
"home": miss(need{exists("/usr/libexec/ip-full"), "ip-full"}, need{kmod("veth"), "kmod-veth"}),
"awg": miss(need{exists("/usr/bin/awg") && exists("/lib/netifd/proto/amneziawg.sh"), "amneziawg-tools"}, need{kmod("amneziawg"), "kmod-amneziawg"}),
}
}
func certOf(path string) *x509.Certificate {
raw, err := os.ReadFile(path)
if err != nil {
return nil
}
block, _ := pem.Decode(raw)
if block == nil {
return nil
}
c, _ := x509.ParseCertificate(block.Bytes)
return c
}
// 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:]
}
// PortalCert is the certificate the portal uses (dawnline.portalcert portal_cert): the one set, an ACME one
// for its address, or Dawnline's self-signed one (made by the service when it starts the portal).
func PortalCert(s model.Settings, dataDir string) (cert string, selfSigned bool) {
if s.PortalCert != "" && s.PortalKey != "" && exists(s.PortalCert) && exists(s.PortalKey) {
return s.PortalCert, false
}
if h := s.PortalHost; h != "" {
for _, base := range []string{"/etc/ssl/acme/" + h, "/etc/acme/" + h + "_ecc/" + h} {
if exists(base+".fullchain.crt") && exists(base+".key") {
return base + ".fullchain.crt", false
}
}
// an ACME certificate issued for several names, the portal's among them
entries, _ := os.ReadDir("/etc/ssl/acme")
for _, e := range entries {
base, is := strings.CutSuffix(e.Name(), ".fullchain.crt")
if !is || base == "" || !exists("/etc/ssl/acme/"+base+".key") {
continue
}
if c := certOf("/etc/ssl/acme/" + e.Name()); c != nil {
for _, n := range c.DNSNames {
if covers(n, h) {
return "/etc/ssl/acme/" + e.Name(), false
}
}
}
}
}
return dataDir + "/portal.crt", true
}
// PortalCertAndKey is the certificate and the key the portal serves with. Dawnline's own self-signed pair
// is made here when it is not there yet (run: a command runner, as the service has openssl for it).
func PortalCertAndKey(s model.Settings, dataDir string, run func(name string, args ...string) (string, int)) (cert, key string) {
cert, self := PortalCert(s, dataDir)
switch base, acme := strings.CutSuffix(cert, ".fullchain.crt"); {
case !self && cert == s.PortalCert:
return cert, s.PortalKey
case !self && acme:
return cert, base + ".key"
}
key = dataDir + "/portal.key"
if !exists(cert) || !exists(key) {
os.MkdirAll(dataDir, 0o700)
cn := s.PortalHost
if cn == "" {
cn = "dawnline"
}
run("openssl", "req", "-x509", "-newkey", "ec", "-pkeyopt", "ec_paramgen_curve:prime256v1", "-nodes", "-days", "3650", "-subj", "/CN="+cn, "-keyout", key, "-out", cert)
os.Chmod(key, 0o600)
}
return cert, key
}
var (
blanks = regexp.MustCompile(`[ \t]+`)
portSuffix = regexp.MustCompile(`:([0-9]+)$`)
portRange = regexp.MustCompile(`^(\d+)([-:](\d+))?$`)
listSep = regexp.MustCompile(`[ ,]+`)
)
// ourPortal tells a command line that is our own portal's: its uhttpd, or this backend told to serve it.
func ourPortal(cmd string) bool {
return strings.Contains(cmd, "/usr/share/dawnline/portal") ||
(strings.Contains(cmd, "/usr/libexec/dawnline/dawnlined") && strings.Contains(cmd, "-portal-port"))
}
type held struct {
port int
name string
}
// programs is who listens on which port of a protocol ("tcp", "udp"), in netstat's order, our own portal left out.
func programs(netstat, proto string, cmdline func(pid string) string) []held {
var out []held
seen := map[int]bool{}
for _, line := range strings.Split(netstat, "\n") {
f := blanks.Split(strings.TrimSpace(line), -1)
if len(f) < 4 || (f[0] != proto && f[0] != proto+"6") {
continue
}
m := portSuffix.FindStringSubmatch(f[3])
pid, name, has := strings.Cut(f[len(f)-1], "/")
if m == nil || !has || name == "" {
continue
}
port, _ := strconv.Atoi(m[1])
if seen[port] || ourPortal(cmdline(pid)) {
continue
}
seen[port] = true
out = append(out, held{port, name})
}
return out
}
type forward struct {
from, to int
name string
}
// forwards is the port forwards of a protocol from a zone that are not Dawnline's (dawnline.core
// wan_forwards): a forwarded port never reaches the router itself.
func forwards(fw *uci.Config, zone, want string) []forward {
var out []forward
if fw == nil {
return nil
}
for _, r := range fw.OfType("redirect") {
get := func(k string) string { v, _ := r.Get(k); return v }
target := get("target")
if target == "" {
target = "DNAT"
}
if strings.HasPrefix(r.Name, "dawnline_") || get("enabled") == "0" || target != "DNAT" || get("src") != zone {
continue
}
// fw4's default is "tcp udp"
proto := "tcp udp"
if v, ok := r.Values["proto"]; ok {
proto = v.Str
if v.IsList {
proto = strings.Join(v.List, " ")
}
}
takes := false
for _, p := range listSep.Split(proto, -1) {
takes = takes || p == want || p == "all" || p == "any"
}
if !takes {
continue
}
name := get("name")
if _, named := r.Values["name"]; !named {
name = r.Name
}
for _, p := range listSep.Split(get("src_dport"), -1) {
if m := portRange.FindStringSubmatch(p); m != nil {
from, _ := strconv.Atoi(m[1])
to := from
if m[3] != "" {
to, _ = strconv.Atoi(m[3])
}
out = append(out, forward{from, to, name})
}
}
}
return out
}
// tcpTaken is the TCP ports the portal cannot take and who holds them: the router's own, other programs,
// port forwards from WAN (dawnline.rpc.common tcp_taken). `who` in the page's language.
func tcpTaken(m *model.Model, t *i18n.Catalog, progs []held, fw *uci.Config) []map[string]any {
out := []map[string]any{}
seen := map[int]bool{}
hold := func(p int, who string) {
if !seen[p] {
seen[p] = true
out = append(out, map[string]any{"from": p, "to": p, "who": who})
}
}
for _, p := range Reserved {
hold(p, t.T("the router itself"))
}
for _, p := range progs {
hold(p.port, fmt.Sprintf(t.T("%s on this router"), p.name))
}
zones := []string{"wan"}
for _, id := range m.ServerIDs() {
z, dup := m.Servers[id].WanZone, false
for _, o := range zones {
dup = dup || o == z
}
if !dup {
zones = append(zones, z)
}
}
for _, z := range zones {
for _, f := range forwards(fw, z, "tcp") {
out = append(out, map[string]any{"from": f.from, "to": f.to, "who": fmt.Sprintf(t.T("port forward “%s”"), f.name)})
}
}
return out
}
func parseFile(path string) *uci.Config {
raw, err := os.ReadFile(path)
if err != nil {
return nil
}
c, _ := uci.Parse(string(raw))
return c
}
// logsInfo is what the log and the traffic history hold right now (Settings → Logs & history).
func logsInfo(w *view.World, d Deps) map[string]any {
var ev []map[string]any
if !files.ReadJSON(d.RunDir+"/events.json", &ev) {
files.ReadJSON(d.DataDir+"/events.json", &ev)
}
by := map[string]int{}
for _, c := range journal.Categories {
by[c] = 0
}
for _, e := range ev {
by[journal.CategoryOf(text(e["type"]))]++
}
days := map[string]bool{}
for _, group := range []map[string]*usage.Record{w.Usage.Devices, w.Usage.People} {
for _, rec := range group {
if rec != nil {
for k := range rec.Days {
days[k] = true
}
}
}
}
keys := make([]string, 0, len(days))
for k := range days {
keys = append(keys, k)
}
sort.Strings(keys)
since := ""
if len(keys) > 0 {
since = keys[0]
}
var oldest any = 0
if len(ev) > 0 {
oldest = ev[0]["ts"]
}
return map[string]any{
"policy": policy(w), "events": len(ev), "oldest": oldest, "by_category": by,
"log_ram": size(d.RunDir + "/events.json"), "log_flash": size(d.DataDir + "/events.json"), "usage_flash": size(d.DataDir + "/usage.json"),
"usage_since": since, "usage_days": len(keys), "geo_flash": size(d.DataDir + "/geo.json"),
}
}
// policy is what the log and the traffic history keep (dawnline.live policy()).
func policy(w *view.World) map[string]any {
pol, keep := journal.PolicyOf(w.M.Settings.Retention), usage.PolicyOf(w.M.Settings.Retention)
cats := pol.Categories
if cats == nil {
cats = []string{}
}
return map[string]any{"keep_days": pol.KeepDays, "max": pol.Max, "categories": cats, "persist": pol.Persist, "usage_days": keep.Days, "usage_months": keep.Months}
}
// settingsGet is the Settings page's data: the settings as written and as they take effect, and what the
// page shows beside them.
func settingsGet(w *view.World, t *i18n.Catalog, d Deps) map[string]any {
out := map[string]any{}
var section *uci.Section
if c := parseFile(d.Config); c != nil {
section = c.Section("settings")
}
for _, k := range Settings {
out[k] = ""
if section != nil {
if v, ok := section.Values[k]; ok {
out[k] = v.Str
if v.IsList {
out[k] = v.List
}
}
}
}
pol := policy(w)
out["log_category"] = pol["categories"]
var cert any
if s := w.M.Settings; s.PortalEnabled {
path, self := PortalCert(s, d.DataDir)
expires := ""
if c := certOf(path); c != nil {
// as `openssl x509 -enddate` writes it
expires = c.NotAfter.UTC().Format("Jan _2 15:04:05 2006") + " GMT"
}
cert = map[string]any{"path": path, "self_signed": self, "expires": expires}
}
services := []any{}
for _, id := range w.M.ServiceIDs() {
services = append(services, w.M.Services[id])
}
var geo struct {
IPs map[string]json.RawMessage `json:"ips"`
}
if !files.ReadJSON(d.RunDir+"/geo.json", &geo) {
files.ReadJSON(d.DataDir+"/geo.json", &geo)
}
return map[string]any{
"settings": out, "needs": Needs(), "effective": w.M.Settings, "services": services, "cert": cert,
"tcp_taken": tcpTaken(w.M, t, listening("tcp"), parseFile("/etc/config/firewall")),
"portal_public_port": w.M.Settings.PortalPort, "geo_cached": len(geo.IPs), "logs": logsInfo(w, d),
}
}
var backupFile = regexp.MustCompile(`^dawnline-([0-9]{8}-[0-9]{6})(-([a-z0-9-]+))?\.tar\.gz$`)
// backupRead is a backup's file, in base64 (the page saves it).
func backupRead(dir, name string) (map[string]any, error) {
if !backupFile.MatchString(name) {
return nil, errors.New("Invalid backup name.")
}
raw, err := os.ReadFile(dir + "/" + name)
if err != nil {
return nil, errors.New("Backup not found.")
}
return map[string]any{"data": base64.StdEncoding.EncodeToString(raw)}, nil
}
// listening is the programs that listen on the router now, by netstat.
func listening(proto string) []held {
flag := "-lntp"
if proto == "udp" {
flag = "-lnup"
}
netstat, _ := exec.Command("netstat", flag).Output()
return programs(string(netstat), proto, func(pid string) string {
raw, _ := os.ReadFile("/proc/" + pid + "/cmdline")
return string(raw)
})
}
// " 12: 00000000:1194 00000000:0000 07 …": the local address, then the remote one (none).
var udpUnconnected = regexp.MustCompile(`^ *[0-9]+: [0-9A-F]+:([0-9A-F]{4}) [0-9A-F]+:0000 `)
// udpBound is the UDP ports an unconnected socket is bound to, as /proc/net/udp and udp6 list them: nothing
// else can listen there.
func udpBound(tables ...string) []int {
var out []int
seen := map[int]bool{}
for _, t := range tables {
for _, line := range strings.Split(t, "\n") {
if m := udpUnconnected.FindStringSubmatch(line); m != nil {
p, _ := strconv.ParseInt(m[1], 16, 32)
if !seen[int(p)] {
seen[int(p)] = true
out = append(out, int(p))
}
}
}
}
return out
}
// udpTaken is the UDP ports a server cannot listen on and who holds them: other servers, tunnel interfaces,
// programs on the router and port forwards from its WAN zone (dawnline.rpc.servers udp_taken). Its own port
// is not counted.
func udpTaken(m *model.Model, srv *model.Server, t *i18n.Catalog, network, fw *uci.Config, bound []int, progs []held) []map[string]any {
out := []map[string]any{}
seen := map[int]bool{}
hold := func(p int, who string) {
if p > 0 && !seen[p] {
seen[p] = true
out = append(out, map[string]any{"from": p, "to": p, "who": who})
}
}
for _, id := range m.ServerIDs() {
if o := m.Servers[id]; id != srv.ID {
hold(o.Port, fmt.Sprintf(t.T("server “%s”"), o.Name))
}
}
if network != nil {
for _, s := range network.OfType("interface") {
if lp, _ := s.Get("listen_port"); lp != "" && s.Name != srv.Ifname {
p, _ := strconv.Atoi(strings.TrimSpace(lp))
hold(p, fmt.Sprintf(t.T("interface %s"), s.Name))
}
}
}
names := map[int]string{}
for _, p := range progs {
names[p.port] = p.name
}
for _, p := range bound {
if p == srv.Port {
continue
}
if n := names[p]; n != "" {
hold(p, fmt.Sprintf(t.T("%s on this router"), n))
} else {
hold(p, t.T("another program on this router"))
}
}
for _, f := range forwards(fw, srv.WanZone, "udp") {
out = append(out, map[string]any{"from": f.from, "to": f.to, "who": fmt.Sprintf(t.T("port forward “%s”"), f.name)})
}
return out
}
// Address is one IPv4 address of a netifd interface, with its prefix ("192.168.1.1/24").
type Address struct {
Iface, CIDR string
}
// takenNetworks is the networks a tunnel network must not overlap, by name: the router's and the servers'.
func takenNetworks(m *model.Model, addrs []Address) []map[string]any {
out := []map[string]any{}
seen := map[string]bool{}
add := func(name, cidr string) {
if n, ok := ipnet.Subnet(cidr); ok && !seen[n.String()] {
seen[n.String()] = true
out = append(out, map[string]any{"name": name, "cidr": n.String()})
}
}
for _, a := range addrs {
add(a.Iface, a.CIDR)
}
for _, id := range m.ServerIDs() {
add(m.Servers[id].Name, m.Servers[id].Subnet)
}
return out
}
// serverPorts is what the ports and the network of an existing server must avoid (the AmneziaWG card checks
// as you type).
func serverPorts(w *view.World, t *i18n.Catalog, d Deps, id string) map[string]any {
srv := w.M.Servers[id]
if srv == nil {
return map[string]any{"error": t.T("Unknown server.")}
}
fw := parseFile("/etc/config/firewall")
udp, _ := os.ReadFile("/proc/net/udp")
udp6, _ := os.ReadFile("/proc/net/udp6")
var addrs []Address
if d.Addresses != nil {
addrs = d.Addresses()
}
networks := []map[string]any{}
for _, n := range takenNetworks(w.M, addrs) {
// everything but this server's own
if n["cidr"] != srv.Subnet {
networks = append(networks, n)
}
}
return map[string]any{
"udp_taken": udpTaken(w.M, srv, t, parseFile("/etc/config/network"), fw, udpBound(string(udp), string(udp6)), listening("udp")),
"tcp_taken": tcpTaken(w.M, t, listening("tcp"), fw), "networks": networks,
}
}
+258
View File
@@ -0,0 +1,258 @@
package methods
import (
"fmt"
"math/rand/v2"
"os"
"os/exec"
"sort"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"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/roster"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/view"
)
// DefaultPort is the usual AmneziaWG port.
const DefaultPort = 4500
// ReservedIDs may name nothing in the settings; portalPaths are the portal's own, which a server's id (the
// first part of nothing else) must not be either.
var (
ReservedIDs = []string{"settings", "main", "global", "defaults"}
portalPaths = []string{"api", "assets", "cgi-bin"}
)
func within(taken []map[string]any, p int) bool {
for _, t := range taken {
if t["from"].(int) <= p && p <= t["to"].(int) {
return true
}
}
return false
}
// randPort draws a high port. A test puts its own in its place.
var randPort = func() int { return 40000 + rand.IntN(20001) }
// freePort is the usual port when free, otherwise a random high one.
func freePort(taken []map[string]any) int {
if !within(taken, DefaultPort) {
return DefaultPort
}
for i := 0; i < 50; i++ {
if p := randPort(); !within(taken, p) {
return p
}
}
return 51830
}
// freeSubnet is a /24 that overlaps none of the networks taken: 10.x first, past 200 tries the 172.16-31 range.
func freeSubnet(taken []string) string {
for i := 0; i < 300; i++ {
cand := fmt.Sprintf("10.%d.%d.0/24", 60+rand.IntN(191), rand.IntN(251))
if i >= 200 {
cand = fmt.Sprintf("172.%d.%d.0/24", 16+rand.IntN(16), rand.IntN(251))
}
c, _ := ipnet.Subnet(cand)
bad := false
for _, t := range taken {
if n, ok := ipnet.Subnet(t); ok && uint64(c.Base) < uint64(n.Base)+n.Size() && uint64(n.Base) < uint64(c.Base)+c.Size() {
bad = true
break
}
}
if !bad {
return c.String()
}
}
return "10.77.0.0/24"
}
// freePortalPort is a portal port free together with the next one (plain HTTP for IP links): first (the
// server's UDP number) before the usual ones.
func freePortalPort(first, cur int, taken []map[string]any) int {
if cur == 0 {
cur = 8443
}
for _, p := range []int{first, cur, 8443, 9443, 10443, 7443, 4443} {
if !within(taken, p) && !within(taken, p+1) {
return p
}
}
return 9443
}
func idTaken(m *model.Model, id string) bool {
for _, r := range append(append([]string{}, ReservedIDs...), portalPaths...) {
if r == id {
return true
}
}
return m.Servers[id] != nil || m.People[id] != nil || m.Devices[id] != nil || m.Services[id] != nil || m.Circles[id] != nil
}
// freeServerID is home, then server2, server3, … (never an id anything else has).
func freeServerID(m *model.Model) string {
if !idTaken(m, "home") {
return "home"
}
for n := 2; ; n++ {
if id := fmt.Sprintf("server%d", n); !idTaken(m, id) {
return id
}
}
}
// freeIfname is a network section name nothing uses: not the router's, not Dawnline's own links, not another server's.
func freeIfname(m *model.Model, network *uci.Config) string {
for i := 0; i < 10; i++ {
n := fmt.Sprintf("dawn%d", i)
free := network == nil || network.Section(n) == nil
for _, s := range m.Servers {
free = free && s.Ifname != n
}
if free {
return n
}
}
return "dawnx"
}
// wanKind is what kind of IPv4 the WAN has: public, cgnat (100.64/10), private (behind another router), none.
func wanKind(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"
}
// setupDefaults is what a new server starts from (dawnline.rpc.servers setup_defaults): a free id, name,
// interface, port and network, the address to reach the router by and where it came from, the zones.
func setupDefaults(w *view.World, t *i18n.Catalog, d Deps) map[string]any {
m := w.M
var addrs []Address
if d.Addresses != nil {
addrs = d.Addresses()
}
networks := takenNetworks(m, addrs)
fw := parseFile("/etc/config/firewall")
zones := []any{}
names := []string{}
if fw != nil {
for _, z := range fw.OfType("zone") {
// as ucode lists them: a zone with no name is a null in the list
if n, ok := z.Get("name"); ok {
zones = append(zones, n)
names = append(names, n)
} else {
zones = append(zones, nil)
names = append(names, "")
}
}
}
has := func(n string) bool {
for _, z := range names {
if z == n {
return true
}
}
return false
}
snap := d.Snapshot()
wanIP := ""
if dev := snap.Devices[snap.WAN]; dev != nil && len(dev.Addrs) > 0 {
wanIP = strings.SplitN(dev.Addrs[0], "/", 2)[0]
}
kind := wanKind(wanIP)
host, from := m.Settings.PortalHost, ""
if host != "" {
from = "portal"
} else {
// a DDNS name, then an ACME certificate's, then the WAN address, but never a private or CGNAT one
if ddns := parseFile("/etc/config/ddns"); ddns != nil {
for _, s := range ddns.OfType("service") {
lookup, _ := s.Get("lookup_host")
if on, _ := s.Get("enabled"); host == "" && lookup != "" && on == "1" {
host, from = lookup, "ddns"
}
}
}
if host == "" {
entries, _ := os.ReadDir("/etc/ssl/acme")
var certs []string
for _, e := range entries {
if base, is := strings.CutSuffix(e.Name(), ".fullchain.crt"); is {
certs = append(certs, base)
}
}
sort.Strings(certs)
if len(certs) > 0 && certs[0] != "" {
host, from = certs[0], "cert"
}
}
if host == "" && kind == "public" {
host, from = wanIP, "wan"
}
}
// the zones the WAN and LAN devices are in, whatever they are called
zoneOf := func(dev, usual string, fallback int) any {
if x := snap.Devices[dev]; x != nil && x.Zone != "" {
return x.Zone
}
if has(usual) {
return usual
}
if fallback < len(zones) {
return zones[fallback]
}
return nil
}
wanZone, lanZone := zoneOf(snap.WAN, "wan", 1), zoneOf(snap.LAN, "lan", 0)
network := parseFile("/etc/config/network")
udp, _ := os.ReadFile("/proc/net/udp")
udp6, _ := os.ReadFile("/proc/net/udp6")
// no server is being edited: every server's port counts
wz, _ := wanZone.(string)
udpHeld := udpTaken(m, &model.Server{WanZone: wz}, t, network, fw, udpBound(string(udp), string(udp6)), listening("udp"))
tcpHeld := tcpTaken(m, t, listening("tcp"), fw)
port := freePort(udpHeld)
name := t.T("Home")
if n := len(m.Servers); n > 0 {
name = fmt.Sprintf(t.T("Server %d"), n+1)
}
cidrs := []string{}
for _, n := range networks {
cidrs = append(cidrs, n["cidr"].(string))
}
portal := m.Settings.PortalPort
if !m.Settings.PortalEnabled {
portal = freePortalPort(port, m.Settings.PortalPort, tcpHeld)
}
// an installed module that is not loaded yet is loaded: nothing loads it before the first tunnel comes up
state := roster.AwgState(d.Handler)
if state["kmod"] == true && state["loaded"] == false {
exec.Command("modprobe", "amneziawg").Run()
state = roster.AwgState(d.Handler)
}
return map[string]any{
"id": freeServerID(m), "name": name, "ifname": freeIfname(m, network), "port": port, "subnet": freeSubnet(cidrs),
"endpoint": host, "endpoint_from": from, "wan_kind": kind, "wan_ip": orNull(wanIP), "mtu": 0,
"zones": zones, "lan_zone": lanZone, "wan_zone": wanZone, "portal_port": portal,
"awg": awg.SystemCaps(d.DataDir), "awg_state": state, "networks": networks, "udp_taken": udpHeld, "tcp_taken": tcpHeld,
}
}
+13
View File
@@ -0,0 +1,13 @@
{
"config": "config 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 public_keyLine truncated
"archives": [
{
"name": "dawnline-20261112-050743.tar.gz",
"data": "H4sIAAAAAAAAA+1ZTW/bOBD1tfkVgi+6dBXJku14gR7S9CNts8kCe9jDYmFQFG2zpkhBkp01gvz3pWypJilS4hbeFAX8LpLJ0cybEYePUVAJLyGjC7y8TMAjJZiiwanhc0yiaH/lUK5BMAnDQTAeTcejKJqE04EfhGEUDhz/5Ew02BQlyB1nkDNWdtn1zf+kOLx7p0BliemycNzmzr14xbISM+pkLC8BmSMKYoISxw1aU9XFcaOx77emVqzgU9uMeugfkGYEeSxfciuC+XhtAwg3Gfmh53tBEHpT9+LiG618i3LHXbEUHV1TkCLHvZXG8OIwWi1ihYXKrNjEFPHRwPemUx7TvxxFwhM53oISzddo57jAGtdvBBebmGB48BBbQ/QANiWbE0Ad129qxX/NE7TFsEqS1AVpzcT5L/zn0VGKipXjQpxDgoRXWj1C8Brt/avFhiDGVK32jTwoljtolXtiKvdVd7mhNUzlTqzxPeWWisoWi3btWIZapXuQxsTKjVqVm5oqN+uuHLKGqXILa/yHyumXIyDkWLnD2uTxQSpk3tH3H0CKya7t4JHlazsPf+4txVe3J0tBoVreX/8h5JokOSr4FhnMRl4wuap2j7G4lfGpKFJHgnCmiQXFbJsOS1EOOsKNfS8U2jVDecHNXEApsMv7WrIEEO7dx6xcHUel16GGilmOC7tYb2XT7wgGCcgtE7uRTZtgmJYo5x3U9p3g1NL1O8my8XxY1PvXXD0037dps02owdDWMtb7bXcoZaXKY1BXwwVKWG7ZVrJp9wt7lDqoibYkKLYL9lGy1MfKwKaojxtqoBXQLXmtbt0C85pX3fL9lFq6/SSZdrtdY9061inFF6xbxnqvBG3tnN6Jht0+KbPtuHtmZqrtbrVZGmVoKDSKka0YFaLpN7kDh99l0+N+WZ/rRHUVhO6tNSqhU/kRkJUssyN4p9i2GIZ6hjfWEKW40t14l4F9gOPwGlPeRJCl2Ybvhu2EGEnssnkQDVupRPpU3llDTCXBxfFvDZVwbFgiijjVMpT1LJKxnvd7a+gWCTRQVCStFq8+ihM9xQ/W0FFMDBRlZaw1sI/hVM/wozV0DJGBoSSntXD2EbzSE7y1ho7gwkBQEd1aXvsozvQUP1lDR3FpoCgrda3JfQwDX0/xszV0FFcGirLG12LeRfHKvOV/sYaOITYwlI8L9bmgj6Fhy7+zho7h2sBQPnnUZ4wuhjNzDX+zhlY2DQylU0ytmn0EDSW8t4aOIDUQlI9E9eGnt1MCPcUHa1QUX+T7Hyrhtw+/l5sCLJH3lSd+0hjd3385xpHy/TcKovH5++9L4Gl46IBi+OvT8+thhlhG0OE+BQm/G8Y7ZwMZv33+0VzPOD2k/kdbRMvi5BtAb/+3/v8ThdHo3P8vgb+ehiXv/fHrYbnLqnbnukgRLIfPf/9oamecccYZZ/yP+BdCElAAACIAAA=="
},
{
"name": "dawnline-20261112-050743-pre-update.tar.gz",
"data": "H4sIAAAAAAAAA+2YwXKbMBCGfW2eghunOmCwHR96SNM2SZsmnemhR48A2dZYSIzATvP2FTbUkpBgm8lMp538FydivftpkfQrwVV6nnK2IuvzDD0yShgevbQCqVkcHz6ljM8wnEXRKJxO5tNJHM+i+SgIoyiORl7w4iQW7coKCc8bCc6rvrih5/+oju/eK3FVEbYuPb/9yT97w4uKcOYVXFSILjFDCcWZ54edR/WH58fTIOg82vBSPtoXbIx/orygeMzFWkZRIsebGERlyCSIxsE4DKPx3D87+40l9lh4/obn+JSaoRx7/o02RlbH0XoRGxQmWblLGJajYTCez2XN4HwSK98QZI8qvNziJ89HYF2+U1LsEkrSY4YELDUD2lV8SRHz/KDtlfxtmeE9SetJ0qYhnSeJeCt/PSXKcbnx/JSIlGLlldZfoWSLD/nNZqcoIczs9pU+qLY77LR75mr3RX+7U7Bc7c7Aek67taby1arbO17gTusetDG1c5NO5+auzi36O4fBcnVuBdYfdM6+HBGlp84d16asj3Jl5j37/hPKCX3qJnjkYgvL8OMQqb66AyxDpRl5f/ldmWuWCVzKIzJcTMbh7KI+PabqUSYfxbE5EkYLS61UnW27w3IsUE+5aTCOlO1aYFHKMB8xhmDzvtQiUZoe0ie82pxGtddhlkq4ICWs1ns99BnFUooEcGJXemhbjLAKC7mDurkzkgNTf9Ai28zHRX14zfWXlodt2h4TZjG8B9b6uO8vZaxUfSy19XCFMy6A20oP7X9hj9oOaqutKU5gxa61SHutAu3K5rphFtog25K3+tYNcq95M608Txkw7a0W2p92S2zr2OYUX4htGduzUryHJb1TA/tzMg7dcffcTWrd3eZmaZ2hRWgdo9hwplSzH3JHhm966Om8bO51qrsqRvcerNroTD6KiooXMMA7I7ZDGNkJr8BSrbj23eSpQIcCp+EtYXITpTwvdvI07E6I0ww2mwc1sDOV2D6VD2CpU8lIefpbwwROHEvEMKfGhoqBRTK1c38Ey7ZIUgeiYWmNeQ0hzuyIn8CyIWYORN0ZGw8cIpzbCa/BshFiB6Fmp41xDgFe2AFvwLIBrhyAhuk29jqEuLAj3oJlQ1w7EHWnbjx5iDAM7IifwbIhbhyIusc3Zt6HeOE+8r+AZSMkDkL9utDcC4YIHUf+HVg2wq2DUL95NHeMPsKFu4dfwbLapoNQu8U0rjkE6GjhPVg2QOYA1K9EzeVncKeEdsQHsGrEv/2vwVe96lX/uX4BGc9cFAAaAAA="
}
]
}
File diff suppressed because it is too large. Load diff
+55
View File
@@ -0,0 +1,55 @@
package methods
import (
"errors"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// uninstall removes Dawnline's state from the router (the package is removed by the page afterwards): a
// backup first, then the settings emptied but for the settings section, then everything Dawnline made on
// the router taken away, then the service stopped.
//
// The settings go first: whatever applies in between (the agent's tick) then works from settings with
// nothing in them, and puts nothing back. The service stops last, once the answer is out: this process is
// part of it.
func uninstall(d Deps, confirm string) (map[string]any, error) {
if confirm != "remove" {
return nil, errors.New(`Type "remove" to confirm.`)
}
// not in the middle of an update (the package manager may be installing Dawnline itself)
if updating(d.RunDir) {
return nil, errors.New("An update is running: wait for it to finish, then remove Dawnline.")
}
if d.Teardown == nil || d.Store == nil {
return nil, errors.New("Dawnline cannot be removed from here.")
}
backupCreate(d, "pre-remove")
var was *model.Model
err := d.Store.Transact(func(c *uci.Config, m *model.Model) error {
was = m
var rm []string
for _, s := range c.Sections {
if s.Type != "settings" {
rm = append(rm, s.Name)
}
}
for _, n := range rm {
c.DeleteSection(n)
}
if c.Section("settings") == nil {
c.Add("settings", "settings")
}
c.Set("settings", "portal_enabled", "0")
return nil
})
if err != nil {
return nil, err
}
d.Teardown(was)
if d.Stop != nil {
d.Stop()
}
return map[string]any{"ok": true}, nil
}
+53
View File
@@ -0,0 +1,53 @@
package methods
import (
"os"
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/store"
"github.com/tickcount/luci-app-dawnline/backend/view"
)
// Removing Dawnline: only on the word, a backup first, the settings left with their own section alone,
// the router cleaned from the settings as they were, the service stopped last.
func TestUninstall(t *testing.T) {
dir := t.TempDir()
text, _ := os.ReadFile("../access/testdata/access.uci")
os.WriteFile(dir+"/dawnline", text, 0o600)
var order []string
var servers int
d := Deps{World: func() *view.World { return &view.World{} }, Store: &store.Store{Config: dir + "/dawnline", RunDir: dir}, Config: dir + "/dawnline", RunDir: dir, DataDir: dir + "/data",
Teardown: func(m *model.Model) {
servers = len(m.Servers)
now, _ := os.ReadFile(dir + "/dawnline")
if strings.Contains(string(now), "config server") {
t.Error("the router is cleaned while the settings still hold servers")
}
order = append(order, "teardown")
},
Stop: func() { order = append(order, "stop") }}
if res := ask(t, d, "uninstall", map[string]any{"confirm": "yes"}); res["error"] != `Type "remove" to confirm.` || len(order) != 0 {
t.Fatalf("removed without the word: %v", res)
}
if now, _ := os.ReadFile(dir + "/dawnline"); string(now) != string(text) {
t.Fatal("the settings changed without the word")
}
res := ask(t, d, "uninstall", map[string]any{"confirm": "remove"})
if res["ok"] != true || strings.Join(order, " ") != "teardown stop" || servers != 3 {
t.Fatalf("not removed as it should be: %v, %v, %d servers", res, order, servers)
}
now, _ := os.ReadFile(dir + "/dawnline")
if strings.Count(string(now), "\nconfig ") != 1 || !strings.Contains(string(now), "config settings 'settings'") || !strings.Contains(string(now), "option portal_enabled '0'") {
t.Errorf("the settings after:\n%s", now)
}
backups, _ := os.ReadDir(dir + "/data/backups")
found := false
for _, b := range backups {
found = found || strings.Contains(b.Name(), "pre-remove")
}
if !found {
t.Errorf("no backup before the removal: %v", backups)
}
}
+233
View File
@@ -0,0 +1,233 @@
package methods
import (
"errors"
"os"
"regexp"
"sort"
"strings"
"syscall"
"time"
backend "github.com/tickcount/luci-app-dawnline/backend"
"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/view"
)
// Repo is where Dawnline's releases are.
const Repo = "tickcount/luci-app-dawnline"
var (
releaseLine = regexp.MustCompile(`^DISTRIB_(RELEASE|TARGET|ARCH)='([^']*)'`)
catalogFile = regexp.MustCompile(`^dawnline\.([A-Za-z_-]+)\.lmo$`)
)
// updating tells whether an update job runs: its worker holds the update lock for the whole job.
func updating(run string) bool {
f, err := os.OpenFile(run+"/update.lock", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0o644)
if err != nil {
return true
}
defer f.Close()
if syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) != nil {
return true
}
syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
return false
}
// kept is the version a component had before its last update, when its files are still there.
func kept(dir, component, firmware string) any {
var k struct {
Version string `json:"version"`
Build any `json:"build"`
Firmware string `json:"firmware"`
Files []string `json:"files"`
Saved any `json:"saved"`
}
if !files.ReadJSON(dir+"/"+component+"/meta.json", &k) || k.Version == "" || len(k.Files) == 0 {
return nil
}
for _, f := range k.Files {
if !exists(f) {
return nil
}
}
// a kept AmneziaWG build is for one firmware only
if component == "amneziawg" && k.Firmware != "" && firmware != "" && k.Firmware != firmware {
return nil
}
return map[string]any{"version": k.Version, "build": k.Build, "saved": k.Saved}
}
// updatesStatus is the Components tab's data (dawnline.rpc.updates status): what is installed, the last
// check and job, the policy, what is kept for a rollback.
func updatesStatus(w *view.World, t *i18n.Catalog, d Deps) map[string]any {
rel := map[string]string{}
release, _ := os.ReadFile("/etc/openwrt_release")
for _, line := range strings.Split(string(release), "\n") {
if m := releaseLine.FindStringSubmatch(line); m != nil {
rel[strings.ToLower(m[1])] = m[2]
}
}
home := false
for _, s := range w.M.Servers {
home = home || s.LanLike
}
var check, job any
files.ReadJSON(d.RunDir+"/update-check.json", &check)
files.ReadJSON(d.RunDir+"/update-job.json", &job)
// a running job whose worker is gone reads as failed
if j, ok := job.(map[string]any); ok && j["state"] == "running" && !updating(d.RunDir) {
j["state"], j["error"] = "failed", t.T("The update stopped unexpectedly.")
if j["finished"] == nil {
j["finished"] = time.Now().Unix()
}
}
auto := map[string]any{}
files.ReadJSON(d.DataDir+"/update-auto.json", &auto)
var history []any
files.ReadJSON(d.DataDir+"/update-history.json", &history)
if history == nil {
history = []any{}
}
if len(history) > 12 {
history = history[:12]
}
// this firmware as the AmneziaWG builds name it
firmware := ""
if d.Board != nil {
r, _ := d.Board()["release"].(map[string]any)
target := strings.Split(text(r["target"]), "/")
arch := text(r["arch"])
if arch == "" {
arch = rel["arch"]
}
if v := text(r["version"]); v != "" && len(target) == 2 && arch != "" {
firmware = "_v" + v + "_" + arch + "_" + target[0] + "_" + target[1]
}
}
langs := []string{}
entries, _ := os.ReadDir(d.Catalogs)
for _, e := range entries {
if m := catalogFile.FindStringSubmatch(e.Name()); m != nil {
langs = append(langs, m[1])
}
}
sort.Strings(langs)
tunnels := []string{}
if network := parseFile("/etc/config/network"); network != nil {
for _, s := range network.OfType("interface") {
proto, _ := s.Get("proto")
if off, _ := s.Get("disabled"); proto != "amneziawg" || off == "1" {
continue
}
name := s.Name
if n, ok := s.Get("_dawnline_name"); ok {
name = n
}
tunnels = append(tunnels, name)
}
}
return map[string]any{
"version": backend.Version,
"firmware": map[string]any{"version": rel["release"], "target": rel["target"], "arch": rel["arch"]},
"portal": w.M.Settings.PortalEnabled, "home": home, "needs": Needs(), "check": check, "job": job, "now": time.Now().Unix(), "repo": Repo,
"policy": w.M.Settings.Updates, "auto": auto,
"kept": map[string]any{"app": kept(d.DataDir+"/keep", "app", firmware), "amneziawg": kept(d.DataDir+"/keep", "amneziawg", firmware)},
"translations": langs, "history": history, "tunnels": tunnels,
}
}
// The AmneziaWG builds' repositories (dawnline.awg_builds SOURCES).
var awgRepos = map[string]string{"slava": "Slava-Shchipunov/awg-openwrt", "grey": "2Grey/awg-openwrt"}
var tagName = regexp.MustCompile(`^v?[0-9][0-9A-Za-z.+~-]{0,40}$`)
// shown is a release as the page gets it: what only the updater needs is left out.
func shown(r map[string]any, more map[string]any) map[string]any {
out := map[string]any{}
for k, v := range r {
out[k] = v
}
out["digests"], out["ids"] = nil, nil
for k, v := range more {
out[k] = v
}
return out
}
// releases is Dawnline's releases for the Versions list, five a page.
func releases(gh *github.Client, page any) (map[string]any, error) {
n := 1.0
if page != nil {
n = num(page)
}
if n != float64(int(n)) || n < 1 || n > 50 {
return nil, errors.New("Unknown page.")
}
list, err := gh.Releases(Repo, int(n))
if err != nil {
return nil, err
}
out := []any{}
for _, r := range list {
m, _ := r.(map[string]any)
out = append(out, shown(m, map[string]any{"body": nil}))
}
return map[string]any{"releases": out, "page": int(n)}, nil
}
// notes is a release's notes for What's new: Dawnline's by tag, or an AmneziaWG build's.
func notes(gh *github.Client, component, tag, build string) (map[string]any, error) {
repo := Repo
if component == "amneziawg" {
if build == "" {
build = "slava"
}
repo = awgRepos[build]
}
if repo == "" || !tagName.MatchString(tag) {
return nil, errors.New("Unknown release.")
}
r, err := gh.Release(repo, tag)
if err == nil && r == nil && component != "amneziawg" && !strings.HasPrefix(tag, "v") {
r, err = gh.Release(repo, "v"+tag)
}
if err != nil {
return nil, err
}
if r == nil {
return nil, said{"No release %s in %s.", []any{tag, repo}}
}
return shown(r, map[string]any{"repo": repo}), nil
}
// changes is what changed between the installed Dawnline and a release, as points.
func changes(gh *github.Client, from, to string) (map[string]any, error) {
if !tagName.MatchString(to) || (from != "" && !tagName.MatchString(from)) {
return nil, errors.New("Unknown release.")
}
// the installed version as a tag when it was released (a build ahead of the releases has none)
base := ""
if from != "" {
bare := strings.TrimPrefix(from, "v")
for _, t := range []string{"v" + bare, bare} {
if r, err := gh.Release(Repo, t); err != nil {
return nil, err
} else if base == "" && r != nil {
base = t
}
}
}
items, err := gh.Changes(Repo, base, to)
if err != nil {
return nil, err
}
return map[string]any{"items": items}, nil
}
// Updating tells whether an update job runs.
func Updating(run string) bool { return updating(run) }
+115
View File
@@ -0,0 +1,115 @@
package methods
import (
"os"
"os/exec"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/apply"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/roster"
"github.com/tickcount/luci-app-dawnline/backend/watchdog"
)
// sysSaid is the last lines of syslog that explain a failure of something.
func sysSaid(pattern, only string, n string) string {
out, _ := exec.Command("sh", "-c", "logread -l 400 2>/dev/null | grep -E "+shq(pattern)+" | grep -iE "+shq(only)+" | tail -n "+n).Output()
return strings.Trim(string(out), " \t\r\n\v\f")
}
func shq(s string) string { return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" }
// WatchdogFacts reads the router for the watchdog's probes (package watchdog): r is the router an apply
// works on, reapply the full apply.
func WatchdogFacts(d Deps, m *model.Model, r *apply.Router, reapply func()) watchdog.Facts {
st := roster.AwgState(d.Handler)
f := watchdog.Facts{Tools: st["tools"] == true, Kmod: st["kmod"] == true, Loaded: st["loaded"] == true, Handler: st["handler"] == true}
for _, p := range st["missing"].([]any) {
f.Missing = append(f.Missing, p.(string))
}
f.Owner = r.NetifdOwner
f.Iface = func(ifname string) watchdog.Iface {
if d.Bus == nil {
return watchdog.Iface{}
}
s := d.Bus("network.interface."+ifname, "status", nil)
return watchdog.Iface{Exists: s != nil, Up: s != nil && s["up"] == true}
}
f.IfaceErrors = func(ifname string) string {
var parts []string
if d.Bus != nil {
var errs []string
list, _ := d.Bus("network.interface."+ifname, "status", nil)["errors"].([]any)
for _, e := range list {
if em, ok := e.(map[string]any); ok {
one := text(em["code"])
if data, _ := em["data"].([]any); len(data) > 0 {
var words []string
for _, w := range data {
words = append(words, text(w))
}
one += " (" + strings.Join(words, " ") + ")"
}
errs = append(errs, one)
}
}
if len(errs) > 0 {
parts = append(parts, "netifd: "+strings.Join(errs, ", "))
}
}
// only what explains a failure, not the ordinary "is now up/down" lines
if lines := sysSaid(ifname+"|amneziawg", "error|fail|invalid|unrecogni|cannot|unable|not found|refused|denied", "6"); lines != "" {
parts = append(parts, lines)
}
return strings.Join(parts, "\n")
}
udp, _ := os.ReadFile("/proc/net/udp")
udp6, _ := os.ReadFile("/proc/net/udp6")
bound := udpBound(string(udp), string(udp6))
f.UDPListening = func(port int) bool {
for _, p := range bound {
if p == port {
return true
}
}
return false
}
if f.Tools && f.Kmod && f.Loaded && f.Handler {
f.Drift = r.NetifdDrift(m)
}
f.NftPresent = r.Present()
keyed := false
for _, srv := range m.Servers {
keyed = keyed || srv.PrivateKey != ""
}
if keyed {
f.Firewall = r.FirewallDrift(m)
}
if s := m.Settings; s.PortalEnabled {
others := false
for _, h := range listening("tcp") {
others = others || h.port == s.PortalPort
}
tcp, _ := os.ReadFile("/proc/net/tcp")
tcp6, _ := os.ReadFile("/proc/net/tcp6")
f.PortalCan, f.PortalListening = !others, tcpListening(s.PortalPort, string(tcp), string(tcp6))
}
f.ResolverConf = exists("/var/etc/dawnline-dns.conf")
f.ResolverRunning = r.ResolverRunning()
f.Said = func(what string) string {
switch what {
case "modprobe":
out, _ := exec.Command("sh", "-c", "modprobe amneziawg 2>&1; dmesg 2>/dev/null | grep -i amneziawg | grep -iE 'error|fail|invalid|unknown|disagrees|taint' | tail -n 4").Output()
return strings.Trim(string(out), " \t\r\n\v\f")
case "portal":
return sysSaid("dawnline", "portal.*(error|fail|cert|key|bind|address)", "4")
}
return ""
}
f.Reapply = reapply
f.Run = func(name string, args ...string) { exec.Command(name, args...).Run() }
// procd keeps unchanged instances and starts missing ones
f.StartServices = func() { exec.Command("/etc/init.d/dawnline", "start").Run() }
f.BlockedNote, f.BlockedFix = apply.BlockedNote, apply.BlockedFix
return f
}
+31
View File
@@ -0,0 +1,31 @@
package methods
import (
"unicode"
"unicode/utf8"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
)
// Unworded is what a person is told for a failure that has no sentence of its own.
const Unworded = "Something went wrong on the router. Try again; if it goes on, look at the Log."
// Worded tells whether a failure's text is a sentence for a person: one of ours begins with a capital letter, an
// error Go wrote for programmers never does (Go's own convention: "open /etc/config/dawnline: no such file").
func Worded(msg string) bool {
r, _ := utf8.DecodeRuneInString(msg)
return unicode.IsUpper(r)
}
// wordsOf is what a failure says to a person, in their language: its own sentence, or, for an error that is not
// one, the plain sentence, the error itself going to the system log (`say`, nil: nowhere).
func wordsOf(t *i18n.Catalog, err error, say func(line string)) string {
msg := err.Error()
if Worded(msg) {
return t.T(msg)
}
if say != nil {
say(msg)
}
return t.T(Unworded)
}
+27
View File
@@ -0,0 +1,27 @@
package methods
import (
"errors"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/i18n"
)
func TestWordsOf(t *testing.T) {
none := i18n.Use("", "")
var logged []string
say := func(line string) { logged = append(logged, line) }
if got := wordsOf(none, errors.New("Port 51820 is already taken."), say); got != "Port 51820 is already taken." || len(logged) != 0 {
t.Fatalf("a sentence is said as it is: %q, logged %v", got, logged)
}
if got := wordsOf(none, errors.New("Порт занят."), say); got != "Порт занят." {
t.Fatalf("a sentence in another script too: %q", got)
}
raw := "open /etc/config/dawnline: no such file or directory"
if got := wordsOf(none, errors.New(raw), say); got != Unworded || len(logged) != 1 || logged[0] != raw {
t.Fatalf("Go's own error is not said, it is logged: %q, logged %v", got, logged)
}
if got := wordsOf(none, errors.New(""), nil); got != Unworded {
t.Fatalf("nothing to say is not a sentence: %q", got)
}
}
+399
View File
@@ -0,0 +1,399 @@
package methods
import (
crand "crypto/rand"
"encoding/hex"
"errors"
"fmt"
"math/rand/v2"
"regexp"
"strconv"
"strings"
"time"
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
"github.com/tickcount/luci-app-dawnline/backend/journal"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/store"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
const blank = " \t\r\n\v\f"
// trimmed is an argument as a trimmed string ("" for none).
func trimmed(v any) string {
switch x := v.(type) {
case string:
return strings.Trim(x, blank)
case nil:
return ""
}
return strings.Trim(fmt.Sprint(v), blank)
}
// ValidName tells a name people see: 1 to 48 characters (not bytes), none of < > " \ or a control character.
func ValidName(s string) bool {
n := 0
for _, r := range s {
if r == '<' || r == '>' || r == '"' || r == '\\' || r < 0x20 || r == 0x7f {
return false
}
n++
}
return n <= 48 && strings.Trim(s, blank) != ""
}
var portDigits = regexp.MustCompile(`^[0-9]{1,5}$`)
// ValidPort tells a TCP or UDP port as typed: 1 to 65535.
func ValidPort(s string) bool {
s = strings.Trim(s, blank)
n, _ := strconv.Atoi(s)
return portDigits.MatchString(s) && n >= 1 && n <= 65535
}
// taken tells an id anything in the settings has, or that is reserved.
func taken(m *model.Model, id string) bool {
for _, r := range ReservedIDs {
if r == id {
return true
}
}
return m.Servers[id] != nil || m.People[id] != nil || m.Devices[id] != nil || m.Services[id] != nil || m.Circles[id] != nil
}
// newID is an id from make() that no section has yet.
func newID(m *model.Model, make func() string) (string, error) {
for i := 0; i < 20; i++ {
if id := make(); !taken(m, id) {
return id, nil
}
}
return "", errors.New("Could not find a free id, try again.")
}
// write runs a change of the settings: written, and not yet put into effect.
func write(d Deps, fn func(c *uci.Config, m *model.Model) error) error {
if d.Store == nil {
return errors.New("The router is still starting. Try again in a moment.")
}
if err := d.Store.Transact(fn); err != nil {
if u, unsaved := err.(store.Unsaved); unsaved {
return said{"Couldn't save /etc/config/dawnline: %s. Is the flash full?", []any{u.Why}}
}
return err
}
return nil
}
// change runs a change of the settings and has the ucode backend put it into effect; the warning is that
// sync's, for the admin.
func change(d Deps, lang string, fn func(c *uci.Config, m *model.Model) error) (warning any, err error) {
if err := write(d, fn); err != nil {
return nil, err
}
if d.Sync != nil {
warning = d.Sync(lang)
}
if d.Told != nil {
d.Told()
}
return warning, nil
}
var serviceID = regexp.MustCompile(`^[A-Za-z0-9_]{1,32}$`)
// serviceSet adds a home service or changes one (dawnline.rpc.home service_set).
func serviceSet(d Deps, a map[string]any) (map[string]any, error) {
name := trimmed(a["name"])
if !ValidName(name) {
return nil, errors.New(`Service: Enter a name: up to 48 characters, without < > " or \.`)
}
address := trimmed(a["address"])
if _, ok := ipnet.IP(address); !ok {
return nil, errors.New("Address must be an IPv4 address.")
}
var ports []string
if list, isList := a["ports"].([]any); isList {
for _, p := range list {
ports = append(ports, trimmed(p))
}
} else {
ports = regexp.MustCompile(`[ ,]+`).Split(trimmed(a["ports"]), -1)
}
kept := ports[:0]
for _, p := range ports {
if p == "" {
continue
}
if !ValidPort(p) {
return nil, said{"Invalid port %s.", []any{p}}
}
kept = append(kept, p)
}
id := trimmed(a["id"])
warning, err := change(d, text(a["lang"]), func(c *uci.Config, m *model.Model) error {
switch {
case id == "":
var err error
if id, err = newID(m, func() string { return fmt.Sprintf("svc_%d", 100000+rand.IntN(900000)) }); err != nil {
return err
}
case !serviceID.MatchString(id):
return errors.New("Invalid service id.")
// never turn a person, device, server, circle or the settings into a service
case m.Services[id] == nil && taken(m, id):
return errors.New("That id is taken.")
}
c.Add("service", id)
c.Set(id, "name", name)
c.Set(id, "address", address)
if len(kept) > 0 {
c.SetList(id, "port", kept)
} else {
c.Delete(id, "port")
}
return nil
})
if err != nil {
return nil, err
}
return map[string]any{"id": id, "warning": warning}, nil
}
// serviceDelete removes a home service and takes it from everyone who had it.
func serviceDelete(d Deps, id, lang string) (map[string]any, error) {
warning, err := change(d, lang, func(c *uci.Config, m *model.Model) error {
if m.Services[id] == nil {
return errors.New("Unknown service.")
}
c.DeleteSection(id)
for _, pid := range m.PersonIDs() {
p := m.People[pid]
var rest []string
had := false
for _, s := range p.Services {
if s == id {
had = true
} else {
rest = append(rest, s)
}
}
if !had {
continue
}
if len(rest) > 0 {
c.SetList(pid, "service", rest)
} else {
c.Delete(pid, "service")
}
}
return nil
})
if err != nil {
return nil, err
}
return map[string]any{"ok": true, "warning": warning}, nil
}
// randHex is n random bytes as hex: an id's tail. A test puts its own in its place.
var randHex = func(n int) string {
b := make([]byte, n)
crand.Read(b)
return hex.EncodeToString(b)
}
// lower is s with its ASCII letters in lower case, as ucode's lc() leaves it.
func lower(s string) string {
b := []byte(s)
for i, c := range b {
if c >= 'A' && c <= 'Z' {
b[i] = c + 32
}
}
return string(b)
}
const circleColors = 8
// circles is the circles as the settings hold them now, for a page to show after its change.
func circles(d Deps) []any {
out := []any{}
if _, m, err := d.Store.Read(); err == nil {
for _, id := range m.CircleIDs() {
out = append(out, m.Circles[id])
}
}
return out
}
// circleSet creates a circle or edits one; `members` (person ids), when given, replaces who is in it
// (dawnline.rpc.home circle_set).
func circleSet(d Deps, a map[string]any) (map[string]any, error) {
name := trimmed(a["name"])
if !ValidName(name) || len([]rune(name)) > 32 {
return nil, errors.New(`Enter a circle name: up to 32 characters, without < > " or \.`)
}
id := text(a["id"])
edit, serverName := false, ""
warning, err := change(d, text(a["lang"]), func(c *uci.Config, m *model.Model) error {
ci := m.Circles[id]
if id != "" && ci == nil {
return errors.New("Unknown circle.")
}
server := text(a["server"])
if ci != nil {
server = ci.Server
}
if m.Servers[server] == nil {
return errors.New("Unknown server.")
}
for _, oid := range m.CircleIDs() {
if o := m.Circles[oid]; oid != id && o.Server == server && lower(o.Name) == lower(name) {
return said{"%s already exists.", []any{name}}
}
}
if ci == nil {
var err error
if id, err = newID(m, func() string { return "c_" + randHex(4) }); err != nil {
return err
}
}
color := ((int(num(a["color"])) % circleColors) + circleColors) % circleColors
c.Add("circle", id)
c.Set(id, "server", server)
c.Set(id, "name", name)
c.Set(id, "color", strconv.Itoa(color))
if list, given := a["members"].([]any); given {
want := map[string]bool{}
for _, pid := range list {
if p := m.People[text(pid)]; p != nil && p.Server == server {
want[text(pid)] = true
}
}
for _, pid := range m.PersonIDs() {
if want[pid] {
c.Set(pid, "circle", id)
} else if m.People[pid].Circle == id {
c.Delete(pid, "circle")
}
}
}
edit, serverName = ci != nil, m.Servers[server].Name
return nil
})
if err != nil {
return nil, err
}
if d.Log != nil {
msg := "added"
if edit {
msg = "changed"
}
d.Log.Admin("awg", msg, "circle", name, serverName)
}
return map[string]any{"id": id, "circles": circles(d), "warning": warning}, nil
}
// circleDelete removes a circle and takes everyone out of it.
func circleDelete(d Deps, id, lang string) (map[string]any, error) {
name, serverName := "", ""
warning, err := change(d, lang, func(c *uci.Config, m *model.Model) error {
ci := m.Circles[id]
if ci == nil {
return errors.New("Unknown circle.")
}
c.DeleteSection(id)
for _, pid := range m.PersonIDs() {
if m.People[pid].Circle == id {
c.Delete(pid, "circle")
}
}
name = ci.Name
if srv := m.Servers[ci.Server]; srv != nil {
serverName = srv.Name
}
return nil
})
if err != nil {
return nil, err
}
if d.Log != nil {
d.Log.Admin("awg", "removed", "circle", name, serverName)
}
return map[string]any{"circles": circles(d), "warning": warning}, nil
}
// logged puts events in the activity log and, those about the system, in syslog (dawnline.live add_events):
// each with the names it carries, as the settings have them now.
func logged(d Deps, events ...journal.Event) {
if d.Store == nil {
return
}
_, m, err := d.Store.Read()
if err != nil {
return
}
names := journal.NamesOf(m)
for _, e := range events {
journal.Named(e, names)
if lvl := journal.Level(e); lvl != "" && d.Log != nil {
kind, _ := e["type"].(string)
d.Log.Say(lvl, journal.CategoryOf(kind), journal.Line(e))
}
}
journal.Log{Run: d.RunDir, Data: d.DataDir}.Add(events, journal.PolicyOf(m.Settings.Retention), names, time.Now().Unix())
}
// personPause pauses a person, or lets them on again.
func personPause(d Deps, id string, paused bool, lang string) (map[string]any, error) {
warning, err := change(d, lang, func(c *uci.Config, m *model.Model) error {
if m.People[id] == nil {
return errors.New("Unknown person.")
}
if paused {
c.Set(id, "paused", "1")
} else {
c.Delete(id, "paused")
}
return nil
})
if err != nil {
return nil, err
}
kind := "resumed"
if paused {
kind = "paused"
}
logged(d, journal.Event{"type": kind, "person": id, "by": "admin"})
if d.Told != nil {
d.Told()
}
return map[string]any{"warning": warning}, nil
}
// personDelete removes a person and their devices. The event carries their name: the log still reads well
// once they are gone.
func personDelete(d Deps, id, lang string) (map[string]any, error) {
name := ""
warning, err := change(d, lang, func(c *uci.Config, m *model.Model) error {
p := m.People[id]
if p == nil {
return errors.New("Unknown person.")
}
for _, dev := range p.Devices {
c.DeleteSection(dev)
}
c.DeleteSection(id)
name = p.Name
return nil
})
if err != nil {
return nil, err
}
logged(d, journal.Event{"type": "person_removed", "person": id, "detail": name, "pname": name, "by": "admin"})
if d.Told != nil {
d.Told()
}
return map[string]any{"warning": warning}, nil
}
+173
View File
@@ -0,0 +1,173 @@
package methods
import (
"encoding/json"
"os"
"regexp"
"strconv"
"strings"
"testing"
"github.com/tickcount/luci-app-dawnline/backend/awg"
"github.com/tickcount/luci-app-dawnline/backend/netview"
"github.com/tickcount/luci-app-dawnline/backend/store"
"github.com/tickcount/luci-app-dawnline/backend/uci"
"github.com/tickcount/luci-app-dawnline/backend/view"
)
// A row of changes to access's household: after each, the settings file is the one the ucode backend's
// own method leaves, and a refusal has its words (testdata/write.uc, scripts/ucode-golden.sh).
func TestChangesAsUcode(t *testing.T) {
raw, err := os.ReadFile("testdata/write.json")
if err != nil {
t.Fatal(err)
}
var golden struct {
Steps []struct {
Method string `json:"method"`
Args map[string]any `json:"args"`
Error *string `json:"error"`
File string `json:"file"`
} `json:"steps"`
}
if err := json.Unmarshal(raw, &golden); err != nil {
t.Fatal(err)
}
if len(golden.Steps) < 287 {
t.Fatal("the golden is too small")
}
dir := t.TempDir()
text, _ := os.ReadFile("../access/testdata/access.uci")
// as libuci keeps it: the golden's first commit rewrites the file whole
c, _ := uci.Parse(string(text))
os.WriteFile(dir+"/dawnline", []byte(c.Format()), 0o600)
// 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"`
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)
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) }})
}
installed := true
d := Deps{World: func() *view.World { return &view.World{} }, Store: &store.Store{Config: dir + "/dawnline", RunDir: dir}, Snapshot: snapshot,
Config: dir + "/dawnline", RunDir: dir, DataDir: dir + "/data",
// as in the golden's container: no AmneziaWG at all
Caps: func() awg.Caps { return awg.Caps{} }, AWG: func() map[string]any {
// the made-up device steps stand for a router that has it (they pass ucode's check by going around it)
return map[string]any{"tools": installed, "kmod": installed, "loaded": installed, "handler": true, "missing": []any{"kmod-amneziawg", "amneziawg-tools"}}
}}
changed, refused := 0, 0
before := c.Format()
made := regexp.MustCompile(`config circle 'c_([0-9a-f]+)'`)
codeOf := regexp.MustCompile(`option code '([a-z]{6}-[a-z]{6}-[a-z]{6})'`)
momentOf := regexp.MustCompile(`option (?:code_set|created) '([0-9]+)'`)
device := regexp.MustCompile(`config device 'd_([0-9a-f]+)'`)
portOf := regexp.MustCompile(`option port '([0-9]+)'`)
for i, step := range golden.Steps {
// an id made at random is the one ucode's dice gave: the golden's file names it
randHex = func(int) string {
for _, m := range made.FindAllStringSubmatch(step.File, -1) {
if !strings.Contains(before, "config circle 'c_"+m[1]+"'") {
return m[1]
}
}
return "none"
}
// a code and a moment likewise: the ones the golden's file has and the file before did not
newCode = func() string {
for _, m := range codeOf.FindAllStringSubmatch(step.File, -1) {
if !strings.Contains(before, "'"+m[1]+"'") {
return m[1]
}
}
return "none"
}
clock = func() int64 {
var latest int64
for _, m := range momentOf.FindAllStringSubmatch(step.File, -1) {
if n, _ := strconv.ParseInt(m[1], 10, 64); n > latest {
latest = n
}
}
return latest
}
// the keys the golden made up, and the device id its dice gave
method := strings.TrimPrefix(step.Method, "x_")
installed = step.Method != "server_obfuscation" && step.Method != "server_create"
needLoaded = step.Method != "server_create"
// a port drawn when the usual one is taken is ucode's dice too
randPort = func() int {
for _, m := range portOf.FindAllStringSubmatch(step.File, -1) {
if !strings.Contains(before, "option port '"+m[1]+"'") {
n, _ := strconv.Atoi(m[1])
return n
}
}
return 51830
}
if step.Method == "server_create" {
// the keys the golden's stand-in tool gives
newKeys = func() Keys {
return Keys{Private: strings.Repeat("S", 42) + "A=", Public: strings.Repeat("T", 42) + "A="}
}
}
if k, _ := step.Args["key"].(string); k != "" {
key := func(c string) string { return strings.Repeat(c, 42)[:42*len(c)] + "A=" }
newKeys = func() Keys { return Keys{Private: key(k + "x"), Public: key(k), Preshared: key(k + "y")} }
randHex = func(int) string {
for _, m := range device.FindAllStringSubmatch(step.File, -1) {
if !strings.Contains(before, "config device 'd_"+m[1]+"'") {
return m[1]
}
}
return "none"
}
}
res := ask(t, d, method, step.Args)
got, _ := os.ReadFile(dir + "/dawnline")
// ucode's full apply, which the server methods end with, notes a migration in the settings the first
// time it runs; here nothing applies, so the note is put where that apply puts it
const note = "\tlist migrated 'auto_mtu'\n"
if strings.Contains(step.File, note) && !strings.Contains(string(got), note) {
c, _ := uci.Parse(string(got))
c.SetList("settings", "migrated", []string{"auto_mtu"})
got = []byte(c.Format())
os.WriteFile(dir+"/dawnline", got, 0o600)
}
if string(got) != step.File {
t.Fatalf("step %d %s %v: the file differs\n%s\n--- ucode:\n%s", i, step.Method, step.Args, got, step.File)
}
if string(got) == before {
// nothing was written: a refusal in the same words, or a change that changes nothing
var want any
if step.Error != nil {
want = *step.Error
refused++
}
if res["error"] != want {
t.Errorf("step %d %s %v: refused with %v, ucode %v", i, step.Method, step.Args, res["error"], want)
}
} else {
changed++
if res["error"] != nil {
t.Errorf("step %d %s %v: written, and yet %v", i, step.Method, step.Args, res["error"])
}
}
before = string(got)
}
if changed < 115 || refused < 106 {
t.Fatalf("the golden exercises too little: %d changes, %d refusals", changed, refused)
}
}