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,144 @@
|
||||
package nft
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/tickcount/luci-app-dawnline/backend/model"
|
||||
"github.com/tickcount/luci-app-dawnline/backend/netview"
|
||||
"github.com/tickcount/luci-app-dawnline/backend/uci"
|
||||
)
|
||||
|
||||
// sitesOf is access's household on the golden's router, with routers that have networks behind them:
|
||||
// Boris's in Anna's circle (devices within circles), Kira's where everyone reaches everyone, Clara's with
|
||||
// the internet alone, Dima's with one home network, Eva's with home services, and Hana's whose network is
|
||||
// one the router has itself.
|
||||
func sitesOf(t *testing.T) (*model.Model, *netview.Snapshot) {
|
||||
t.Helper()
|
||||
text, err := os.ReadFile("../access/testdata/access.uci")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := uci.Parse(string(text))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for id, net := range map[string]string{"bphone": "192.168.2.0/24", "kphone": "192.168.3.0/24", "cphone": "192.168.4.0/24",
|
||||
"dphone": "192.168.5.0/24", "ephone": "192.168.6.0/24", "hphone": "192.168.50.0/24"} {
|
||||
c.Set(id, "kind", "router")
|
||||
c.SetList(id, "behind_net", []string{net})
|
||||
}
|
||||
raw, err := os.ReadFile("../netview/testdata/trace.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var router struct {
|
||||
Routes json.RawMessage `json:"routes"`
|
||||
Addrs json.RawMessage `json:"addrs"`
|
||||
Links map[string]netview.Link `json:"links"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &router); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
snap := netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return router.Addrs },
|
||||
Links: func() map[string]netview.Link { return router.Links }})
|
||||
return model.Load(c, nil), snap
|
||||
}
|
||||
|
||||
// A router stands for the networks behind it: they are wherever its address is, and nowhere else.
|
||||
func TestNetworksBehindRoutersInTheSets(t *testing.T) {
|
||||
m, snap := sitesOf(t)
|
||||
states := map[string]string{}
|
||||
for _, id := range m.PersonIDs() {
|
||||
states[id] = "active"
|
||||
}
|
||||
sets := AccessSets(m, states, snap)
|
||||
in := func(set, member string) bool {
|
||||
for _, x := range sets[set] {
|
||||
if x == member {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
for _, c := range []struct {
|
||||
set, member string
|
||||
want bool
|
||||
}{
|
||||
// part of home: with home, with the internet, and with the circle (never with itself)
|
||||
{"lan_ok", "192.168.2.0/24", true},
|
||||
{"lan_all", "192.168.2.0/24", true},
|
||||
{"inet_ok", "192.168.2.0/24", true},
|
||||
{"mesh_pairs", "10.77.0.2 . 192.168.2.0/24", true},
|
||||
{"mesh_pairs", "192.168.2.0/24 . 10.77.0.2", true},
|
||||
{"mesh_pairs", "10.77.0.5 . 192.168.2.0/24", false},
|
||||
{"mesh_pairs", "192.168.2.0/24 . 10.77.0.5", false},
|
||||
// another circle's phone does not reach it
|
||||
{"mesh_pairs", "10.77.0.9 . 192.168.2.0/24", false},
|
||||
// where everyone reaches everyone the pairs are not kept: being part of home is enough
|
||||
{"lan_ok", "192.168.3.0/24", true},
|
||||
// the internet alone
|
||||
{"inet_ok", "192.168.4.0/24", true},
|
||||
{"lan_ok", "192.168.4.0/24", false},
|
||||
{"lan_all", "192.168.4.0/24", false},
|
||||
// one home network: only that one
|
||||
{"lan_part", "192.168.5.0/24 . 192.168.50.0/24", true},
|
||||
{"lan_all", "192.168.5.0/24", false},
|
||||
// home services: what the owner's own devices reach of them
|
||||
{"svc_host", "192.168.6.0/24 . 192.168.50.20", in("svc_host", "10.77.0.8 . 192.168.50.20")},
|
||||
{"lan_ok", "192.168.6.0/24", false},
|
||||
// a network the router has itself is behind nobody
|
||||
{"lan_ok", "192.168.50.0/24", false},
|
||||
{"lan_all", "192.168.50.0/24", false},
|
||||
{"inet_ok", "192.168.50.0/24", false},
|
||||
} {
|
||||
if in(c.set, c.member) != c.want {
|
||||
t.Errorf("%s has %q: %v, want %v\n%v", c.set, c.member, !c.want, c.want, sets[c.set])
|
||||
}
|
||||
}
|
||||
// Eva's services reach the network as they reach her phone: member for member
|
||||
for _, set := range []string{"svc_ok", "svc_host"} {
|
||||
phone, site := 0, 0
|
||||
for _, x := range sets[set] {
|
||||
if strings.HasPrefix(x, "10.77.0.8 . ") {
|
||||
phone++
|
||||
}
|
||||
if strings.HasPrefix(x, "192.168.6.0/24 . ") {
|
||||
site++
|
||||
}
|
||||
}
|
||||
if phone != site {
|
||||
t.Errorf("%s: %d for the phone, %d for the network behind it", set, phone, site)
|
||||
}
|
||||
}
|
||||
// someone paused takes their network with them
|
||||
states["boris"] = "paused"
|
||||
for _, set := range Names {
|
||||
for _, x := range AccessSets(m, states, snap)[set] {
|
||||
if strings.Contains(x, "192.168.2.0/24") {
|
||||
t.Errorf("%s still has %q with its owner paused", set, x)
|
||||
}
|
||||
}
|
||||
}
|
||||
// a router that waits for the admin's answer is nowhere: neither its address nor what is behind it
|
||||
m.Devices["kphone"].Waiting = true
|
||||
for _, set := range Names {
|
||||
for _, x := range AccessSets(m, states, snap)[set] {
|
||||
if strings.Contains(x, "192.168.3.0/24") || strings.Contains(x, "10.79.0.2") {
|
||||
t.Errorf("%s has %q of a router that waits", set, x)
|
||||
}
|
||||
}
|
||||
}
|
||||
m.Devices["kphone"].Waiting = false
|
||||
// every set that holds people's sources takes a network
|
||||
table := Render(m, sets, Ctx{Snap: snap, ResolverPort: 5354})
|
||||
for _, name := range Names {
|
||||
for _, l := range strings.Split(table, "\n") {
|
||||
if strings.HasPrefix(l, "\tset "+name+" ") && !strings.Contains(l, "flags interval;") {
|
||||
t.Errorf("set %s takes no network: %s", name, l)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,399 @@
|
||||
// Package nft makes the table `inet dawnline` as dawnline.nft does: each person's access, enforced on the
|
||||
// tunnel interfaces before anything else on the router sees the traffic, and where tunnel DNS goes. Its
|
||||
// shape follows the servers and their home networks (Render); who is in which set follows people's state
|
||||
// and changes often (Sets, Update: one atomic transaction).
|
||||
//
|
||||
// What is loaded decides who reaches what, so the text is held to ucode's byte for byte on a household made
|
||||
// up for it (testdata/trace.uc, scripts/ucode-golden.sh).
|
||||
package nft
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/tickcount/luci-app-dawnline/backend/ipnet"
|
||||
"github.com/tickcount/luci-app-dawnline/backend/model"
|
||||
"github.com/tickcount/luci-app-dawnline/backend/netview"
|
||||
)
|
||||
|
||||
// Private4 is the private IPv4 ranges: internet people never reach them.
|
||||
var Private4 = []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "100.64.0.0/10", "169.254.0.0/16", "224.0.0.0/3"}
|
||||
|
||||
// Names is the sets whose members change at run time (Update replaces them together), in the table's order.
|
||||
var Names = []string{"lan_ok", "lan_all", "lan_part", "mesh_pairs", "inet_ok", "svc_ok", "svc_host", "dns_real"}
|
||||
|
||||
// Sets is the members of each run-time set.
|
||||
type Sets map[string][]string
|
||||
|
||||
func ip4(a string) bool {
|
||||
_, ok := ipnet.IP(a)
|
||||
return ok
|
||||
}
|
||||
|
||||
var digits = regexp.MustCompile(`^[0-9]+$`)
|
||||
|
||||
// tunnel is a server's own link: its interface, its network and the router's address in it. ok is false
|
||||
// for a server without keys, kept down, or with an interface name or a network that will not do.
|
||||
func tunnel(srv *model.Server) (ifname, subnet, address string, ok bool) {
|
||||
if srv == nil || srv.PrivateKey == "" || srv.Blocked != nil || !model.ValidIfname(srv.Ifname) {
|
||||
return "", "", "", false
|
||||
}
|
||||
n, isNet := ipnet.Subnet(srv.Subnet)
|
||||
if !isNet {
|
||||
return "", "", "", false
|
||||
}
|
||||
return srv.Ifname, srv.Subnet, ipnet.String(n.First()), true
|
||||
}
|
||||
|
||||
// AccessSets is who is in which set: which tunnel addresses may reach what, by each person's state. A
|
||||
// router stands for the networks behind it: wherever its address is, they are too, so a host there has
|
||||
// exactly its owner's access and is reached by exactly those who reach the router.
|
||||
//
|
||||
// lan_ok home access (the router itself, the mesh)
|
||||
// lan_all every home network
|
||||
// lan_part device . network for people limited to some networks
|
||||
// mesh_pairs device . device within a circle (or one person's own devices)
|
||||
// inet_ok the internet
|
||||
// svc_ok device . service . port; svc_host: device . service with no ports (all of them)
|
||||
// dns_real home names from the plain resolver
|
||||
func AccessSets(m *model.Model, states map[string]string, snap *netview.Snapshot) Sets {
|
||||
sets := Sets{}
|
||||
for _, k := range Names {
|
||||
sets[k] = []string{}
|
||||
}
|
||||
homes := map[string]home{}
|
||||
// a circle's (or one person's) devices, each with the networks behind it
|
||||
groups := map[string][][]string{}
|
||||
var order []string
|
||||
behind := snap.Behind(m)
|
||||
for _, id := range m.DeviceIDs() {
|
||||
d := m.Devices[id]
|
||||
p := m.People[d.Person]
|
||||
// off: paused, ended or over quota, or a router that waits for the admin's answer; addresses go into
|
||||
// nft as they are: IPv4 only
|
||||
if p == nil || d.Disabled || d.Waiting || !ip4(d.Address) || states[p.ID] != "active" {
|
||||
continue
|
||||
}
|
||||
// the device as the rules see it: its address, then the networks behind it
|
||||
for _, from := range append([]string{d.Address}, behind[id]...) {
|
||||
member(sets, m, p, from, homes, snap)
|
||||
}
|
||||
if p.HomeAccess() {
|
||||
srv := m.Servers[p.Server]
|
||||
// who this device may reach directly: its circle, or at least its owner's devices
|
||||
if srv != nil && srv.Mesh == "circles" {
|
||||
g := m.MeshGroup(p)
|
||||
if _, seen := groups[g]; !seen {
|
||||
order = append(order, g)
|
||||
}
|
||||
groups[g] = append(groups[g], append([]string{d.Address}, behind[id]...))
|
||||
}
|
||||
}
|
||||
}
|
||||
// every device of a group with every other, never with itself (what is behind a router reaches the
|
||||
// router without the tunnel)
|
||||
for _, g := range order {
|
||||
for i, one := range groups[g] {
|
||||
for j, other := range groups[g] {
|
||||
if i == j || one[0] == other[0] {
|
||||
continue
|
||||
}
|
||||
for _, a := range one {
|
||||
for _, b := range other {
|
||||
sets["mesh_pairs"] = append(sets["mesh_pairs"], a+" . "+b)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return sets
|
||||
}
|
||||
|
||||
type home struct {
|
||||
all bool
|
||||
nets []string
|
||||
}
|
||||
|
||||
// member puts one source of a person's (a device's address, or a network behind their router) into the
|
||||
// sets the person's access names; homes keeps what each person's home is from one device to the next.
|
||||
func member(sets Sets, m *model.Model, p *model.Person, from string, homes map[string]home, snap *netview.Snapshot) {
|
||||
if p.Access == "lan" && p.DNSMode == "home" {
|
||||
sets["dns_real"] = append(sets["dns_real"], from)
|
||||
}
|
||||
if p.Access == "internet" || p.Access == "both" {
|
||||
sets["inet_ok"] = append(sets["inet_ok"], from)
|
||||
}
|
||||
if p.HomeAccess() {
|
||||
sets["lan_ok"] = append(sets["lan_ok"], from)
|
||||
h, known := homes[p.ID]
|
||||
if !known {
|
||||
h = home{all: true}
|
||||
if srv := m.Servers[p.Server]; srv != nil {
|
||||
h = home{all: len(p.HomeNets) == 0, nets: snap.PersonHome(srv, p)}
|
||||
}
|
||||
homes[p.ID] = h
|
||||
}
|
||||
if h.all {
|
||||
sets["lan_all"] = append(sets["lan_all"], from)
|
||||
return
|
||||
}
|
||||
for _, n := range h.nets {
|
||||
sets["lan_part"] = append(sets["lan_part"], from+" . "+n)
|
||||
}
|
||||
return
|
||||
}
|
||||
if p.Access != "lan" {
|
||||
return
|
||||
}
|
||||
for _, sid := range p.Services {
|
||||
s := m.Services[sid]
|
||||
if s == nil || !ip4(s.Address) {
|
||||
continue
|
||||
}
|
||||
if len(s.Ports) == 0 {
|
||||
sets["svc_host"] = append(sets["svc_host"], from+" . "+s.Address)
|
||||
}
|
||||
for _, port := range s.Ports {
|
||||
if digits.MatchString(port) {
|
||||
sets["svc_ok"] = append(sets["svc_ok"], from+" . "+s.Address+" . "+port)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func uniq(list []string) []string {
|
||||
out := make([]string, 0, len(list))
|
||||
seen := map[string]bool{}
|
||||
for _, x := range list {
|
||||
if !seen[x] {
|
||||
seen[x] = true
|
||||
out = append(out, x)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// elems is a set's members as its declaration holds them; duplicate members (two devices with one address)
|
||||
// would make the whole load fail.
|
||||
func elems(list []string) string {
|
||||
if list = uniq(list); len(list) > 0 {
|
||||
return "elements = { " + strings.Join(list, ", ") + " }"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Ctx is what the table is made from besides the settings, read from the router: the netns's DNS address
|
||||
// ("" none), where the Home-only resolver runs (in the netns at an address on port 53, or on the router at
|
||||
// a port), the LAN's DNS ("" none) and the interfaces.
|
||||
type Ctx struct {
|
||||
DNSAddr string
|
||||
ResolverNS string
|
||||
ResolverPort int
|
||||
LanDNS string
|
||||
Snap *netview.Snapshot
|
||||
}
|
||||
|
||||
// Render is the table as text.
|
||||
func Render(m *model.Model, sets Sets, ctx Ctx) string {
|
||||
var ifs, tun []string
|
||||
dnsAddr := ctx.DNSAddr
|
||||
if !ip4(dnsAddr) {
|
||||
dnsAddr = ""
|
||||
}
|
||||
// each server's home networks are its own: with several servers, "all home" on one never reaches the
|
||||
// other's networks (set home_<id>)
|
||||
type served struct {
|
||||
id, ifname, address string
|
||||
nets []string
|
||||
}
|
||||
var homes []served
|
||||
for _, id := range m.ServerIDs() {
|
||||
srv := m.Servers[id]
|
||||
ifname, subnet, address, ok := tunnel(srv)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
ifs = append(ifs, `"`+ifname+`"`)
|
||||
tun = append(tun, subnet)
|
||||
_, _, nets, _ := ctx.Snap.ServerNet(srv)
|
||||
homes = append(homes, served{id, ifname, address, ipnet.Collapse(nets)})
|
||||
}
|
||||
tun = ipnet.Collapse(tun)
|
||||
var t []string
|
||||
add := func(format string, args ...any) { t = append(t, fmt.Sprintf(format, args...)) }
|
||||
add("table inet dawnline {")
|
||||
for _, h := range homes {
|
||||
add("\tset home_%s { type ipv4_addr; flags interval; %s }", h.id, elems(h.nets))
|
||||
}
|
||||
// tunnel subnets are never "home", even inside a wider route to the LAN
|
||||
add("\tset tun_nets { type ipv4_addr; flags interval; %s }", elems(tun))
|
||||
// who is in a set is an address or, for a router, a network behind it: every one takes intervals
|
||||
add("\tset lan_ok { type ipv4_addr; flags interval; %s }", elems(sets["lan_ok"]))
|
||||
add("\tset lan_all { type ipv4_addr; flags interval; %s }", elems(sets["lan_all"]))
|
||||
add("\tset lan_part { type ipv4_addr . ipv4_addr; flags interval; %s }", elems(sets["lan_part"]))
|
||||
add("\tset mesh_pairs { type ipv4_addr . ipv4_addr; flags interval; %s }", elems(sets["mesh_pairs"]))
|
||||
add("\tset inet_ok { type ipv4_addr; flags interval; %s }", elems(sets["inet_ok"]))
|
||||
add("\tset svc_ok { type ipv4_addr . ipv4_addr . inet_service; flags interval; %s }", elems(sets["svc_ok"]))
|
||||
add("\tset svc_host { type ipv4_addr . ipv4_addr; flags interval; %s }", elems(sets["svc_host"]))
|
||||
add("\tset dns_real { type ipv4_addr; flags interval; %s }", elems(sets["dns_real"]))
|
||||
add("\tchain pre {")
|
||||
// ahead of mangle-priority hooks (transparent proxies mark at -150)
|
||||
add("\t\ttype filter hook prerouting priority -160; policy accept;")
|
||||
if len(ifs) > 0 {
|
||||
add("\t\tiifname { %s } jump guard", strings.Join(ifs, ", "))
|
||||
}
|
||||
add("\t}")
|
||||
add("\tchain guard {")
|
||||
add("\t\tmeta nfproto ipv6 drop")
|
||||
// Every packet is judged by the rules below, not only the first of a connection: a change of access cuts
|
||||
// open connections too. Only what has no rule of its own passes on its connection: errors and the like,
|
||||
// answers to the admin's port forwards, and the router's own pings.
|
||||
add("\t\tct state related accept")
|
||||
add("\t\tct status dnat accept")
|
||||
add("\t\ticmp type { echo-request, echo-reply } fib daddr type local accept")
|
||||
add("\t\tfib daddr type local meta l4proto { tcp, udp } th dport 53 accept")
|
||||
if pp := portalPorts(m); len(pp) > 0 {
|
||||
// their own portal, also with the VPN on (the address may be this router's)
|
||||
add("\t\tfib daddr type local tcp dport { %s } accept", strings.Join(pp, ", "))
|
||||
}
|
||||
// the router itself is part of home: for everyone with all of it, and for people limited to some
|
||||
// networks only on the addresses in those
|
||||
add("\t\tfib daddr type local ip saddr @lan_all accept")
|
||||
add("\t\tfib daddr type local ip saddr . ip daddr @lan_part accept")
|
||||
add("\t\tfib daddr type local counter drop")
|
||||
if dnsAddr != "" {
|
||||
// DNS through the netns (it forwards to the LAN's DNS), for everyone
|
||||
add("\t\tip daddr %s meta l4proto { tcp, udp } th dport 53 accept", dnsAddr)
|
||||
}
|
||||
for _, h := range homes {
|
||||
add("\t\tiifname \"%s\" ip daddr @home_%s ip daddr != @tun_nets ip saddr @lan_all accept", h.ifname, h.id)
|
||||
}
|
||||
add("\t\tip saddr . ip daddr @lan_part ip daddr != @tun_nets accept")
|
||||
// Devices with home access are part of home: they reach each other, where the server allows it
|
||||
// (everyone, or within circles). Within one server: the firewall has no forwarding between servers' zones.
|
||||
for _, h := range homes {
|
||||
switch m.Servers[h.id].Mesh {
|
||||
case "all":
|
||||
add("\t\tiifname \"%s\" ip saddr @lan_ok ip daddr @lan_ok accept", h.ifname)
|
||||
case "circles":
|
||||
add("\t\tiifname \"%s\" ip saddr . ip daddr @mesh_pairs accept", h.ifname)
|
||||
}
|
||||
}
|
||||
add("\t\tmeta l4proto { tcp, udp } ip saddr . ip daddr . th dport @svc_ok accept")
|
||||
add("\t\tip saddr . ip daddr @svc_host accept")
|
||||
add("\t\tip daddr { %s } counter drop", strings.Join(Private4, ", "))
|
||||
add("\t\tip saddr @inet_ok accept")
|
||||
add("\t\tcounter drop")
|
||||
add("\t}")
|
||||
// The other way: fw4 lets the server's home zones reach the tunnel; here only connections from its home
|
||||
// networks to people with home access pass. The admin's own port forwards (DNAT) stay with fw4.
|
||||
add("\tchain inbound {")
|
||||
add("\t\ttype filter hook forward priority filter - 5; policy accept;")
|
||||
if len(ifs) > 0 {
|
||||
add("\t\toifname { %s } iifname != { %s } ct state new jump to_device", strings.Join(ifs, ", "), strings.Join(ifs, ", "))
|
||||
}
|
||||
add("\t}")
|
||||
add("\tchain to_device {")
|
||||
add("\t\tmeta nfproto ipv6 drop")
|
||||
add("\t\tct status dnat accept")
|
||||
for _, h := range homes {
|
||||
add("\t\toifname \"%s\" ip saddr @home_%s ip saddr != @tun_nets ip daddr @lan_all accept", h.ifname, h.id)
|
||||
}
|
||||
add("\t\tip daddr . ip saddr @lan_part ip saddr != @tun_nets accept")
|
||||
add("\t\tcounter reject with icmpx admin-prohibited")
|
||||
add("\t}")
|
||||
// devices ask the tunnel address; send each query where it belongs
|
||||
toResolver := fmt.Sprintf("redirect to :%d", ctx.ResolverPort)
|
||||
if ip4(ctx.ResolverNS) {
|
||||
toResolver = "dnat ip to " + ctx.ResolverNS + ":53"
|
||||
}
|
||||
lanDNS := ctx.LanDNS
|
||||
if !ip4(lanDNS) {
|
||||
lanDNS = ""
|
||||
}
|
||||
add("\tchain dns {")
|
||||
add("\t\ttype nat hook prerouting priority dstnat - 5; policy accept;")
|
||||
if len(ifs) > 0 {
|
||||
// home names (and no LAN DNS at all): the plain resolver, whatever address was asked
|
||||
add("\t\tiifname { %s } ip saddr @dns_real meta l4proto { tcp, udp } th dport 53 %s", strings.Join(ifs, ", "), toResolver)
|
||||
}
|
||||
add("\t}")
|
||||
// After the LAN's own DNS interceptors (transparent proxies redirect port 53 at dstnat - 1): only the
|
||||
// first NAT of a connection counts. When none of them takes the tunnel, this sends it to the LAN's DNS.
|
||||
add("\tchain dns_lan {")
|
||||
add("\t\ttype nat hook prerouting priority dstnat + 5; policy accept;")
|
||||
for _, h := range homes {
|
||||
// everyone else: the LAN's DNS, through the netns (from the LAN side) when internet goes that way
|
||||
target := toResolver
|
||||
if m.Servers[h.id].LanLike && dnsAddr != "" {
|
||||
target = "dnat ip to " + dnsAddr + ":53"
|
||||
} else if lanDNS != "" {
|
||||
target = "dnat ip to " + lanDNS + ":53"
|
||||
}
|
||||
add("\t\tiifname \"%s\" ip daddr %s meta l4proto { tcp, udp } th dport 53 %s", h.ifname, h.address, target)
|
||||
}
|
||||
add("\t}")
|
||||
add("}")
|
||||
return strings.Join(t, "\n") + "\n"
|
||||
}
|
||||
|
||||
// portalPorts is the portal's TCP ports people may reach through the tunnel (never SSH, DNS or LuCI,
|
||||
// whatever is stored).
|
||||
func portalPorts(m *model.Model) []string {
|
||||
s := m.Settings
|
||||
if !s.PortalEnabled {
|
||||
return nil
|
||||
}
|
||||
ok := func(p int) bool { return p >= 1 && p <= 65535 && p != 22 && p != 53 && p != 80 && p != 443 }
|
||||
var out []string
|
||||
if ok(s.PortalPort) {
|
||||
out = append(out, fmt.Sprint(s.PortalPort))
|
||||
}
|
||||
plain := false
|
||||
for _, h := range append([]string{s.PortalHost}, s.PortalAlt...) {
|
||||
plain = plain || ip4(h)
|
||||
}
|
||||
if plain && ok(s.PortalPort+1) {
|
||||
out = append(out, fmt.Sprint(s.PortalPort+1))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Rebuild is the script that puts a whole table in place of the one there (one transaction).
|
||||
func Rebuild(table string) string {
|
||||
return "table inet dawnline\ndelete table inet dawnline\n" + table
|
||||
}
|
||||
|
||||
// Update is the script that replaces the members of every run-time set at once. It fails as a whole when
|
||||
// the table or one of its sets is missing: the caller then rebuilds.
|
||||
func Update(sets Sets) string {
|
||||
var b strings.Builder
|
||||
for _, name := range Names {
|
||||
b.WriteString("flush set inet dawnline " + name + "\n")
|
||||
if members := uniq(sets[name]); len(members) > 0 {
|
||||
b.WriteString("add element inet dawnline " + name + " { " + strings.Join(members, ", ") + " }\n")
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// Shape counts what a table's text holds: its chains, its sets and the rules in its chains (for a log line).
|
||||
func Shape(table string) (chains, sets, rules int) {
|
||||
in := false
|
||||
for _, l := range strings.Split(table, "\n") {
|
||||
l = strings.TrimSpace(l)
|
||||
switch {
|
||||
case strings.HasPrefix(l, "chain "):
|
||||
chains++
|
||||
in = true
|
||||
case strings.HasPrefix(l, "set "):
|
||||
sets++
|
||||
case in && l == "}":
|
||||
in = false
|
||||
case in && l != "" && !strings.HasPrefix(l, "type ") && !strings.HasPrefix(l, "policy ") && !strings.HasPrefix(l, "#"):
|
||||
rules++
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package nft
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/tickcount/luci-app-dawnline/backend/model"
|
||||
"github.com/tickcount/luci-app-dawnline/backend/netview"
|
||||
"github.com/tickcount/luci-app-dawnline/backend/uci"
|
||||
)
|
||||
|
||||
// The access table for access's household, byte for byte the one ucode makes, for every row of states and
|
||||
// of what is read from the router (testdata/trace.uc, scripts/ucode-golden.sh).
|
||||
func TestTableAsUcode(t *testing.T) {
|
||||
raw, err := os.ReadFile("testdata/trace.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
type oneCase = struct {
|
||||
States map[string]string `json:"states"`
|
||||
Ctx struct {
|
||||
DNSAddr *string `json:"dns_addr"`
|
||||
Resolver struct {
|
||||
Mode string `json:"mode"`
|
||||
Address string `json:"address"`
|
||||
Port int `json:"port"`
|
||||
} `json:"resolver"`
|
||||
LanDNS *string `json:"lan_dns"`
|
||||
} `json:"ctx"`
|
||||
Sets Sets `json:"sets"`
|
||||
Table string `json:"table"`
|
||||
Shape struct{ Chains, Sets, Rules int }
|
||||
}
|
||||
var trace struct {
|
||||
Cases []oneCase `json:"cases"`
|
||||
Extra []oneCase `json:"extra"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &trace); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(trace.Cases) != 16 || len(trace.Extra) != 3 {
|
||||
t.Fatalf("%d and %d cases", len(trace.Cases), len(trace.Extra))
|
||||
}
|
||||
load := func(path string) *model.Model {
|
||||
text, _ := os.ReadFile(path)
|
||||
c, err := uci.Parse(string(text))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return model.Load(c, nil)
|
||||
}
|
||||
m, m2 := load("../access/testdata/access.uci"), load("testdata/extra.uci")
|
||||
raw, err = os.ReadFile("../netview/testdata/trace.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var router struct {
|
||||
Routes json.RawMessage `json:"routes"`
|
||||
Addrs json.RawMessage `json:"addrs"`
|
||||
Firewall string `json:"firewall"`
|
||||
Links map[string]netview.Link `json:"links"`
|
||||
}
|
||||
json.Unmarshal(raw, &router)
|
||||
fw, _ := uci.Parse(router.Firewall)
|
||||
snap := netview.Take(netview.Sources{Routes: func() []byte { return router.Routes }, Addrs: func() []byte { return router.Addrs },
|
||||
Links: func() map[string]netview.Link { return router.Links }, Zones: func() netview.Zones { return netview.ZonesOf(fw) }})
|
||||
members := 0
|
||||
filled := map[string]bool{}
|
||||
for i, want := range append(trace.Cases, trace.Extra...) {
|
||||
m := m
|
||||
if i >= len(trace.Cases) {
|
||||
m = m2
|
||||
}
|
||||
sets := AccessSets(m, want.States, snap)
|
||||
for _, name := range Names {
|
||||
filled[name] = filled[name] || len(sets[name]) > 0
|
||||
}
|
||||
for _, name := range Names {
|
||||
members += len(sets[name])
|
||||
}
|
||||
if !reflect.DeepEqual(sets, want.Sets) {
|
||||
for _, name := range Names {
|
||||
if !reflect.DeepEqual(sets[name], want.Sets[name]) {
|
||||
t.Errorf("case %d: set %s\n go: %v\n ucode: %v", i, name, sets[name], want.Sets[name])
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
str := func(p *string) string {
|
||||
if p == nil {
|
||||
return ""
|
||||
}
|
||||
return *p
|
||||
}
|
||||
ctx := Ctx{DNSAddr: str(want.Ctx.DNSAddr), LanDNS: str(want.Ctx.LanDNS), ResolverPort: want.Ctx.Resolver.Port, Snap: snap}
|
||||
if want.Ctx.Resolver.Mode == "ns" {
|
||||
ctx.ResolverNS = want.Ctx.Resolver.Address
|
||||
}
|
||||
got := Render(m, sets, ctx)
|
||||
if got != want.Table {
|
||||
g, w := strings.Split(got, "\n"), strings.Split(want.Table, "\n")
|
||||
for n := 0; n < len(g) && n < len(w); n++ {
|
||||
if g[n] != w[n] {
|
||||
t.Errorf("case %d: line %d\n go: %s\n ucode: %s", i, n+1, g[n], w[n])
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(g) != len(w) {
|
||||
t.Errorf("case %d: %d lines, ucode %d", i, len(g), len(w))
|
||||
}
|
||||
continue
|
||||
}
|
||||
if chains, nsets, rules := Shape(got); chains != want.Shape.Chains || nsets != want.Shape.Sets || rules != want.Shape.Rules {
|
||||
t.Errorf("case %d: shape %d %d %d, ucode %+v", i, chains, nsets, rules, want.Shape)
|
||||
}
|
||||
}
|
||||
for _, name := range Names {
|
||||
if !filled[name] {
|
||||
t.Errorf("no case fills %s", name)
|
||||
}
|
||||
}
|
||||
if members < 200 {
|
||||
t.Fatalf("the household fills the sets too little: %d members", members)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateScript(t *testing.T) {
|
||||
got := Update(Sets{"lan_ok": {"10.77.0.2", "10.77.0.2", "10.77.0.3"}, "svc_ok": {"10.77.0.8 . 192.168.77.5 . 445"}})
|
||||
want := "flush set inet dawnline lan_ok\nadd element inet dawnline lan_ok { 10.77.0.2, 10.77.0.3 }\nflush set inet dawnline lan_all\nflush set inet dawnline lan_part\n" +
|
||||
"flush set inet dawnline mesh_pairs\nflush set inet dawnline inet_ok\nflush set inet dawnline svc_ok\nadd element inet dawnline svc_ok { 10.77.0.8 . 192.168.77.5 . 445 }\n" +
|
||||
"flush set inet dawnline svc_host\nflush set inet dawnline dns_real\n"
|
||||
if got != want {
|
||||
t.Fatalf("the update:\n%s", got)
|
||||
}
|
||||
if !strings.HasPrefix(Rebuild("table inet dawnline {\n}\n"), "table inet dawnline\ndelete table inet dawnline\ntable inet dawnline {") {
|
||||
t.Fatal("a rebuild does not replace the table in one transaction")
|
||||
}
|
||||
}
|
||||
Vendored
+136
@@ -0,0 +1,136 @@
|
||||
config settings 'settings'
|
||||
option portal_enabled '1'
|
||||
option portal_port '443'
|
||||
option portal_host '203.0.113.7'
|
||||
|
||||
config server 'home'
|
||||
option name 'Home'
|
||||
option ifname 'dawn0'
|
||||
option port '4500'
|
||||
option subnet '10.77.0.0/24'
|
||||
option private_key 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaA='
|
||||
option auto_lan '0'
|
||||
list lan_device 'br-lan'
|
||||
option mesh 'all'
|
||||
option lan_like '1'
|
||||
|
||||
config server 'guest'
|
||||
option name 'Guest'
|
||||
option ifname 'dawn1'
|
||||
option port '4600'
|
||||
option subnet '10.78.0.0/24'
|
||||
option private_key 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbA='
|
||||
option auto_lan '0'
|
||||
list lan_device 'dlhome'
|
||||
list lan_device 'dlguest'
|
||||
option mesh 'off'
|
||||
|
||||
config server 'keyless'
|
||||
option name 'Keyless'
|
||||
option ifname 'dawn2'
|
||||
option port '4700'
|
||||
option subnet '10.79.0.0/24'
|
||||
|
||||
config server 'badname'
|
||||
option name 'Bad name'
|
||||
option ifname 'Not-A-Name'
|
||||
option port '4800'
|
||||
option subnet '10.80.0.0/24'
|
||||
option private_key 'ccccccccccccccccccccccccccccccccccccccccccA='
|
||||
|
||||
config service 'nas'
|
||||
option name 'NAS'
|
||||
option address '192.168.77.5'
|
||||
list port '445'
|
||||
list port '139'
|
||||
list port 'smb'
|
||||
|
||||
config service 'cam'
|
||||
option name 'Camera'
|
||||
option address '192.168.50.30'
|
||||
|
||||
config service 'v6'
|
||||
option name 'Six'
|
||||
option address '2001:db8::5'
|
||||
list port '80'
|
||||
|
||||
config person 'anna'
|
||||
option server 'home'
|
||||
option name 'Anna'
|
||||
option access 'both'
|
||||
|
||||
config person 'eva'
|
||||
option server 'home'
|
||||
option name 'Eva'
|
||||
option access 'lan'
|
||||
option dns_mode 'home'
|
||||
list service 'nas'
|
||||
list service 'cam'
|
||||
list service 'v6'
|
||||
list service 'gone'
|
||||
|
||||
config person 'dima'
|
||||
option server 'guest'
|
||||
option name 'Dima'
|
||||
option access 'lan'
|
||||
option dns_mode 'home'
|
||||
list home_net 'dlguest'
|
||||
|
||||
config person 'kira'
|
||||
option server 'keyless'
|
||||
option name 'Kira'
|
||||
option access 'both'
|
||||
|
||||
config person 'lost'
|
||||
option server 'nowhere'
|
||||
option name 'Lost'
|
||||
option access 'both'
|
||||
|
||||
config device 'aphone'
|
||||
option person 'anna'
|
||||
option name 'Phone'
|
||||
option address '10.77.0.2'
|
||||
option public_key 'ddddddddddddddddddddddddddddddddddddddddddA='
|
||||
|
||||
config device 'atwin'
|
||||
option person 'anna'
|
||||
option name 'Twin'
|
||||
option address '10.77.0.2'
|
||||
option public_key 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeA='
|
||||
|
||||
config device 'aoff'
|
||||
option person 'anna'
|
||||
option name 'Off'
|
||||
option address '10.77.0.3'
|
||||
option public_key 'ffffffffffffffffffffffffffffffffffffffffffA='
|
||||
option disabled '1'
|
||||
|
||||
config device 'av6'
|
||||
option person 'anna'
|
||||
option name 'Six'
|
||||
option address 'fd00::2'
|
||||
option public_key 'ggggggggggggggggggggggggggggggggggggggggggA='
|
||||
|
||||
config device 'ephone'
|
||||
option person 'eva'
|
||||
option name 'Phone'
|
||||
option address '10.77.0.8'
|
||||
option public_key 'hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhA='
|
||||
|
||||
config device 'dphone'
|
||||
option person 'dima'
|
||||
option name 'Phone'
|
||||
option address '10.78.0.2'
|
||||
option public_key 'iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiA='
|
||||
|
||||
config device 'kphone'
|
||||
option person 'kira'
|
||||
option name 'Phone'
|
||||
option address '10.79.0.2'
|
||||
option public_key 'jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjA='
|
||||
|
||||
config device 'lphone'
|
||||
option person 'lost'
|
||||
option name 'Phone'
|
||||
option address '10.81.0.2'
|
||||
option public_key 'kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkA='
|
||||
Vendored
+1311
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user