Files
dawnline/package/luci-app-dawnline/root/usr/share/ucode/dawnline/lookup.uc
T
Aemeath f8130e39a8
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
Dawnline 0.18.0
- Add V2RayTun to a device's set-up on Android: its awg:// link as a code and to copy, in the portal and in the admin (phones, tablets and other devices), with the server's IP, which its Android app needs; not with the Maximum disguise. Its Windows app is ready but stays off until it gives the tunnel the server's DNS.
- Portal: show V2RayTun's Telegram channel beside Google Play.
- Make WireGuard part of AmneziaWG's choice when the disguise is off: the same file and code, both apps named in the steps, WireGuard's store a tap away; a router still picks its package.
- Portal: Reconnect asks again what the device is and about LAN Bypass, and LAN Bypass switches from a device's ⋯ menu, in a group of its own between the copy actions and Rename, Reconnect, Remove (the keys stay; the device takes its updated configuration).
- Turn LAN Bypass on by default for new devices, in the portal and the admin.
- Portal: an app's name too long for the picker fades at its edge and glides to its end on hover or tap; two store buttons stack where they do not fit side by side; the month chart's "Today" stays inside its axis.
- Write to the router's system log by level (Settings → Logs & history → System log: errors, warnings, normal, verbose): the service starting and stopping, every apply with its time, network, firewall, nftables and peer changes, changed settings and portal errors, in English whatever the admin's language, with keys masked.
- Activity: say what a device's change on the portal was (LAN Bypass on or off, its type).
- Demo: build again after a force-pushed main instead of failing on the commit that is gone.
2026-11-05 13:56:20 +03:00

115 lines
3.8 KiB
Ucode

// Dawnline — where names point and the router's public address, for Health
// and the portal settings. Lookups are cached in RUN_DIR for a few minutes
// and made in parallel with a short timeout: rpcd answers one call at a
// time, so a slow DNS server must not hold up the whole admin UI.
import { readfile } from 'fs';
import { q, sh, ensure_dir, read_json, write_json, ip2int, is_ip6, rand_hex, RUN_DIR } from 'dawnline.util';
import * as live from 'dawnline.live';
import * as net from 'dawnline.net';
const CACHE = `${RUN_DIR}/resolve.json`;
// Answers are kept 5 minutes, failures 1 minute.
const TTL = 300;
const TTL_FAIL = 60;
const TIMEOUT = 2;
const MAX_NAMES = 16;
// Public resolvers first: local DNS often rewrites the router's own name to
// its LAN address (split horizon).
const SERVERS = [ '1.1.1.1', '8.8.8.8', '127.0.0.1' ];
function valid_name(h) {
return type(h) == 'string' && length(h) <= 253 && match(h, /^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$/) != null;
}
/**
* Addresses of each name, IPv4 and IPv6: { host: [ ip, … ] } (empty when it
* does not resolve). IP addresses map to themselves. Every uncached name is
* asked of every server at once, A and AAAA; the first server (in SERVERS
* order) that answers wins.
*/
export function resolve_all(hosts) {
let out = {}, now = time(), todo = [];
let cache = read_json(CACHE, {});
for (let h in hosts) {
if (h == null || out[h] != null)
continue;
if (ip2int(h) != null || is_ip6(h)) {
out[h] = [ h ];
continue;
}
let c = cache[h];
if (c && now - c.ts < (length(c.ips) ? TTL : TTL_FAIL))
out[h] = c.ips;
else if (valid_name(h) && length(todo) < MAX_NAMES) {
out[h] = [];
push(todo, h);
}
else
out[h] = [];
}
if (!length(todo))
return out;
ensure_dir(RUN_DIR);
let dir = `${RUN_DIR}/resolve.${rand_hex(4)}`;
let cmds = [];
for (let i = 0; i < length(todo); i++)
for (let j = 0; j < length(SERVERS); j++)
for (let t in [ 'a', 'aaaa' ])
// nslookup's own timeout: some firmware builds BusyBox without `timeout`.
push(cmds, `( nslookup -type=${t} -timeout=${TIMEOUT} -retry=1 ${q(todo[i])} ${SERVERS[j]} 2>/dev/null | awk '/^Address/ && NR>2 {print $NF}' > ${dir}/${i}.${j}.${t} ) &`);
sh(`mkdir -p ${dir}; ${join(' ', cmds)} wait`);
for (let i = 0; i < length(todo); i++) {
let ips = [];
for (let j = 0; j < length(SERVERS) && !length(ips); j++)
for (let t in [ 'a', 'aaaa' ]) {
let r = readfile(`${dir}/${i}.${j}.${t}`) ?? '';
push(ips, ...filter(split(trim(r), '\n'), (x) => ip2int(x) != null || is_ip6(x)));
}
out[todo[i]] = ips;
cache[todo[i]] = { ts: now, ips };
}
system(`rm -rf ${dir}`);
// Only what is still fresh is kept.
for (let h, c in cache)
if (now - c.ts >= TTL)
delete cache[h];
write_json(CACHE, cache, 0o644);
return out;
};
/** IPv4 addresses of one name (see resolve_all). */
export function resolve(host) {
return resolve_all([ host ])[host] ?? [];
};
/**
* The address to give an app that takes no name for the server (V2RayTun's
* Android core hands the endpoint over as it is): IPv4 first, as a phone on
* mobile data may have no IPv6. The host itself when it is an IP; null when
* the name does not resolve.
*/
export function host_ip(host) {
let ips = resolve(host);
let v4 = filter(ips, (x) => ip2int(x) != null);
return v4[0] ?? ips[0] ?? null;
};
/** The IPv4 address of a netifd interface (a zone name for older configs). */
export function wan_ipv4(zone) {
let st = live.iface_status(zone == 'wan' ? 'wan' : zone);
return st?.['ipv4-address']?.[0]?.address;
};
/** The router's public IPv4 (the first server's WAN, else the wan interface). */
export function router_wan(m) {
for (let id, srv in m.servers)
if (srv.private_key) {
let a = net.wan_address(srv) ?? wan_ipv4(srv.wan_zone);
if (a)
return a;
}
return wan_ipv4('wan');
};