From bc73ed15b251fd1e4ea41a8794b355ee16605b87 Mon Sep 17 00:00:00 2001 From: bangbang93 Date: Thu, 14 Nov 2024 13:11:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=A3=80=E6=9F=A5UPnP=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E7=9A=84ip=E6=98=AF=E5=90=A6=E4=B8=BA=E5=85=AC?= =?UTF-8?q?=E7=BD=91IP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 35 +++++++++++++++++++++++++++++++++++ package.json | 2 ++ src/cluster.ts | 10 ++++++++-- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6efcc96..d58b7da 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,6 +24,7 @@ "fs-extra": "^8.1.0", "got": "^14.2.0", "http2-express-bridge": "^1.0.7", + "ip": "^2.0.1", "json-stringify-safe": "^5.0.1", "keyv": "^4.5.4", "keyv-file": "^0.3.0", @@ -54,6 +55,7 @@ "@types/dotenv": "^6.1.1", "@types/express": "^4.17.13", "@types/fs-extra": "^8.0.0", + "@types/ip": "^1.1.3", "@types/json-stringify-safe": "^5.0.3", "@types/lodash-es": "^4.17.7", "@types/morgan": "^1.7.36", @@ -71,6 +73,9 @@ "rimraf": "^5.0.5", "tsc-watch": "^6.0.4", "typescript": "^5.3.3" + }, + "engines": { + "node": ">=18 <24" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -812,6 +817,16 @@ "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.4.tgz", "integrity": "sha512-1m0bIFVc7eJWyve9S0RnuRgcQqF/Xd5QsUZAZeQFr1Q3/p9JWoQQEqmVy+DPTNpGXwhgIetAoYF8JSc33q29QA==" }, + "node_modules/@types/ip": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/@types/ip/-/ip-1.1.3.tgz", + "integrity": "sha512-64waoJgkXFTYnCYDUWgSATJ/dXEBanVkaP5d4Sbk7P6U7cTTMhxVyROTckc6JKdwCrgnAjZMn0k3177aQxtDEA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/json-schema": { "version": "7.0.15", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.15.tgz", @@ -3427,6 +3442,12 @@ "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", @@ -6520,6 +6541,15 @@ "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.4.tgz", "integrity": "sha512-1m0bIFVc7eJWyve9S0RnuRgcQqF/Xd5QsUZAZeQFr1Q3/p9JWoQQEqmVy+DPTNpGXwhgIetAoYF8JSc33q29QA==" }, + "@types/ip": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/@types/ip/-/ip-1.1.3.tgz", + "integrity": "sha512-64waoJgkXFTYnCYDUWgSATJ/dXEBanVkaP5d4Sbk7P6U7cTTMhxVyROTckc6JKdwCrgnAjZMn0k3177aQxtDEA==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/json-schema": { "version": "7.0.15", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.15.tgz", @@ -8315,6 +8345,11 @@ "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", diff --git a/package.json b/package.json index 9d02e78..ebcdeae 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "fs-extra": "^8.1.0", "got": "^14.2.0", "http2-express-bridge": "^1.0.7", + "ip": "^2.0.1", "json-stringify-safe": "^5.0.1", "keyv": "^4.5.4", "keyv-file": "^0.3.0", @@ -68,6 +69,7 @@ "@types/dotenv": "^6.1.1", "@types/express": "^4.17.13", "@types/fs-extra": "^8.0.0", + "@types/ip": "^1.1.3", "@types/json-stringify-safe": "^5.0.3", "@types/lodash-es": "^4.17.7", "@types/morgan": "^1.7.36", diff --git a/src/cluster.ts b/src/cluster.ts index ede29f7..322f60a 100644 --- a/src/cluster.ts +++ b/src/cluster.ts @@ -39,6 +39,7 @@ import type {TokenManager} from './token.js' import type {IFileList} from './types.js' import {setupUpnp} from './upnp.js' import {checkSign, hashToFilename} from './util.js' +import {isPrivate} from 'ip' interface ICounters { hits: number @@ -59,7 +60,7 @@ export class Cluster { public readonly storage: IStorage private readonly prefixUrl = process.env.CLUSTER_BMCLAPI ?? 'https://openbmclapi.bangbang93.com' - private readonly host?: string + private host?: string private _port: number | string private readonly publicPort: number private readonly ua: string @@ -133,7 +134,12 @@ export class Cluster { public async init(): Promise { await this.storage.init?.() if (config.enableUpnp) { - await setupUpnp(config.port, config.clusterPublicPort) + const ip = await setupUpnp(config.port, config.clusterPublicPort) + if (isPrivate(ip)) { + throw new Error(`无法获取公网IP, UPNP返回的IP位于私有地址段, IP: ${ip}`) + } + logger.info(`upnp映射成功,外网IP: ${ip}`) + this.host ??= ip } }