Files
Aemeath ac90575b0b
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / Backend (Go: vet, tests, a real ubusd and uhttpd) (push) Blocked by required conditions
CI / Package files (push) Blocked by required conditions
Dawnline 0.20.0
- **Backend rewritten in Go.** One daemon, `dawnlined`, replaces the whole ucode backend: the rpcd plugin, the agent, the CLI and the updater. The package no longer depends on ucode.
- **Per-architecture builds.** The release carries the backend for arm64, ARMv7, mips, mipsle and amd64; the router fetches its own after the package is installed and checks its sha256. Dawnline does not run on other architectures (older ARM, 32-bit x86, RISC-V).
- **Faster on the router.** A firewall sync takes 0.06 s instead of 0.9 s; a full apply takes about 300 ms. The portal is served by the daemon itself, without a CGI process per request.
- **Live pages.** The admin and the portal follow a live stream from the router instead of re-fetching the whole state every 30 seconds.
- **Admin redesigned in the portal's style.** Three sections (Overview · Log · Settings); a server is a page of its own with People, Places and Settings; a device opens as a dialog over its person.
- **Changes wait for «Apply».** Every setting, and a person's access, is staged in one dock before it reaches the router.
- **Configuration rollout.** After a change that makes devices fetch their configuration again, the admin shows how many have fetched it and who has not. Every setting shows beforehand (⟳N) how many devices it will touch.
- **Waiting for a decision.** Overview and People put first the people who need one (access ended, limit reached, a device that does not connect), each with its one action a tap away.
- **Bulk actions on people.** «Select» (or a long press on a card) picks several people to extend by a month, pause or resume at once.
- **Undo.** An action done from a card or a request is undone from its toast.
- **Update dialogs redesigned.** An update is one view from its question to its result; Dawnline's own update runs in it like every other, and the page reloads itself once the new version answers. Other versions are a list, each with its notes and Install on a page of its own.
- **Site to site: networks behind a router.** A router connected as a device can have networks behind it (up to eight, /16 to /30): the server routes them and applies the router's access to them. A network that shares addresses with a tunnel, the router's own networks or another site is refused by name.
- **Routers from the portal.** People can add a router and name its networks on their own page; the admin accepts or declines with a reason, or lets them through without asking.
- **Check access and Health know the networks behind routers.**
- **Usage limits reworked.** A monthly limit no longer follows the calendar. A person's month starts with their first traffic and ends a month later; the next one starts only when they connect again, so time away uses up none of it. Before, everyone's limit reset on the 1st.
- Extra data lasts until that person's month ends; «Reset usage» starts the month anew.
- A limit is for a month or for the whole term; the admin and the portal show when it resets.
- Charts that said «this month» show the last 30 days.
- After the update nobody loses what was counted: the month that was running becomes the person's first and ends on the 1st.
- **Access by time of day removed.** The Hours limit is gone from the admin, the portal and the backend; saved hours are dropped.
- **Expiry simplified.** An end date always stops access, with a heads-up three days before; the per-person choice is gone.
- **No false disconnects from sleeping phones.** A phone or a tablet stays online through ten minutes of silence.
- **Mobile Safari fixes.** Pressing a control no longer sends the page to its top; tab tracks that do not fit scroll; switches animate after a press.
2026-11-13 09:43:50 +03:00

318 lines
6.8 KiB
Go

