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.
360 lines
10 KiB
Go
360 lines
10 KiB
Go
// Package pkgmgr is one interface over the router's package manager: opkg on OpenWrt 24.10, apk on 25.12
|
|
// (dawnline.pkg). Commands that change packages take a log(line) callback, which gets what they say.
|
|
package pkgmgr
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// Manager is the router's package manager.
|
|
type Manager struct {
|
|
// Run runs a shell command: what it said (both streams) and its exit code.
|
|
Run func(cmd string) (out string, code int)
|
|
// Apk tells whether this router has apk (else opkg).
|
|
Apk bool
|
|
// T puts a sentence in the job's language (nil: as it is).
|
|
T func(msg string) string
|
|
}
|
|
|
|
// Failure is a refusal with its words (already in the job's language).
|
|
type Failure struct{ Msg string }
|
|
|
|
func (f Failure) Error() string { return f.Msg }
|
|
|
|
// System is the router's own, by what is installed.
|
|
func System(run func(cmd string) (string, int)) *Manager {
|
|
st, err := os.Stat("/usr/bin/apk")
|
|
return &Manager{Run: run, Apk: err == nil && st.Mode()&0o111 != 0}
|
|
}
|
|
|
|
func (m *Manager) t(msg string) string {
|
|
if m.T != nil {
|
|
return m.T(msg)
|
|
}
|
|
return msg
|
|
}
|
|
|
|
// Q quotes a word for the shell.
|
|
func Q(s string) string { return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" }
|
|
|
|
func qAll(list []string) string {
|
|
q := make([]string, len(list))
|
|
for i, s := range list {
|
|
q[i] = Q(s)
|
|
}
|
|
return strings.Join(q, " ")
|
|
}
|
|
|
|
const blank = " \t\r\n\v\f"
|
|
|
|
func (m *Manager) logged(cmd string, log func(string)) (string, int) {
|
|
out, code := m.Run(cmd)
|
|
if log != nil {
|
|
for _, line := range strings.Split(strings.Trim(out, blank), "\n") {
|
|
if strings.Trim(line, blank) != "" {
|
|
log(" " + line)
|
|
}
|
|
}
|
|
}
|
|
return out, code
|
|
}
|
|
|
|
// Ext is the package files' extension here.
|
|
func (m *Manager) Ext() string {
|
|
if m.Apk {
|
|
return "apk"
|
|
}
|
|
return "ipk"
|
|
}
|
|
|
|
// Version is the installed version of a package, or "".
|
|
func (m *Manager) Version(name string) string {
|
|
esc := regexp.QuoteMeta(name)
|
|
if m.Apk {
|
|
out, _ := m.Run("apk list --installed " + Q(name))
|
|
if f := regexp.MustCompile(`^` + esc + `-([^ ]+) `).FindStringSubmatch(out); f != nil {
|
|
return f[1]
|
|
}
|
|
// a name another package provides (OpenWrt 25.12: «acme» is acme-acmesh's): that package's version
|
|
out, _ = m.Run("apk list --installed --providers " + Q(name))
|
|
if f := regexp.MustCompile(`(?m)^<` + esc + `> \S+?-(\d[^ ]*) `).FindStringSubmatch(out); f != nil {
|
|
return f[1]
|
|
}
|
|
return ""
|
|
}
|
|
// libraries carry their ABI in the name (libustream-openssl20201210)
|
|
re := regexp.MustCompile(`^` + esc + `([0-9]+)? - (.+)$`)
|
|
out, _ := m.Run("opkg list-installed")
|
|
found, exact := "", false
|
|
for _, line := range strings.Split(out, "\n") {
|
|
if f := re.FindStringSubmatch(strings.Trim(line, blank)); f != nil && (found == "" || (f[1] == "" && !exact)) {
|
|
found, exact = strings.Trim(f[2], blank), f[1] == ""
|
|
}
|
|
}
|
|
return found
|
|
}
|
|
|
|
var (
|
|
ipkName = regexp.MustCompile(`(^|\n)Package: *([^\n]+)`)
|
|
ipkVersion = regexp.MustCompile(`(^|\n)Version: *([^\n]+)`)
|
|
apkName = regexp.MustCompile(`\n *name: *([^\n]+)`)
|
|
apkVersion = regexp.MustCompile(`\n *version: *([^\n]+)`)
|
|
)
|
|
|
|
// ControlInfo is the package a control text names (name, version; each "" when it does not say). Pure: an
|
|
// ipk's control file, else `apk adbdump` of an apk, whose first name and version are the package's own.
|
|
func ControlInfo(text string, ipk bool) (name, version string) {
|
|
pick := func(re *regexp.Regexp, i int) string {
|
|
if f := re.FindStringSubmatch(text); f != nil {
|
|
return f[i]
|
|
}
|
|
return ""
|
|
}
|
|
if ipk {
|
|
return pick(ipkName, 2), pick(ipkVersion, 2)
|
|
}
|
|
return pick(apkName, 1), pick(apkVersion, 1)
|
|
}
|
|
|
|
// FileInfo is the package inside a file.
|
|
func (m *Manager) FileInfo(path string) (name, version string) {
|
|
if strings.HasSuffix(path, ".ipk") {
|
|
out, _ := m.Run("tar -xzOf " + Q(path) + " ./control.tar.gz | tar -xzO ./control")
|
|
return ControlInfo(out, true)
|
|
}
|
|
out, _ := m.Run("apk adbdump " + Q(path))
|
|
return ControlInfo(out, false)
|
|
}
|
|
|
|
// FileVersion is the version inside a package file, or "".
|
|
func (m *Manager) FileVersion(path string) string {
|
|
_, v := m.FileInfo(path)
|
|
return v
|
|
}
|
|
|
|
// Cmp compares two package versions the way the package manager does (so -r10 is newer than -r9 and ~rc
|
|
// before the release): -1, 0 or 1; ok is false when one is unknown.
|
|
func (m *Manager) Cmp(a, b string) (int, bool) {
|
|
if a == "" || b == "" {
|
|
return 0, false
|
|
}
|
|
if a == b {
|
|
return 0, true
|
|
}
|
|
if m.Apk {
|
|
out, _ := m.Run("apk version -t " + Q(a) + " " + Q(b))
|
|
switch strings.Trim(out, blank) {
|
|
case "<":
|
|
return -1, true
|
|
case ">":
|
|
return 1, true
|
|
case "=":
|
|
return 0, true
|
|
}
|
|
return 0, false
|
|
}
|
|
if _, code := m.Run("opkg compare-versions " + Q(a) + " '<<' " + Q(b)); code == 0 {
|
|
return -1, true
|
|
}
|
|
if _, code := m.Run("opkg compare-versions " + Q(a) + " '>>' " + Q(b)); code == 0 {
|
|
return 1, true
|
|
}
|
|
return 0, true
|
|
}
|
|
|
|
// Refresh updates the package lists.
|
|
func (m *Manager) Refresh(log func(string)) bool {
|
|
cmd := "opkg update"
|
|
if m.Apk {
|
|
cmd = "apk update"
|
|
}
|
|
_, code := m.logged(cmd, log)
|
|
return code == 0
|
|
}
|
|
|
|
var dfFree = regexp.MustCompile(`\n\S+\s+[0-9]+\s+[0-9]+\s+([0-9]+)\s`)
|
|
|
|
// preflight: room for the files (or needKB from the feeds). The package manager unpacks before it removes,
|
|
// so a full flash leaves half an install behind. It fails with the numbers.
|
|
func (m *Manager) preflight(paths []string, needKB int64, log func(string)) error {
|
|
out, _ := m.Run("df -Pk /overlay 2>/dev/null || df -Pk / 2>/dev/null")
|
|
f := dfFree.FindStringSubmatch(out)
|
|
if f == nil {
|
|
return nil
|
|
}
|
|
free, _ := strconv.ParseInt(f[1], 10, 64)
|
|
need := needKB
|
|
if need == 0 {
|
|
need = 512
|
|
}
|
|
for _, p := range paths {
|
|
if st, err := os.Stat(p); err == nil {
|
|
need += st.Size() * 3 / 1024
|
|
}
|
|
}
|
|
if free < need {
|
|
return Failure{fmt.Sprintf(m.t("Not enough free space on the router: %d MB free, about %d MB needed. Remove old backups or packages first"), free/1024, need/1024+1)}
|
|
}
|
|
if log != nil {
|
|
log(" " + fmt.Sprintf(m.t("%d MB free on the overlay"), free/1024))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Dawnline's own package: a file of it tells whether apk really reinstalled it.
|
|
const (
|
|
self = "luci-app-dawnline"
|
|
selfMarker = "/usr/share/dawnline/version"
|
|
)
|
|
|
|
// Wanted is a package file and what it holds.
|
|
type Wanted struct {
|
|
Name, Version, File string
|
|
}
|
|
|
|
// Mismatched is the packages an apk install did not leave at their file's version: apk takes a given file
|
|
// as it is (an older version too, a rollback) but leaves an installed version alone, whatever the file.
|
|
// Pure: have is the installed version of each name.
|
|
func Mismatched(want []Wanted, have map[string]string) []Wanted {
|
|
var bad []Wanted
|
|
for _, w := range want {
|
|
if w.Name == "" || w.Version == "" || have[w.Name] != w.Version {
|
|
bad = append(bad, w)
|
|
}
|
|
}
|
|
return bad
|
|
}
|
|
|
|
// InstallFiles installs package files (from a release): true when every one is in. force also reinstalls
|
|
// the same version or goes back to an older one (opkg; apk takes a file as it is: Dawnline's own package
|
|
// is checked by a file of it, any other by its installed version).
|
|
func (m *Manager) InstallFiles(paths []string, force bool, log func(string)) (bool, error) {
|
|
if err := m.preflight(paths, 0, log); err != nil {
|
|
return false, err
|
|
}
|
|
args := qAll(paths)
|
|
if !m.Apk {
|
|
flags := ""
|
|
if force {
|
|
flags = "--force-reinstall --force-downgrade "
|
|
}
|
|
_, code := m.logged("opkg install "+flags+args, log)
|
|
return code == 0, nil
|
|
}
|
|
var want []Wanted
|
|
own := false
|
|
for _, f := range paths {
|
|
n, v := m.FileInfo(f)
|
|
want = append(want, Wanted{n, v, f})
|
|
own = own || n == self
|
|
}
|
|
// Dawnline's own reinstall that changed nothing is a failure, not a silent "done"
|
|
var before int64
|
|
if st, err := os.Stat(selfMarker); own && force && err == nil {
|
|
before = st.ModTime().UnixNano()
|
|
}
|
|
if _, code := m.logged("apk add --allow-untrusted "+args, log); code != 0 {
|
|
return false, nil
|
|
}
|
|
if own {
|
|
if st, err := os.Stat(selfMarker); force && before != 0 && err == nil && st.ModTime().UnixNano() == before {
|
|
if log != nil {
|
|
log(" " + m.t("apk did not reinstall the version that is already installed"))
|
|
}
|
|
return false, Failure{m.t("apk doesn't reinstall an installed version. Install a different release, or run install.sh on the router")}
|
|
}
|
|
return true, nil
|
|
}
|
|
// any other package took when it is at its file's version (the same version again counts)
|
|
have := map[string]string{}
|
|
for _, w := range want {
|
|
if w.Name != "" {
|
|
have[w.Name] = m.Version(w.Name)
|
|
}
|
|
}
|
|
bad := Mismatched(want, have)
|
|
for _, w := range bad {
|
|
name, version, got := w.Name, w.Version, have[w.Name]
|
|
if name == "" {
|
|
name = w.File
|
|
}
|
|
if version == "" {
|
|
version = "?"
|
|
}
|
|
if got == "" {
|
|
got = "-"
|
|
}
|
|
if log != nil {
|
|
log(" " + fmt.Sprintf(m.t("apk did not install %s %s (installed: %s)"), name, version, got))
|
|
}
|
|
}
|
|
return len(bad) == 0, nil
|
|
}
|
|
|
|
func (m *Manager) feeds(apk, opkg string, names []string, log func(string)) (bool, error) {
|
|
if err := m.preflight(nil, 2048, log); err != nil {
|
|
return false, err
|
|
}
|
|
cmd := opkg
|
|
if m.Apk {
|
|
cmd = apk
|
|
}
|
|
_, code := m.logged(cmd+" "+qAll(names), log)
|
|
return code == 0, nil
|
|
}
|
|
|
|
// Add installs packages by name from the feeds; Upgrade updates them.
|
|
func (m *Manager) Add(names []string, log func(string)) (bool, error) {
|
|
return m.feeds("apk add", "opkg install", names, log)
|
|
}
|
|
|
|
func (m *Manager) Upgrade(names []string, log func(string)) (bool, error) {
|
|
return m.feeds("apk upgrade", "opkg upgrade", names, log)
|
|
}
|
|
|
|
// Remove takes packages away.
|
|
func (m *Manager) Remove(names []string, log func(string)) bool {
|
|
cmd := "opkg remove"
|
|
if m.Apk {
|
|
cmd = "apk del"
|
|
}
|
|
_, code := m.logged(cmd+" "+qAll(names), log)
|
|
return code == 0
|
|
}
|
|
|
|
var (
|
|
// "name-version-rN < newer": the version is what follows the last dash before the release suffix (a
|
|
// name may hold digits, a version no dash)
|
|
apkNewer = regexp.MustCompile(`^(\S+)-([^- ]+-r[0-9]+)\s+<\s+(\S+)`)
|
|
opkgNewer = regexp.MustCompile(`^(\S+) - (\S+) - (\S+)`)
|
|
)
|
|
|
|
// ParseUpgradable reads the package manager's list of what the feeds have newer: the latest by name. Pure.
|
|
func ParseUpgradable(out string, apk bool) map[string]string {
|
|
up := map[string]string{}
|
|
re := opkgNewer
|
|
if apk {
|
|
re = apkNewer
|
|
}
|
|
for _, line := range strings.Split(out, "\n") {
|
|
if f := re.FindStringSubmatch(line); f != nil {
|
|
up[f[1]] = f[3]
|
|
}
|
|
}
|
|
return up
|
|
}
|
|
|
|
// Upgradable is the installed packages with a newer version in the feeds.
|
|
func (m *Manager) Upgradable() map[string]string {
|
|
cmd := "opkg list-upgradable"
|
|
if m.Apk {
|
|
cmd = `apk version -l "<"`
|
|
}
|
|
out, _ := m.Run(cmd)
|
|
return ParseUpgradable(out, m.Apk)
|
|
}
|