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,509 @@
|
||||
// Package link tells how each device's link behaves, from the counters read off its tunnel every second:
|
||||
// a device that knocks but never finishes connecting, one whose configuration is in use on two devices,
|
||||
// one whose traffic the router drops, one whose connection keeps breaking. It is dawnline.link in Go, rule
|
||||
// for rule; testdata holds the two to each other on the same reads (scripts/ucode-golden.sh).
|
||||
//
|
||||
// One History per device: a few events of the last ten minutes, not an entry per second. Classify reads it
|
||||
// (pure); a state is shown only once it held for Hold seconds (Settle). The same history tells the set-up
|
||||
// sheet, at once and without the hold, what a device being set up does right now (SetupState).
|
||||
//
|
||||
// What the counters mean (kmod 3.1, receive.c and send.c):
|
||||
// - an accepted handshake initiation adds exactly Initiation bytes to the peer's rx. The router answers
|
||||
// it; its own handshake time moves only when the first packet under the new keys arrives.
|
||||
// - a keepalive adds Keepalive plus its content padding; a handshake response (the router initiated)
|
||||
// Response; data 32 plus its padded length. Plain data is a multiple of 16, so 148, 296 and 444 never
|
||||
// are data.
|
||||
// - a data packet the router then drops (its source is not the device's address: an old configuration
|
||||
// after renumbering) adds nothing to rx, but the router still answers it with a keepalive within
|
||||
// about 10 s.
|
||||
package link
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/tickcount/luci-app-dawnline/backend/model"
|
||||
)
|
||||
|
||||
const (
|
||||
// History is the seconds of events kept per device.
|
||||
HistorySpan = 600
|
||||
// Hold: a state is shown once it held this long (a flapping mobile link passes).
|
||||
Hold = 180
|
||||
// CheckEvery: a device is classified at least this often when nothing happened (its spans age).
|
||||
CheckEvery = 15
|
||||
|
||||
// Initiation is the bytes an accepted initiation adds to rx.
|
||||
Initiation = 148
|
||||
// Response is the bytes a handshake response adds to rx (the router initiated).
|
||||
Response = 92
|
||||
// Keepalive is the bytes an empty data packet adds to rx, before content padding.
|
||||
Keepalive = 32
|
||||
// Random trailers without content padding: a keepalive grows up to a full packet.
|
||||
trailerMax = 1500
|
||||
// Initiations a device sends at most in one second (retries are 5 s apart).
|
||||
knocksPerTick = 3
|
||||
|
||||
// trying: at least tryKnocks accepted initiations within trySpan seconds while not online.
|
||||
trySpan = 120
|
||||
tryKnocks = 3
|
||||
// Churning: at least churnCount handshakes within churnSpan seconds, with churnCount-1 of the gaps
|
||||
// under churnGap seconds. A device that is there renews its keys every 100-120 s, more only when its
|
||||
// packets get nothing back.
|
||||
churnSpan = 300
|
||||
churnCount = 4
|
||||
churnGap = 45
|
||||
// shared: while churning, the endpoint went back to an earlier one this often.
|
||||
shareReturns = 2
|
||||
// dropped: online with a handshake within dropHS seconds, nothing but handshakes and keepalives
|
||||
// received for dropSpan seconds, and the router sent something at least dropAnswers times in that span
|
||||
// with nothing coming in, under dropTx bytes in all (a real download is far more).
|
||||
dropSpan = 180
|
||||
dropHS = 240
|
||||
dropAnswers = 6
|
||||
dropTx = 65536
|
||||
|
||||
// The set-up sheet's trying: at least setupKnocks accepted initiations within setupSpan seconds, none
|
||||
// of them followed by a handshake.
|
||||
setupSpan = 20
|
||||
setupKnocks = 2
|
||||
// nodata: online for setupQuiet seconds since the handshake its session began with, nothing but
|
||||
// handshakes and keepalives in since, and the router answered at least once with nothing coming in.
|
||||
setupQuiet = 15
|
||||
|
||||
// Per list, the newest entries kept (a device whose endpoint flips every second).
|
||||
keepEntries = 64
|
||||
)
|
||||
|
||||
// The states a link can be shown in; "" is nothing wrong, or nothing to tell.
|
||||
const (
|
||||
Trying = "trying"
|
||||
Shared = "shared"
|
||||
Dropped = "dropped"
|
||||
Unstable = "unstable"
|
||||
// The set-up sheet's own.
|
||||
Flowing = "flowing"
|
||||
NoData = "nodata"
|
||||
)
|
||||
|
||||
// Read is a device's peer as its tunnel shows it at one look.
|
||||
type Read struct {
|
||||
Endpoint string
|
||||
Handshake int64
|
||||
Rx, Tx int64
|
||||
}
|
||||
|
||||
// Shown is a state and when it began.
|
||||
type Shown struct {
|
||||
State string `json:"state"`
|
||||
Since int64 `json:"since"`
|
||||
}
|
||||
|
||||
type move struct {
|
||||
at int64
|
||||
to, from string
|
||||
}
|
||||
|
||||
type answer struct {
|
||||
at, bytes int64
|
||||
}
|
||||
|
||||
// History is what is kept of one device's link.
|
||||
type History struct {
|
||||
start int64
|
||||
// The counters last read; counted is false before the first read and after the peer was gone.
|
||||
rx, tx int64
|
||||
counted bool
|
||||
// The newest handshake, the endpoint (seen: one was read), and when rx last carried data.
|
||||
hs int64
|
||||
ep string
|
||||
seen bool
|
||||
data int64
|
||||
// Handshake times, endpoint changes, accepted initiations, and what the router sent with nothing received.
|
||||
shakes []int64
|
||||
moves []move
|
||||
knocks []int64
|
||||
answer []answer
|
||||
|
||||
// What is shown (State, "" for nothing), a state waiting for Hold, the last classification and when
|
||||
// it was made.
|
||||
State string
|
||||
Since int64
|
||||
pending *Shown
|
||||
// Raw is the last classification, before the hold.
|
||||
Raw string
|
||||
checked int64
|
||||
|
||||
// For the set-up sheet: online at the last read, the handshake its online stretch began with (0 while
|
||||
// away), what the sheet is told, and when that may change with time alone (0 for never).
|
||||
on bool
|
||||
sess int64
|
||||
Setup *Shown
|
||||
due int64
|
||||
}
|
||||
|
||||
// Fresh is a new, empty history for a device first seen at now.
|
||||
func Fresh(now int64) *History { return &History{start: now} }
|
||||
|
||||
// Change is what Settle changed in what is shown; either may be "".
|
||||
type Change struct {
|
||||
From, To string
|
||||
}
|
||||
|
||||
// KeepaliveMax is the largest rx a keepalive adds for a server's disguise: Keepalive plus the top of its
|
||||
// ContentPaddingAddition, or a full packet with random trailers and no content padding.
|
||||
func KeepaliveMax(srv *model.Server) int64 {
|
||||
if srv == nil {
|
||||
return Keepalive
|
||||
}
|
||||
if cpa := srv.Obf["content_padding_addition"]; cpa != "" {
|
||||
parts := strings.Split(cpa, "-")
|
||||
if hi, err := strconv.ParseFloat(strings.TrimSpace(parts[len(parts)-1]), 64); err == nil && hi > 0 {
|
||||
return Keepalive + int64(hi)
|
||||
}
|
||||
}
|
||||
if t := srv.Obf["random_trailers"]; t == "on" || t == "1" {
|
||||
return Keepalive + trailerMax
|
||||
}
|
||||
return Keepalive
|
||||
}
|
||||
|
||||
// knock tells an rx increment made of accepted initiations only.
|
||||
func knock(drx int64) bool {
|
||||
return drx > 0 && drx%Initiation == 0 && drx <= knocksPerTick*Initiation
|
||||
}
|
||||
|
||||
// bare tells an rx increment that carried no data: nothing, or one keepalive of up to ka bytes; in the
|
||||
// second a handshake completed (shook) also the handshake messages that came with it. Outside that second
|
||||
// a handshake message is never counted in: with a padded keepalive their sums would cover every small
|
||||
// packet up to some 670 bytes, and a device with light traffic would look like one whose data never comes
|
||||
// through.
|
||||
func bare(drx, ka int64, shook bool) bool {
|
||||
if drx <= 0 {
|
||||
return true
|
||||
}
|
||||
if !shook {
|
||||
return drx >= Keepalive && drx <= ka
|
||||
}
|
||||
if drx > knocksPerTick*Initiation+Response+ka {
|
||||
return false
|
||||
}
|
||||
for k := int64(0); k <= knocksPerTick; k++ {
|
||||
for r := int64(0); r <= 1; r++ {
|
||||
rest := drx - k*Initiation - r*Response
|
||||
if rest == 0 || (rest >= Keepalive && rest <= ka) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func keep[T any](list []T, entry T) []T {
|
||||
list = append(list, entry)
|
||||
if len(list) > keepEntries {
|
||||
list = list[1:]
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
// Observe folds one read of the device's peer into its history; ka is its KeepaliveMax. It returns true
|
||||
// when something worth classifying again happened (a handshake, a new endpoint, a knock, an answer, data
|
||||
// after a quiet span). Counters that went down (the peer added again, its tunnel set up again) start a new
|
||||
// baseline.
|
||||
func (h *History) Observe(p Read, now, ka int64) bool {
|
||||
changed := false
|
||||
shook := p.Handshake > h.hs
|
||||
if shook {
|
||||
h.shakes = keep(h.shakes, p.Handshake)
|
||||
h.hs = p.Handshake
|
||||
changed = true
|
||||
}
|
||||
if !h.seen || p.Endpoint != h.ep {
|
||||
if h.seen && p.Endpoint != "" {
|
||||
h.moves = keep(h.moves, move{at: now, to: p.Endpoint, from: h.ep})
|
||||
changed = true
|
||||
}
|
||||
h.ep, h.seen = p.Endpoint, true
|
||||
}
|
||||
if !h.counted || p.Rx < h.rx || p.Tx < h.tx {
|
||||
h.rx, h.tx, h.counted = p.Rx, p.Tx, true
|
||||
return changed
|
||||
}
|
||||
drx, dtx := p.Rx-h.rx, p.Tx-h.tx
|
||||
h.rx, h.tx = p.Rx, p.Tx
|
||||
switch {
|
||||
case drx > 0 && !shook && knock(drx):
|
||||
h.knocks = keep(h.knocks, now)
|
||||
changed = true
|
||||
case drx > 0 && !bare(drx, ka, shook):
|
||||
if now-h.data >= dropSpan {
|
||||
changed = true
|
||||
}
|
||||
h.data = now
|
||||
case drx <= 0 && dtx > 0:
|
||||
h.answer = keep(h.answer, answer{at: now, bytes: dtx})
|
||||
changed = true
|
||||
}
|
||||
return changed
|
||||
}
|
||||
|
||||
// Gone: the device's peer is not in its tunnel; the next read starts a new baseline.
|
||||
func (h *History) Gone() { h.counted = false }
|
||||
|
||||
// prune forgets events older than HistorySpan seconds.
|
||||
func (h *History) prune(now int64) {
|
||||
cut := now - HistorySpan
|
||||
h.shakes = after(h.shakes, func(t int64) bool { return t > cut })
|
||||
h.knocks = after(h.knocks, func(t int64) bool { return t > cut })
|
||||
h.moves = after(h.moves, func(e move) bool { return e.at > cut })
|
||||
h.answer = after(h.answer, func(e answer) bool { return e.at > cut })
|
||||
}
|
||||
|
||||
func after[T any](list []T, keep func(T) bool) []T {
|
||||
out := list[:0:0]
|
||||
for _, e := range list {
|
||||
if keep(e) {
|
||||
out = append(out, e)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// returns counts endpoint changes within the span that went back to an endpoint seen before in it
|
||||
// (A → B → A → B: 2).
|
||||
func (h *History) returns(now int64) int {
|
||||
var seen map[string]bool
|
||||
n := 0
|
||||
for _, e := range h.moves {
|
||||
if now-e.at >= churnSpan {
|
||||
continue
|
||||
}
|
||||
if seen == nil {
|
||||
seen = map[string]bool{e.from: true}
|
||||
}
|
||||
if seen[e.to] {
|
||||
n++
|
||||
}
|
||||
seen[e.to] = true
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (h *History) churning(now int64) bool {
|
||||
var hs []int64
|
||||
for _, t := range h.shakes {
|
||||
if now-t < churnSpan {
|
||||
hs = append(hs, t)
|
||||
}
|
||||
}
|
||||
if len(hs) < churnCount {
|
||||
return false
|
||||
}
|
||||
short := 0
|
||||
for i := 1; i < len(hs); i++ {
|
||||
if hs[i]-hs[i-1] < churnGap {
|
||||
short++
|
||||
}
|
||||
}
|
||||
return short >= churnCount-1
|
||||
}
|
||||
|
||||
func (h *History) dropped(now int64) bool {
|
||||
if now-h.start < dropSpan || now-h.data < dropSpan || !(h.hs > 0 && now-h.hs < dropHS) {
|
||||
return false
|
||||
}
|
||||
var n, bytes int64
|
||||
for _, a := range h.answer {
|
||||
if now-a.at < dropSpan {
|
||||
n++
|
||||
bytes += a.bytes
|
||||
}
|
||||
}
|
||||
return n >= dropAnswers && bytes < dropTx
|
||||
}
|
||||
|
||||
// Classify is what the history says about the device at now (on: it is online, as presence decides it):
|
||||
//
|
||||
// "" nothing wrong, or nothing to tell
|
||||
// trying not online, but its initiations reach the router and are answered, and it never finishes
|
||||
// connecting (the network may block it, or its configuration is out of date)
|
||||
// shared connecting over and over from two or more alternating endpoints: one configuration on two devices
|
||||
// unstable connecting over and over from one place: the connection keeps breaking
|
||||
// dropped connected, but nothing it sends gets through: the router drops it (an old address in its
|
||||
// configuration)
|
||||
func (h *History) Classify(now int64, on bool) string {
|
||||
if !on {
|
||||
n := 0
|
||||
for _, t := range h.knocks {
|
||||
if now-t < trySpan {
|
||||
n++
|
||||
}
|
||||
}
|
||||
if n >= tryKnocks {
|
||||
return Trying
|
||||
}
|
||||
return ""
|
||||
}
|
||||
if h.churning(now) {
|
||||
if h.returns(now) >= shareReturns {
|
||||
return Shared
|
||||
}
|
||||
return Unstable
|
||||
}
|
||||
if h.dropped(now) {
|
||||
return Dropped
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Settle turns a classification into what is shown: a state, or a change of state, shows once Classify
|
||||
// gave it for Hold seconds (Since: when it began); nothing wrong shows at once when the device is online
|
||||
// (it works), else after Hold as well. It returns the change, or nil when what is shown stays.
|
||||
func (h *History) Settle(raw string, now int64, on bool) *Change {
|
||||
if raw == h.State {
|
||||
h.pending = nil
|
||||
return nil
|
||||
}
|
||||
since := now
|
||||
if raw != "" || !on {
|
||||
if h.pending == nil || h.pending.State != raw {
|
||||
h.pending = &Shown{State: raw, Since: now}
|
||||
return nil
|
||||
}
|
||||
if now-h.pending.Since < Hold {
|
||||
return nil
|
||||
}
|
||||
since = h.pending.Since
|
||||
}
|
||||
c := &Change{From: h.State, To: raw}
|
||||
h.State = raw
|
||||
h.Since = 0
|
||||
if raw != "" {
|
||||
h.Since = since
|
||||
}
|
||||
h.pending = nil
|
||||
return c
|
||||
}
|
||||
|
||||
// SetupState is what the device's link does at now for the set-up sheet (pure), and when that may change
|
||||
// with time alone (0 for never: anything else comes with a read):
|
||||
//
|
||||
// trying its initiations reach the router and are answered, and no handshake follows
|
||||
// flowing online, and data came in during this session
|
||||
// nodata online for a while, nothing but handshakes and keepalives came in, and the router answered
|
||||
// packets it dropped: an old address in the configuration
|
||||
// "" none of these (waiting, idle, or away)
|
||||
func (h *History) SetupState(now int64, on bool) (state string, due int64) {
|
||||
n := 0
|
||||
for i := len(h.knocks) - 1; i >= 0; i-- {
|
||||
t := h.knocks[i]
|
||||
if t <= h.hs || now-t >= setupSpan {
|
||||
break
|
||||
}
|
||||
if n++; n == setupKnocks {
|
||||
return Trying, t + setupSpan
|
||||
}
|
||||
}
|
||||
if !on || h.sess == 0 {
|
||||
return "", 0
|
||||
}
|
||||
if h.data >= h.sess {
|
||||
return Flowing, 0
|
||||
}
|
||||
if now-h.sess < setupQuiet {
|
||||
return "", h.sess + setupQuiet
|
||||
}
|
||||
if len(h.answer) > 0 && h.answer[len(h.answer)-1].at >= h.sess {
|
||||
return NoData, 0
|
||||
}
|
||||
return "", 0
|
||||
}
|
||||
|
||||
// setup brings Setup to SetupState after a read (moved: what Observe returned). Most reads cost a few
|
||||
// compares: the state is worked out again only when something happened, the device came or went, data
|
||||
// came in while it is not flowing, or its due time came. It returns true when Setup changed.
|
||||
func (h *History) setup(now int64, on, moved bool) bool {
|
||||
// A session begins with the handshake that brought the device online.
|
||||
if !on {
|
||||
h.sess = 0
|
||||
} else if h.sess == 0 {
|
||||
h.sess = h.hs
|
||||
}
|
||||
came := on != h.on
|
||||
h.on = on
|
||||
cur := ""
|
||||
if h.Setup != nil {
|
||||
cur = h.Setup.State
|
||||
}
|
||||
if !moved && !came && !(h.due != 0 && now >= h.due) && !(h.data == now && cur != Flowing) {
|
||||
return false
|
||||
}
|
||||
state, due := h.SetupState(now, on)
|
||||
h.due = due
|
||||
if state == cur {
|
||||
return false
|
||||
}
|
||||
h.Setup = nil
|
||||
if state != "" {
|
||||
h.Setup = &Shown{State: state, Since: now}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Step is one look at the device: its read folded in (nil: its peer is not in its tunnel), the set-up
|
||||
// state brought up to date, and, when something happened or CheckEvery passed, the link classified again
|
||||
// and settled. It returns the change in what is shown (nil for none) and whether Setup changed.
|
||||
func (h *History) Step(p *Read, now int64, on bool, ka int64) (change *Change, setupChanged bool) {
|
||||
moved := false
|
||||
if p != nil {
|
||||
moved = h.Observe(*p, now, ka)
|
||||
} else {
|
||||
h.Gone()
|
||||
}
|
||||
setupChanged = h.setup(now, on, moved)
|
||||
if !moved && now-h.checked < CheckEvery {
|
||||
return nil, setupChanged
|
||||
}
|
||||
h.checked = now
|
||||
h.prune(now)
|
||||
h.Raw = h.Classify(now, on)
|
||||
return h.Settle(h.Raw, now, on), setupChanged
|
||||
}
|
||||
|
||||
// ShownOf is what is shown for every device, devices with nothing to show left out (link.json).
|
||||
func ShownOf(hist map[string]*History) map[string]Shown {
|
||||
out := map[string]Shown{}
|
||||
for id, h := range hist {
|
||||
if h.State != "" {
|
||||
out[id] = Shown{State: h.State, Since: h.Since}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// SetupsOf is the set-up sheet's state of every device, devices with none left out (setup.json).
|
||||
func SetupsOf(hist map[string]*History) map[string]Shown {
|
||||
out := map[string]Shown{}
|
||||
for id, h := range hist {
|
||||
if h.Setup != nil {
|
||||
out[id] = *h.Setup
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Event is what the activity log is told of a change: device_failing (the new state) when a state shows,
|
||||
// device_recovered (the state it had) when it clears while the device is online. One that clears while the
|
||||
// device is away (it stopped trying, it was paused) logs nothing: nobody saw it work. kind is "" for none.
|
||||
func Event(c *Change, on bool) (kind, detail string) {
|
||||
switch {
|
||||
case c == nil:
|
||||
return "", ""
|
||||
case c.To != "":
|
||||
return "device_failing", c.To
|
||||
case on && c.From != "":
|
||||
return "device_recovered", c.From
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package link
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/tickcount/luci-app-dawnline/backend/model"
|
||||
)
|
||||
|
||||
// The generator of testdata/trace.uc, number for number.
|
||||
type gen struct{ x int64 }
|
||||
|
||||
func (g *gen) rnd(n int64) int64 {
|
||||
g.x = (g.x*1103515245 + 12345) % 2147483648
|
||||
return (g.x >> 8) % n
|
||||
}
|
||||
|
||||
var endpoints = []string{"203.0.113.7:41821", "198.51.100.4:9000", "[2001:db8::1]:500"}
|
||||
|
||||
var modes = [][]int64{
|
||||
{3, 40, 30, 60, 200, 300, 310, 150, 160},
|
||||
{1, 0, 5, 400, 420, 420, 425, 300, 300},
|
||||
{2, 150, 200, 20, 300, 500, 505, 100, 105},
|
||||
{1, 10, 2, 0, 250, 250, 252, 400, 400},
|
||||
}
|
||||
|
||||
type run struct {
|
||||
Seed int64 `json:"seed"`
|
||||
Mode int `json:"mode"`
|
||||
Ka int64 `json:"ka"`
|
||||
Steps int `json:"steps"`
|
||||
Hash int64 `json:"hash"`
|
||||
Setups int `json:"setups"`
|
||||
Changes [][]any `json:"changes"`
|
||||
}
|
||||
|
||||
// The same made-up reads through this package as trace.uc put through dawnline.link.
|
||||
func trace(seed int64, mode int, ka int64, steps int) run {
|
||||
g := &gen{x: seed}
|
||||
p := modes[mode]
|
||||
now, hs, rx, tx := int64(1000000), int64(0), int64(0), int64(0)
|
||||
ep, present := "", true
|
||||
h := Fresh(now)
|
||||
out := run{Seed: seed, Mode: mode, Ka: ka, Steps: steps, Hash: 7, Changes: [][]any{}}
|
||||
for i := 0; i < steps; i++ {
|
||||
now++
|
||||
r := g.rnd(1000)
|
||||
if present {
|
||||
if r < p[0] {
|
||||
present = false
|
||||
} else {
|
||||
if g.rnd(1000) < p[1] {
|
||||
hs = now - g.rnd(2)
|
||||
}
|
||||
if g.rnd(1000) < p[2] {
|
||||
ep = endpoints[g.rnd(3)]
|
||||
}
|
||||
switch k := g.rnd(1000); {
|
||||
case k < p[3]:
|
||||
rx += 148 * (1 + g.rnd(3))
|
||||
case k < p[4]:
|
||||
rx += 32 + g.rnd(ka-31)
|
||||
case k < p[5]:
|
||||
rx += 48 + 16*g.rnd(90)
|
||||
case k < p[6]:
|
||||
rx = g.rnd(500)
|
||||
tx = g.rnd(500)
|
||||
}
|
||||
if t := g.rnd(1000); t < p[7] {
|
||||
tx += 32 + g.rnd(200)
|
||||
} else if t < p[8] {
|
||||
tx += 100000
|
||||
}
|
||||
}
|
||||
} else if r < 100 {
|
||||
present, rx, tx, hs = true, 0, 0, 0
|
||||
}
|
||||
on := present && hs > 0 && now-hs < 190
|
||||
var read *Read
|
||||
if present {
|
||||
read = &Read{Endpoint: ep, Handshake: hs, Rx: rx, Tx: tx}
|
||||
}
|
||||
change, sc := h.Step(read, now, on, ka)
|
||||
word, flag := "-", 0
|
||||
if sc {
|
||||
out.Setups++
|
||||
flag = 1
|
||||
}
|
||||
if change != nil {
|
||||
out.Changes = append(out.Changes, []any{float64(i), change.From, change.To})
|
||||
word = change.From + ">" + change.To
|
||||
}
|
||||
setup, since := "", int64(0)
|
||||
if h.Setup != nil {
|
||||
setup, since = h.Setup.State, h.Setup.Since
|
||||
}
|
||||
for _, c := range []byte(fmt.Sprintf("%s|%d|%s|%d|%s|%s|%d\n", word, flag, h.State, h.Since, h.Raw, setup, since)) {
|
||||
out.Hash = (out.Hash*131 + int64(c)) % 1000000007
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Step for step what dawnline.link does (testdata/trace.json, written by scripts/ucode-golden.sh).
|
||||
func TestHeldToUcode(t *testing.T) {
|
||||
raw, err := os.ReadFile("testdata/trace.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var want []run
|
||||
if err := json.Unmarshal(raw, &want); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(want) < 20 {
|
||||
t.Fatalf("%d runs in the golden", len(want))
|
||||
}
|
||||
states := map[string]bool{}
|
||||
for _, w := range want {
|
||||
got := trace(w.Seed, w.Mode, w.Ka, w.Steps)
|
||||
if !reflect.DeepEqual(got.Changes, w.Changes) {
|
||||
t.Errorf("mode %d ka %d seed %d: shown changes %v, ucode has %v", w.Mode, w.Ka, w.Seed, got.Changes, w.Changes)
|
||||
continue
|
||||
}
|
||||
if got.Setups != w.Setups || got.Hash != w.Hash {
|
||||
t.Errorf("mode %d ka %d seed %d: the same changes, but %d set-up changes and hash %d; ucode has %d and %d",
|
||||
w.Mode, w.Ka, w.Seed, got.Setups, got.Hash, w.Setups, w.Hash)
|
||||
}
|
||||
for _, c := range w.Changes {
|
||||
states[c[2].(string)] = true
|
||||
}
|
||||
}
|
||||
for _, s := range []string{Trying, Shared, Dropped, Unstable, ""} {
|
||||
if !states[s] {
|
||||
t.Errorf("the golden never shows %q: the runs no longer exercise it", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeepaliveMax(t *testing.T) {
|
||||
for name, c := range map[string]struct {
|
||||
obf map[string]string
|
||||
want int64
|
||||
}{
|
||||
"plain": {map[string]string{}, 32},
|
||||
"padded": {map[string]string{"content_padding_addition": "10-100"}, 132},
|
||||
"one padding": {map[string]string{"content_padding_addition": "40"}, 72},
|
||||
"trailers": {map[string]string{"random_trailers": "on"}, 1532},
|
||||
"padding over trailers": {map[string]string{"content_padding_addition": "10-100", "random_trailers": "on"}, 132},
|
||||
"no padding to speak of": {map[string]string{"content_padding_addition": "0", "random_trailers": "1"}, 1532},
|
||||
} {
|
||||
if got := KeepaliveMax(&model.Server{Obf: c.obf}); got != c.want {
|
||||
t.Errorf("%s: %d, want %d", name, got, c.want)
|
||||
}
|
||||
}
|
||||
if KeepaliveMax(nil) != 32 {
|
||||
t.Error("no server")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEvent(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
change *Change
|
||||
on bool
|
||||
kind, detail string
|
||||
}{
|
||||
{nil, true, "", ""},
|
||||
{&Change{To: Trying}, false, "device_failing", Trying},
|
||||
{&Change{From: Shared, To: Dropped}, true, "device_failing", Dropped},
|
||||
{&Change{From: Dropped}, true, "device_recovered", Dropped},
|
||||
{&Change{From: Trying}, false, "", ""},
|
||||
} {
|
||||
if k, d := Event(c.change, c.on); k != c.kind || d != c.detail {
|
||||
t.Errorf("%+v on=%v: %q %q", c.change, c.on, k, d)
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+402
@@ -0,0 +1,402 @@
|
||||
[
|
||||
{
|
||||
"seed": 43,
|
||||
"mode": 0,
|
||||
"ka": 32,
|
||||
"steps": 1500,
|
||||
"hash": 509250670,
|
||||
"setups": 53,
|
||||
"changes": [
|
||||
[
|
||||
562,
|
||||
"",
|
||||
"shared"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 2058,
|
||||
"mode": 0,
|
||||
"ka": 32,
|
||||
"steps": 1500,
|
||||
"hash": 116816300,
|
||||
"setups": 57,
|
||||
"changes": [
|
||||
[
|
||||
301,
|
||||
"",
|
||||
"shared"
|
||||
],
|
||||
[
|
||||
684,
|
||||
"shared",
|
||||
"unstable"
|
||||
],
|
||||
[
|
||||
1100,
|
||||
"unstable",
|
||||
"shared"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 143,
|
||||
"mode": 0,
|
||||
"ka": 132,
|
||||
"steps": 1500,
|
||||
"hash": 874068518,
|
||||
"setups": 62,
|
||||
"changes": [
|
||||
[
|
||||
877,
|
||||
"",
|
||||
"unstable"
|
||||
],
|
||||
[
|
||||
1178,
|
||||
"unstable",
|
||||
""
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 2158,
|
||||
"mode": 0,
|
||||
"ka": 132,
|
||||
"steps": 1500,
|
||||
"hash": 984212801,
|
||||
"setups": 52,
|
||||
"changes": [
|
||||
[
|
||||
361,
|
||||
"",
|
||||
"shared"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 1543,
|
||||
"mode": 0,
|
||||
"ka": 1532,
|
||||
"steps": 1500,
|
||||
"hash": 902450080,
|
||||
"setups": 69,
|
||||
"changes": [
|
||||
[
|
||||
255,
|
||||
"",
|
||||
"unstable"
|
||||
],
|
||||
[
|
||||
474,
|
||||
"unstable",
|
||||
""
|
||||
],
|
||||
[
|
||||
729,
|
||||
"",
|
||||
"shared"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 3558,
|
||||
"mode": 0,
|
||||
"ka": 1532,
|
||||
"steps": 1500,
|
||||
"hash": 322096214,
|
||||
"setups": 60,
|
||||
"changes": [
|
||||
[
|
||||
325,
|
||||
"",
|
||||
"shared"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 1043,
|
||||
"mode": 1,
|
||||
"ka": 32,
|
||||
"steps": 1500,
|
||||
"hash": 336330855,
|
||||
"setups": 3,
|
||||
"changes": [
|
||||
[
|
||||
187,
|
||||
"",
|
||||
"trying"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 3058,
|
||||
"mode": 1,
|
||||
"ka": 32,
|
||||
"steps": 1500,
|
||||
"hash": 418109476,
|
||||
"setups": 5,
|
||||
"changes": [
|
||||
[
|
||||
188,
|
||||
"",
|
||||
"trying"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 1143,
|
||||
"mode": 1,
|
||||
"ka": 132,
|
||||
"steps": 1500,
|
||||
"hash": 984771206,
|
||||
"setups": 3,
|
||||
"changes": [
|
||||
[
|
||||
197,
|
||||
"",
|
||||
"trying"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 3158,
|
||||
"mode": 1,
|
||||
"ka": 132,
|
||||
"steps": 1500,
|
||||
"hash": 839850834,
|
||||
"setups": 9,
|
||||
"changes": [
|
||||
[
|
||||
186,
|
||||
"",
|
||||
"trying"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 2543,
|
||||
"mode": 1,
|
||||
"ka": 1532,
|
||||
"steps": 1500,
|
||||
"hash": 243396944,
|
||||
"setups": 1,
|
||||
"changes": [
|
||||
[
|
||||
187,
|
||||
"",
|
||||
"trying"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 4558,
|
||||
"mode": 1,
|
||||
"ka": 1532,
|
||||
"steps": 1500,
|
||||
"hash": 72908559,
|
||||
"setups": 1,
|
||||
"changes": [
|
||||
[
|
||||
185,
|
||||
"",
|
||||
"trying"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 2043,
|
||||
"mode": 2,
|
||||
"ka": 32,
|
||||
"steps": 1500,
|
||||
"hash": 493844650,
|
||||
"setups": 3,
|
||||
"changes": [
|
||||
[
|
||||
211,
|
||||
"",
|
||||
"shared"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 4058,
|
||||
"mode": 2,
|
||||
"ka": 32,
|
||||
"steps": 1500,
|
||||
"hash": 65535240,
|
||||
"setups": 13,
|
||||
"changes": [
|
||||
[
|
||||
196,
|
||||
"",
|
||||
"shared"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 2143,
|
||||
"mode": 2,
|
||||
"ka": 132,
|
||||
"steps": 1500,
|
||||
"hash": 401228843,
|
||||
"setups": 15,
|
||||
"changes": [
|
||||
[
|
||||
228,
|
||||
"",
|
||||
"shared"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 4158,
|
||||
"mode": 2,
|
||||
"ka": 132,
|
||||
"steps": 1500,
|
||||
"hash": 984996124,
|
||||
"setups": 9,
|
||||
"changes": [
|
||||
[
|
||||
206,
|
||||
"",
|
||||
"shared"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 3543,
|
||||
"mode": 2,
|
||||
"ka": 1532,
|
||||
"steps": 1500,
|
||||
"hash": 450519450,
|
||||
"setups": 7,
|
||||
"changes": [
|
||||
[
|
||||
227,
|
||||
"",
|
||||
"shared"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 5558,
|
||||
"mode": 2,
|
||||
"ka": 1532,
|
||||
"steps": 1500,
|
||||
"hash": 981222292,
|
||||
"setups": 9,
|
||||
"changes": [
|
||||
[
|
||||
207,
|
||||
"",
|
||||
"shared"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 3043,
|
||||
"mode": 3,
|
||||
"ka": 32,
|
||||
"steps": 1500,
|
||||
"hash": 247211500,
|
||||
"setups": 5,
|
||||
"changes": [
|
||||
[
|
||||
366,
|
||||
"",
|
||||
"dropped"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 5058,
|
||||
"mode": 3,
|
||||
"ka": 32,
|
||||
"steps": 1500,
|
||||
"hash": 199176889,
|
||||
"setups": 5,
|
||||
"changes": [
|
||||
[
|
||||
726,
|
||||
"",
|
||||
"dropped"
|
||||
],
|
||||
[
|
||||
1296,
|
||||
"dropped",
|
||||
""
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 3143,
|
||||
"mode": 3,
|
||||
"ka": 132,
|
||||
"steps": 1500,
|
||||
"hash": 546318254,
|
||||
"setups": 5,
|
||||
"changes": [
|
||||
[
|
||||
455,
|
||||
"",
|
||||
"unstable"
|
||||
],
|
||||
[
|
||||
652,
|
||||
"unstable",
|
||||
""
|
||||
],
|
||||
[
|
||||
988,
|
||||
"",
|
||||
"dropped"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 5158,
|
||||
"mode": 3,
|
||||
"ka": 132,
|
||||
"steps": 1500,
|
||||
"hash": 746160914,
|
||||
"setups": 2,
|
||||
"changes": [
|
||||
[
|
||||
361,
|
||||
"",
|
||||
"dropped"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 4543,
|
||||
"mode": 3,
|
||||
"ka": 1532,
|
||||
"steps": 1500,
|
||||
"hash": 147637710,
|
||||
"setups": 5,
|
||||
"changes": [
|
||||
[
|
||||
362,
|
||||
"",
|
||||
"dropped"
|
||||
]
|
||||
]
|
||||
},
|
||||
{
|
||||
"seed": 6558,
|
||||
"mode": 3,
|
||||
"ka": 1532,
|
||||
"steps": 1500,
|
||||
"hash": 890972522,
|
||||
"setups": 7,
|
||||
"changes": [
|
||||
[
|
||||
1019,
|
||||
"",
|
||||
"dropped"
|
||||
]
|
||||
]
|
||||
}
|
||||
]
|
||||
Reference in new issue
Block a user