Refactor IP interface query

This commit is contained in:
Cohee
2025-02-26 15:44:39 +00:00
parent b64273ab94
commit 68eecc77bb
2 changed files with 23 additions and 32 deletions

View File

@@ -308,17 +308,11 @@ export class ServerStartup {
let useIPv6 = (this.cliArgs.enableIPv6 === true);
let useIPv4 = (this.cliArgs.enableIPv4 === true);
let hasIPv6 = false,
hasIPv4 = false,
hasIPv6Local = false,
hasIPv4Local = false,
hasIPv6Any = false,
hasIPv4Any = false;
if (this.cliArgs.enableIPv6 === 'auto' || this.cliArgs.enableIPv4 === 'auto') {
[hasIPv6Any, hasIPv4Any, hasIPv6Local, hasIPv4Local] = await getHasIP();
const ipQuery = await getHasIP();
let hasIPv6 = false, hasIPv4 = false;
hasIPv6 = this.cliArgs.listen ? hasIPv6Any : hasIPv6Local;
hasIPv6 = this.cliArgs.listen ? ipQuery.hasIPv6Any : ipQuery.hasIPv6Local;
if (this.cliArgs.enableIPv6 === 'auto') {
useIPv6 = hasIPv6;
}
@@ -330,7 +324,7 @@ export class ServerStartup {
}
}
hasIPv4 = this.cliArgs.listen ? hasIPv4Any : hasIPv4Local;
hasIPv4 = this.cliArgs.listen ? ipQuery.hasIPv4Any : ipQuery.hasIPv4Local;
if (this.cliArgs.enableIPv4 === 'auto') {
useIPv4 = hasIPv4;
}
@@ -351,7 +345,7 @@ export class ServerStartup {
}
if (!useIPv6 && !useIPv4) {
console.error('Both IPv6 and IPv4 are disabled');
console.error('Both IPv6 and IPv4 are disabled or not detected');
process.exit(1);
}

View File

@@ -774,17 +774,18 @@ export async function canResolve(name, useIPv6 = true, useIPv4 = true) {
/**
* Checks the network interfaces to determine the presence of IPv6 and IPv4 addresses.
*
* @returns {Promise<[boolean, boolean, boolean, boolean]>} A promise that resolves to an array containing:
* - [0]: `hasIPv6` (boolean) - Whether the computer has any IPv6 address, including (`::1`).
* - [1]: `hasIPv4` (boolean) - Whether the computer has any IPv4 address, including (`127.0.0.1`).
* - [2]: `hasIPv6Local` (boolean) - Whether the computer has local IPv6 address (`::1`).
* - [3]: `hasIPv4Local` (boolean) - Whether the computer has local IPv4 address (`127.0.0.1`).
* @typedef {object} IPQueryResult
* @property {boolean} hasIPv6Any - Whether the computer has any IPv6 address, including (`::1`).
* @property {boolean} hasIPv4Any - Whether the computer has any IPv4 address, including (`127.0.0.1`).
* @property {boolean} hasIPv6Local - Whether the computer has local IPv6 address (`::1`).
* @property {boolean} hasIPv4Local - Whether the computer has local IPv4 address (`127.0.0.1`).
* @returns {Promise<IPQueryResult>} A promise that resolves to an array containing:
*/
export async function getHasIP() {
let hasIPv6 = false;
let hasIPv6Any = false;
let hasIPv6Local = false;
let hasIPv4 = false;
let hasIPv4Any = false;
let hasIPv4Local = false;
const interfaces = os.networkInterfaces();
@@ -796,28 +797,24 @@ export async function getHasIP() {
for (const info of iface) {
if (info.family === 'IPv6') {
hasIPv6 = true;
hasIPv6Any = true;
if (info.address === '::1') {
hasIPv6Local = true;
}
}
if (info.family === 'IPv4') {
hasIPv4 = true;
hasIPv4Any = true;
if (info.address === '127.0.0.1') {
hasIPv4Local = true;
}
}
if (hasIPv6 && hasIPv4 && hasIPv6Local && hasIPv4Local) break;
if (hasIPv6Any && hasIPv4Any && hasIPv6Local && hasIPv4Local) break;
}
if (hasIPv6 && hasIPv4 && hasIPv6Local && hasIPv4Local) break;
if (hasIPv6Any && hasIPv4Any && hasIPv6Local && hasIPv4Local) break;
}
return [
hasIPv6,
hasIPv4,
hasIPv6Local,
hasIPv4Local,
];
return { hasIPv6Any, hasIPv4Any, hasIPv6Local, hasIPv4Local };
}