- Access split into Internet and Home network cards; network tiles inline, Auto-detect LAN beside them, other interfaces folded by kind - Devices reach each other: Off, Everyone or Circles (per server); circles with names, colours and members; a person's own devices always see each other while it is not off - Per-person home networks: people can be kept out of some of the server's networks (nft lan_part set, AllowedIPs follow) - Home services moved from Settings to Access, added from the devices the router sees over DHCP (ports they announce over Bonjour when umdns runs) - Who reaches what (rules summed up per access) and Check access (walks the same rules as nft and the client configuration, step by step) - Find home devices (Bonjour): umdns services published as DNS-SD records in dnsmasq; installs umdns from Access - Connections from home show the DHCP name with the address in brackets - Repaints keep the scroll position
This commit is contained in:
1 parent
82ab86848e
commit
aa8256bc60
26 files changed
+1661
-289
No files matched your search
@@ -0,0 +1,252 @@
|
||||
// Dawnline: "Check access" — walks the same rules the nft table and the
|
||||
// client configuration apply, and says in words where a connection stops.
|
||||
|
||||
'use strict';
|
||||
|
||||
import { ip2int } from 'dawnline.util';
|
||||
import * as core from 'dawnline.core';
|
||||
import * as net from 'dawnline.net';
|
||||
import * as live from 'dawnline.live';
|
||||
import * as view from 'dawnline.view';
|
||||
|
||||
const STATE_TEXT = {
|
||||
no_server: 'their server is turned off',
|
||||
paused: 'they are paused',
|
||||
paused_all: 'everyone is paused',
|
||||
expired: 'their access has ended',
|
||||
quota: 'they used up their data',
|
||||
hours: 'it is outside their allowed hours',
|
||||
};
|
||||
|
||||
const ACCESS_TEXT = { lan: 'Home only', internet: 'Internet only', both: 'Home and Internet' };
|
||||
|
||||
function step(ok, title, detail) {
|
||||
return { ok: !!ok, title, detail: detail ?? '' };
|
||||
}
|
||||
|
||||
/** Add the step where it stops and end the walk. */
|
||||
function stop(steps, s) {
|
||||
push(steps, s);
|
||||
return steps;
|
||||
}
|
||||
|
||||
/** The first of `nets` (IPv4 CIDRs, any prefix) that holds `ip`. */
|
||||
function in_any(ip, nets) {
|
||||
let n = ip2int(ip);
|
||||
if (n == null)
|
||||
return null;
|
||||
for (let c in nets) {
|
||||
let mm = match(c, /^([0-9.]+)\/([0-9]+)$/);
|
||||
let base = mm ? ip2int(mm[1]) : null;
|
||||
if (base == null)
|
||||
continue;
|
||||
let size = 1 << (32 - +mm[2]);
|
||||
if (+mm[2] == 0 || (n >= base - base % size && n < base - base % size + size))
|
||||
return c;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** "10.0.1.20:445" → [ip, port]; a device id → its address. */
|
||||
function parse_target(m, s) {
|
||||
s = trim(`${s ?? ''}`);
|
||||
if (m.devices[s])
|
||||
return { dev: m.devices[s], ip: m.devices[s].address, port: null };
|
||||
let mm = match(s, /^([0-9.]+)(:([0-9]{1,5}))?$/);
|
||||
if (!mm || ip2int(mm[1]) == null)
|
||||
return null;
|
||||
let ip = mm[1];
|
||||
for (let id, d in m.devices)
|
||||
if (d.address == ip)
|
||||
return { dev: d, ip, port: mm[3] ? +mm[3] : null };
|
||||
return { ip, port: mm[3] ? +mm[3] : null };
|
||||
}
|
||||
|
||||
function home_access(p) {
|
||||
return p.access == 'both' || (p.access == 'lan' && !length(p.services));
|
||||
}
|
||||
|
||||
function peer_of(dmp, d) {
|
||||
return dmp.peers[d.public_key];
|
||||
}
|
||||
|
||||
/** The home network (interface) of a server that holds an address. */
|
||||
function home_dev_of(srv, ip, snap) {
|
||||
for (let h in net.server_net(srv, snap).home)
|
||||
if (in_any(ip, h.nets))
|
||||
return h.dev;
|
||||
return null;
|
||||
}
|
||||
|
||||
function router_addrs(snap) {
|
||||
let out = [];
|
||||
for (let dev, d in snap.devices)
|
||||
for (let a in d.addrs ?? [])
|
||||
push(out, split(a, '/')[0]);
|
||||
return out;
|
||||
}
|
||||
|
||||
function person_ok(m, p, d, states) {
|
||||
let st = states[p.id] ?? 'active';
|
||||
if (d.disabled)
|
||||
return step(false, `${d.name} is turned off`, 'Turn the device on again on its page.');
|
||||
if (st != 'active')
|
||||
return step(false, `${p.name} can't connect: ${STATE_TEXT[st] ?? st}`, '');
|
||||
return step(true, `${p.name} is active`, ACCESS_TEXT[p.access]);
|
||||
}
|
||||
|
||||
function connected(dmp, d, who) {
|
||||
let peer = peer_of(dmp, d);
|
||||
if (!peer || !(peer.handshake > 0))
|
||||
return step(false, `${who} has never connected`, 'There is no session the router could reach.');
|
||||
if (time() - peer.handshake >= live.ONLINE_WINDOW)
|
||||
return step(false, `${who} isn't connected right now`, 'The router reaches it only while its VPN is on.');
|
||||
return step(true, `${who} is connected`, '');
|
||||
}
|
||||
|
||||
/** A device → somewhere: the router, home, another device or the internet. */
|
||||
function from_device(m, d, t, states, dmp, snap) {
|
||||
let p = m.people[d.person];
|
||||
let srv = p ? m.servers[p.server] : null;
|
||||
if (!p || !srv)
|
||||
return [ step(false, `${d.name} has no server`, '') ];
|
||||
let steps = [ person_ok(m, p, d, states) ];
|
||||
if (!steps[0].ok)
|
||||
return steps;
|
||||
let allowed = core.allowed_ips(m, srv, p);
|
||||
let via = (ip) => {
|
||||
let hit = in_any(ip, allowed);
|
||||
return hit
|
||||
? step(true, 'Its configuration sends it through the tunnel', `AllowedIPs ${join(', ', filter(allowed, (x) => index(x, ':') < 0))}`)
|
||||
: step(false, "Its configuration doesn't send it through the tunnel", `AllowedIPs ${join(', ', allowed)}: the device's own network handles it.`);
|
||||
};
|
||||
|
||||
// Another device.
|
||||
if (t.dev) {
|
||||
let q = m.people[t.dev.person];
|
||||
let who = `${q?.name ?? '?'} · ${t.dev.name}`;
|
||||
if (t.dev.id == d.id)
|
||||
return [ step(false, 'That is the same device', '') ];
|
||||
if (!q || q.server != p.server)
|
||||
return stop(steps, step(false, `${who} is on another server`, 'Devices of different servers never reach each other.'));
|
||||
if (!home_access(p))
|
||||
return stop(steps, step(false, `${p.name} has ${ACCESS_TEXT[p.access]}`, 'Only people with home access reach other devices.'));
|
||||
if (!home_access(q))
|
||||
return stop(steps, step(false, `${q.name} has ${ACCESS_TEXT[q.access]}`, 'Only devices of people with home access can be reached.'));
|
||||
if (srv.mesh == 'off')
|
||||
return stop(steps, step(false, `"Devices reach each other" is off on ${srv.name}`, 'Server → Access → Home network.'));
|
||||
if (srv.mesh == 'circles' && q.id != p.id) {
|
||||
let a = core.person_circle(m, p), b = core.person_circle(m, q);
|
||||
if (!a || !b || a.id != b.id)
|
||||
return stop(steps, step(false, `${p.name} and ${q.name} are not in one circle`,
|
||||
`${p.name}: ${a?.name ?? 'no circle'} · ${q.name}: ${b?.name ?? 'no circle'}`));
|
||||
push(steps, step(true, `Both are in ${a.name}`, 'Devices in one circle reach each other.'));
|
||||
}
|
||||
else
|
||||
push(steps, step(true, q.id == p.id ? `Both are ${p.name}'s devices` : 'Devices reach each other on this server', ''));
|
||||
let st = states[q.id] ?? 'active';
|
||||
if (st != 'active' || t.dev.disabled)
|
||||
return stop(steps, step(false, `${who} can't be reached: ${t.dev.disabled ? 'it is turned off' : STATE_TEXT[st] ?? st}`, ''));
|
||||
push(steps, via(t.ip));
|
||||
push(steps, connected(dmp, t.dev, who));
|
||||
return steps;
|
||||
}
|
||||
|
||||
// The router itself.
|
||||
if (index(router_addrs(snap), t.ip) >= 0) {
|
||||
if (home_access(p))
|
||||
push(steps, step(true, `${t.ip} is this router`, 'Home access includes the router.'));
|
||||
else if (t.port == 53)
|
||||
push(steps, step(true, `${t.ip} is this router: DNS is open to everyone`, ''));
|
||||
else
|
||||
return stop(steps, step(false, `${t.ip} is this router`, `${p.name} has ${ACCESS_TEXT[p.access]}: only DNS on the router.`));
|
||||
push(steps, via(t.ip));
|
||||
return steps;
|
||||
}
|
||||
|
||||
// Home.
|
||||
let sn = net.server_net(srv, snap);
|
||||
let hdev = home_dev_of(srv, t.ip, snap);
|
||||
if (hdev) {
|
||||
if (p.access == 'internet')
|
||||
return stop(steps, step(false, `${t.ip} is at home (${hdev})`, `${p.name} has Internet only.`));
|
||||
if (p.access == 'lan' && length(p.services)) {
|
||||
let hit = null;
|
||||
for (let sid in p.services) {
|
||||
let s = m.services[sid];
|
||||
if (s?.address == t.ip && (!length(s.ports) || t.port == null || index(s.ports, `${t.port}`) >= 0))
|
||||
hit = s;
|
||||
}
|
||||
let names = join(', ', map(filter(map(p.services, (x) => m.services[x]), (x) => x), (x) => x.name));
|
||||
if (!hit)
|
||||
return stop(steps, step(false, `${t.ip}${t.port ? `:${t.port}` : ''} is not one of ${p.name}'s home services`, `Allowed: ${names}`));
|
||||
push(steps, step(true, `${t.ip} is ${hit.name}, one of ${p.name}'s home services`,
|
||||
length(hit.ports) ? `Ports ${join(', ', hit.ports)}` : 'All ports'));
|
||||
}
|
||||
else {
|
||||
let h = core.person_home(srv, p, snap);
|
||||
if (!in_any(t.ip, h.nets))
|
||||
return stop(steps, step(false, `${t.ip} is in ${hdev}, which ${p.name} doesn't reach`, `${p.name} reaches ${join(', ', h.devs) || 'no home network'}.`));
|
||||
push(steps, step(true, `${t.ip} is in ${hdev}`, h.all ? 'Home access reaches every home network.' : `One of the networks ${p.name} reaches.`));
|
||||
}
|
||||
push(steps, via(t.ip));
|
||||
push(steps, step(true, 'The firewall lets it through', `${srv.ifname} → ${join(', ', sn.zones) || 'home'}`));
|
||||
return steps;
|
||||
}
|
||||
|
||||
// Another private address, or the internet.
|
||||
if (in_any(t.ip, [ '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' ]))
|
||||
return stop(steps, step(false, `${t.ip} isn't in ${srv.name}'s home network`, 'Private addresses outside the home network are blocked.'));
|
||||
if (p.access == 'lan')
|
||||
return stop(steps, step(true, `${t.ip} is on the internet`, `${p.name} has Home only: their device's own internet reaches it, not the tunnel.`));
|
||||
push(steps, via(t.ip));
|
||||
push(steps, step(true, `${t.ip} is on the internet`, srv.lan_like ? 'It leaves like a home device would.' : `It leaves through ${sn.wan.dev ?? 'the WAN'}.`));
|
||||
return steps;
|
||||
}
|
||||
|
||||
/** A home address → a device. */
|
||||
function to_device(m, ip, d, states, dmp, snap) {
|
||||
let p = m.people[d.person];
|
||||
let srv = p ? m.servers[p.server] : null;
|
||||
if (!p || !srv)
|
||||
return [ step(false, `${d.name} has no server`, '') ];
|
||||
let who = `${p.name} · ${d.name}`;
|
||||
let hdev = home_dev_of(srv, ip, snap);
|
||||
if (!hdev)
|
||||
return [ step(false, `${ip} isn't in ${srv.name}'s home network`, 'Only home devices reach people\'s devices.') ];
|
||||
let steps = [ step(true, `${ip} is at home (${hdev})`, '') ];
|
||||
if (!home_access(p))
|
||||
return stop(steps, step(false, `${p.name} has ${p.access == 'lan' ? 'only some home services' : ACCESS_TEXT[p.access]}`,
|
||||
'Home devices reach only people with home access.'));
|
||||
let h = core.person_home(srv, p, snap);
|
||||
if (!in_any(ip, h.nets))
|
||||
return stop(steps, step(false, `${p.name} doesn't reach ${hdev}`, 'Networks a person does not reach cannot reach them either.'));
|
||||
push(steps, person_ok(m, p, d, states));
|
||||
if (!steps[length(steps) - 1].ok)
|
||||
return steps;
|
||||
push(steps, step(true, 'The firewall lets it through', `${join(', ', net.server_net(srv, snap).zones) || 'home'} → ${srv.ifname}`));
|
||||
push(steps, connected(dmp, d, who));
|
||||
return steps;
|
||||
}
|
||||
|
||||
/** `from` and `to`: a device id, or an IPv4 address (optionally :port). */
|
||||
export function check(m, from, to) {
|
||||
let a = parse_target(m, from), b = parse_target(m, to);
|
||||
if (!a)
|
||||
die(`"${from}" is not a device or an IPv4 address.`);
|
||||
if (!b)
|
||||
die(`"${to}" is not a device or an IPv4 address.`);
|
||||
let usage = core.load_usage();
|
||||
let states = view.states(m, usage);
|
||||
let dmp = live.dump();
|
||||
let snap = net.snapshot(true);
|
||||
let steps;
|
||||
if (a.dev)
|
||||
steps = from_device(m, a.dev, b, states, dmp, snap);
|
||||
else if (b.dev)
|
||||
steps = to_device(m, a.ip, b.dev, states, dmp, snap);
|
||||
else
|
||||
die('Choose a device on at least one side.');
|
||||
let ok = length(filter(steps, (s) => !s.ok)) == 0;
|
||||
return { ok, steps };
|
||||
};
|
||||
@@ -269,7 +269,12 @@ const PRIVATE4 = [ '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', '100.64.0.0/
|
||||
|
||||
/** Per-person runtime membership: which tunnel addresses may reach what. */
|
||||
export function access_sets(m, usage, states) {
|
||||
let sets = { lan_ok: [], inet_ok: [], svc_ok: [], dns_real: [] };
|
||||
// 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).
|
||||
let sets = { lan_ok: [], lan_all: [], lan_part: [], mesh_pairs: [], inet_ok: [], svc_ok: [], dns_real: [] };
|
||||
let snap = net.snapshot();
|
||||
let homes = {}, groups = {};
|
||||
for (let id, d in m.devices) {
|
||||
let p = m.people[d.person];
|
||||
if (!p || d.disabled || !d.address || states[p.id] != 'active')
|
||||
@@ -280,8 +285,21 @@ export function access_sets(m, usage, states) {
|
||||
push(sets.dns_real, d.address);
|
||||
if (p.access == 'internet' || p.access == 'both')
|
||||
push(sets.inet_ok, d.address);
|
||||
if (p.access == 'both' || (p.access == 'lan' && !length(p.services)))
|
||||
if (p.access == 'both' || (p.access == 'lan' && !length(p.services))) {
|
||||
push(sets.lan_ok, d.address);
|
||||
let srv = m.servers[p.server];
|
||||
let h = homes[p.id] ??= srv ? core.person_home(srv, p, snap) : { all: true, nets: [] };
|
||||
if (h.all)
|
||||
push(sets.lan_all, d.address);
|
||||
else
|
||||
for (let n in h.nets)
|
||||
push(sets.lan_part, `${d.address} . ${n}`);
|
||||
// Who this device may reach directly: its circle, or at least its owner's devices.
|
||||
if (srv?.mesh == 'circles') {
|
||||
let ci = core.person_circle(m, p);
|
||||
push(groups[ci ? `c:${ci.id}` : `p:${p.id}`] ??= [], d.address);
|
||||
}
|
||||
}
|
||||
else if (p.access == 'lan')
|
||||
for (let sid in p.services) {
|
||||
let s = m.services[sid];
|
||||
@@ -292,6 +310,11 @@ export function access_sets(m, usage, states) {
|
||||
push(sets.svc_ok, `${d.address} . ${s.address} . ${port}`);
|
||||
}
|
||||
}
|
||||
for (let g, addrs in groups)
|
||||
for (let a in addrs)
|
||||
for (let b in addrs)
|
||||
if (a != b)
|
||||
push(sets.mesh_pairs, `${a} . ${b}`);
|
||||
return sets;
|
||||
};
|
||||
|
||||
@@ -314,6 +337,9 @@ function nft_table(m, sets) {
|
||||
push(t, 'table inet dawnline {');
|
||||
push(t, ` set lan_nets { type ipv4_addr; flags interval; auto-merge; ${set_elems(lan)} }`);
|
||||
push(t, ` set lan_ok { type ipv4_addr; ${set_elems(sets.lan_ok)} }`);
|
||||
push(t, ` set lan_all { type ipv4_addr; ${set_elems(sets.lan_all)} }`);
|
||||
push(t, ` set lan_part { type ipv4_addr . ipv4_addr; flags interval; ${set_elems(sets.lan_part)} }`);
|
||||
push(t, ` set mesh_pairs { type ipv4_addr . ipv4_addr; ${set_elems(sets.mesh_pairs)} }`);
|
||||
push(t, ` set inet_ok { type ipv4_addr; ${set_elems(sets.inet_ok)} }`);
|
||||
push(t, ` set svc_ok { type ipv4_addr . ipv4_addr . inet_service; ${set_elems(sets.svc_ok)} }`);
|
||||
push(t, ` set dns_real { type ipv4_addr; ${set_elems(sets.dns_real)} }`);
|
||||
@@ -333,13 +359,16 @@ function nft_table(m, sets) {
|
||||
if (dns_addr)
|
||||
// DNS through the netns (it forwards to the LAN's DNS), for everyone.
|
||||
push(t, ` ip daddr ${dns_addr} meta l4proto { tcp, udp } th dport 53 accept`);
|
||||
push(t, ' ip daddr @lan_nets ip saddr @lan_ok accept');
|
||||
push(t, ' ip daddr @lan_nets ip saddr @lan_all accept');
|
||||
push(t, ' ip saddr . ip daddr @lan_part accept');
|
||||
// Devices with home access are part of home: they reach each other, where the
|
||||
// server allows it. Within one server: the firewall has no forwarding between
|
||||
// different servers' zones.
|
||||
// server allows it (everyone, or within circles). Within one server: the
|
||||
// firewall has no forwarding between different servers' zones.
|
||||
for (let id, srv in m.servers)
|
||||
if (srv.private_key && srv.device_mesh)
|
||||
if (srv.private_key && srv.mesh == 'all')
|
||||
push(t, ` iifname "${srv.ifname}" ip saddr @lan_ok ip daddr @lan_ok accept`);
|
||||
else if (srv.private_key && srv.mesh == 'circles')
|
||||
push(t, ` iifname "${srv.ifname}" ip saddr . ip daddr @mesh_pairs accept`);
|
||||
push(t, ' meta l4proto { tcp, udp } ip saddr . ip daddr . th dport @svc_ok accept');
|
||||
push(t, ` ip daddr { ${join(', ', PRIVATE4)} } counter drop`);
|
||||
push(t, ' ip saddr @inet_ok accept');
|
||||
@@ -357,7 +386,8 @@ function nft_table(m, sets) {
|
||||
push(t, ' chain to_device {');
|
||||
push(t, ' meta nfproto ipv6 drop');
|
||||
push(t, ' ct status dnat accept');
|
||||
push(t, ' ip saddr @lan_nets ip daddr @lan_ok accept');
|
||||
push(t, ' ip saddr @lan_nets ip daddr @lan_all accept');
|
||||
push(t, ' ip daddr . ip saddr @lan_part accept');
|
||||
push(t, ' counter reject with icmpx admin-prohibited');
|
||||
push(t, ' }');
|
||||
// Devices ask the tunnel address; send each query where it belongs.
|
||||
@@ -400,7 +430,7 @@ export function rebuild_nft(m, sets) {
|
||||
/** Atomically replace set contents only. */
|
||||
export function update_sets(sets) {
|
||||
let s = '';
|
||||
for (let name in [ 'lan_ok', 'inet_ok', 'svc_ok', 'dns_real' ]) {
|
||||
for (let name in [ 'lan_ok', 'lan_all', 'lan_part', 'mesh_pairs', 'inet_ok', 'svc_ok', 'dns_real' ]) {
|
||||
s += `flush set inet dawnline ${name}\n`;
|
||||
if (length(sets[name]))
|
||||
s += `add element inet dawnline ${name} { ${join(', ', sets[name])} }\n`;
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
// Dawnline: "Find home devices" (Bonjour over the tunnel). Multicast DNS does
|
||||
// not cross WireGuard, so what umdns sees on the home networks is published
|
||||
// as unicast DNS-SD records in dnsmasq: iPhone, iPad and Mac look there
|
||||
// (browse domains under the home domain and the reverse zone of their own
|
||||
// tunnel address) and list AirPlay, printers and shared Macs as at home.
|
||||
|
||||
'use strict';
|
||||
|
||||
import { readfile, writefile, unlink, lsdir, stat } from 'fs';
|
||||
import { read_json, write_json, ensure_dir, RUN_DIR } from 'dawnline.util';
|
||||
import * as ops from 'dawnline.ops';
|
||||
|
||||
const FILE = 'dawnline-bonjour.conf';
|
||||
const STATE = `${RUN_DIR}/bonjour.json`;
|
||||
/** dnsmasq restarts to read the records: at most this often when only TXT data changed. */
|
||||
const QUIET = 600;
|
||||
/** Not useful from a phone, or not a service at all. */
|
||||
const SKIP = [ '_device-info._tcp', '_companion-link._tcp', '_sleep-proxy._udp', '_workstation._tcp', '_rdlink._tcp', '_hap._udp' ];
|
||||
|
||||
export function enabled(m) {
|
||||
for (let id, srv in m.servers)
|
||||
if (srv.bonjour && srv.private_key)
|
||||
return true;
|
||||
return false;
|
||||
};
|
||||
|
||||
/** dnsmasq's conf-dirs (one per instance), from its generated configs. */
|
||||
function conf_dirs() {
|
||||
let out = [];
|
||||
for (let f in lsdir('/var/etc') ?? []) {
|
||||
if (!match(f, /^dnsmasq\.conf\./))
|
||||
continue;
|
||||
for (let line in split(readfile(`/var/etc/${f}`) ?? '', '\n')) {
|
||||
let mm = match(line, /^conf-dir=([^,]+)/);
|
||||
if (mm && index(out, mm[1]) < 0)
|
||||
push(out, mm[1]);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** An instance name as dnsmasq takes it: printable ASCII, no dots or commas. */
|
||||
function instance(s) {
|
||||
s = replace(s ?? '', /[\u2018\u2019]/g, "'");
|
||||
s = replace(s, /[\u201c\u201d]/g, '');
|
||||
s = replace(s, /[^\x20-\x7e]/g, '');
|
||||
s = replace(s, /[.,"\\]/g, ' ');
|
||||
s = trim(replace(s, / +/g, ' '));
|
||||
return substr(s, 0, 63);
|
||||
}
|
||||
|
||||
function label(s) {
|
||||
return substr(trim(replace(replace(s ?? '', /[^A-Za-z0-9-]+/g, '-'), /^-+|-+$/g, '')), 0, 63);
|
||||
}
|
||||
|
||||
/** Reverse zone of a server's tunnel network (its /24), where devices look for browse domains. */
|
||||
function reverse24(subnet) {
|
||||
let mm = match(subnet ?? '', /^([0-9]+)\.([0-9]+)\.([0-9]+)\.[0-9]+\//);
|
||||
return mm ? `0.${mm[3]}.${mm[2]}.${mm[1]}.in-addr.arpa` : null;
|
||||
}
|
||||
|
||||
/** The records, and a signature of what matters without TXT data. */
|
||||
export function records(m, services) {
|
||||
let dom = m.settings.domain || 'lan';
|
||||
let lines = [], sig = [];
|
||||
let zones = [ dom ];
|
||||
for (let id, srv in m.servers)
|
||||
if (srv.bonjour && reverse24(srv.subnet))
|
||||
push(zones, reverse24(srv.subnet));
|
||||
for (let z in zones)
|
||||
for (let b in [ 'b', 'db', 'lb' ])
|
||||
push(lines, `ptr-record=${b}._dns-sd._udp.${z},${dom}`);
|
||||
let types = {}, hosts = {};
|
||||
for (let s in services) {
|
||||
if (!s.port || index(SKIP, s.type) >= 0 || !match(s.type, /^_[A-Za-z0-9-]+\._(tcp|udp)$/))
|
||||
continue;
|
||||
let name = instance(s.name), host = label(s.host) || label(replace(s.address, /\./g, '-'));
|
||||
if (name == '')
|
||||
continue;
|
||||
let full = `${name}.${s.type}.${dom}`;
|
||||
types[s.type] = true;
|
||||
hosts[host] = s.address;
|
||||
push(lines, `ptr-record=${s.type}.${dom},${full}`);
|
||||
push(lines, `srv-host=${full},${host}.${dom},${s.port}`);
|
||||
let txt = filter(s.txt ?? [], (t) => type(t) == 'string' && length(t) <= 255 && !match(t, /["\\\n]/));
|
||||
push(lines, length(txt) ? `txt-record=${full},${join(',', map(txt, (t) => `"${t}"`))}` : `txt-record=${full}`);
|
||||
push(sig, `${full}|${host}|${s.port}`);
|
||||
}
|
||||
for (let t in sort(keys(types)))
|
||||
push(lines, `ptr-record=_services._dns-sd._udp.${dom},${t}.${dom}`);
|
||||
for (let h in sort(keys(hosts))) {
|
||||
push(lines, `host-record=${h}.${dom},${hosts[h]}`);
|
||||
push(sig, `${h}=${hosts[h]}`);
|
||||
}
|
||||
return { conf: '# Written by Dawnline: home devices for Bonjour over the tunnel.\n' + join('\n', lines) + '\n', sig: join('\n', sort(sig)) };
|
||||
};
|
||||
|
||||
function restart_dnsmasq() {
|
||||
system('/etc/init.d/dnsmasq restart >/dev/null 2>&1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep the records in step with what umdns sees (agent tick and apply).
|
||||
* Turned off: the records go away.
|
||||
*/
|
||||
export function sync(m, force) {
|
||||
let dirs = conf_dirs();
|
||||
if (!enabled(m) || !ops.mdns_running()) {
|
||||
let removed = false;
|
||||
for (let d in dirs)
|
||||
if (stat(`${d}/${FILE}`)) {
|
||||
unlink(`${d}/${FILE}`);
|
||||
removed = true;
|
||||
}
|
||||
if (removed)
|
||||
restart_dnsmasq();
|
||||
unlink(STATE);
|
||||
return false;
|
||||
}
|
||||
let r = records(m, ops.mdns_services());
|
||||
let st = read_json(STATE, {});
|
||||
let now = time();
|
||||
let same = st.conf == r.conf;
|
||||
let quiet = !force && st.sig == r.sig && now - (st.ts ?? 0) < QUIET;
|
||||
let missing = length(filter(dirs, (d) => !stat(`${d}/${FILE}`)));
|
||||
if ((same || quiet) && !missing)
|
||||
return false;
|
||||
for (let d in dirs) {
|
||||
ensure_dir(d);
|
||||
writefile(`${d}/${FILE}`, r.conf);
|
||||
system(`chmod 644 ${d}/${FILE}`);
|
||||
}
|
||||
write_json(STATE, { conf: r.conf, sig: r.sig, ts: now }, 0o644);
|
||||
restart_dnsmasq();
|
||||
return true;
|
||||
};
|
||||
|
||||
/** Turning it on: umdns must run and look at the home networks. */
|
||||
export function start_mdns() {
|
||||
system('/etc/init.d/umdns enable >/dev/null 2>&1; /etc/init.d/umdns start >/dev/null 2>&1');
|
||||
};
|
||||
|
||||
export function view(m) {
|
||||
let s = ops.mdns_running() ? ops.mdns_services() : [];
|
||||
return {
|
||||
running: ops.mdns_running(),
|
||||
services: length(filter(s, (x) => x.port && index(SKIP, x.type) < 0)),
|
||||
};
|
||||
};
|
||||
@@ -14,6 +14,10 @@ import * as loop from 'dawnline.loop';
|
||||
export const CONFIG = 'dawnline';
|
||||
export const ACCESS = [ 'lan', 'internet', 'both' ];
|
||||
export const KINDS = [ 'computer', 'phone', 'tablet', 'router', 'other' ];
|
||||
/** Devices of different people reaching each other: never, everyone with home access, or within a circle. */
|
||||
export const MESH = [ 'off', 'all', 'circles' ];
|
||||
/** Circle colours (index into the admin palette). */
|
||||
export const CIRCLE_COLORS = 8;
|
||||
export const OBF_KEYS = [ 'jc', 'jmin', 'jmax', 's1', 's2', 's3', 's4', 'h1', 'h2', 'h3', 'h4', 'i1', 'i2', 'i3', 'i4', 'i5',
|
||||
// AmneziaWG 3.x
|
||||
'header_protection_key', 'content_padding_addition', 'rekey_after_time', 'rekey_timeout', 'reject_after_time',
|
||||
@@ -102,6 +106,7 @@ export function load(c) {
|
||||
people: {},
|
||||
devices: {},
|
||||
services: {},
|
||||
circles: {},
|
||||
};
|
||||
|
||||
c.foreach(CONFIG, 'server', (r) => {
|
||||
@@ -128,11 +133,16 @@ export function load(c) {
|
||||
detected_wan: r.detected_wan ?? '',
|
||||
detected_lan: r.detected_lan ?? '',
|
||||
lan_like: bool(r.lan_like, true),
|
||||
// Devices of people with home access reach each other through the tunnel.
|
||||
device_mesh: bool(r.device_mesh, true),
|
||||
// Devices of people with home access reach each other: off, all, or within circles.
|
||||
// (Older configs: device_mesh 0/1.)
|
||||
mesh: index(MESH, r.mesh) >= 0 ? r.mesh : (bool(r.device_mesh, true) ? 'all' : 'off'),
|
||||
enabled: bool(r.enabled, true),
|
||||
// Home devices found over Bonjour are published as DNS-SD records (needs umdns).
|
||||
bonjour: bool(r.bonjour, false),
|
||||
obf: {},
|
||||
};
|
||||
// A person's own devices reach each other whenever the mesh is not off.
|
||||
srv.device_mesh = srv.mesh != 'off';
|
||||
for (let k in OBF_KEYS)
|
||||
if (r[k] != null && r[k] != '')
|
||||
srv.obf[k] = `${r[k]}`;
|
||||
@@ -160,6 +170,9 @@ export function load(c) {
|
||||
bonus_gb: num(r.bonus_gb, 0),
|
||||
bonus_month: r.bonus_month ?? '',
|
||||
services: list(r.service),
|
||||
// Home networks (interfaces) this person reaches; empty = all of the server's.
|
||||
home_nets: list(r.home_net),
|
||||
circle: r.circle ?? '',
|
||||
note: r.note ?? '',
|
||||
created: toint(r.created, 0),
|
||||
devices: [],
|
||||
@@ -186,6 +199,11 @@ export function load(c) {
|
||||
push(m.people[d.person].devices, id);
|
||||
});
|
||||
|
||||
c.foreach(CONFIG, 'circle', (r) => {
|
||||
let id = r['.name'];
|
||||
m.circles[id] = { id, server: r.server ?? '', name: r.name ?? id, color: toint(r.color, 0) % CIRCLE_COLORS };
|
||||
});
|
||||
|
||||
c.foreach(CONFIG, 'service', (r) => {
|
||||
let id = r['.name'];
|
||||
m.services[id] = {
|
||||
@@ -460,9 +478,33 @@ export function client_dns(m, srv, p) {
|
||||
return out;
|
||||
};
|
||||
|
||||
/**
|
||||
* The home networks a person reaches: all of the server's, or only the
|
||||
* interfaces ticked on their page (networks added later are not included).
|
||||
*/
|
||||
export function person_home(srv, p, snap) {
|
||||
let sn = net.server_net(srv, snap);
|
||||
let pick = p?.home_nets ?? [];
|
||||
if (!length(pick))
|
||||
return { all: true, nets: sn.nets, devs: map(sn.home, (h) => h.dev) };
|
||||
let nets = [], devs = [];
|
||||
for (let h in sn.home)
|
||||
if (index(pick, h.dev) >= 0) {
|
||||
push(nets, ...h.nets);
|
||||
push(devs, h.dev);
|
||||
}
|
||||
return { all: false, nets: net.collapse(nets), devs };
|
||||
};
|
||||
|
||||
/** The circle a person is in on their server (circles mode only), or null. */
|
||||
export function person_circle(m, p) {
|
||||
let c = m.circles[p?.circle];
|
||||
return c && c.server == p.server ? c : null;
|
||||
};
|
||||
|
||||
export function allowed_ips(m, srv, p) {
|
||||
if (p.access == 'lan') {
|
||||
let nets = net.server_net(srv).nets;
|
||||
let nets = person_home(srv, p).nets;
|
||||
// With devices reaching each other, the whole tunnel network (the router's
|
||||
// address is in it); otherwise only the router's own address.
|
||||
let a = server_address(srv);
|
||||
@@ -750,6 +792,19 @@ export function update_person(c, m, id, a) {
|
||||
else
|
||||
c.delete(CONFIG, id, 'service');
|
||||
}
|
||||
if (a.circle != null) {
|
||||
let ci = m.circles[a.circle];
|
||||
if (a.circle != '' && (!ci || ci.server != p.server))
|
||||
fail('Unknown circle.');
|
||||
set('circle', a.circle);
|
||||
}
|
||||
if (type(a.home_nets) == 'array') {
|
||||
let nets = uniq(filter(map(a.home_nets, (x) => trim(`${x}`)), (x) => match(x, /^[A-Za-z0-9_.@-]{1,32}$/)));
|
||||
if (length(nets))
|
||||
c.set(CONFIG, id, 'home_net', nets);
|
||||
else
|
||||
c.delete(CONFIG, id, 'home_net');
|
||||
}
|
||||
if (a.paused != null)
|
||||
set('paused', a.paused ? '1' : '');
|
||||
};
|
||||
|
||||
@@ -266,3 +266,28 @@ export function wan_address(srv, snap) {
|
||||
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;
|
||||
};
|
||||
|
||||
/** 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 ?? '', /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/g) ?? [])
|
||||
if (names[ip[0]])
|
||||
(out ??= {})[ip[0]] = names[ip[0]];
|
||||
return out;
|
||||
};
|
||||
@@ -34,9 +34,39 @@ export function needs() {
|
||||
acme: miss([ [ file_exists('/usr/lib/acme/client/acme.sh'), 'acme' ], [ file_exists('/usr/lib/acme/client/dnsapi/dns_cf.sh'), 'acme-acmesh-dnsapi' ],
|
||||
[ file_exists('/www/luci-static/resources/view/acme.js'), 'luci-app-acme' ] ]),
|
||||
home: miss([ [ file_exists('/usr/libexec/ip-full'), 'ip-full' ], [ veth, 'kmod-veth' ] ]),
|
||||
bonjour: miss([ [ file_exists('/usr/sbin/umdns'), 'umdns' ] ]),
|
||||
};
|
||||
};
|
||||
|
||||
/** True while OpenWrt's mDNS daemon answers on ubus. */
|
||||
export function mdns_running() {
|
||||
return index(split(sh_out('ubus list umdns 2>/dev/null') ?? '', '\n'), 'umdns') >= 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Services announced on the home networks (Bonjour), from umdns:
|
||||
* [{ type: '_airplay._tcp', name, host, address, port, txt }].
|
||||
*/
|
||||
export function mdns_services() {
|
||||
if (!mdns_running())
|
||||
return [];
|
||||
let r = {};
|
||||
try {
|
||||
r = json(sh_out(`ubus call umdns browse '{"address":true}' 2>/dev/null`) || '{}') ?? {};
|
||||
}
|
||||
catch (e) {
|
||||
r = {};
|
||||
}
|
||||
let out = [];
|
||||
for (let t, insts in r)
|
||||
for (let name, i in insts) {
|
||||
let addr = type(i.ipv4) == 'array' ? i.ipv4[0] : i.ipv4;
|
||||
if (addr)
|
||||
push(out, { type: t, name, host: replace(i.host ?? '', /\.local\.?$/, ''), address: addr, port: i.port ?? 0, txt: i.txt ?? [] });
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
/** ACME certificates on the router and the names each covers. */
|
||||
function acme_certs() {
|
||||
let out = [];
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { open, readfile, writefile, rename, mkdir, stat, unlink, popen, lsdir } from 'fs';
|
||||
|
||||
export const VERSION = '0.2.0';
|
||||
export const VERSION = '0.3.0';
|
||||
/** GitHub repository with Dawnline releases (updates and the installer). */
|
||||
export const REPO = 'tickcount/luci-app-dawnline';
|
||||
export const RUN_DIR = '/var/run/dawnline';
|
||||
|
||||
@@ -90,6 +90,8 @@ export function person_view(m, p, usage, dmp, states, opts) {
|
||||
v.links = core.portal_urls(m, p);
|
||||
v.note = p.note;
|
||||
v.services = p.services;
|
||||
v.home_nets = p.home_nets;
|
||||
v.circle = p.circle;
|
||||
v.created = p.created;
|
||||
}
|
||||
return v;
|
||||
|
||||
Reference in new issue
Block a user