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
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:
1 parent
f8130e39a8
commit
ac90575b0b
788 files changed
+139244
-41720
No files matched your search
@@ -0,0 +1,189 @@
|
||||
package awg
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/tickcount/luci-app-dawnline/backend/files"
|
||||
)
|
||||
|
||||
// What the installed AmneziaWG can do, as dawnline.awg caps() works it out: the netifd protocol script and
|
||||
// the awg tool say what the tools know, the kernel module's package version what the kernel runs, and a
|
||||
// feature counts only when both have it. Held to ucode on testdata/caps.uc (scripts/ucode-golden.sh).
|
||||
|
||||
// Caps: V2 is S3/S4, I1-I5 and header ranges (AWG 2.0), V3 header protection, trailers, padding and timing
|
||||
// (AWG 3.1); Tools and Kmod are their generations ("1.0", "2.0", "3.1"; nil unknown); Pending, an installed
|
||||
// module waits for a reboot.
|
||||
type Caps struct {
|
||||
V2 bool `json:"v2"`
|
||||
V3 bool `json:"v3"`
|
||||
Tools any `json:"tools"`
|
||||
Kmod any `json:"kmod"`
|
||||
Mismatch bool `json:"mismatch"`
|
||||
Pending bool `json:"pending"`
|
||||
}
|
||||
|
||||
// Record is what an install of AmneziaWG left in awg.json, as far as the running module is concerned.
|
||||
type Record struct {
|
||||
Reloaded *bool `json:"reloaded"`
|
||||
Boot *string `json:"boot"`
|
||||
PrevCore *int64 `json:"prev_core"`
|
||||
PrevGen *string `json:"prev_gen"`
|
||||
}
|
||||
|
||||
var tail = regexp.MustCompile(`([0-9]+\.[0-9]+\.[0-9]{8})(-r?[0-9]+)?$`)
|
||||
|
||||
// Gen is the AmneziaWG generation of a package version: 3.x packages carry 3.1; 1.0.x ones are 2.0 from
|
||||
// July 2025 on and the first generation before. A kernel module's version has the kernel release in front.
|
||||
func Gen(version string) string {
|
||||
v := version
|
||||
if m := tail.FindStringSubmatch(version); m != nil {
|
||||
v = m[1]
|
||||
}
|
||||
switch {
|
||||
case strings.HasPrefix(v, "3."):
|
||||
return "3.1"
|
||||
case strings.HasPrefix(v, "1.0.2024"):
|
||||
return "1.0"
|
||||
case len(v) >= 10 && strings.HasPrefix(v, "1.0.20250") && v[9] >= '1' && v[9] <= '6':
|
||||
return "1.0"
|
||||
}
|
||||
return "2.0"
|
||||
}
|
||||
|
||||
// Pending tells an installed update that waits for a reboot: the old module stayed loaded, this is still
|
||||
// the boot it ran in, and the module loaded is still that one. boot "" and core 0 stand for "not known".
|
||||
func Pending(rec *Record, boot string, core int64) bool {
|
||||
return rec != nil && rec.Reloaded != nil && !*rec.Reloaded && rec.Boot != nil && boot != "" && *rec.Boot == boot &&
|
||||
core != 0 && rec.PrevCore != nil && *rec.PrevCore == core
|
||||
}
|
||||
|
||||
// RunningGen is the generation of the kernel module that runs: the one from before the install while it
|
||||
// waits for a reboot, else the installed package's (version nil: no package). nil when not known.
|
||||
func RunningGen(rec *Record, boot string, core int64, version *string) any {
|
||||
if Pending(rec, boot, core) {
|
||||
if rec.PrevGen == nil {
|
||||
return nil
|
||||
}
|
||||
return *rec.PrevGen
|
||||
}
|
||||
if version == nil {
|
||||
return nil
|
||||
}
|
||||
return Gen(*version)
|
||||
}
|
||||
|
||||
// Found is what of AmneziaWG the router has, for CapsOf.
|
||||
type Found struct {
|
||||
// Script is the netifd protocol script; Tool, whether the awg tool is there; Tool3, whether it knows the 3.1 keys.
|
||||
Script string
|
||||
Tool, Tool3 bool
|
||||
Record *Record
|
||||
Boot string
|
||||
Core int64
|
||||
// Version is the kmod-amneziawg package's, nil when it is not a package.
|
||||
Version *string
|
||||
}
|
||||
|
||||
// CapsOf works the capabilities out of what was found.
|
||||
func CapsOf(f Found) Caps {
|
||||
t2 := strings.Contains(f.Script, "awg_s3") && strings.Contains(f.Script, "awg_i1")
|
||||
t3 := t2 && strings.Contains(f.Script, "awg_header_protection_key") && f.Tool3
|
||||
var tools any
|
||||
if f.Tool {
|
||||
tools = "1.0"
|
||||
if t3 {
|
||||
tools = "3.1"
|
||||
} else if t2 {
|
||||
tools = "2.0"
|
||||
}
|
||||
}
|
||||
kmod := RunningGen(f.Record, f.Boot, f.Core, f.Version)
|
||||
return Caps{
|
||||
V2: t2 && (kmod == nil || kmod != "1.0"), V3: t3 && (kmod == nil || kmod == "3.1"),
|
||||
Tools: tools, Kmod: kmod, Mismatch: tools != nil && kmod != nil && tools != kmod,
|
||||
Pending: Pending(f.Record, f.Boot, f.Core),
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
toolPath = "/usr/bin/awg"
|
||||
protoPath = "/lib/netifd/proto/amneziawg.sh"
|
||||
)
|
||||
|
||||
// Version is the installed version of a package (apk on OpenWrt 25.12, opkg before), or nil.
|
||||
func Version(name string) *string {
|
||||
if st, err := os.Stat("/usr/bin/apk"); err == nil && st.Mode()&0o111 != 0 {
|
||||
out, _ := exec.Command("apk", "list", "--installed", name).CombinedOutput()
|
||||
if m := regexp.MustCompile(`^` + regexp.QuoteMeta(name) + `-([^ ]+) `).FindSubmatch(out); m != nil {
|
||||
v := string(m[1])
|
||||
return &v
|
||||
}
|
||||
return nil
|
||||
}
|
||||
out, _ := exec.Command("opkg", "list-installed").CombinedOutput()
|
||||
re := regexp.MustCompile(`^` + regexp.QuoteMeta(name) + `([0-9]+)? - (.+)$`)
|
||||
var found []string
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
if m := re.FindStringSubmatch(strings.TrimSpace(line)); m != nil && (found == nil || m[1] == "") {
|
||||
found = m
|
||||
}
|
||||
}
|
||||
if found == nil {
|
||||
return nil
|
||||
}
|
||||
v := strings.TrimSpace(found[2])
|
||||
return &v
|
||||
}
|
||||
|
||||
var kept struct {
|
||||
sync.Mutex
|
||||
key string
|
||||
caps Caps
|
||||
ok bool
|
||||
}
|
||||
|
||||
// SystemCaps is this router's capabilities. Worked out again only when the module's file, the tool, the
|
||||
// protocol script, the install's record (awg.json in dataDir) or the loaded module changed.
|
||||
func SystemCaps(dataDir string) Caps {
|
||||
mtime := func(path string) int64 {
|
||||
st, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return st.ModTime().Unix()
|
||||
}
|
||||
read := func(path string) string {
|
||||
raw, _ := os.ReadFile(path)
|
||||
return strings.TrimSpace(string(raw))
|
||||
}
|
||||
var core int64
|
||||
fmt.Sscan(read("/sys/module/amneziawg/coresize"), &core)
|
||||
if core < 0 {
|
||||
core = 0
|
||||
}
|
||||
record := dataDir + "/awg.json"
|
||||
key := fmt.Sprintf("%d/%d/%d/%d/%d", mtime("/lib/modules/"+read("/proc/sys/kernel/osrelease")+"/amneziawg.ko"), mtime(toolPath), mtime(protoPath), mtime(record), core)
|
||||
kept.Lock()
|
||||
defer kept.Unlock()
|
||||
if kept.ok && kept.key == key {
|
||||
return kept.caps
|
||||
}
|
||||
f := Found{Boot: read("/proc/sys/kernel/random/boot_id"), Core: core, Version: Version("kmod-amneziawg")}
|
||||
script, _ := os.ReadFile(protoPath)
|
||||
f.Script = string(script)
|
||||
if tool, err := os.ReadFile(toolPath); err == nil {
|
||||
f.Tool, f.Tool3 = true, bytes.Contains(tool, []byte("HeaderProtectionKey"))
|
||||
}
|
||||
var rec Record
|
||||
if files.ReadJSON(record, &rec) {
|
||||
f.Record = &rec
|
||||
}
|
||||
kept.key, kept.caps, kept.ok = key, CapsOf(f), true
|
||||
return kept.caps
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package awg
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Generations and the wait for a reboot, as ucode tells them (testdata/caps.uc, scripts/ucode-golden.sh).
|
||||
func TestGenerationsAsUcode(t *testing.T) {
|
||||
raw, err := os.ReadFile("testdata/caps.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var g struct {
|
||||
Gens map[string]string `json:"gens"`
|
||||
Runs []struct {
|
||||
Rec *Record `json:"rec"`
|
||||
Boot *string `json:"boot"`
|
||||
Core *int64 `json:"core"`
|
||||
Version *string `json:"version"`
|
||||
Pending bool `json:"pending"`
|
||||
Gen any `json:"gen"`
|
||||
} `json:"runs"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &g); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(g.Gens) < 12 || len(g.Runs) < 100 {
|
||||
t.Fatalf("too little: %d versions, %d runs", len(g.Gens), len(g.Runs))
|
||||
}
|
||||
for v, want := range g.Gens {
|
||||
if got := Gen(v); got != want {
|
||||
t.Errorf("Gen(%q) = %s, ucode %s", v, got, want)
|
||||
}
|
||||
}
|
||||
waits := 0
|
||||
for i, r := range g.Runs {
|
||||
boot, core := "", int64(0)
|
||||
if r.Boot != nil {
|
||||
boot = *r.Boot
|
||||
}
|
||||
if r.Core != nil {
|
||||
core = *r.Core
|
||||
}
|
||||
if r.Pending {
|
||||
waits++
|
||||
}
|
||||
if got := Pending(r.Rec, boot, core); got != r.Pending {
|
||||
t.Errorf("run %d: pending %v, ucode %v", i, got, r.Pending)
|
||||
}
|
||||
if got := RunningGen(r.Rec, boot, core, r.Version); got != r.Gen {
|
||||
t.Errorf("run %d: generation %v, ucode %v", i, got, r.Gen)
|
||||
}
|
||||
}
|
||||
if waits == 0 {
|
||||
t.Fatal("no run waits for a reboot")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCapsOf(t *testing.T) {
|
||||
s := func(v string) *string { return &v }
|
||||
no := false
|
||||
core := int64(65536)
|
||||
v2 := "proto_config_add_int awg_s3\nawg_i1"
|
||||
v3 := v2 + "\nawg_header_protection_key"
|
||||
for name, c := range map[string]struct {
|
||||
f Found
|
||||
want Caps
|
||||
}{
|
||||
"nothing installed": {Found{}, Caps{}},
|
||||
"3.1 all through": {Found{Script: v3, Tool: true, Tool3: true, Version: s("6.12.94.3.1.20260906-r1")}, Caps{V2: true, V3: true, Tools: "3.1", Kmod: "3.1"}},
|
||||
"3.1 tools over a 2.0 module": {Found{Script: v3, Tool: true, Tool3: true, Version: s("6.6.73.1.0.20250903-r2")},
|
||||
Caps{V2: true, Tools: "3.1", Kmod: "2.0", Mismatch: true}},
|
||||
"a script ahead of its tool": {Found{Script: v3, Tool: true, Version: s("3.1.20260906-r1")}, Caps{V2: true, Tools: "2.0", Kmod: "3.1", Mismatch: true}},
|
||||
"a module put there by hand": {Found{Script: v3, Tool: true, Tool3: true}, Caps{V2: true, V3: true, Tools: "3.1"}},
|
||||
"the first generation": {Found{Script: "awg_jc", Tool: true, Version: s("1.0.20240213")}, Caps{Tools: "1.0", Kmod: "1.0"}},
|
||||
"an update that waits for a reboot": {Found{Script: v3, Tool: true, Tool3: true, Version: s("3.1.20260906-r1"), Boot: "b", Core: core,
|
||||
Record: &Record{Reloaded: &no, Boot: s("b"), PrevCore: &core, PrevGen: s("1.0")}},
|
||||
Caps{Tools: "3.1", Kmod: "1.0", Mismatch: true, Pending: true}},
|
||||
} {
|
||||
if got := CapsOf(c.f); got != c.want {
|
||||
t.Errorf("%s: %+v", name, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
// Package awg is the backend's one door to AmneziaWG: what the tunnels say of their peers.
|
||||
//
|
||||
// This file reads `awg show <ifname> dump`, as dawnline.awg dump() does: Dawnline's own tunnels only, each
|
||||
// on its own (never `all`: another AmneziaWG interface on the router is not ours to read, and a long one
|
||||
// would slow a read that runs every second). The tool and /sys are reached through Reader's two functions,
|
||||
// so a test stands in for both.
|
||||
package awg
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Peer is a device as its tunnel sees it.
|
||||
type Peer struct {
|
||||
Ifname string `json:"ifname"`
|
||||
// Endpoint is where it last spoke from ("203.0.113.7:41821"), empty when it never did.
|
||||
Endpoint string `json:"endpoint"`
|
||||
// Handshake is when its session was last made (unix seconds), 0 for never.
|
||||
Handshake int64 `json:"handshake"`
|
||||
// Rx and Tx are the bytes received from it and sent to it since the tunnel last added it.
|
||||
Rx int64 `json:"rx"`
|
||||
Tx int64 `json:"tx"`
|
||||
}
|
||||
|
||||
// Iface is a tunnel's own line.
|
||||
type Iface struct {
|
||||
PublicKey string `json:"public_key"`
|
||||
Port int `json:"port"`
|
||||
// Ifindex is the link's index: a new one each time the tunnel is set up, so it is the epoch of its
|
||||
// peers' counters.
|
||||
Ifindex int `json:"ifindex"`
|
||||
}
|
||||
|
||||
// How a tunnel's read went.
|
||||
const (
|
||||
Read = "read"
|
||||
// Down: the interface does not exist; its peers and their counters are gone with it.
|
||||
Down = "down"
|
||||
// Failed: it exists but could not be read, or was set up again during the read: that says nothing
|
||||
// about its peers.
|
||||
Failed = "error"
|
||||
)
|
||||
|
||||
// Dump is one look at the tunnels. Ifaces and Peers hold only what was read; OK is false when any tunnel's
|
||||
// read failed.
|
||||
type Dump struct {
|
||||
OK bool `json:"ok"`
|
||||
Reads map[string]string `json:"reads"`
|
||||
Ifaces map[string]Iface `json:"ifaces"`
|
||||
Peers map[string]Peer `json:"peers"`
|
||||
}
|
||||
|
||||
// Reader reads tunnels. The zero value is not usable: System() is the router's.
|
||||
type Reader struct {
|
||||
// Show is `awg show <ifname> dump`: its output, and whether it ran and ended well.
|
||||
Show func(ifname string) (string, bool)
|
||||
// Index is the interface's index while it exists.
|
||||
Index func(ifname string) (int, bool)
|
||||
}
|
||||
|
||||
// System reads the router's own tunnels.
|
||||
func System() Reader {
|
||||
return Reader{
|
||||
Show: func(ifname string) (string, bool) {
|
||||
out, err := exec.Command("awg", "show", ifname, "dump").Output()
|
||||
return string(out), err == nil
|
||||
},
|
||||
Index: func(ifname string) (int, bool) {
|
||||
raw, err := os.ReadFile("/sys/class/net/" + ifname + "/ifindex")
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
n, err := strconv.Atoi(strings.TrimSpace(string(raw)))
|
||||
return n, err == nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Dump reads these tunnels.
|
||||
func (r Reader) Dump(ifnames []string) Dump {
|
||||
d := Dump{OK: true, Reads: map[string]string{}, Ifaces: map[string]Iface{}, Peers: map[string]Peer{}}
|
||||
for _, ifname := range ifnames {
|
||||
// Read around the call: a link set up again in between gave counters of either one.
|
||||
before, there := r.Index(ifname)
|
||||
// Not there (a tunnel netifd could not bring up, AmneziaWG gone after an upgrade): no process to
|
||||
// learn it. One that appears is read next time.
|
||||
if !there {
|
||||
d.Reads[ifname] = Down
|
||||
continue
|
||||
}
|
||||
out, ran := r.Show(ifname)
|
||||
after, still := r.Index(ifname)
|
||||
// Gone during the read: its peers and their counters are gone with it.
|
||||
if !still {
|
||||
d.Reads[ifname] = Down
|
||||
continue
|
||||
}
|
||||
var iface *Iface
|
||||
var peers map[string]Peer
|
||||
if ran {
|
||||
iface, peers = Parse(out, ifname)
|
||||
}
|
||||
if iface == nil || after != before {
|
||||
d.Reads[ifname] = Failed
|
||||
d.OK = false
|
||||
continue
|
||||
}
|
||||
iface.Ifindex = after
|
||||
d.Reads[ifname] = Read
|
||||
d.Ifaces[ifname] = *iface
|
||||
for k, p := range peers {
|
||||
d.Peers[k] = p
|
||||
}
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
// Parse reads a dump's lines. They are told apart by their shape, not their length (each AmneziaWG
|
||||
// generation adds fields to the interface line): an interface line carries its private and public key and
|
||||
// a port; a peer line the peer's key, a preshared key or "(none)", and an endpoint. The interface is nil
|
||||
// when the dump has no such line.
|
||||
func Parse(out, ifname string) (*Iface, map[string]Peer) {
|
||||
var iface *Iface
|
||||
peers := map[string]Peer{}
|
||||
for _, line := range strings.Split(out, "\n") {
|
||||
f := strings.Split(line, "\t")
|
||||
if len(f) < 4 || !ValidKey(f[0]) {
|
||||
continue
|
||||
}
|
||||
second := ValidKey(f[1])
|
||||
if second && digits(f[2]) {
|
||||
port, _ := strconv.Atoi(f[2])
|
||||
iface = &Iface{PublicKey: f[1], Port: port}
|
||||
continue
|
||||
}
|
||||
if len(f) >= 8 && (second || f[1] == "(none)") {
|
||||
p := Peer{Ifname: ifname, Handshake: count(f[4]), Rx: count(f[5]), Tx: count(f[6])}
|
||||
if f[2] != "(none)" {
|
||||
p.Endpoint = f[2]
|
||||
}
|
||||
peers[f[0]] = p
|
||||
}
|
||||
}
|
||||
return iface, peers
|
||||
}
|
||||
|
||||
func digits(s string) bool {
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] < '0' || s[i] > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// count is a counter or a moment of the dump; what is no number counts as none.
|
||||
func count(s string) int64 {
|
||||
n, err := strconv.ParseInt(s, 10, 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// ValidKey tells a WireGuard key: 32 bytes in base64, so 43 letters and "=", the last letter one of those
|
||||
// that end 32 bytes.
|
||||
func ValidKey(s string) bool {
|
||||
if len(s) != 44 || s[43] != '=' || !strings.ContainsRune("AEIMQUYcgkosw048", rune(s[42])) {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < 42; i++ {
|
||||
c := s[i]
|
||||
if !(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '+' || c == '/') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package awg
|
||||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const (
|
||||
serverKey = "xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg="
|
||||
peerOn = "HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw="
|
||||
peerNew = "TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0="
|
||||
)
|
||||
|
||||
// The project's fixtures are `awg show all dump` (the ifname first); one interface's dump has no ifname.
|
||||
func fixture(t *testing.T, name string) string {
|
||||
raw, err := os.ReadFile("testdata/awg-dump-" + name + ".txt")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out []string
|
||||
for _, l := range strings.Split(string(raw), "\n") {
|
||||
if l != "" {
|
||||
out = append(out, strings.TrimPrefix(l, "awgmain\t"))
|
||||
}
|
||||
}
|
||||
return strings.Join(out, "\n") + "\n"
|
||||
}
|
||||
|
||||
// A stand-in for the router: the dump it prints and the index /sys gives, by the read.
|
||||
func stand(out string, ok bool, indexes ...int) Reader {
|
||||
i := 0
|
||||
return Reader{
|
||||
Show: func(string) (string, bool) { return out, ok },
|
||||
Index: func(string) (int, bool) {
|
||||
ix := indexes[min(i, len(indexes)-1)]
|
||||
i++
|
||||
return ix, ix != 0
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// What dawnline.awg dump() reads of the same fixtures (tests/ucode/awg.test.uc).
|
||||
func TestDumpGenerations(t *testing.T) {
|
||||
d := stand(fixture(t, "1.0"), true, 7).Dump([]string{"dawn0"})
|
||||
want := Dump{OK: true, Reads: map[string]string{"dawn0": Read},
|
||||
Ifaces: map[string]Iface{"dawn0": {PublicKey: serverKey, Port: 51820, Ifindex: 7}},
|
||||
Peers: map[string]Peer{
|
||||
peerOn: {Ifname: "dawn0", Endpoint: "203.0.113.7:41821", Handshake: 1760000000, Rx: 123456, Tx: 654321},
|
||||
peerNew: {Ifname: "dawn0"},
|
||||
}}
|
||||
if !reflect.DeepEqual(d, want) {
|
||||
t.Fatalf("1.0: %+v", d)
|
||||
}
|
||||
d = stand(fixture(t, "2.0"), true, 7).Dump([]string{"dawn0"})
|
||||
if d.Ifaces["dawn0"].Port != 51820 || len(d.Peers) != 1 || d.Peers[peerOn].Handshake == 0 {
|
||||
t.Fatalf("2.0: %+v", d)
|
||||
}
|
||||
d = stand(fixture(t, "3.1"), true, 7).Dump([]string{"dawn0"})
|
||||
if d.Ifaces["dawn0"].PublicKey != serverKey || len(d.Peers) != 2 {
|
||||
t.Fatalf("3.1: %+v", d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDumpStates(t *testing.T) {
|
||||
asked := false
|
||||
r := Reader{Show: func(string) (string, bool) { asked = true; return "", true }, Index: func(string) (int, bool) { return 0, false }}
|
||||
if d := r.Dump([]string{"dawn0"}); !d.OK || d.Reads["dawn0"] != Down || asked {
|
||||
t.Fatalf("a tunnel that is not there: %+v, asked %v", d, asked)
|
||||
}
|
||||
one := fixture(t, "1.0")
|
||||
if d := stand(one, true, 7, 0).Dump([]string{"dawn0"}); d.Reads["dawn0"] != Down || len(d.Peers) != 0 {
|
||||
t.Fatalf("gone during the read: %+v", d)
|
||||
}
|
||||
if d := stand(one, true, 7, 9).Dump([]string{"dawn0"}); d.OK || d.Reads["dawn0"] != Failed || len(d.Peers) != 0 {
|
||||
t.Fatalf("set up again during the read: %+v", d)
|
||||
}
|
||||
if d := stand("", false, 7).Dump([]string{"dawn0"}); d.OK || d.Reads["dawn0"] != Failed {
|
||||
t.Fatalf("the tool failed: %+v", d)
|
||||
}
|
||||
if d := stand("not a dump\n", true, 7).Dump([]string{"dawn0"}); d.Reads["dawn0"] != Failed {
|
||||
t.Fatalf("no interface line: %+v", d)
|
||||
}
|
||||
if d := (Reader{}).Dump(nil); !d.OK || len(d.Reads) != 0 {
|
||||
t.Fatalf("no tunnels: %+v", d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidKey(t *testing.T) {
|
||||
for _, k := range []string{serverKey, peerOn, peerNew} {
|
||||
if !ValidKey(k) {
|
||||
t.Errorf("%s refused", k)
|
||||
}
|
||||
}
|
||||
for _, k := range []string{"", "(none)", serverKey[:43], serverKey[:42] + "B=", "!" + serverKey[1:], serverKey + "="} {
|
||||
if ValidKey(k) {
|
||||
t.Errorf("%q accepted", k)
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
awgmain yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk= xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg= 51820 off 4 64 128 20 40 1234567 2345678 3456789 4567890
|
||||
awgmain HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw= (none) 203.0.113.7:41821 10.9.0.2/32 1760000000 123456 654321 off
|
||||
awgmain TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0= (none) (none) 10.9.0.3/32 0 0 0 off
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
awgmain yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk= xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg= 51820 off 4 64 128 20 40 12 12 1234567 2345678 3456789 4567890 <b 0xdead>
|
||||
awgmain HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw= (none) 203.0.113.7:41821 10.9.0.2/32 1760000000 123456 654321 off
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
awgmain yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk= xTIBA5rboUvnH4htodjb6e697QjLERt1NAB4mZqp8Dg= 51820 off 4 64 128 20 40 12 12 1234567 2345678 3456789 4567890 <r 2><b 0x858000010001> yAnz5TF+lXXJte14tji3zlMNq+hd2rYUIgJBgB3fBmk= 10-100 on on
|
||||
awgmain HIgo9xNzJMWLKASShiTqIybxZ0U3wGLiUeJ1PKf8ykw= (none) 203.0.113.7:41821 10.9.0.2/32 1760000000 123456 654321 off
|
||||
awgmain TrMvSoP4jYQlY6RIzBgbssQqY3vxI2Pi+y71lOWWXX0= (none) (none) 10.9.0.3/32 0 0 0 off
|
||||
Vendored
+1730
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user