This commit is contained in:
2026-02-12 00:14:12 +08:00
parent 34697e07b5
commit 786ee80839
2 changed files with 39 additions and 8 deletions

View File

@@ -1,4 +1,6 @@
import { createKeepAlive } from '@kevisual/cnb/keep' import { createKeepAlive } from '@kevisual/cnb/keep'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { program, Command } from '@/program.ts'; import { program, Command } from '@/program.ts';
@@ -8,20 +10,47 @@ const cnbCmd = new Command('cnb')
console.log('CNB 命令'); console.log('CNB 命令');
}); });
// cnb live -j '{"wss":"wss://...","cookie":"...","url":"..."}'
// # 或
// cnb live --json '{"wss":"wss://...","cookie":"...","url":"..."}'
const liveCmd = new Command('live') const liveCmd = new Command('live')
.description('启动 CNB Keep Alive 服务') .description('启动 CNB Keep Alive 服务')
.option('-u, --url <url>', 'WebSocket 服务器地址') .option('-j, --json <string>', 'JSON数据 (不提供则从 stdin 读取)')
.option('-c, --cookie <cookie>', '认证 Cookie')
.action(async (opts) => { .action(async (opts) => {
if (!opts.url || !opts.cookie) { let config: { wss: string; cookie: string; url?: string };
console.log('请提供 WebSocket 服务器地址和认证 Cookie');
return; try {
let jsonString = opts.json;
// 如果没有提供 json 参数,从 stdin 读取
if (!jsonString) {
const chunks: Buffer[] = [];
for await (const chunk of process.stdin) {
chunks.push(chunk);
}
jsonString = Buffer.concat(chunks).toString('utf-8').trim();
}
// 从文件读取
else if (jsonString.startsWith('@')) {
const filePath = resolve(jsonString.slice(1));
jsonString = readFileSync(filePath, 'utf-8').trim();
}
config = JSON.parse(jsonString);
} catch (error) {
console.error('JSON 解析错误: 请检查输入的 JSON 格式是否正确');
process.exit(1);
}
if (!config.wss || !config.cookie) {
console.error('配置错误: 必须包含 wss 和 cookie 字段');
process.exit(1);
} }
const keepAlive = createKeepAlive({ const keepAlive = createKeepAlive({
wsUrl: opts.url, wsUrl: config.wss,
cookie: opts.cookie, cookie: config.cookie,
debug: true,
onConnect: () => { onConnect: () => {
console.log('已连接到 CNB 服务器'); console.log('已连接到 CNB 服务器');
}, },

View File

@@ -22,6 +22,8 @@ import './command/coding-plan/oc.ts'
import './command/docker.ts'; import './command/docker.ts';
import './command/jwks.ts'; import './command/jwks.ts';
import './command/cnb/index.ts';
// program.parse(process.argv); // program.parse(process.argv);
export const runParser = async (argv: string[]) => { export const runParser = async (argv: string[]) => {