mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Refactor IP interface query
This commit is contained in:
@@ -308,17 +308,11 @@ export class ServerStartup {
|
|||||||
let useIPv6 = (this.cliArgs.enableIPv6 === true);
|
let useIPv6 = (this.cliArgs.enableIPv6 === true);
|
||||||
let useIPv4 = (this.cliArgs.enableIPv4 === 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') {
|
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') {
|
if (this.cliArgs.enableIPv6 === 'auto') {
|
||||||
useIPv6 = hasIPv6;
|
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') {
|
if (this.cliArgs.enableIPv4 === 'auto') {
|
||||||
useIPv4 = hasIPv4;
|
useIPv4 = hasIPv4;
|
||||||
}
|
}
|
||||||
@@ -351,7 +345,7 @@ export class ServerStartup {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!useIPv6 && !useIPv4) {
|
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);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
31
src/util.js
31
src/util.js
@@ -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.
|
* 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:
|
* @typedef {object} IPQueryResult
|
||||||
* - [0]: `hasIPv6` (boolean) - Whether the computer has any IPv6 address, including (`::1`).
|
* @property {boolean} hasIPv6Any - Whether the computer has any IPv6 address, including (`::1`).
|
||||||
* - [1]: `hasIPv4` (boolean) - Whether the computer has any IPv4 address, including (`127.0.0.1`).
|
* @property {boolean} hasIPv4Any - Whether the computer has any IPv4 address, including (`127.0.0.1`).
|
||||||
* - [2]: `hasIPv6Local` (boolean) - Whether the computer has local IPv6 address (`::1`).
|
* @property {boolean} hasIPv6Local - Whether the computer has local IPv6 address (`::1`).
|
||||||
* - [3]: `hasIPv4Local` (boolean) - Whether the computer has local IPv4 address (`127.0.0.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() {
|
export async function getHasIP() {
|
||||||
let hasIPv6 = false;
|
let hasIPv6Any = false;
|
||||||
let hasIPv6Local = false;
|
let hasIPv6Local = false;
|
||||||
|
|
||||||
let hasIPv4 = false;
|
let hasIPv4Any = false;
|
||||||
let hasIPv4Local = false;
|
let hasIPv4Local = false;
|
||||||
|
|
||||||
const interfaces = os.networkInterfaces();
|
const interfaces = os.networkInterfaces();
|
||||||
@@ -796,28 +797,24 @@ export async function getHasIP() {
|
|||||||
|
|
||||||
for (const info of iface) {
|
for (const info of iface) {
|
||||||
if (info.family === 'IPv6') {
|
if (info.family === 'IPv6') {
|
||||||
hasIPv6 = true;
|
hasIPv6Any = true;
|
||||||
if (info.address === '::1') {
|
if (info.address === '::1') {
|
||||||
hasIPv6Local = true;
|
hasIPv6Local = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (info.family === 'IPv4') {
|
if (info.family === 'IPv4') {
|
||||||
hasIPv4 = true;
|
hasIPv4Any = true;
|
||||||
if (info.address === '127.0.0.1') {
|
if (info.address === '127.0.0.1') {
|
||||||
hasIPv4Local = true;
|
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,
|
return { hasIPv6Any, hasIPv4Any, hasIPv6Local, hasIPv4Local };
|
||||||
hasIPv4,
|
|
||||||
hasIPv6Local,
|
|
||||||
hasIPv4Local,
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user