mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-12-19 13:10:16 +08:00
fix: 更新默认主机地址获取逻辑,支持Docker环境
This commit is contained in:
parent
69c5b78678
commit
b2ff556aa6
@ -7,12 +7,12 @@ import { resolve } from 'node:path';
|
|||||||
|
|
||||||
import { deepMerge } from '../utils/object';
|
import { deepMerge } from '../utils/object';
|
||||||
import { themeType } from '../types/theme';
|
import { themeType } from '../types/theme';
|
||||||
import { getRandomToken } from '../utils/url'
|
import { getRandomToken, getDefaultHost } from '../utils/url'
|
||||||
|
|
||||||
// 限制尝试端口的次数,避免死循环
|
// 限制尝试端口的次数,避免死循环
|
||||||
// 定义配置的类型
|
// 定义配置的类型
|
||||||
const WebUiConfigSchema = Type.Object({
|
const WebUiConfigSchema = Type.Object({
|
||||||
host: Type.String({ default: '127.0.0.1' }),
|
host: Type.String({ default: getDefaultHost() }),
|
||||||
port: Type.Number({ default: 6099 }),
|
port: Type.Number({ default: 6099 }),
|
||||||
token: Type.String({ default: getRandomToken(8) }),
|
token: Type.String({ default: getRandomToken(8) }),
|
||||||
loginRate: Type.Number({ default: 10 }),
|
loginRate: Type.Number({ default: 10 }),
|
||||||
|
|||||||
@ -1,10 +1,58 @@
|
|||||||
/**
|
/**
|
||||||
* @file URL工具
|
* @file URL工具
|
||||||
*/
|
*/
|
||||||
|
import fs from 'node:fs'
|
||||||
import { isIP } from 'node:net';
|
import { isIP } from 'node:net'
|
||||||
import { randomBytes } from 'node:crypto'
|
import { randomBytes } from 'node:crypto'
|
||||||
|
|
||||||
|
type Protocol = 'http' | 'https'
|
||||||
|
|
||||||
|
let isDockerCached: boolean
|
||||||
|
|
||||||
|
function hasDockerEnv () {
|
||||||
|
try {
|
||||||
|
fs.statSync('/.dockerenv')
|
||||||
|
return true
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function hasDockerCGroup () {
|
||||||
|
try {
|
||||||
|
return fs.readFileSync('/proc/self/cgroup', 'utf8').includes('docker')
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasContainerEnv = () => {
|
||||||
|
try {
|
||||||
|
fs.statSync('/run/.containerenv')
|
||||||
|
return true
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const isDocker = () => {
|
||||||
|
if (isDockerCached === undefined) {
|
||||||
|
isDockerCached = hasContainerEnv() || hasDockerEnv() || hasDockerCGroup()
|
||||||
|
}
|
||||||
|
|
||||||
|
return isDockerCached
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取默认host地址
|
||||||
|
* @returns 根据环境返回合适的host地址
|
||||||
|
* @example getDefaultHost() => '127.0.0.1' // 非Docker环境
|
||||||
|
* @example getDefaultHost() => '0.0.0.0' // Docker环境
|
||||||
|
*/
|
||||||
|
export const getDefaultHost = (): string => {
|
||||||
|
return isDocker() ? '0.0.0.0' : '127.0.0.1'
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将 host(主机地址) 转换为标准格式
|
* 将 host(主机地址) 转换为标准格式
|
||||||
* @param host 主机地址
|
* @param host 主机地址
|
||||||
@ -14,9 +62,9 @@ import { randomBytes } from 'node:crypto'
|
|||||||
* @example normalizeHost('2001:4860:4801:51::27') => '[2001:4860:4801:51::27]'
|
* @example normalizeHost('2001:4860:4801:51::27') => '[2001:4860:4801:51::27]'
|
||||||
*/
|
*/
|
||||||
export const normalizeHost = (host: string) => {
|
export const normalizeHost = (host: string) => {
|
||||||
if (isIP(host) === 6) return `[${host}]`;
|
if (isIP(host) === 6) return `[${host}]`
|
||||||
return host;
|
return host
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建URL
|
* 创建URL
|
||||||
@ -35,16 +83,16 @@ export const createUrl = (
|
|||||||
search?: Record<string, any>,
|
search?: Record<string, any>,
|
||||||
protocol: Protocol = 'http'
|
protocol: Protocol = 'http'
|
||||||
) => {
|
) => {
|
||||||
const url = new URL(`${protocol}://${normalizeHost(host)}`);
|
const url = new URL(`${protocol}://${normalizeHost(host)}`)
|
||||||
url.port = port;
|
url.port = port
|
||||||
url.pathname = path;
|
url.pathname = path
|
||||||
if (search) {
|
if (search) {
|
||||||
for (const key in search) {
|
for (const key in search) {
|
||||||
url.searchParams.set(key, search[key]);
|
url.searchParams.set(key, search[key])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return url.toString();
|
return url.toString()
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成随机Token
|
* 生成随机Token
|
||||||
@ -53,5 +101,5 @@ export const createUrl = (
|
|||||||
* @example getRandomToken
|
* @example getRandomToken
|
||||||
*/
|
*/
|
||||||
export const getRandomToken = (length = 8) => {
|
export const getRandomToken = (length = 8) => {
|
||||||
return randomBytes(36).toString('hex').slice(0, length);
|
return randomBytes(36).toString('hex').slice(0, length)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user