Refactor IP interface query

68eecc77bb2a9bff0e560cafa687f8e3699fd219

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +19 -28Showing whitespace changes
src/server-startup.js+5 -11
@@ -308,17 +308,11 @@ export class ServerStartup {
308 let useIPv6 = (this.cliArgs.enableIPv6 === true);308 let useIPv6 = (this.cliArgs.enableIPv6 === true);
309 let useIPv4 = (this.cliArgs.enableIPv4 === true);309 let useIPv4 = (this.cliArgs.enableIPv4 === true);
310310
311 let hasIPv6 = false,
312 hasIPv4 = false,
313 hasIPv6Local = false,
314 hasIPv4Local = false,
315 hasIPv6Any = false,
316 hasIPv4Any = false;
317
318 if (this.cliArgs.enableIPv6 === 'auto' || this.cliArgs.enableIPv4 === 'auto') {311 if (this.cliArgs.enableIPv6 === 'auto' || this.cliArgs.enableIPv4 === 'auto') {
319 [hasIPv6Any, hasIPv4Any, hasIPv6Local, hasIPv4Local] = await getHasIP();312 const ipQuery = await getHasIP();
313 let hasIPv6 = false, hasIPv4 = false;
320314
321 hasIPv6 = this.cliArgs.listen ? hasIPv6Any : hasIPv6Local;315 hasIPv6 = this.cliArgs.listen ? ipQuery.hasIPv6Any : ipQuery.hasIPv6Local;
322 if (this.cliArgs.enableIPv6 === 'auto') {316 if (this.cliArgs.enableIPv6 === 'auto') {
323 useIPv6 = hasIPv6;317 useIPv6 = hasIPv6;
324 }318 }
@@ -330,7 +324,7 @@ export class ServerStartup {
330 }324 }
331 }325 }
332326
333 hasIPv4 = this.cliArgs.listen ? hasIPv4Any : hasIPv4Local;327 hasIPv4 = this.cliArgs.listen ? ipQuery.hasIPv4Any : ipQuery.hasIPv4Local;
334 if (this.cliArgs.enableIPv4 === 'auto') {328 if (this.cliArgs.enableIPv4 === 'auto') {
335 useIPv4 = hasIPv4;329 useIPv4 = hasIPv4;
336 }330 }
@@ -351,7 +345,7 @@ export class ServerStartup {
351 }345 }
352346
353 if (!useIPv6 && !useIPv4) {347 if (!useIPv6 && !useIPv4) {
354 console.error('Both IPv6 and IPv4 are disabled');348 console.error('Both IPv6 and IPv4 are disabled or not detected');
355 process.exit(1);349 process.exit(1);
356 }350 }
357351
src/util.js+14 -17
@@ -774,17 +774,18 @@ export async function canResolve(name, useIPv6 = true, useIPv4 = true) {
774/**774/**
775 * Checks the network interfaces to determine the presence of IPv6 and IPv4 addresses.775 * Checks the network interfaces to determine the presence of IPv6 and IPv4 addresses.
776 *776 *
777 * @returns {Promise<[boolean, boolean, boolean, boolean]>} A promise that resolves to an array containing:777 * @typedef {object} IPQueryResult
778 * - [0]: `hasIPv6` (boolean) - Whether the computer has any IPv6 address, including (`::1`).778 * @property {boolean} hasIPv6Any - Whether the computer has any IPv6 address, including (`::1`).
779 * - [1]: `hasIPv4` (boolean) - Whether the computer has any IPv4 address, including (`127.0.0.1`).779 * @property {boolean} hasIPv4Any - Whether the computer has any IPv4 address, including (`127.0.0.1`).
780 * - [2]: `hasIPv6Local` (boolean) - Whether the computer has local IPv6 address (`::1`).780 * @property {boolean} hasIPv6Local - Whether the computer has local IPv6 address (`::1`).
781 * - [3]: `hasIPv4Local` (boolean) - Whether the computer has local IPv4 address (`127.0.0.1`).781 * @property {boolean} hasIPv4Local - Whether the computer has local IPv4 address (`127.0.0.1`).
782 * @returns {Promise<IPQueryResult>} A promise that resolves to an array containing:
782 */783 */
783export async function getHasIP() {784export async function getHasIP() {
784 let hasIPv6 = false;785 let hasIPv6Any = false;
785 let hasIPv6Local = false;786 let hasIPv6Local = false;
786787
787 let hasIPv4 = false;788 let hasIPv4Any = false;
788 let hasIPv4Local = false;789 let hasIPv4Local = false;
789790
790 const interfaces = os.networkInterfaces();791 const interfaces = os.networkInterfaces();
@@ -796,28 +797,24 @@ export async function getHasIP() {
796797
797 for (const info of iface) {798 for (const info of iface) {
798 if (info.family === 'IPv6') {799 if (info.family === 'IPv6') {
799 hasIPv6 = true;800 hasIPv6Any = true;
800 if (info.address === '::1') {801 if (info.address === '::1') {
801 hasIPv6Local = true;802 hasIPv6Local = true;
802 }803 }
803 }804 }
804805
805 if (info.family === 'IPv4') {806 if (info.family === 'IPv4') {
806 hasIPv4 = true;807 hasIPv4Any = true;
807 if (info.address === '127.0.0.1') {808 if (info.address === '127.0.0.1') {
808 hasIPv4Local = true;809 hasIPv4Local = true;
809 }810 }
810 }811 }
811 if (hasIPv6 && hasIPv4 && hasIPv6Local && hasIPv4Local) break;812 if (hasIPv6Any && hasIPv4Any && hasIPv6Local && hasIPv4Local) break;
812 }813 }
813 if (hasIPv6 && hasIPv4 && hasIPv6Local && hasIPv4Local) break;814 if (hasIPv6Any && hasIPv4Any && hasIPv6Local && hasIPv4Local) break;
814 }815 }
815 return [816
816 hasIPv6,817 return { hasIPv6Any, hasIPv4Any, hasIPv6Local, hasIPv4Local };
817 hasIPv4,
818 hasIPv6Local,
819 hasIPv4Local,
820 ];
821}818}
822819
823820