Files
dawnline/backend/apply/resolver.go
T
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

199 lines
6.3 KiB
Go

package apply
import (
"encoding/json"
"os"
"regexp"
"strconv"
"strings"
"github.com/tickcount/luci-app-dawnline/backend/model"
"github.com/tickcount/luci-app-dawnline/backend/uci"
)
// The plain resolver for people with Home-only access (dawnline.resolver): real addresses from the WAN's
// DNS, home names from the LAN's. Their internet does not use the tunnel, so fake-IP answers would break
// it. It runs inside the netns while that exists, otherwise on the tunnel interfaces; procd runs dnsmasq
// through /usr/libexec/dawnline/resolver, which reads where from the configuration's first line.
// ResolverPort is the port on the tunnel addresses while the resolver runs on the router itself.
const ResolverPort = 5354
var (
dotted = regexp.MustCompile(`^[0-9.]+$`)
digits = regexp.MustCompile(`^[0-9]+$`)
netifdOK = regexp.MustCompile(`^[A-Za-z0-9_.-]+$`)
)
// Target is where the resolver runs: "ns" (inside the netns, on Address) or "root" (on the tunnels).
type Target struct {
Mode string
Address string
Port int
}
func (r *Router) varEtc() string {
if r.VarEtc != "" {
return r.VarEtc
}
return "/var/etc"
}
func (r *Router) resolverPid() string {
if r.ResolverPid != "" {
return r.ResolverPid
}
return "/var/run/dawnline-dns.pid"
}
// ResolverTarget is where Home-only devices' DNS goes now: inside the netns while it is up, otherwise on
// the tunnel addresses (dawnline.loop resolver_target).
func (r *Router) ResolverTarget() Target {
if active, _, _, transit := r.likeHome(); active && transit != "" {
return Target{Mode: "ns", Address: transit, Port: 53}
}
return Target{Mode: "root", Port: ResolverPort}
}
// wanDNS is the DNS servers the WAN was given (IPv4), else two public ones.
func (r *Router) wanDNS() []string {
var out []string
snap := r.Snapshot()
wan := ""
if d := snap.Devices[snap.WAN]; d != nil {
wan = d.Logical
}
for _, name := range uniq([]string{wan, "wan", "wan6"}) {
if name == "" || !netifdOK.MatchString(name) {
continue
}
said, _ := r.Exec("ubus", "call", "network.interface."+name, "status")
var st struct {
DNS []string `json:"dns-server"`
}
if json.Unmarshal([]byte(said), &st) != nil {
continue
}
for _, a := range st.DNS {
if dotted.MatchString(a) {
out = append(out, a)
}
}
}
if len(out) == 0 {
return []string{"1.1.1.1", "8.8.8.8"}
}
return uniq(out)
}
// lanDomains is the local domain of the LAN's DNS (dnsmasq `domain`) and Dawnline's own for device names.
func (r *Router) lanDomains(m *model.Model) []string {
var out []string
if raw, err := os.ReadFile(r.DHCP); err == nil {
if c, err := uci.Parse(string(raw)); err == nil {
for _, s := range c.OfType("dnsmasq") {
if d, _ := s.Get("domain"); d != "" {
out = append(out, d)
break
}
}
}
}
if m.Settings.Domain != "" {
out = append(out, m.Settings.Domain)
}
return uniq(out)
}
// ResolverRunning tells whether the resolver's process is alive.
func (r *Router) ResolverRunning() bool {
raw, _ := os.ReadFile(r.resolverPid())
pid := strings.Trim(string(raw), " \t\r\n\v\f")
if !digits.MatchString(pid) {
return false
}
_, err := os.Stat("/proc/" + pid)
return err == nil
}
// Resolver writes the resolver's configuration for target, with home names from lan (the LAN's DNS, "":
// none); true when it changed (the running one is killed, and procd starts it again from the new file).
func (r *Router) Resolver(m *model.Model, target Target, lan string) bool {
etc := r.varEtc()
os.MkdirAll(etc, 0o755)
conf, resolv := etc+"/dawnline-dns.conf", etc+"/dawnline-dns.resolv"
// servers that are on: with none, the resolver idles on loopback
var ifs []string
for _, id := range m.ServerIDs() {
if s := m.Servers[id]; s.Enabled && tunnelOK(s) {
ifs = append(ifs, s.Ifname)
}
}
// the paths dnsmasq is told are the router's own, wherever a test keeps the files
lines := []string{"# mode=" + target.Mode, "# Generated by Dawnline: plain DNS for Home-only tunnel clients.", "port=" + strconv.Itoa(target.Port),
"no-hosts", "bogus-priv", "cache-size=1000", "pid-file=/var/run/dawnline-dns.pid", "user=dnsmasq", "group=dnsmasq"}
upstream := r.wanDNS()
if target.Mode == "ns" {
lines = append(lines, "listen-address="+target.Address, "bind-interfaces", "no-resolv")
// upstream from the transit address: forwarded through the router to the WAN
for _, a := range upstream {
lines = append(lines, "server="+a+"@"+target.Address)
}
} else {
// no tunnel on: loopback only (without any interface= line dnsmasq would answer everywhere)
lines = append(lines, "bind-dynamic")
if len(ifs) == 0 {
lines = append(lines, "listen-address=127.0.0.1")
}
for _, i := range ifs {
lines = append(lines, "interface="+i)
}
lines = append(lines, "resolv-file=/var/etc/dawnline-dns.resolv")
body := ""
for _, a := range upstream {
body += "nameserver " + a + "\n"
}
if was, _ := os.ReadFile(resolv); string(was) != body {
os.WriteFile(resolv, []byte(body), 0o644)
os.Chmod(resolv, 0o644)
}
}
if lan != "" {
for _, d := range r.lanDomains(m) {
lines = append(lines, "server=/"+d+"/"+lan)
}
}
text := strings.Join(lines, "\n") + "\n"
where := "loopback only (no tunnel on)"
if target.Mode == "ns" {
where = target.Address + ":" + strconv.Itoa(target.Port) + " in the netns"
} else if len(ifs) > 0 {
where = "port " + strconv.Itoa(target.Port) + " on " + strings.Join(ifs, ", ")
}
if was, _ := os.ReadFile(conf); string(was) == text {
r.say("debug", "dns", "resolver for Home-only devices: unchanged · "+where)
return false
}
os.WriteFile(conf, []byte(text), 0o644)
os.Chmod(conf, 0o644)
home := ""
if lan != "" {
home = " · home names via " + lan
}
r.say("info", "dns", "resolver for Home-only devices: "+conf+" written · "+where+" · upstream "+strings.Join(upstream, " ")+home+"; restarting it")
for _, l := range lines {
r.say("debug", "dns", "dnsmasq: "+l)
}
raw, _ := os.ReadFile(r.resolverPid())
if pid := strings.Trim(string(raw), " \t\r\n\v\f"); digits.MatchString(pid) {
r.Exec("kill", pid)
}
return true
}
// SyncResolver writes the resolver's configuration where it should run now; true when it changed.
func (r *Router) SyncResolver(m *model.Model) bool {
_, _, known, _ := r.likeHome()
return r.Resolver(m, r.ResolverTarget(), r.lanDNS(r.Snapshot(), known))
}