// Package i18n puts the backend's messages into the admin's language, as dawnline.i18n does through LuCI's
// catalog reader: text is English in the code and looked up in the luci-i18n-dawnline-<lang> catalog
// (/usr/lib/lua/luci/i18n/dawnline.<lang>.lmo), placeholders filled after the lookup.
//
// A catalog: the translations, then an index entry per string sorted by key (key hash, number of forms,
// offset, length: big-endian uint32s), then the index's offset. A key is the msgid, "ctx\x01" before it and
// "\x02<n>" after it for plural form n; its hash is Paul Hsieh's SuperFastHash seeded with the length. The
// Plural-Forms formula sits under key 0. Held to LuCI's own reader on testdata (scripts/ucode-golden.sh).
package i18n
import (
"encoding/binary"
"os"
"regexp"
"sort"
"strconv"
"strings"
)
// Dir is where LuCI keeps its catalogs.
const Dir = "/usr/lib/lua/luci/i18n"
// A Catalog is one language's translations. A nil one speaks English.
type Catalog struct {
data []byte
index []byte
// plural is the Plural-Forms expression ("" when the catalog has none)
plural string
}
var tag = regexp.MustCompile(`^[a-z][a-z][a-z]?([_-][A-Za-z][A-Za-z][A-Za-z]?[A-Za-z]?)?$`)
// Use is the catalog for a language as a page names it ("ru", "pt_BR"), or nil for English, a language
// with no Dawnline catalog, or anything that is no language.
func Use(dir, lang string) *Catalog {
if lang == "en" || !tag.MatchString(lang) {
return nil
}
c, _ := Load(dir + "/dawnline." + strings.ToLower(strings.ReplaceAll(lang, "_", "-")) + ".lmo")
return c
}
// Load reads a catalog file.
func Load(path string) (*Catalog, error) {
raw, err := os.ReadFile(path)
if err != nil {
return nil, err
}
if len(raw) < 4 {
return nil, os.ErrInvalid
}
at := int(binary.BigEndian.Uint32(raw[len(raw)-4:]))
if at > len(raw)-4 || (len(raw)-4-at)%16 != 0 {
return nil, os.ErrInvalid
}
c := &Catalog{data: raw[:at], index: raw[at : len(raw)-4]}
if formula, ok := c.find(0); ok {
if i := strings.Index(formula, "plural="); i >= 0 {
c.plural = strings.TrimSpace(strings.SplitN(formula[i+len("plural="):], ";", 2)[0])
}
}
return c, nil
}
func (c *Catalog) find(key uint32) (string, bool) {
n := len(c.index) / 16
i := sort.Search(n, func(i int) bool { return binary.BigEndian.Uint32(c.index[i*16:]) >= key })
if i == n || binary.BigEndian.Uint32(c.index[i*16:]) != key {
return "", false
}
off, size := int(binary.BigEndian.Uint32(c.index[i*16+8:])), int(binary.BigEndian.Uint32(c.index[i*16+12:]))
if off+size > len(c.data) {
return "", false
}
return string(c.data[off : off+size]), true
}
// canon is a key as the reader looks it up: runs of white space are one space, none at either end.
func canon(s string) string {
return strings.Join(strings.FieldsFunc(s, func(r rune) bool {
return r == ' ' || r == '\t' || r == '\n' || r == '\v' || r == '\f' || r == '\r'
}), " ")
}
// T is msg in the catalog's language: the English when it has no translation.
func (c *Catalog) T(msg string) string {
if c != nil {
if k := canon(msg); k != "" {
if s, ok := c.find(hash([]byte(k))); ok {
return s
}
}
}
return msg
}
// N is one or many for n, by the language's plural rules (Russian has three forms).
func (c *Catalog) N(n int64, one, many string) string {
if c != nil && c.plural != "" {
if form, ok := eval(c.plural, n); ok {
if s, found := c.find(hash([]byte(canon(one) + "\x02" + strconv.FormatInt(form, 10)))); found {
return s
}
}
}
if n == 1 {
return one
}
return many
}
// hash is SuperFastHash of the bytes, seeded with their length (lmo.c sfh_hash).
func hash(data []byte) uint32 {
if len(data) == 0 {
return 0
}
get16 := func(i int) uint32 { return uint32(data[i]) | uint32(data[i+1])<<8 }
s8 := func(i int) uint32 { return uint32(int32(int8(data[i]))) }
h := uint32(len(data))
i := 0
for n := len(data) >> 2; n > 0; n, i = n-1, i+4 {
h += get16(i)
tmp := get16(i+2)<<11 ^ h
h = h<<16 ^ tmp
h += h >> 11
}
switch len(data) & 3 {
case 3:
h += get16(i)
h ^= h << 16
h ^= s8(i+2) << 18
h += h >> 11
case 2:
h += get16(i)
h ^= h << 11
h += h >> 17
case 1:
h += s8(i)
h ^= h << 10
h += h >> 1
}
h ^= h << 3
h += h >> 5
h ^= h << 4
h += h >> 17
h ^= h << 25
h += h >> 6
return h
}
// eval works a Plural-Forms expression out for n: C's operators on whole numbers.
func eval(expr string, n int64) (v int64, ok bool) {
p := &parser{s: expr, n: n}
defer func() {
if recover() != nil {
v, ok = 0, false
}
}()
v = p.ternary()
p.space()
return v, p.i == len(p.s)
}
type parser struct {
s string
i int
n int64
}
func (p *parser) space() {
for p.i < len(p.s) && (p.s[p.i] == ' ' || p.s[p.i] == '\t') {
p.i++
}
}
// take eats op when it is next (and not the start of a longer operator).
func (p *parser) take(op string) bool {
p.space()
if !strings.HasPrefix(p.s[p.i:], op) {
return false
}
if rest := p.s[p.i+len(op):]; len(op) == 1 && rest != "" && strings.ContainsRune("<>!=", rune(op[0])) && rest[0] == '=' {
return false
}
p.i += len(op)
return true
}
func truth(b bool) int64 {
if b {
return 1
}
return 0
}
func (p *parser) ternary() int64 {
c := p.or()
if !p.take("?") {
return c
}
a := p.ternary()
if !p.take(":") {
panic("no :")
}
b := p.ternary()
if c != 0 {
return a
}
return b
}
func (p *parser) or() int64 {
v := p.and()
for p.take("||") {
r := p.and()
v = truth(v != 0 || r != 0)
}
return v
}
func (p *parser) and() int64 {
v := p.equal()
for p.take("&&") {
r := p.equal()
v = truth(v != 0 && r != 0)
}
return v
}
func (p *parser) equal() int64 {
v := p.compare()
for {
switch {
case p.take("=="):
v = truth(v == p.compare())
case p.take("!="):
v = truth(v != p.compare())
default:
return v
}
}
}
func (p *parser) compare() int64 {
v := p.sum()
for {
switch {
case p.take("<="):
v = truth(v <= p.sum())
case p.take(">="):
v = truth(v >= p.sum())
case p.take("<"):
v = truth(v < p.sum())
case p.take(">"):
v = truth(v > p.sum())
default:
return v
}
}
}
func (p *parser) sum() int64 {
v := p.product()
for {
switch {
case p.take("+"):
v += p.product()
case p.take("-"):
v -= p.product()
default:
return v
}
}
}
func (p *parser) product() int64 {
v := p.unary()
for {
switch {
case p.take("*"):
v *= p.unary()
case p.take("/"):
v /= p.unary()
case p.take("%"):
v %= p.unary()
default:
return v
}
}
}
func (p *parser) unary() int64 {
if p.take("!") {
return truth(p.unary() == 0)
}
if p.take("(") {
v := p.ternary()
if !p.take(")") {
panic("no )")
}
return v
}
if p.take("n") {
return p.n
}
p.space()
j := p.i
for j < len(p.s) && p.s[j] >= '0' && p.s[j] <= '9' {
j++
}
v, err := strconv.ParseInt(p.s[p.i:j], 10, 64)
if err != nil {
panic(err)
}
p.i = j
return v
}