moshcode 0.78.0 → 0.79.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/cli-schema.mjs +2 -1
- package/src/dns-service.mjs +8 -0
- package/src/dns.mjs +78 -4
package/package.json
CHANGED
package/src/cli-schema.mjs
CHANGED
|
@@ -1007,7 +1007,7 @@ export const DNS_VERBS = [
|
|
|
1007
1007
|
{ name: "disable", description: "undo enable" },
|
|
1008
1008
|
{ name: "status", description: "what is running, what is routed, does it work" },
|
|
1009
1009
|
{ name: "refresh", description: "re-apply routing for endings claimed since" },
|
|
1010
|
-
{ name: "start", description: "run the bridge in the foreground" },
|
|
1010
|
+
{ name: "start", description: "run the bridge in the foreground (--upstream IP to say where to forward)" },
|
|
1011
1011
|
{ name: "install", description: "print the resolver config without applying it" },
|
|
1012
1012
|
{
|
|
1013
1013
|
name: "service",
|
|
@@ -1017,6 +1017,7 @@ export const DNS_VERBS = [
|
|
|
1017
1017
|
["moshcode dns service --write", "install and start it as this user; needs no root"],
|
|
1018
1018
|
["moshcode dns service --system", "a system unit instead — place it with sudo tee"],
|
|
1019
1019
|
["moshcode dns service --remove", "stop it and take the unit away"],
|
|
1020
|
+
["moshcode dns service --upstream IP", "where to forward, when discovery cannot see past the bridge"],
|
|
1020
1021
|
],
|
|
1021
1022
|
},
|
|
1022
1023
|
{ name: "tlds", description: "list the endings claimed in the Pit" },
|
package/src/dns-service.mjs
CHANGED
|
@@ -66,12 +66,20 @@ export function serviceUnit({
|
|
|
66
66
|
entry,
|
|
67
67
|
port,
|
|
68
68
|
registryBase = null,
|
|
69
|
+
upstreams = [],
|
|
69
70
|
user = process.env.USER || process.env.LOGNAME,
|
|
70
71
|
} = {}) {
|
|
71
72
|
if (!entry) throw new Error("serviceUnit needs the entry script to run");
|
|
72
73
|
|
|
73
74
|
const args = [entry, "dns", "start", "--port", String(port)];
|
|
74
75
|
if (registryBase) args.push("--registry", registryBase);
|
|
76
|
+
// The reason this unit exists at all is that the bridge now starts at boot —
|
|
77
|
+
// and at boot the resolved drop-in is already in place, so the only
|
|
78
|
+
// nameserver discovery can find is this bridge. It refuses loopback, comes up
|
|
79
|
+
// with nowhere to forward, and NXDOMAINs every clearnet name including the
|
|
80
|
+
// registry. Recorded here, while a working resolver is still around to be
|
|
81
|
+
// asked, rather than rediscovered at boot when it cannot be.
|
|
82
|
+
if (upstreams.length) args.push("--upstream", upstreams.join(","));
|
|
75
83
|
const exec = [execPath, ...args].map((part) => (/\s/.test(part) ? JSON.stringify(part) : part)).join(" ");
|
|
76
84
|
|
|
77
85
|
const lines = [
|
package/src/dns.mjs
CHANGED
|
@@ -1629,6 +1629,48 @@ export function dnsmasqCatchAllConf({ host = DEFAULT_HOST, port = DEFAULT_PORT }
|
|
|
1629
1629
|
* wrote 127.0.0.53 into resolv.conf is the thing sending us the query, and
|
|
1630
1630
|
* forwarding back to it is a loop that ends in a timeout rather than an answer.
|
|
1631
1631
|
*/
|
|
1632
|
+
/**
|
|
1633
|
+
* Upstreams named on the command line, which override discovery entirely.
|
|
1634
|
+
*
|
|
1635
|
+
* Discovery reads the machine's resolv.conf and drops loopback, which is right
|
|
1636
|
+
* until the machine's resolver is this bridge. Then the only nameserver on file
|
|
1637
|
+
* IS the bridge, discovery correctly refuses to return it, and the daemon comes
|
|
1638
|
+
* up with nowhere to forward — every clearnet name NXDOMAIN, including the
|
|
1639
|
+
* registry it needs in order to know which endings are Moshpit at all. The box
|
|
1640
|
+
* loses DNS entirely and the bridge cannot bootstrap out of it, because the
|
|
1641
|
+
* lookup that would fix it goes through the bridge.
|
|
1642
|
+
*
|
|
1643
|
+
* `dns enable` never hit this: it runs before the routing exists. A supervised
|
|
1644
|
+
* bridge starting at boot hits it every time, because the drop-in is a file and
|
|
1645
|
+
* is already in place. So the upstreams have to be sayable rather than only
|
|
1646
|
+
* discoverable, and `dns service` bakes the ones it found into the unit.
|
|
1647
|
+
*
|
|
1648
|
+
* Repeatable and comma-separated both work. `address#port` matches resolv.conf
|
|
1649
|
+
* and dnsmasq rather than inventing a third spelling.
|
|
1650
|
+
*/
|
|
1651
|
+
export function upstreamsFromArgs(args = []) {
|
|
1652
|
+
const servers = [];
|
|
1653
|
+
const invalid = [];
|
|
1654
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
1655
|
+
if (args[i] !== "--upstream") continue;
|
|
1656
|
+
const value = args[i + 1];
|
|
1657
|
+
// A bare trailing `--upstream`, or one followed by the next flag, is a
|
|
1658
|
+
// typo rather than a request for no upstreams. Reported, not ignored.
|
|
1659
|
+
if (value === undefined || value.startsWith("--")) {
|
|
1660
|
+
invalid.push("(missing value)");
|
|
1661
|
+
continue;
|
|
1662
|
+
}
|
|
1663
|
+
for (const part of value.split(",")) {
|
|
1664
|
+
const server = part.trim();
|
|
1665
|
+
if (!server) continue;
|
|
1666
|
+
const [address] = server.split("#");
|
|
1667
|
+
if (!isIP(address)) invalid.push(server);
|
|
1668
|
+
else if (!servers.includes(server)) servers.push(server);
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
return { servers, invalid };
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1632
1674
|
export function parseUpstreams(resolvConf) {
|
|
1633
1675
|
const out = [];
|
|
1634
1676
|
for (const line of String(resolvConf ?? "").split("\n")) {
|
|
@@ -2307,6 +2349,9 @@ const USAGE = `moshcode dns — resolve Moshpit names on this machine
|
|
|
2307
2349
|
look a name up; --open opens a parked name in the Pit
|
|
2308
2350
|
--json prints one stable document for scripts
|
|
2309
2351
|
moshcode dns start [--port N] run the resolver in the foreground
|
|
2352
|
+
--upstream IP[,IP] where to forward clearnet
|
|
2353
|
+
lookups; overrides resolv.conf discovery, which
|
|
2354
|
+
finds nothing once this machine is routed here
|
|
2310
2355
|
also serves parked names over HTTP so \`curl <name>\`
|
|
2311
2356
|
lands on the Pit; --parking-port N, --no-parking-http
|
|
2312
2357
|
--no-filter runs it with blocklists off
|
|
@@ -2514,7 +2559,12 @@ export async function dnsCommand(args = [], out = console.log, deps = {}) {
|
|
|
2514
2559
|
if (!park) out("! parking host did not resolve — unpointed names will return NXDOMAIN");
|
|
2515
2560
|
// Without these the bridge answers only for endings it is authoritative
|
|
2516
2561
|
// for, which is correct for per-ending routing and fatal for catch-all.
|
|
2517
|
-
|
|
2562
|
+
// Named upstreams win outright. Discovery is a fallback for the ordinary
|
|
2563
|
+
// case, not a second opinion: someone who says where to forward has almost
|
|
2564
|
+
// always said it because discovery got it wrong.
|
|
2565
|
+
const named = upstreamsFromArgs(rest);
|
|
2566
|
+
for (const bad of named.invalid) out(`! ignoring --upstream ${bad} — not an IP address`);
|
|
2567
|
+
const upstreams = named.servers.length ? named.servers : await discoverUpstreams();
|
|
2518
2568
|
// Swallowing this was the quietest way to turn the namespace off. An empty
|
|
2519
2569
|
// ending set makes isOurs() say no to every name, so with upstreams present
|
|
2520
2570
|
// the bridge forwards the whole of Moshpit to the clearnet, which denies it
|
|
@@ -2528,8 +2578,17 @@ export async function dnsCommand(args = [], out = console.log, deps = {}) {
|
|
|
2528
2578
|
(err) => ({ error: err?.message || String(err) }),
|
|
2529
2579
|
);
|
|
2530
2580
|
const tldSet = new Set(tlds.found || []);
|
|
2531
|
-
if (upstreams.length)
|
|
2532
|
-
|
|
2581
|
+
if (upstreams.length) {
|
|
2582
|
+
out(`forwarding non-Moshpit lookups to ${upstreams.join(", ")}${named.servers.length ? " (--upstream)" : ""}`);
|
|
2583
|
+
} else {
|
|
2584
|
+
out("! no upstreams found in /etc/resolv.conf — this bridge can only answer Moshpit names");
|
|
2585
|
+
// The specific way this happens is worth naming, because the symptom
|
|
2586
|
+
// (every name NXDOMAIN) looks nothing like the cause and the machine
|
|
2587
|
+
// cannot look the cause up.
|
|
2588
|
+
out(" if this machine's resolver is already routed here, discovery has only");
|
|
2589
|
+
out(" the bridge to find and correctly refuses it — say where to forward:");
|
|
2590
|
+
out(` moshcode dns start --port ${port} --upstream 1.1.1.1`);
|
|
2591
|
+
}
|
|
2533
2592
|
if (tldSet.size) out(`answering for ${tldSet.size} endings`);
|
|
2534
2593
|
else {
|
|
2535
2594
|
out(`! could not read the ending list from ${registryBase}${tlds.error ? ` — ${tlds.error}` : ""}`);
|
|
@@ -2707,7 +2766,22 @@ export async function dnsCommand(args = [], out = console.log, deps = {}) {
|
|
|
2707
2766
|
return 0;
|
|
2708
2767
|
}
|
|
2709
2768
|
|
|
2710
|
-
|
|
2769
|
+
// Asked now, not at boot. This command runs while the machine still has a
|
|
2770
|
+
// resolver that answers; the service it writes will not.
|
|
2771
|
+
const upstreams = upstreamsFromArgs(rest).servers.length
|
|
2772
|
+
? upstreamsFromArgs(rest).servers
|
|
2773
|
+
: await discoverUpstreams();
|
|
2774
|
+
const unit = serviceUnit({ system, entry: cliEntry(), port, registryBase, upstreams });
|
|
2775
|
+
|
|
2776
|
+
if (!upstreams.length) {
|
|
2777
|
+
out("! no upstream nameservers found, and none given");
|
|
2778
|
+
out(" a bridge with nowhere to forward answers NXDOMAIN for every clearnet");
|
|
2779
|
+
out(" name — including the registry it needs to know which endings exist.");
|
|
2780
|
+
out(" If this machine is already routed here, discovery can only see the");
|
|
2781
|
+
out(" bridge and correctly refuses it. Say where to forward:");
|
|
2782
|
+
out(` moshcode dns service --upstream 1.1.1.1${rest.includes("--write") ? " --write" : ""}`);
|
|
2783
|
+
out("");
|
|
2784
|
+
}
|
|
2711
2785
|
|
|
2712
2786
|
if (rest.includes("--write")) {
|
|
2713
2787
|
const result = await installService(unit, { system });
|