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)) }