add live
This commit is contained in:
@@ -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 <url>', 'WebSocket 服务器地址')
|
||||
.option('-c, --cookie <cookie>', '认证 Cookie')
|
||||
.option('-j, --json <string>', '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 服务器');
|
||||
},
|
||||
|
||||
@@ -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[]) => {
|
||||
|
||||
Reference in New Issue
Block a user