@tina_summer/codepocket 2.6.1 → 2.6.3
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 +4 -0
- package/bin/codepocket.js +23 -4
- package/package.json +5 -5
- package/src/index.ts +48 -18
- package/src/mcp-server.js +1 -4
package/README.md
CHANGED
|
@@ -63,6 +63,10 @@ codepocket
|
|
|
63
63
|
- **YOLO 模式** — 全自动审批,无人值守运行
|
|
64
64
|
- **手机工具** — GPS、日历、剪贴板、地图等原生能力通过 MCP 暴露给 Claude
|
|
65
65
|
|
|
66
|
+
## 安全
|
|
67
|
+
|
|
68
|
+
自 v2.6.1 起,CLI 与小程序之间经由 Relay 的所有业务流量都端到端加密(X25519 + XSalsa20-Poly1305)。Relay 服务器只转发密文信封,无法解密会话内容、文件读取或命令输出。配对时双方通过 Relay 交换 X25519 临时公钥派生共享密钥,Nonce 使用计数器 + 方向标签避免重用。详见根目录 [why-codepocket.md](../why-codepocket.md#为什么端到端加密)。
|
|
69
|
+
|
|
66
70
|
## 可选:CC-Switch
|
|
67
71
|
|
|
68
72
|
[CC-Switch](https://github.com/farion1231/cc-switch) 是管理 Claude Code 多 API 密钥/中转服务商的切换工具,下载地址:[Releases](https://github.com/farion1231/cc-switch/releases/latest)
|
package/bin/codepocket.js
CHANGED
|
@@ -496,15 +496,32 @@ async function authenticateHub(token) {
|
|
|
496
496
|
async function startSSE() {
|
|
497
497
|
const url = `${hubUrl}/api/events?all=true`
|
|
498
498
|
try {
|
|
499
|
-
|
|
500
|
-
|
|
499
|
+
let res = await fetch(url, { headers: { Accept: 'text/event-stream', ...(jwt ? { Authorization: `Bearer ${jwt}` } : {}) } })
|
|
500
|
+
// JWT expired — refresh and retry once before entering the retry loop
|
|
501
|
+
if (res.status === 401) {
|
|
502
|
+
try { const token = await findHubToken(); jwt = await authenticateHub(token) } catch {}
|
|
503
|
+
res = await fetch(url, { headers: { Accept: 'text/event-stream', ...(jwt ? { Authorization: `Bearer ${jwt}` } : {}) } })
|
|
504
|
+
}
|
|
505
|
+
if (!res.ok || !res.body) { log(`[codepocket] SSE connect failed: ${res.status}, retry in 5s`); setTimeout(startSSE, 5000); return }
|
|
506
|
+
log('[codepocket] SSE connected')
|
|
501
507
|
const reader = res.body.getReader()
|
|
502
508
|
const decoder = new TextDecoder()
|
|
503
509
|
let buffer = ''
|
|
510
|
+
let lastDataAt = Date.now()
|
|
511
|
+
// Watchdog: hub emits heartbeat every 10s. If silent for 45s, the TCP
|
|
512
|
+
// connection is zombie (NAT/Nginx/CF silent drop) — force reconnect.
|
|
513
|
+
const watchdog = setInterval(() => {
|
|
514
|
+
if (Date.now() - lastDataAt > 45000) {
|
|
515
|
+
log('[codepocket] SSE watchdog: no data for 45s, forcing reconnect')
|
|
516
|
+
clearInterval(watchdog)
|
|
517
|
+
try { reader.cancel() } catch {}
|
|
518
|
+
}
|
|
519
|
+
}, 15000)
|
|
504
520
|
async function pump() {
|
|
505
521
|
while (true) {
|
|
506
522
|
const { done, value } = await reader.read()
|
|
507
523
|
if (done) break
|
|
524
|
+
lastDataAt = Date.now()
|
|
508
525
|
buffer += decoder.decode(value, { stream: true })
|
|
509
526
|
const lines = buffer.split('\n')
|
|
510
527
|
buffer = lines.pop() || ''
|
|
@@ -514,10 +531,12 @@ async function startSSE() {
|
|
|
514
531
|
}
|
|
515
532
|
}
|
|
516
533
|
}
|
|
534
|
+
clearInterval(watchdog)
|
|
535
|
+
log('[codepocket] SSE stream ended, reconnecting in 5s')
|
|
517
536
|
setTimeout(startSSE, 5000)
|
|
518
537
|
}
|
|
519
|
-
pump().catch(() => { setTimeout(startSSE, 5000) })
|
|
520
|
-
} catch { setTimeout(startSSE, 5000) }
|
|
538
|
+
pump().catch(() => { clearInterval(watchdog); setTimeout(startSSE, 5000) })
|
|
539
|
+
} catch (e) { log(`[codepocket] SSE error: ${e?.message}, retry in 5s`); setTimeout(startSSE, 5000) }
|
|
521
540
|
}
|
|
522
541
|
|
|
523
542
|
// ── End-to-end encryption (X25519 + XSalsa20-Poly1305) ──
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tina_summer/codepocket",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.3",
|
|
4
4
|
"description": "CodePocket — 基于ClaudeCode的微信小程序工作台",
|
|
5
5
|
"bin": {
|
|
6
6
|
"codepocket": "bin/codepocket.js",
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
"ws": "^8.18.0"
|
|
21
21
|
},
|
|
22
22
|
"optionalDependencies": {
|
|
23
|
-
"@tina_summer/codepocket-darwin-arm64": "2.6.
|
|
24
|
-
"@tina_summer/codepocket-darwin-x64": "2.6.
|
|
25
|
-
"@tina_summer/codepocket-linux-x64": "2.6.
|
|
26
|
-
"@tina_summer/codepocket-win32-x64": "2.6.
|
|
23
|
+
"@tina_summer/codepocket-darwin-arm64": "2.6.3",
|
|
24
|
+
"@tina_summer/codepocket-darwin-x64": "2.6.3",
|
|
25
|
+
"@tina_summer/codepocket-linux-x64": "2.6.3",
|
|
26
|
+
"@tina_summer/codepocket-win32-x64": "2.6.3"
|
|
27
27
|
},
|
|
28
28
|
"engines": {
|
|
29
29
|
"node": ">=18"
|
package/src/index.ts
CHANGED
|
@@ -80,42 +80,72 @@ function startSSE() {
|
|
|
80
80
|
|
|
81
81
|
;(async () => {
|
|
82
82
|
try {
|
|
83
|
-
|
|
83
|
+
let res = await fetch(url, {
|
|
84
84
|
signal: sseController!.signal,
|
|
85
85
|
headers: { Accept: 'text/event-stream' },
|
|
86
86
|
})
|
|
87
87
|
|
|
88
|
+
// JWT expired — refresh and retry once
|
|
89
|
+
if (res.status === 401) {
|
|
90
|
+
try { jwt = await authenticateHub() } catch {}
|
|
91
|
+
res = await fetch(`${hubUrl}/api/events?token=${encodeURIComponent(jwt)}&all=true`, {
|
|
92
|
+
signal: sseController!.signal,
|
|
93
|
+
headers: { Accept: 'text/event-stream' },
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
|
|
88
97
|
if (!res.ok || !res.body) {
|
|
89
98
|
console.error(`[SSE] connection failed: ${res.status}`)
|
|
99
|
+
setTimeout(startSSE, 5000)
|
|
90
100
|
return
|
|
91
101
|
}
|
|
92
102
|
|
|
103
|
+
console.log('[SSE] connected')
|
|
93
104
|
const reader = res.body.getReader()
|
|
94
105
|
const decoder = new TextDecoder()
|
|
95
106
|
let buffer = ''
|
|
107
|
+
let lastDataAt = Date.now()
|
|
108
|
+
|
|
109
|
+
// Watchdog: hub emits heartbeat every 10s. If silent for 45s, the TCP
|
|
110
|
+
// connection is zombie (NAT/Nginx/CF silent drop) — force reconnect.
|
|
111
|
+
const watchdog = setInterval(() => {
|
|
112
|
+
if (Date.now() - lastDataAt > 45000) {
|
|
113
|
+
console.error('[SSE] watchdog: no data for 45s, forcing reconnect')
|
|
114
|
+
clearInterval(watchdog)
|
|
115
|
+
try { reader.cancel() } catch {}
|
|
116
|
+
}
|
|
117
|
+
}, 15000)
|
|
96
118
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
119
|
+
try {
|
|
120
|
+
while (true) {
|
|
121
|
+
const { done, value } = await reader.read()
|
|
122
|
+
if (done) break
|
|
123
|
+
|
|
124
|
+
lastDataAt = Date.now()
|
|
125
|
+
buffer += decoder.decode(value, { stream: true })
|
|
126
|
+
const lines = buffer.split('\n')
|
|
127
|
+
buffer = lines.pop() || ''
|
|
128
|
+
|
|
129
|
+
for (const line of lines) {
|
|
130
|
+
if (line.startsWith('data:')) {
|
|
131
|
+
const raw = line.slice(5).trim()
|
|
132
|
+
if (!raw) continue
|
|
133
|
+
try {
|
|
134
|
+
const event = JSON.parse(raw)
|
|
135
|
+
forwardToRelay({ type: 'sse-event', event })
|
|
136
|
+
} catch {}
|
|
137
|
+
}
|
|
113
138
|
}
|
|
114
139
|
}
|
|
140
|
+
console.error('[SSE] stream ended, reconnecting in 5s...')
|
|
141
|
+
} finally {
|
|
142
|
+
clearInterval(watchdog)
|
|
115
143
|
}
|
|
144
|
+
|
|
145
|
+
setTimeout(startSSE, 5000)
|
|
116
146
|
} catch (err: any) {
|
|
117
147
|
if (err.name !== 'AbortError') {
|
|
118
|
-
console.error(
|
|
148
|
+
console.error(`[SSE] error: ${err.message}, reconnecting in 5s...`)
|
|
119
149
|
setTimeout(startSSE, 5000)
|
|
120
150
|
}
|
|
121
151
|
}
|
package/src/mcp-server.js
CHANGED
|
@@ -177,10 +177,7 @@ function startMcpServer(relaySender) {
|
|
|
177
177
|
|
|
178
178
|
return new Promise((resolve, reject) => {
|
|
179
179
|
const server = http.createServer((req, res) => {
|
|
180
|
-
// CORS headers
|
|
181
|
-
res.setHeader('Access-Control-Allow-Origin', '*')
|
|
182
|
-
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
|
|
183
|
-
res.setHeader('Access-Control-Allow-Headers', 'Content-Type')
|
|
180
|
+
// No CORS headers — MCP server is local-only (127.0.0.1), Claude Code doesn't need CORS
|
|
184
181
|
if (req.method === 'OPTIONS') { res.writeHead(204); res.end(); return }
|
|
185
182
|
|
|
186
183
|
if (req.url === '/sse' && req.method === 'GET') {
|