diff --git a/package-lock.json b/package-lock.json index 715bf22..3ffb819 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,7 @@ "fs-extra": "^8.1.0", "got": "^14.2.0", "http2-express-bridge": "^1.0.7", - "ip": "^2.0.1", + "ipaddr.js": "^2.2.0", "json-stringify-safe": "^5.0.1", "keyv": "^4.5.4", "keyv-file": "^0.3.0", @@ -3442,18 +3442,13 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" }, - "node_modules/ip": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.1.tgz", - "integrity": "sha512-lJUL9imLTNi1ZfXT+DU6rBBdbiKGBuay9B6xGSPVjUeQwaH1RIGqef8RZkUtHioLmSNpPR5M4HVKJGm1j8FWVQ==", - "license": "MIT" - }, "node_modules/ipaddr.js": { - "version": "1.9.1", - "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", - "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==", + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.2.0.tgz", + "integrity": "sha512-Ag3wB2o37wslZS19hZqorUnrnzSkpOVy+IiiDEiTqNubEYpYuHWIf6K4psgN2ZWKExS4xhVCrRVfb/wfW8fWJA==", + "license": "MIT", "engines": { - "node": ">= 0.10" + "node": ">= 10" } }, "node_modules/is-buffer": { @@ -4854,6 +4849,15 @@ "node": ">= 0.10" } }, + "node_modules/proxy-addr/node_modules/ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==", + "license": "MIT", + "engines": { + "node": ">= 0.10" + } + }, "node_modules/ps-tree": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/ps-tree/-/ps-tree-1.2.0.tgz", @@ -8345,15 +8349,10 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" }, - "ip": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.1.tgz", - "integrity": "sha512-lJUL9imLTNi1ZfXT+DU6rBBdbiKGBuay9B6xGSPVjUeQwaH1RIGqef8RZkUtHioLmSNpPR5M4HVKJGm1j8FWVQ==" - }, "ipaddr.js": { - "version": "1.9.1", - "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", - "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-2.2.0.tgz", + "integrity": "sha512-Ag3wB2o37wslZS19hZqorUnrnzSkpOVy+IiiDEiTqNubEYpYuHWIf6K4psgN2ZWKExS4xhVCrRVfb/wfW8fWJA==" }, "is-buffer": { "version": "1.1.6", @@ -9303,6 +9302,13 @@ "requires": { "forwarded": "0.2.0", "ipaddr.js": "1.9.1" + }, + "dependencies": { + "ipaddr.js": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz", + "integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==" + } } }, "ps-tree": { diff --git a/package.json b/package.json index e4fca90..0b6175b 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "fs-extra": "^8.1.0", "got": "^14.2.0", "http2-express-bridge": "^1.0.7", - "ip": "^2.0.1", + "ipaddr.js": "^2.2.0", "json-stringify-safe": "^5.0.1", "keyv": "^4.5.4", "keyv-file": "^0.3.0", diff --git a/src/cluster.ts b/src/cluster.ts index f0704b0..45bda55 100644 --- a/src/cluster.ts +++ b/src/cluster.ts @@ -12,6 +12,7 @@ import {createServer, Server} from 'http' import {createSecureServer} from 'http2' import http2Express from 'http2-express-bridge' import {Agent as HttpsAgent} from 'https' +import ipaddr from 'ipaddr.js' import stringifySafe from 'json-stringify-safe' import {template, toString} from 'lodash-es' import morgan from 'morgan' @@ -39,7 +40,6 @@ import type {TokenManager} from './token.js' import type {IFileList} from './types.js' import {setupUpnp} from './upnp.js' import {checkSign, hashToFilename} from './util.js' -import ipPkg from 'ip' interface ICounters { hits: number @@ -135,7 +135,11 @@ export class Cluster { await this.storage.init?.() if (config.enableUpnp) { const ip = await setupUpnp(config.port, config.clusterPublicPort) - if (ipPkg.isPrivate(ip)) { + const addr = ipaddr.parse(ip) + if (addr.kind() !== 'ipv4') { + throw new Error('不支持ipv6') + } + if (addr.range() !== 'unicast') { throw new Error(`无法获取公网IP, UPNP返回的IP位于私有地址段, IP: ${ip}`) } logger.info(`upnp映射成功,外网IP: ${ip}`)