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 / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- "Internet like a home device" starts off on new servers: sending the tunnel through the home network costs speed. Both server wizards (the first one and "Add server") have the switch, and on it says what it costs. Servers that have it on keep it - With it off, Server → Access says what that means: DNS still goes through the router, its proxies and filters see the tunnel only once its interface is added to their source interfaces, and sites the router's DNS answers with proxy addresses won't open - With it on, established connections skip the second pass through netfilter in Dawnline's network namespace (a software flowtable): about 12% less router CPU per packet, and about a quarter more speed through a proxy on the router. Without kmod-nft-offload the old rules load and the service log says so - Its address at home stays the same when it is turned off and on again; it moved down by one each time (.254, .253, .252) - Less CPU for the same work: the background agent's once-a-second look at the tunnels, Status, Health and the portal no longer compile the same patterns again for each line, address and device. At idle the agent uses about a quarter of the CPU it did; a portal request 5-10 ms less - On LuCI's Bootstrap themes the admin looks as it does on Outline: cards and menus a step above the page, icon tiles that keep their colour, Outline's status colours in dark mode. The choices in Settings → Logs & history and Updates are LuCI's own controls in every theme - "Add from devices at home" keeps a device's name readable in a narrow window: its ports go under the name - DESIGN.md describes the portal's look, components and motion, and the rules send portal work there first. lint-ucode no longer ends a function at a brace inside a template literal
309 lines
9.8 KiB
Ucode
309 lines
9.8 KiB
Ucode
// Dawnline — the router's network interfaces: what exists, which one is the
|
|
// WAN, and which ones make up a server's "home network".
|
|
//
|
|
// Per server (uci `server` section):
|
|
// auto_wan 1 = use the interface of the default route (detected_wan
|
|
// is kept as a fallback while the WAN is down)
|
|
// wan_device the WAN interface when auto_wan is off
|
|
// auto_lan 1 = include the detected LAN (detected_lan as fallback)
|
|
// lan_device extra interfaces that count as the home network (list)
|
|
//
|
|
// Home access is enforced by destination: the networks routed out of the
|
|
// chosen interfaces form the server's nft `home_<id>` set, and the tunnel's
|
|
// firewall zone gets a forwarding to each of their zones.
|
|
|
|
import { readfile, readlink, lsdir, stat } from 'fs';
|
|
import { cursor } from 'uci';
|
|
import { sh_out, ip2int, int2ip, cidr_any, uniq } from 'dawnline.util';
|
|
|
|
const SYS = '/sys/class/net';
|
|
/** Links Dawnline creates itself (dawnline.loop); never offered as interfaces. */
|
|
export const INTERNAL = { transit: 'dlx0', lan: 'dll0' };
|
|
const VPN_PROTOS = [ 'amneziawg', 'wireguard', 'openvpn', 'openconnect', 'vpnc', 'pptp', 'l2tp', 'gre', 'gretap', 'vti', 'xfrm' ];
|
|
const VPN_NAMES = /^(wg|awg|tun|tap|ovpn|ipsec|vti|xfrm)/;
|
|
// Never the WAN while another default route exists: VPN clients. (PPTP and
|
|
// L2TP stay eligible: some ISPs connect the WAN with them.)
|
|
const NOT_WAN_PROTOS = [ 'amneziawg', 'wireguard', 'openvpn', 'openconnect', 'vpnc' ];
|
|
const NOT_WAN_NAMES = /^(wg|awg|tun|tap|ovpn)/;
|
|
// Kernel placeholder devices that never carry a network on their own.
|
|
const NOISE = /^(ifb|teql|sit|ip6tnl|gre|gretap|erspan|ip6gre|ip6gretap|ip_vti|ip6_vti|tunl|bond)[0-9]*$/;
|
|
const PRIVATE = [ [ '10.0.0.0', 8 ], [ '172.16.0.0', 12 ], [ '192.168.0.0', 16 ], [ '100.64.0.0', 10 ] ];
|
|
|
|
function sysread(dev, f) {
|
|
return trim(readfile(`${SYS}/${dev}/${f}`) ?? '');
|
|
}
|
|
|
|
function ip_json(args) {
|
|
try {
|
|
return json(sh_out(`ip -j ${args} 2>/dev/null`) || '[]') ?? [];
|
|
}
|
|
catch (e) {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function ubus_json(path, method) {
|
|
try {
|
|
return json(sh_out(`ubus call ${path} ${method} 2>/dev/null`) || 'null');
|
|
}
|
|
catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** "a.b.c.d[/len]" → { net, prefix, size } for any prefix 1-32; null for default or junk. */
|
|
function net_of(s) {
|
|
let c = cidr_any(s);
|
|
return c && c.prefix >= 1 ? c : null;
|
|
}
|
|
|
|
function net_str(n) {
|
|
return `${int2ip(n.net)}/${n.prefix}`;
|
|
}
|
|
|
|
/** Drop networks already covered by a wider one. */
|
|
export function collapse(list) {
|
|
let nets = filter(map(uniq(list), net_of), (x) => x != null);
|
|
nets = sort(nets, (a, b) => a.prefix - b.prefix || a.net - b.net);
|
|
let keep = [];
|
|
for (let n in nets) {
|
|
let inside = false;
|
|
for (let k in keep)
|
|
if (n.net >= k.net && n.net < k.net + k.size) {
|
|
inside = true;
|
|
break;
|
|
}
|
|
if (!inside)
|
|
push(keep, n);
|
|
}
|
|
return map(sort(keep, (a, b) => a.net - b.net), net_str);
|
|
};
|
|
|
|
function is_private(cidr) {
|
|
let n = net_of(cidr);
|
|
if (!n)
|
|
return false;
|
|
for (let p in PRIVATE) {
|
|
let base = ip2int(p[0]), size = 1 << (32 - p[1]);
|
|
if (n.net >= base && n.net < base + size)
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/* ---------- snapshot ---------- */
|
|
|
|
let cache = null;
|
|
|
|
/**
|
|
* One consistent view of the router's interfaces, cached for a few seconds
|
|
* (rpcd keeps this module loaded, so the cache must expire).
|
|
*/
|
|
export function snapshot(fresh) {
|
|
if (!fresh && cache && time() - cache.ts < 5)
|
|
return cache;
|
|
|
|
// Networks reachable through each device (main table, IPv4).
|
|
let routes = {}, defaults = [];
|
|
for (let r in ip_json('-4 route show table main')) {
|
|
if (!r.dev)
|
|
continue;
|
|
if (r.dst == 'default') {
|
|
push(defaults, r);
|
|
continue;
|
|
}
|
|
push(routes[r.dev] ??= [], r.dst);
|
|
}
|
|
let addrs = {};
|
|
for (let l in ip_json('-4 addr show'))
|
|
for (let a in l.addr_info ?? [])
|
|
if (a.family == 'inet')
|
|
push(addrs[l.ifname] ??= [], `${a.local}/${a.prefixlen}`);
|
|
|
|
// Logical (netifd) interfaces by device, and firewall zones by network.
|
|
let logical = {};
|
|
for (let i in ubus_json('network.interface', 'dump')?.interface ?? []) {
|
|
let dev = i.l3_device ?? i.device;
|
|
if (dev && !logical[dev])
|
|
logical[dev] = { name: i.interface, proto: i.proto, up: i.up };
|
|
}
|
|
// The WAN: the default route with the lowest metric that does not go into
|
|
// a VPN client (its default route is not where the tunnels listen); only
|
|
// VPN routes: the lowest of them.
|
|
let vpn = (dev) => index(NOT_WAN_PROTOS, logical[dev]?.proto) >= 0 || match(dev, NOT_WAN_NAMES) != null;
|
|
let wan_dev = null, wan_metric = null, wan_vpn = true;
|
|
for (let r in defaults) {
|
|
let v = vpn(r.dev), metric = r.metric ?? 0;
|
|
if (wan_dev == null || (wan_vpn && !v) || (wan_vpn == v && metric < wan_metric)) {
|
|
wan_dev = r.dev;
|
|
wan_metric = metric;
|
|
wan_vpn = v;
|
|
}
|
|
}
|
|
let zone_by_net = {}, zone_by_dev = {}, zones = [];
|
|
let fc = cursor();
|
|
fc.load('firewall');
|
|
fc.foreach('firewall', 'zone', (z) => {
|
|
if (!z.name)
|
|
return;
|
|
push(zones, z.name);
|
|
for (let n in (type(z.network) == 'array' ? z.network : split(z.network ?? '', /\s+/)))
|
|
if (n != '')
|
|
zone_by_net[n] ??= z.name;
|
|
for (let d in (type(z.device) == 'array' ? z.device : split(z.device ?? '', /\s+/)))
|
|
if (d != '')
|
|
zone_by_dev[d] ??= z.name;
|
|
});
|
|
|
|
let devices = {};
|
|
for (let dev in sort(lsdir(SYS) ?? [])) {
|
|
if (dev == 'lo' || dev == INTERNAL.transit || dev == INTERNAL.lan)
|
|
continue;
|
|
let lg = logical[dev];
|
|
let flags = hex(sysread(dev, 'flags') || '0');
|
|
let kind = +(sysread(dev, 'type') || '0');
|
|
let master = readlink(`${SYS}/${dev}/master`);
|
|
master = master ? substr(master, rindex(master, '/') + 1) : null;
|
|
let bridge = stat(`${SYS}/${dev}/bridge`) != null;
|
|
let wifi = stat(`${SYS}/${dev}/phy80211`) != null || stat(`${SYS}/${dev}/wireless`) != null;
|
|
let nets = routes[dev] ? collapse(routes[dev]) : [];
|
|
let up = (flags & 1) != 0;
|
|
if (match(dev, NOISE) && !length(nets))
|
|
continue;
|
|
let cat;
|
|
if (wifi)
|
|
cat = 'wifi';
|
|
else if ((lg && index(VPN_PROTOS, lg.proto) >= 0) || kind == 65534 || ((flags & 0x10) && kind != 1) || match(dev, VPN_NAMES))
|
|
cat = 'vpn';
|
|
else if (kind == 1 || bridge)
|
|
cat = 'ethernet';
|
|
else
|
|
cat = 'other';
|
|
devices[dev] = {
|
|
name: dev,
|
|
category: dev == wan_dev ? 'wan' : cat,
|
|
kind: bridge ? 'bridge' : cat,
|
|
up,
|
|
master,
|
|
members: [],
|
|
nets,
|
|
addrs: addrs[dev] ?? [],
|
|
logical: lg?.name,
|
|
proto: lg?.proto,
|
|
zone: zone_by_dev[dev] ?? (lg ? zone_by_net[lg.name] : null),
|
|
};
|
|
}
|
|
for (let dev, d in devices)
|
|
if (d.master && devices[d.master])
|
|
push(devices[d.master].members, dev);
|
|
|
|
// The LAN: netifd's "lan" if present, otherwise the first bridge (then
|
|
// ethernet device) carrying a private network.
|
|
let lan_dev = null;
|
|
for (let dev, lg in logical)
|
|
if (lg.name == 'lan' && devices[dev])
|
|
lan_dev = dev;
|
|
if (!lan_dev)
|
|
for (let pass in [ 'bridge', 'ethernet' ])
|
|
for (let dev, d in devices)
|
|
if (!lan_dev && dev != wan_dev && d.kind == pass && !d.master && length(filter(d.nets, is_private)))
|
|
lan_dev = dev;
|
|
|
|
cache = { ts: time(), devices, wan: wan_dev, lan: lan_dev, zones };
|
|
return cache;
|
|
};
|
|
|
|
/* ---------- per server ---------- */
|
|
|
|
/**
|
|
* What a server actually uses right now:
|
|
* wan: { dev, zone, auto, missing }
|
|
* home: [{ dev, auto, nets, zone, missing }]
|
|
* nets: home networks (collapsed), zones: their firewall zones
|
|
*/
|
|
export function server_net(srv, snap) {
|
|
snap ??= snapshot();
|
|
let devs = snap.devices;
|
|
|
|
let wan_dev = srv.auto_wan ? (snap.wan ?? srv.detected_wan) : srv.wan_device;
|
|
let wan = {
|
|
dev: wan_dev || null,
|
|
auto: !!srv.auto_wan,
|
|
zone: devs[wan_dev]?.zone ?? srv.wan_zone ?? 'wan',
|
|
missing: !!wan_dev && !devs[wan_dev],
|
|
};
|
|
|
|
let picked = [];
|
|
let auto_lan = srv.auto_lan ? (snap.lan ?? srv.detected_lan) : null;
|
|
if (auto_lan)
|
|
push(picked, [ auto_lan, true ]);
|
|
for (let d in srv.lan_devices ?? [])
|
|
if (d != auto_lan)
|
|
push(picked, [ d, false ]);
|
|
|
|
let home = [], nets = [], zones = [];
|
|
for (let p in picked) {
|
|
let dev = p[0];
|
|
// Never the WAN, the tunnel itself, or a bridge port (routing only sees the bridge).
|
|
if (dev == wan.dev || dev == srv.ifname || devs[dev]?.master)
|
|
continue;
|
|
let d = devs[dev];
|
|
// The detected LAN missing for a moment (bridge recreated) keeps its zone,
|
|
// so its forwardings are not removed meanwhile.
|
|
let h = { dev, auto: p[1], nets: d?.nets ?? [], zone: d?.zone ?? (p[1] ? srv.lan_zone : null), missing: !d };
|
|
push(home, h);
|
|
push(nets, ...h.nets);
|
|
if (h.zone)
|
|
push(zones, h.zone);
|
|
}
|
|
// Older configs and routers where nothing could be resolved: the zone set at setup.
|
|
if (!length(home) && srv.auto_lan && srv.lan_zone)
|
|
push(zones, srv.lan_zone);
|
|
return { wan, home, nets: collapse(nets), zones: uniq(zones) };
|
|
};
|
|
|
|
/** Router address on the home network (DNS for tunnel clients). */
|
|
export function home_address(srv, snap) {
|
|
snap ??= snapshot();
|
|
let sn = server_net(srv, snap);
|
|
for (let h in sn.home)
|
|
for (let a in snap.devices[h.dev]?.addrs ?? [])
|
|
return split(a, '/')[0];
|
|
return null;
|
|
};
|
|
|
|
/** WAN IPv4 address of a server's WAN interface. */
|
|
export function wan_address(srv, snap) {
|
|
snap ??= snapshot();
|
|
let a = snap.devices[server_net(srv, snap).wan.dev]?.addrs?.[0];
|
|
return a ? split(a, '/')[0] : null;
|
|
};
|
|
|
|
/** Names of devices at home by address: DHCP leases, then static hosts. */
|
|
export function lan_names() {
|
|
let out = {};
|
|
for (let line in split(readfile('/tmp/dhcp.leases') ?? '', '\n')) {
|
|
let f = split(line, ' ');
|
|
if (length(f) >= 4 && f[3] != '*')
|
|
out[f[2]] = f[3];
|
|
}
|
|
let c = cursor();
|
|
c.foreach('dhcp', 'host', (h) => {
|
|
if (h.ip && h.name)
|
|
out[h.ip] ??= h.name;
|
|
});
|
|
return out;
|
|
};
|
|
|
|
// A module constant, compiled once: event_hosts runs for every event of the log.
|
|
const ANY_IPV4 = /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/g;
|
|
|
|
/** Event addresses that belong to devices at home, with their names ({ ip: name }). */
|
|
export function event_hosts(e, names) {
|
|
let out = null;
|
|
for (let ip in match(e.detail ?? '', ANY_IPV4) ?? [])
|
|
if (names[ip[0]])
|
|
(out ??= {})[ip[0]] = names[ip[0]];
|
|
return out;
|
|
};
|