@tina_summer/codepocket 2.6.0 → 2.6.2
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 +109 -4
- package/package.json +6 -5
- package/src/index.ts +11 -1
- 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
|
@@ -14,6 +14,8 @@ const fs = require('fs')
|
|
|
14
14
|
const path = require('path')
|
|
15
15
|
const os = require('os')
|
|
16
16
|
const readline = require('readline')
|
|
17
|
+
const crypto = require('crypto')
|
|
18
|
+
const nacl = require('tweetnacl')
|
|
17
19
|
const { HttpsProxyAgent } = require('https-proxy-agent')
|
|
18
20
|
const { startMcpServer, handlePhoneActionResponse: onPhoneActionResponse } = require('../src/mcp-server')
|
|
19
21
|
const { runTestCli } = require('../src/mcp-client')
|
|
@@ -494,7 +496,12 @@ async function authenticateHub(token) {
|
|
|
494
496
|
async function startSSE() {
|
|
495
497
|
const url = `${hubUrl}/api/events?all=true`
|
|
496
498
|
try {
|
|
497
|
-
|
|
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
|
+
}
|
|
498
505
|
if (!res.ok || !res.body) { setTimeout(startSSE, 5000); return }
|
|
499
506
|
const reader = res.body.getReader()
|
|
500
507
|
const decoder = new TextDecoder()
|
|
@@ -518,6 +525,101 @@ async function startSSE() {
|
|
|
518
525
|
} catch { setTimeout(startSSE, 5000) }
|
|
519
526
|
}
|
|
520
527
|
|
|
528
|
+
// ── End-to-end encryption (X25519 + XSalsa20-Poly1305) ──
|
|
529
|
+
// The relay only forwards e2ePubKey + opaque `enc` envelopes; it cannot decrypt.
|
|
530
|
+
// Nonces are counter-based with a direction tag to avoid reuse across directions
|
|
531
|
+
// under the same shared key: connector uses 0xFF prefix, phone uses 0x00.
|
|
532
|
+
|
|
533
|
+
let e2eKeypair = null // { publicKey, secretKey } Uint8Arrays (X25519)
|
|
534
|
+
let e2eSharedKey = null // Uint8Array(32), set when peer pubkey arrives
|
|
535
|
+
let e2eNonceCounter = 0
|
|
536
|
+
|
|
537
|
+
function ensureE2EKeypair() {
|
|
538
|
+
if (e2eKeypair) return e2eKeypair
|
|
539
|
+
// Node has crypto.randomBytes — nacl.box.keyPair auto-detects it
|
|
540
|
+
e2eKeypair = nacl.box.keyPair()
|
|
541
|
+
return e2eKeypair
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
function getE2EPubKeyB64() {
|
|
545
|
+
return Buffer.from(ensureE2EKeypair().publicKey).toString('base64')
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function setPeerE2EPubKey(peerPubKeyB64) {
|
|
549
|
+
if (!peerPubKeyB64 || typeof peerPubKeyB64 !== 'string') return
|
|
550
|
+
try {
|
|
551
|
+
const peerPub = Buffer.from(peerPubKeyB64, 'base64')
|
|
552
|
+
if (peerPub.length !== 32) return
|
|
553
|
+
e2eSharedKey = nacl.box.before(peerPub, ensureE2EKeypair().secretKey)
|
|
554
|
+
e2eNonceCounter = 0
|
|
555
|
+
log('[codepocket] ✓ E2E 加密通道已建立')
|
|
556
|
+
} catch (e) {
|
|
557
|
+
log(`[codepocket] E2E 密钥协商失败: ${e.message}`)
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
function nextNonce() {
|
|
562
|
+
e2eNonceCounter++
|
|
563
|
+
const n = new Uint8Array(24)
|
|
564
|
+
n[0] = 0xFF // connector→phone direction tag
|
|
565
|
+
let c = e2eNonceCounter
|
|
566
|
+
for (let i = 6; i >= 1; i--) { n[i] = c & 0xff; c = Math.floor(c / 256) }
|
|
567
|
+
return n
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
function e2eEncrypt(obj) {
|
|
571
|
+
if (!e2eSharedKey) return undefined
|
|
572
|
+
try {
|
|
573
|
+
const msgBytes = Buffer.from(JSON.stringify(obj), 'utf8')
|
|
574
|
+
const nonce = nextNonce()
|
|
575
|
+
const cipher = nacl.secretbox(msgBytes, nonce, e2eSharedKey)
|
|
576
|
+
if (!cipher) return undefined
|
|
577
|
+
return { n: Buffer.from(nonce).toString('base64'), c: Buffer.from(cipher).toString('base64') }
|
|
578
|
+
} catch { return undefined }
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function e2eDecrypt(envelope) {
|
|
582
|
+
if (!envelope || typeof envelope !== 'object') return undefined
|
|
583
|
+
if (typeof envelope.n !== 'string' || typeof envelope.c !== 'string') return undefined
|
|
584
|
+
if (!e2eSharedKey) return undefined
|
|
585
|
+
try {
|
|
586
|
+
const nonce = new Uint8Array(Buffer.from(envelope.n, 'base64'))
|
|
587
|
+
const cipher = new Uint8Array(Buffer.from(envelope.c, 'base64'))
|
|
588
|
+
const plain = nacl.secretbox.open(cipher, nonce, e2eSharedKey)
|
|
589
|
+
if (!plain) return undefined
|
|
590
|
+
return JSON.parse(Buffer.from(plain).toString('utf8'))
|
|
591
|
+
} catch { return undefined }
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// Pack a message for sending: moves sensitive fields into an `enc` envelope.
|
|
595
|
+
// Routing fields (type, requestId, actionId, success, status, ok) stay plaintext.
|
|
596
|
+
function encryptMessage(msg) {
|
|
597
|
+
if (!e2eSharedKey) return msg
|
|
598
|
+
const routing = ['type', 'requestId', 'actionId', 'success', 'status', 'ok', 'reconnected']
|
|
599
|
+
const content = {}
|
|
600
|
+
for (const [k, v] of Object.entries(msg)) {
|
|
601
|
+
if (!routing.includes(k)) content[k] = v
|
|
602
|
+
}
|
|
603
|
+
if (Object.keys(content).length === 0) return msg
|
|
604
|
+
const enc = e2eEncrypt(content)
|
|
605
|
+
if (!enc) return msg
|
|
606
|
+
const out = { type: msg.type }
|
|
607
|
+
for (const k of routing) if (k in msg) out[k] = msg[k]
|
|
608
|
+
out.enc = enc
|
|
609
|
+
return out
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
// Unpack a received message: merges decrypted content back from `enc`.
|
|
613
|
+
function decryptMessage(msg) {
|
|
614
|
+
if (!msg || !msg.enc || !e2eSharedKey) return msg
|
|
615
|
+
const content = e2eDecrypt(msg.enc)
|
|
616
|
+
if (content) {
|
|
617
|
+
delete msg.enc
|
|
618
|
+
Object.assign(msg, content)
|
|
619
|
+
}
|
|
620
|
+
return msg
|
|
621
|
+
}
|
|
622
|
+
|
|
521
623
|
// ── Relay ──
|
|
522
624
|
|
|
523
625
|
function connectRelay() {
|
|
@@ -547,6 +649,7 @@ function connectRelay() {
|
|
|
547
649
|
type: 'register',
|
|
548
650
|
key: relayKey,
|
|
549
651
|
sessionToken: savedSession?.sessionToken || undefined,
|
|
652
|
+
e2ePubKey: getE2EPubKeyB64(),
|
|
550
653
|
}))
|
|
551
654
|
relayPingInterval = setInterval(() => {
|
|
552
655
|
if (relayWs?.readyState === WebSocket.OPEN) {
|
|
@@ -558,6 +661,7 @@ function connectRelay() {
|
|
|
558
661
|
relayWs.on('message', (raw) => {
|
|
559
662
|
let msg
|
|
560
663
|
try { msg = JSON.parse(raw.toString()) } catch { return }
|
|
664
|
+
msg = decryptMessage(msg)
|
|
561
665
|
switch (msg.type) {
|
|
562
666
|
case 'registered': {
|
|
563
667
|
try {
|
|
@@ -572,6 +676,7 @@ function connectRelay() {
|
|
|
572
676
|
break
|
|
573
677
|
}
|
|
574
678
|
case 'client-connected': {
|
|
679
|
+
if (msg.clientE2EPubKey) setPeerE2EPubKey(msg.clientE2EPubKey)
|
|
575
680
|
log('[codepocket] ✓ 小程序已连接')
|
|
576
681
|
break
|
|
577
682
|
}
|
|
@@ -633,7 +738,7 @@ function connectRelay() {
|
|
|
633
738
|
}
|
|
634
739
|
|
|
635
740
|
function sendToRelay(msg) {
|
|
636
|
-
if (relayWs?.readyState === WebSocket.OPEN) relayWs.send(JSON.stringify(msg))
|
|
741
|
+
if (relayWs?.readyState === WebSocket.OPEN) relayWs.send(JSON.stringify(encryptMessage(msg)))
|
|
637
742
|
}
|
|
638
743
|
|
|
639
744
|
function sendSpawnResult(requestId, sessionId, error) {
|
|
@@ -642,12 +747,12 @@ function sendSpawnResult(requestId, sessionId, error) {
|
|
|
642
747
|
: { type: 'spawn-session-result', requestId, success: true, sessionId }
|
|
643
748
|
// Retry sending if relay connection is temporarily down
|
|
644
749
|
if (relayWs?.readyState === WebSocket.OPEN) {
|
|
645
|
-
relayWs.send(JSON.stringify(msg))
|
|
750
|
+
relayWs.send(JSON.stringify(encryptMessage(msg)))
|
|
646
751
|
} else {
|
|
647
752
|
log(`[codepocket] Relay not open, retrying spawn result in 2s...`)
|
|
648
753
|
setTimeout(() => {
|
|
649
754
|
if (relayWs?.readyState === WebSocket.OPEN) {
|
|
650
|
-
relayWs.send(JSON.stringify(msg))
|
|
755
|
+
relayWs.send(JSON.stringify(encryptMessage(msg)))
|
|
651
756
|
} else {
|
|
652
757
|
log(`[codepocket] Failed to send spawn result, relay still down`)
|
|
653
758
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tina_summer/codepocket",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.2",
|
|
4
4
|
"description": "CodePocket — 基于ClaudeCode的微信小程序工作台",
|
|
5
5
|
"bin": {
|
|
6
6
|
"codepocket": "bin/codepocket.js",
|
|
@@ -16,13 +16,14 @@
|
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"https-proxy-agent": "^7.0.0",
|
|
18
18
|
"qrcode-terminal": "^0.12.0",
|
|
19
|
+
"tweetnacl": "^1.0.3",
|
|
19
20
|
"ws": "^8.18.0"
|
|
20
21
|
},
|
|
21
22
|
"optionalDependencies": {
|
|
22
|
-
"@tina_summer/codepocket-darwin-arm64": "2.6.
|
|
23
|
-
"@tina_summer/codepocket-darwin-x64": "2.6.
|
|
24
|
-
"@tina_summer/codepocket-linux-x64": "2.6.
|
|
25
|
-
"@tina_summer/codepocket-win32-x64": "2.6.
|
|
23
|
+
"@tina_summer/codepocket-darwin-arm64": "2.6.2",
|
|
24
|
+
"@tina_summer/codepocket-darwin-x64": "2.6.2",
|
|
25
|
+
"@tina_summer/codepocket-linux-x64": "2.6.2",
|
|
26
|
+
"@tina_summer/codepocket-win32-x64": "2.6.2"
|
|
26
27
|
},
|
|
27
28
|
"engines": {
|
|
28
29
|
"node": ">=18"
|
package/src/index.ts
CHANGED
|
@@ -80,13 +80,23 @@ 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
|
|
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') {
|