dsh-plugin-opencode2api 1.3.3 → 1.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -1
- package/lib/index.js +233 -39
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -45,7 +45,7 @@ dsh plugin --profile web add dsh-plugin-opencode2api
|
|
|
45
45
|
|
|
46
46
|
Windows / macOS / Linux 装好后,打开 DSH Web UI(默认 `http://127.0.0.1:3080`)→ **设置 → 本地免费 API**,就能看到状态和操作按钮;手机 App 则在「本地API」胶囊/面板里。
|
|
47
47
|
|
|
48
|
-
## 接入云端设备池(1.3.
|
|
48
|
+
## 接入云端设备池(1.3.4)
|
|
49
49
|
|
|
50
50
|
如果你有一个自己的中转站(例如本项目作者的 oc-proxy),可以把这台设备变成它的一个后端,让中转站把请求分过来。
|
|
51
51
|
|
|
@@ -55,8 +55,20 @@ Windows / macOS / Linux 装好后,打开 DSH Web UI(默认 `http://127.0.0.1
|
|
|
55
55
|
- 桌面:设置 → 本地免费 API → **云端设备池** → 粘入接入码 → 加入设备池(服务器地址已默认填好,需要换服才展开「自定义服务器地址」);
|
|
56
56
|
3. 插件会自动创建 Cloudflare 隧道、下载并启动 cloudflared、每 60 秒上报心跳。
|
|
57
57
|
|
|
58
|
+
隧道固定走 `--protocol http2 --edge-ip-version 4`:手机网络经常把 QUIC 的 UDP 7844 掐掉,走 QUIC 会一直「握手超时」然后反复重连;换成 TCP 的 http2 稳很多。
|
|
59
|
+
|
|
60
|
+
插件每 20 秒检查一次隧道真实连接数(cloudflared 的 `/ready`,只认已注册的连接):进程还在但一条都没连上就会重启,并且带指数退避,不会秒退秒起。心跳会把这个状态一起上报,中转站据此停止往断隧道的设备派请求。
|
|
61
|
+
|
|
58
62
|
管理端看到设备「在线」后就会把请求分配过来。心跳超过 3 分钟没到会自动标记离线,重新上线自动恢复;长期不用时在管理端点「删除」会同时清理隧道和 DNS。
|
|
59
63
|
|
|
64
|
+
### 隧道连不上怎么查(Windows 尤其看这里)
|
|
65
|
+
|
|
66
|
+
1. 看日志:`<DSH_HOME>\dsh-go\opencode2api\cloudflared.log`(默认 `C:\Users\你的用户名\.dsh\...`),搜索 `ERR`、`exit`、`[plugin]`。出现 `下载到的不是可执行文件` 说明镜像返回了网页,删掉同目录的 `cloudflared.exe` 让插件重下即可。
|
|
67
|
+
2. Windows Defender 有时会拦 cloudflared:在「病毒和威胁防护 → 排除项」里把插件数据目录加进去,再重新接入。
|
|
68
|
+
3. 1.3.5 起,插件启动 cloudflared 后等 1.5 秒确认进程还活着;秒退会直接显示「隧道启动失败」,不再假装在线。
|
|
69
|
+
4. 旧版留下的 `cloudflared.pid` 可能被别的程序复用,导致插件误以为隧道在跑。1.3.5 会核对进程名(Windows 用 `tasklist`,Linux 读 `/proc/<pid>/comm`,macOS 用 `ps`),不是 cloudflared 就忽略并重新启动。
|
|
70
|
+
5. 回源地址统一用 `http://127.0.0.1:8080`,避免 Windows 上 `localhost` 先解析到 `::1` 而本地服务只监听 IPv4。
|
|
71
|
+
|
|
60
72
|
退出:面板里点「退出设备池」,会同时从中转站删除这台设备。
|
|
61
73
|
|
|
62
74
|
## 使用
|
package/lib/index.js
CHANGED
|
@@ -310,17 +310,30 @@ function readJson(req) {
|
|
|
310
310
|
|
|
311
311
|
|
|
312
312
|
// ---- 云端设备池 ----
|
|
313
|
-
const PLUGIN_VERSION =
|
|
313
|
+
const PLUGIN_VERSION = (() => {
|
|
314
|
+
try {
|
|
315
|
+
const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf8'))
|
|
316
|
+
return String(pkg.version || '1.3.4')
|
|
317
|
+
} catch {
|
|
318
|
+
return '1.3.4'
|
|
319
|
+
}
|
|
320
|
+
})()
|
|
314
321
|
const POOL = path.join(DATA, 'pool.json')
|
|
315
322
|
const CLOUDFLARED = path.join(DATA, IS_WIN ? 'cloudflared.exe' : 'cloudflared')
|
|
316
323
|
const TUNNEL_LOG = path.join(DATA, 'cloudflared.log')
|
|
317
324
|
const TUNNEL_PID = path.join(DATA, 'cloudflared.pid')
|
|
325
|
+
const METRICS_PORT = Number(process.env.OCP_TUNNEL_METRICS_PORT || 20241)
|
|
318
326
|
|
|
319
327
|
let poolInfo = { status: '未接入', tone: 'muted', hostname: '', lastError: '' }
|
|
320
328
|
let tunnelChild = null
|
|
321
329
|
let heartbeatTimer = null
|
|
322
330
|
let tunnelTimer = null
|
|
323
331
|
let joining = null
|
|
332
|
+
let heartbeatFails = 0
|
|
333
|
+
let tunnelDownStreak = 0
|
|
334
|
+
let tunnelNextTry = 0
|
|
335
|
+
let tunnelExits = 0
|
|
336
|
+
let tunnelState = { ok: false, at: 0, reason: '' }
|
|
324
337
|
|
|
325
338
|
function readPool() {
|
|
326
339
|
try {
|
|
@@ -346,7 +359,14 @@ function poolSnapshot() {
|
|
|
346
359
|
const age = pool.lastHeartbeat ? Date.now() - pool.lastHeartbeat : null
|
|
347
360
|
if (pool.lastError) return { status: '心跳失败', tone: 'warn', hostname: pool.hostname || '', lastError: pool.lastError }
|
|
348
361
|
if (age === null) return { status: '等待心跳', tone: 'warn', hostname: pool.hostname || '', lastError: '' }
|
|
349
|
-
if (age < 180000)
|
|
362
|
+
if (age < 180000) {
|
|
363
|
+
const fresh = tunnelState.at > 0 && Date.now() - tunnelState.at < 150000
|
|
364
|
+
if (fresh && !tunnelState.ok) {
|
|
365
|
+
return { status: '隧道重连中', tone: 'warn', hostname: pool.hostname || '', lastError: tunnelState.reason || '' }
|
|
366
|
+
}
|
|
367
|
+
if (!tunnelRunning()) return { status: '隧道重连中', tone: 'warn', hostname: pool.hostname || '', lastError: '' }
|
|
368
|
+
return { status: '在线', tone: 'good', hostname: pool.hostname || '', lastError: '' }
|
|
369
|
+
}
|
|
350
370
|
return { status: '离线', tone: 'bad', hostname: pool.hostname || '', lastError: '' }
|
|
351
371
|
}
|
|
352
372
|
|
|
@@ -358,8 +378,36 @@ function cloudflaredTarget() {
|
|
|
358
378
|
return { file: `cloudflared-linux-${arch}`, archive: false }
|
|
359
379
|
}
|
|
360
380
|
|
|
381
|
+
function expectedMagicOk(buf) {
|
|
382
|
+
if (!buf || buf.length < 1024) return false
|
|
383
|
+
if (process.platform === 'win32') return buf[0] === 0x4d && buf[1] === 0x5a
|
|
384
|
+
if (process.platform === 'darwin') return true
|
|
385
|
+
return buf[0] === 0x7f && buf[1] === 0x45 && buf[2] === 0x4c && buf[3] === 0x46
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function existingBinaryOk(file) {
|
|
389
|
+
try {
|
|
390
|
+
if (!fs.existsSync(file)) return false
|
|
391
|
+
if (fs.statSync(file).size < 1024 * 1024) return false
|
|
392
|
+
const fd = fs.openSync(file, 'r')
|
|
393
|
+
const head = Buffer.alloc(4)
|
|
394
|
+
try {
|
|
395
|
+
fs.readSync(fd, head, 0, 4, 0)
|
|
396
|
+
} finally {
|
|
397
|
+
fs.closeSync(fd)
|
|
398
|
+
}
|
|
399
|
+
return expectedMagicOk(head)
|
|
400
|
+
} catch {
|
|
401
|
+
return false
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
361
405
|
async function ensureCloudflared() {
|
|
362
|
-
if (
|
|
406
|
+
if (existingBinaryOk(CLOUDFLARED)) return
|
|
407
|
+
// 之前下到的可能是镜像的错误网页,留着只会每次启动都失败
|
|
408
|
+
try {
|
|
409
|
+
if (fs.existsSync(CLOUDFLARED)) fs.unlinkSync(CLOUDFLARED)
|
|
410
|
+
} catch {}
|
|
363
411
|
const t = cloudflaredTarget()
|
|
364
412
|
let lastFailure = null
|
|
365
413
|
for (const prefix of MIRRORS) {
|
|
@@ -376,7 +424,11 @@ async function ensureCloudflared() {
|
|
|
376
424
|
await fsp.copyFile(found, CLOUDFLARED)
|
|
377
425
|
await fsp.rm(temp, { recursive: true, force: true }).catch(() => {})
|
|
378
426
|
} else {
|
|
427
|
+
if (!expectedMagicOk(body)) {
|
|
428
|
+
throw new Error(`下载到的不是可执行文件(${body.length} 字节,多半是镜像错误页)`)
|
|
429
|
+
}
|
|
379
430
|
await fsp.writeFile(CLOUDFLARED, body)
|
|
431
|
+
logTunnel(`cloudflared downloaded bytes=${body.length} from=${prefix || 'github'}`)
|
|
380
432
|
}
|
|
381
433
|
if (!IS_WIN) fs.chmodSync(CLOUDFLARED, 0o755)
|
|
382
434
|
return
|
|
@@ -387,51 +439,136 @@ async function ensureCloudflared() {
|
|
|
387
439
|
throw lastFailure || new Error('cloudflared 下载失败')
|
|
388
440
|
}
|
|
389
441
|
|
|
442
|
+
function readTunnelPid() {
|
|
443
|
+
try {
|
|
444
|
+
return Number(fs.readFileSync(TUNNEL_PID, 'utf8').trim()) || 0
|
|
445
|
+
} catch {
|
|
446
|
+
return 0
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
async function processName(pid) {
|
|
451
|
+
if (!pid) return ''
|
|
452
|
+
if (IS_WIN) {
|
|
453
|
+
const out = await execLines('tasklist', ['/FI', `PID eq ${pid}`, '/FO', 'CSV', '/NH'], 5000)
|
|
454
|
+
const match = /^"([^"]+)"/.exec(out.trim())
|
|
455
|
+
return match ? match[1].toLowerCase() : ''
|
|
456
|
+
}
|
|
457
|
+
if (process.platform === 'linux') {
|
|
458
|
+
try {
|
|
459
|
+
return fs.readFileSync(`/proc/${pid}/comm`, 'utf8').trim().toLowerCase()
|
|
460
|
+
} catch {
|
|
461
|
+
// /proc 读不到就退回 ps
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
const out = await execLines('ps', ['-p', String(pid), '-o', 'comm='], 3000)
|
|
465
|
+
return out.trim().toLowerCase()
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
async function pidIsCloudflared(pid) {
|
|
469
|
+
if (!pid || !alive(pid)) return false
|
|
470
|
+
const name = await processName(pid)
|
|
471
|
+
return Boolean(name) && name.includes('cloudflared')
|
|
472
|
+
}
|
|
473
|
+
|
|
390
474
|
function tunnelRunning() {
|
|
391
475
|
if (tunnelChild && tunnelChild.exitCode === null) return true
|
|
476
|
+
const pid = readTunnelPid()
|
|
477
|
+
return Boolean(pid && alive(pid))
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// 光看进程号会被复用骗:重启或异常退出后,pid 文件里的号可能已经属于别的程序
|
|
481
|
+
async function tunnelRunningVerified() {
|
|
482
|
+
if (tunnelChild && tunnelChild.exitCode === null) return true
|
|
483
|
+
return await pidIsCloudflared(readTunnelPid())
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
async function tunnelReady() {
|
|
487
|
+
try {
|
|
488
|
+
const res = await fetch(`http://127.0.0.1:${METRICS_PORT}/ready`, {
|
|
489
|
+
signal: AbortSignal.timeout(2500),
|
|
490
|
+
})
|
|
491
|
+
if (!res.ok) return { known: true, ok: false, connections: 0 }
|
|
492
|
+
const body = await res.json().catch(() => null)
|
|
493
|
+
const conn = Number(body?.readyConnections) || 0
|
|
494
|
+
return { known: true, ok: conn > 0, connections: conn }
|
|
495
|
+
} catch {
|
|
496
|
+
return { known: false, ok: false, connections: 0 }
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function logTunnel(message) {
|
|
392
501
|
try {
|
|
393
|
-
|
|
394
|
-
if (pid && alive(pid)) return true
|
|
502
|
+
fs.appendFileSync(TUNNEL_LOG, `${new Date().toISOString()} [plugin] ${message}\n`)
|
|
395
503
|
} catch {}
|
|
396
|
-
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
function tunnelArgs(token) {
|
|
507
|
+
const args = ['tunnel', '--no-autoupdate']
|
|
508
|
+
// 手机/办公网经常把 UDP 7844 掐掉,强制走 TCP/http2,省得 QUIC 一直握手超时
|
|
509
|
+
args.push('--protocol', 'http2', '--edge-ip-version', '4')
|
|
510
|
+
args.push('--metrics', `127.0.0.1:${METRICS_PORT}`)
|
|
511
|
+
args.push('run', '--token', token)
|
|
512
|
+
return args
|
|
397
513
|
}
|
|
398
514
|
|
|
399
515
|
async function startTunnel() {
|
|
400
516
|
const pool = readPool()
|
|
401
517
|
if (!pool?.tunnelToken) return false
|
|
402
|
-
if (
|
|
518
|
+
if (await tunnelRunningVerified()) return true
|
|
519
|
+
if (tunnelRunning()) {
|
|
520
|
+
logTunnel(`stale pid=${readTunnelPid()} is not cloudflared, clearing it`)
|
|
521
|
+
try { fs.unlinkSync(TUNNEL_PID) } catch {}
|
|
522
|
+
}
|
|
403
523
|
if (!fs.existsSync(CLOUDFLARED)) await ensureCloudflared()
|
|
404
524
|
const fd = fs.openSync(TUNNEL_LOG, 'a')
|
|
525
|
+
let child
|
|
405
526
|
try {
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
)
|
|
527
|
+
child = spawn(CLOUDFLARED, tunnelArgs(pool.tunnelToken), {
|
|
528
|
+
stdio: ['ignore', fd, fd],
|
|
529
|
+
detached: false,
|
|
530
|
+
windowsHide: true,
|
|
531
|
+
})
|
|
532
|
+
tunnelChild = child
|
|
411
533
|
} finally {
|
|
412
534
|
fs.closeSync(fd)
|
|
413
535
|
}
|
|
414
|
-
|
|
415
|
-
|
|
536
|
+
child.once('error', (error) => logTunnel(`spawn error: ${error?.message || error}`))
|
|
537
|
+
child.once('exit', (code, signal) => {
|
|
538
|
+
tunnelExits += 1
|
|
539
|
+
logTunnel(`cloudflared exit code=${code} signal=${signal || ''} total=${tunnelExits}`)
|
|
540
|
+
})
|
|
541
|
+
child.unref()
|
|
416
542
|
try {
|
|
417
|
-
fs.writeFileSync(TUNNEL_PID, String(
|
|
543
|
+
fs.writeFileSync(TUNNEL_PID, String(child.pid))
|
|
418
544
|
} catch {}
|
|
545
|
+
logTunnel(`cloudflared started pid=${child.pid} protocol=http2 edge=4 metrics=${METRICS_PORT}`)
|
|
546
|
+
// 下载到的是网页、缺 DLL、被杀软拦下时进程会立刻退,别把这种当成启动成功
|
|
547
|
+
await sleep(1500)
|
|
548
|
+
if (tunnelChild && tunnelChild.exitCode !== null) {
|
|
549
|
+
logTunnel(`cloudflared died right after start code=${tunnelChild.exitCode}`)
|
|
550
|
+
tunnelChild = null
|
|
551
|
+
try { fs.unlinkSync(TUNNEL_PID) } catch {}
|
|
552
|
+
return false
|
|
553
|
+
}
|
|
419
554
|
return true
|
|
420
555
|
}
|
|
421
556
|
|
|
422
|
-
function stopTunnel() {
|
|
423
|
-
const
|
|
424
|
-
if (tunnelChild?.pid)
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
if (filePid)
|
|
428
|
-
|
|
429
|
-
|
|
557
|
+
async function stopTunnel() {
|
|
558
|
+
const targets = new Set()
|
|
559
|
+
if (tunnelChild?.pid) targets.add(tunnelChild.pid)
|
|
560
|
+
const filePid = readTunnelPid()
|
|
561
|
+
if (filePid && filePid !== tunnelChild?.pid) {
|
|
562
|
+
if (await pidIsCloudflared(filePid)) targets.add(filePid)
|
|
563
|
+
else logTunnel(`skip killing stale pid=${filePid} (not cloudflared)`)
|
|
564
|
+
}
|
|
565
|
+
for (const pid of targets) {
|
|
430
566
|
try {
|
|
431
567
|
process.kill(pid)
|
|
432
568
|
} catch {}
|
|
433
569
|
}
|
|
434
570
|
tunnelChild = null
|
|
571
|
+
tunnelDownStreak = 0
|
|
435
572
|
try {
|
|
436
573
|
fs.unlinkSync(TUNNEL_PID)
|
|
437
574
|
} catch {}
|
|
@@ -440,6 +577,12 @@ function stopTunnel() {
|
|
|
440
577
|
export async function heartbeat() {
|
|
441
578
|
const pool = readPool()
|
|
442
579
|
if (!pool?.deviceId || !pool?.deviceSecret) return
|
|
580
|
+
const tunnel = await tunnelReady()
|
|
581
|
+
tunnelState = {
|
|
582
|
+
ok: tunnel.ok,
|
|
583
|
+
at: Date.now(),
|
|
584
|
+
reason: tunnel.known ? (tunnel.ok ? '' : '隧道没连上') : '隧道状态未知',
|
|
585
|
+
}
|
|
443
586
|
try {
|
|
444
587
|
const res = await fetch(`${pool.url}/api/device/heartbeat`, {
|
|
445
588
|
method: 'POST',
|
|
@@ -451,8 +594,9 @@ export async function heartbeat() {
|
|
|
451
594
|
body: JSON.stringify({
|
|
452
595
|
version: PLUGIN_VERSION,
|
|
453
596
|
platform: target() ? `${target().os}/${target().arch}` : process.platform,
|
|
597
|
+
tunnel: tunnel.known ? tunnel.ok : undefined,
|
|
454
598
|
}),
|
|
455
|
-
signal: AbortSignal.timeout(
|
|
599
|
+
signal: AbortSignal.timeout(25000),
|
|
456
600
|
})
|
|
457
601
|
const data = await res.json().catch(() => ({}))
|
|
458
602
|
if (res.status === 401) {
|
|
@@ -460,20 +604,31 @@ export async function heartbeat() {
|
|
|
460
604
|
return
|
|
461
605
|
}
|
|
462
606
|
if (!res.ok) throw new Error(data.error || `HTTP ${res.status}`)
|
|
607
|
+
heartbeatFails = 0
|
|
463
608
|
pool.lastHeartbeat = Date.now()
|
|
464
609
|
pool.lastError = ''
|
|
465
610
|
writePool(pool)
|
|
466
611
|
if (data.enabled === false) {
|
|
467
|
-
stopTunnel()
|
|
612
|
+
void stopTunnel()
|
|
468
613
|
poolInfo = { status: '已被管理端停用', tone: 'warn', hostname: pool.hostname || '', lastError: '' }
|
|
469
614
|
} else {
|
|
470
|
-
|
|
471
|
-
|
|
615
|
+
const started = await startTunnel()
|
|
616
|
+
if (!started) {
|
|
617
|
+
poolInfo = { status: '隧道启动失败', tone: 'bad', hostname: pool.hostname || '', lastError: 'cloudflared 没能启动,看 cloudflared.log' }
|
|
618
|
+
} else if (tunnel.known && !tunnel.ok) {
|
|
619
|
+
poolInfo = { status: '隧道重连中', tone: 'warn', hostname: pool.hostname || '', lastError: '' }
|
|
620
|
+
} else {
|
|
621
|
+
poolInfo = { status: '在线', tone: 'good', hostname: pool.hostname || '', lastError: '' }
|
|
622
|
+
}
|
|
472
623
|
}
|
|
473
624
|
} catch (error) {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
625
|
+
heartbeatFails += 1
|
|
626
|
+
// 单次网络抖动不算掉线,连续两次失败才改状态,避免面板来回跳
|
|
627
|
+
if (heartbeatFails >= 2) {
|
|
628
|
+
pool.lastError = human(error)
|
|
629
|
+
writePool(pool)
|
|
630
|
+
poolInfo = { status: '心跳失败', tone: 'warn', hostname: pool.hostname || '', lastError: pool.lastError }
|
|
631
|
+
}
|
|
477
632
|
}
|
|
478
633
|
}
|
|
479
634
|
|
|
@@ -486,13 +641,47 @@ function startPoolTimers() {
|
|
|
486
641
|
}
|
|
487
642
|
if (!tunnelTimer) {
|
|
488
643
|
tunnelTimer = setInterval(() => {
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
}, 45_000)
|
|
644
|
+
void checkTunnel()
|
|
645
|
+
}, 20_000)
|
|
492
646
|
tunnelTimer.unref?.()
|
|
493
647
|
}
|
|
494
648
|
}
|
|
495
649
|
|
|
650
|
+
async function checkTunnel() {
|
|
651
|
+
const pool = readPool()
|
|
652
|
+
if (!pool?.tunnelToken) return
|
|
653
|
+
if (Date.now() < tunnelNextTry) return
|
|
654
|
+
const running = await tunnelRunningVerified()
|
|
655
|
+
if (running) {
|
|
656
|
+
const ready = await tunnelReady()
|
|
657
|
+
tunnelState = {
|
|
658
|
+
ok: ready.ok,
|
|
659
|
+
at: Date.now(),
|
|
660
|
+
reason: ready.known ? (ready.ok ? '' : '隧道没连上') : '隧道状态未知',
|
|
661
|
+
}
|
|
662
|
+
if (ready.ok) {
|
|
663
|
+
tunnelDownStreak = 0
|
|
664
|
+
return
|
|
665
|
+
}
|
|
666
|
+
// 进程在但一条连接都没连上:连续两次才动,别把刚起步的隧道掐掉
|
|
667
|
+
tunnelDownStreak += 1
|
|
668
|
+
if (tunnelDownStreak < 2) return
|
|
669
|
+
logTunnel(`tunnel unhealthy (connections=${ready.connections} known=${ready.known}), restarting`)
|
|
670
|
+
await stopTunnel()
|
|
671
|
+
} else if (tunnelRunning()) {
|
|
672
|
+
logTunnel(`stale pid=${readTunnelPid()} is not cloudflared, clearing it`)
|
|
673
|
+
try { fs.unlinkSync(TUNNEL_PID) } catch {}
|
|
674
|
+
}
|
|
675
|
+
// 进程没了或确认不健康:重启并做指数退避,避免秒退秒起
|
|
676
|
+
tunnelDownStreak = 0
|
|
677
|
+
const started = await startTunnel().catch((error) => {
|
|
678
|
+
logTunnel(`restart failed: ${error?.message || error}`)
|
|
679
|
+
return false
|
|
680
|
+
})
|
|
681
|
+
if (!started) tunnelState = { ok: false, at: Date.now(), reason: 'cloudflared 启动失败' }
|
|
682
|
+
tunnelNextTry = Date.now() + Math.min(5000 * 2 ** Math.min(tunnelExits, 5), 180000)
|
|
683
|
+
}
|
|
684
|
+
|
|
496
685
|
function stopPoolTimers() {
|
|
497
686
|
if (heartbeatTimer) clearInterval(heartbeatTimer)
|
|
498
687
|
if (tunnelTimer) clearInterval(tunnelTimer)
|
|
@@ -540,9 +729,13 @@ export async function joinPool(input = {}) {
|
|
|
540
729
|
lastError: '',
|
|
541
730
|
})
|
|
542
731
|
await ensureCloudflared()
|
|
543
|
-
await startTunnel()
|
|
732
|
+
const started = await startTunnel()
|
|
544
733
|
startPoolTimers()
|
|
545
734
|
await heartbeat()
|
|
735
|
+
if (!started) {
|
|
736
|
+
poolInfo = { status: '隧道启动失败', tone: 'bad', hostname: data.hostname || '', lastError: 'cloudflared 没能启动,看 cloudflared.log' }
|
|
737
|
+
return `设备已登记,但隧道没启动:${data.hostname || data.device_id}`
|
|
738
|
+
}
|
|
546
739
|
poolInfo = { status: '在线', tone: 'good', hostname: data.hostname || '', lastError: '' }
|
|
547
740
|
return `已接入设备池:${data.hostname || data.device_id}`
|
|
548
741
|
})()
|
|
@@ -555,7 +748,7 @@ export async function joinPool(input = {}) {
|
|
|
555
748
|
|
|
556
749
|
export function resetPoolLocally(reason) {
|
|
557
750
|
stopPoolTimers()
|
|
558
|
-
stopTunnel()
|
|
751
|
+
void stopTunnel()
|
|
559
752
|
try {
|
|
560
753
|
fs.unlinkSync(POOL)
|
|
561
754
|
} catch {}
|
|
@@ -565,7 +758,7 @@ export function resetPoolLocally(reason) {
|
|
|
565
758
|
async function leavePool() {
|
|
566
759
|
const pool = readPool()
|
|
567
760
|
stopPoolTimers()
|
|
568
|
-
stopTunnel()
|
|
761
|
+
await stopTunnel()
|
|
569
762
|
if (pool?.deviceId && pool?.deviceSecret && pool?.url) {
|
|
570
763
|
await fetch(`${pool.url}/api/device/leave`, {
|
|
571
764
|
method: 'POST',
|
|
@@ -620,6 +813,7 @@ async function buildState() {
|
|
|
620
813
|
poolTone: pool.tone,
|
|
621
814
|
poolHost: pool.hostname,
|
|
622
815
|
poolError: pool.lastError,
|
|
816
|
+
poolTunnel: tunnelState.at ? { ok: tunnelState.ok, reason: tunnelState.reason, at: tunnelState.at } : null,
|
|
623
817
|
}
|
|
624
818
|
}
|
|
625
819
|
|
|
@@ -756,7 +950,7 @@ function findFile(dir, filename) {
|
|
|
756
950
|
|
|
757
951
|
async function runCommand(command, args) {
|
|
758
952
|
return new Promise((resolve, reject) => {
|
|
759
|
-
const child = spawn(command, args, { stdio: 'ignore' })
|
|
953
|
+
const child = spawn(command, args, { stdio: 'ignore', windowsHide: true })
|
|
760
954
|
child.once('error', reject)
|
|
761
955
|
child.once('close', (code) => (code === 0 ? resolve() : reject(new Error(`${command} exit ${code}`))))
|
|
762
956
|
})
|
|
@@ -878,7 +1072,7 @@ function alive(pid) {
|
|
|
878
1072
|
|
|
879
1073
|
function execLines(command, args, timeout = 3000) {
|
|
880
1074
|
return new Promise((resolve) => {
|
|
881
|
-
const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'ignore'] })
|
|
1075
|
+
const child = spawn(command, args, { stdio: ['ignore', 'pipe', 'ignore'], windowsHide: true })
|
|
882
1076
|
let out = ''
|
|
883
1077
|
let done = false
|
|
884
1078
|
let timer
|
|
@@ -1018,7 +1212,7 @@ async function start() {
|
|
|
1018
1212
|
const fd = fs.openSync(LOG, 'a')
|
|
1019
1213
|
let child
|
|
1020
1214
|
try {
|
|
1021
|
-
child = spawn(BIN, ['-config', CFG], { cwd: DATA, detached: true, stdio: ['ignore', fd, fd] })
|
|
1215
|
+
child = spawn(BIN, ['-config', CFG], { cwd: DATA, detached: true, stdio: ['ignore', fd, fd], windowsHide: true })
|
|
1022
1216
|
} finally {
|
|
1023
1217
|
fs.closeSync(fd)
|
|
1024
1218
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-opencode2api",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.5",
|
|
4
4
|
"description": "Unofficial one-click installer and auto-runner for opencode2api — a local free OpenAI-compatible API gateway — inside DSH, with a desktop settings UI and a dsh-go native panel (Windows, macOS, Linux, Android).",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|