From 786ee808399090d1250b69e1a0f6cdb7ba984316 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Thu, 12 Feb 2026 00:14:12 +0800 Subject: [PATCH] add live --- src/command/cnb/index.ts | 45 +++++++++++++++++++++++++++++++++------- src/index.ts | 2 ++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/command/cnb/index.ts b/src/command/cnb/index.ts index 3555788..ff160e2 100644 --- a/src/command/cnb/index.ts +++ b/src/command/cnb/index.ts @@ -1,4 +1,6 @@ import { createKeepAlive } from '@kevisual/cnb/keep' +import { readFileSync } from 'node:fs' +import { resolve } from 'node:path' import { program, Command } from '@/program.ts'; @@ -8,20 +10,47 @@ const cnbCmd = new Command('cnb') console.log('CNB 命令'); }); +// cnb live -j '{"wss":"wss://...","cookie":"...","url":"..."}' +// # 或 +// cnb live --json '{"wss":"wss://...","cookie":"...","url":"..."}' + const liveCmd = new Command('live') .description('启动 CNB Keep Alive 服务') - .option('-u, --url ', 'WebSocket 服务器地址') - .option('-c, --cookie ', '认证 Cookie') + .option('-j, --json ', 'JSON数据 (不提供则从 stdin 读取)') .action(async (opts) => { - if (!opts.url || !opts.cookie) { - console.log('请提供 WebSocket 服务器地址和认证 Cookie'); - return; + let config: { wss: string; cookie: string; url?: string }; + + 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({ - wsUrl: opts.url, - cookie: opts.cookie, - debug: true, + wsUrl: config.wss, + cookie: config.cookie, onConnect: () => { console.log('已连接到 CNB 服务器'); }, diff --git a/src/index.ts b/src/index.ts index 566244c..ee55c3b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,6 +22,8 @@ import './command/coding-plan/oc.ts' import './command/docker.ts'; import './command/jwks.ts'; +import './command/cnb/index.ts'; + // program.parse(process.argv); export const runParser = async (argv: string[]) => {