Files
mihomo-patchbay/server/index.ts
T
im0o 60fb015e36 关掉 Bun.serve 的空闲超时,静默的日志流不再被掐成 500
内核 /logs 在没有日志产生时一个字节都不发(响应头随第一行日志才写出),
Bun 默认 10 秒空闲就关连接,经 vite 代理变成无响应体的 500,界面上只剩
一句指不到病根的「日志接口请求失败」。只监听本机的工具,不设空闲超时
没有被拖死的风险。
2026-08-11 23:25:52 +08:00

62 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 启动入口。
*
* 启动时把该报的问题一次报清楚:环境变量缺没缺、配置文件读不读得到、内核在不在线。
* 配置类工具最糟糕的失败方式是界面能打开、点了保存才发现根本连不上。
*/
import { readEnv } from './env.ts'
import { resolveToken } from './auth.ts'
import { createRouter } from './routes.ts'
import { readConfig } from './config-store.ts'
import { kernelVersion, warmUpKernelBinary } from './kernel.ts'
const env = readEnv()
const { token, source } = await resolveToken(env)
// 先确认配置文件读得到、解析得动。读不到就没必要往下走了。
try {
const snapshot = await readConfig(env)
const errors = snapshot.diagnostics.filter((d) => d.severity === 'error')
console.log(`配置文件 ${env.configPath}`)
if (errors.length > 0) {
console.log(` 解析出 ${String(errors.length)} 个问题,界面上会标出来:`)
for (const e of errors) console.log(` ${e.path.join('.')}:${e.message}`)
}
} catch (error) {
console.error(`读不到配置文件 ${env.configPath}`)
console.error(String(error))
console.error('检查 .env 里的 PATCHBAY_CONFIG 指对了没有,以及这个进程有没有读权限。')
process.exit(1)
}
warmUpKernelBinary(env)
const kernel = await kernelVersion(env)
console.log(
kernel.online
? `内核 在线 ${kernel.version ?? ''}(${env.mihomoApi})`
: `内核 连不上 ${env.mihomoApi}——界面仍然能编辑配置,但看不到订阅里的线路,也无法重载`,
)
const handle = createRouter(env, token)
const server = Bun.serve({
hostname: env.host,
port: env.port,
// Bun 默认 10 秒空闲就掐连接,而透传的内核日志流在没有日志产生时一个字节都不发
// ——连响应头都没有(内核源码 hub/route/server.go:510-534,头随第一行日志才写出)。
// 被掐的连接经 vite 代理会变成无响应体的 500,界面上是一句指不到病根的「请求失败」。
// 这是只监听本机的工具,不设空闲超时没有被拖死的风险
idleTimeout: 0,
fetch: handle,
})
console.log(`令牌 ${token}`)
console.log(` 来自 ${source}`)
console.log(`Patchbay http://${server.hostname}:${String(server.port)}`)
if (env.host === '127.0.0.1') {
console.log(` 只监听本机。从别的机器访问请开隧道:`)
console.log(` ssh -N -L ${String(env.port)}:127.0.0.1:${String(env.port)} 用户名@这台机器`)
} else {
console.log(` ⚠ 监听在 ${env.host},本机之外也能访问。确认这是你要的。`)
}