Skip to content

Commit

Permalink
feat: 使用ipaddr.js替换ip
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Dec 9, 2024
1 parent 4c0ca62 commit f217fd7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
44 changes: 25 additions & 19 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 6 additions & 2 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}`)
Expand Down

0 comments on commit f217fd7

Please sign in to comment.