Skip to content

Commit

Permalink
feat: 检查UPnP返回的ip是否为公网IP
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Nov 14, 2024
1 parent 58d10ed commit bc73ed1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
10 changes: 8 additions & 2 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -133,7 +134,12 @@ export class Cluster {
public async init(): Promise<void> {
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
}
}

Expand Down

0 comments on commit bc73ed1

Please sign in to comment.