feedback7 G-TOOLS: listen on :: by default so one socket serves IPv6 and IPv4 (PR #291 by @LimitlessHPPK)
Ported only the dual-stack listen from PR #291 (the change accepted as #188). With HOST unset the server binds :: and, if that fails with EAFNOSUPPORT, EADDRNOTAVAIL or EINVAL, retries 0.0.0.0. An explicit HOST, including 0.0.0.0 and 127.0.0.1, is tried once. The boot banner and doctor list shareable IPv6 addresses in brackets, skip fe80::, and keep one URL per /64. An explicit IPv6 literal in the local URL is bracketed. The Windows service listing no longer drops those bracketed lines. Not ported: /dev/grant, the settings-default migration, and the firewall scripts. The Docker image stays on 0.0.0.0; the alpine fallback was not booted. Tests: node --test test/ipv6-bind.test.js test/doctor.test.js test/ci.test.js — 20 pass, 0 fail (8 bind, 4 doctor, 8 ci).
This commit is contained in:
1 parent
c36320fe4a
commit
51c53de189
11 files changed
+374
-54
No files matched your search
+29
-4
@@ -14,18 +14,43 @@ import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { APP_VERSION, DEV_BUILD } from '../../shared/constants.js';
|
||||
import { limitKeyOf } from '../net.js';
|
||||
import { applyPendingUpdate } from '../update.js';
|
||||
import { ROOT } from './config.js';
|
||||
|
||||
/** Non-internal IPv4 addresses as http URLs. @param {number} port */
|
||||
/**
|
||||
* Non-internal addresses as http URLs — IPv4 first, then IPv6 (an IPv6 literal needs brackets: `http://[240e:…]:3000`).
|
||||
* Link-local (`fe80::`) is left out: a URL cannot carry the zone id. Several privacy addresses in one /64 collapse
|
||||
* to one URL (`limitKeyOf`).
|
||||
* @param {number} port
|
||||
*/
|
||||
export function lanUrls(port) {
|
||||
const out = [];
|
||||
const v4 = [];
|
||||
const v6 = [];
|
||||
const v6Seen = new Set();
|
||||
for (const addrs of Object.values(os.networkInterfaces())) {
|
||||
for (const a of addrs || []) {
|
||||
if ((a.family === 'IPv4' || a.family === 4) && !a.internal) out.push(`http://${a.address}:${port}`);
|
||||
if (a.internal) continue;
|
||||
if (a.family === 'IPv4' || a.family === 4) { v4.push(`http://${a.address}:${port}`); continue; }
|
||||
if (a.family !== 'IPv6' && a.family !== 6) continue;
|
||||
if (/^fe80:/i.test(a.address)) continue;
|
||||
const prefix = limitKeyOf(a.address);
|
||||
if (v6Seen.has(prefix)) continue;
|
||||
v6Seen.add(prefix);
|
||||
v6.push(`http://[${a.address}]:${port}`);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
return [...v4, ...v6];
|
||||
}
|
||||
|
||||
/**
|
||||
* The host as it appears in the local URL. A wildcard bind (`0.0.0.0` or `::`) reads `localhost`. An IPv6 literal
|
||||
* is bracketed (`http://[2001:db8::1]:3000`); an IPv4 address or a name is kept.
|
||||
* @param {string} host
|
||||
*/
|
||||
export function displayHost(host) {
|
||||
if (host === '0.0.0.0' || host === '::') return 'localhost';
|
||||
return host.includes(':') ? `[${host}]` : host;
|
||||
}
|
||||
|
||||
/** Is the module whose `import.meta.url` is `metaUrl` the file node was started with? */
|
||||
|
||||
+21
-3
@@ -1,6 +1,6 @@
|
||||
// server/http/config.js — where the server's settings come from. startServer() options win over the environment:
|
||||
//
|
||||
// * PORT (default 3000), HOST (default 0.0.0.0);
|
||||
// * PORT (default 3000), HOST (default '::', one dual-stack socket for IPv6 and IPv4);
|
||||
// * TRUST_PROXY ('auto' default: honour CF-Connecting-IP / X-Real-IP / X-Forwarded-For only from loopback/private
|
||||
// peers such as a local cloudflared; '1' always; '0' never) → net.js trustProxy;
|
||||
// * DEBUG → the console logger's debug level;
|
||||
@@ -21,18 +21,36 @@ const NET_OPTION_KEYS = ['reconnectWindowMs', 'heartbeatMs', 'helloTimeoutMs', '
|
||||
const LOBBY_OPTION_KEYS = ['lobbyGraceMs', 'maxRooms', 'maxRoomsPerAddr', 'maxMatchesPerAddr', 'resyncMinGapMs', 'soloReconnectWindowMs'];
|
||||
|
||||
/**
|
||||
* Where to listen: the `port` / `host` options, else PORT / HOST, else port 3000 on 0.0.0.0.
|
||||
* Bind address used when neither the `host` option nor `HOST` says otherwise: one dual-stack socket, so the server
|
||||
* answers IPv6 and IPv4 alike without a second listener (Node keeps `ipv6Only` off for `::`).
|
||||
* `HOST=0.0.0.0` still means IPv4 only, `HOST=127.0.0.1` still means loopback only (a reverse proxy in front).
|
||||
*/
|
||||
export const DEFAULT_BIND_HOST = '::';
|
||||
|
||||
/**
|
||||
* Where to listen: the `port` / `host` options, else PORT / HOST, else port 3000 on DEFAULT_BIND_HOST.
|
||||
* An empty host is treated as unset.
|
||||
* @param {{ port?: number, host?: string }} opts
|
||||
* @returns {{ port: number, host: string }}
|
||||
* @throws {RangeError} when the port is not an integer in 0…65535
|
||||
*/
|
||||
export function listenAddress(opts) {
|
||||
const port = opts.port ?? (process.env.PORT != null && process.env.PORT !== '' ? Number(process.env.PORT) : 3000);
|
||||
const host = opts.host ?? process.env.HOST ?? '0.0.0.0';
|
||||
const host = (opts.host || process.env.HOST) || DEFAULT_BIND_HOST;
|
||||
if (!Number.isInteger(port) || port < 0 || port > 65535) throw new RangeError(`invalid PORT ${port}`);
|
||||
return { port, host };
|
||||
}
|
||||
|
||||
/**
|
||||
* The hosts to try, in order, for a bind. Only the default is retried: a host with IPv6 switched off refuses `::`
|
||||
* with EAFNOSUPPORT / EADDRNOTAVAIL / EINVAL, and falling back to IPv4 beats not booting. An explicit HOST is literal.
|
||||
* @param {string} host a host out of listenAddress()
|
||||
* @returns {string[]}
|
||||
*/
|
||||
export function bindCandidates(host) {
|
||||
return host === DEFAULT_BIND_HOST ? [DEFAULT_BIND_HOST, '0.0.0.0'] : [host];
|
||||
}
|
||||
|
||||
/**
|
||||
* The directories the static server reads (packsDir: the pack folders, server/packs.js).
|
||||
* @param {{ publicDir?: string, dataDir?: string, sharedDir?: string, packsDir?: string }} opts
|
||||
|
||||
Reference in new issue
Block a user