@tina_summer/codepocket 2.6.0 → 2.6.1
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/bin/codepocket.js +103 -3
- package/package.json +6 -5
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')
|
|
@@ -518,6 +520,101 @@ async function startSSE() {
|
|
|
518
520
|
} catch { setTimeout(startSSE, 5000) }
|
|
519
521
|
}
|
|
520
522
|
|
|
523
|
+
// ── End-to-end encryption (X25519 + XSalsa20-Poly1305) ──
|
|
524
|
+
// The relay only forwards e2ePubKey + opaque `enc` envelopes; it cannot decrypt.
|
|
525
|
+
// Nonces are counter-based with a direction tag to avoid reuse across directions
|
|
526
|
+
// under the same shared key: connector uses 0xFF prefix, phone uses 0x00.
|
|
527
|
+
|
|
528
|
+
let e2eKeypair = null // { publicKey, secretKey } Uint8Arrays (X25519)
|
|
529
|
+
let e2eSharedKey = null // Uint8Array(32), set when peer pubkey arrives
|
|
530
|
+
let e2eNonceCounter = 0
|
|
531
|
+
|
|
532
|
+
function ensureE2EKeypair() {
|
|
533
|
+
if (e2eKeypair) return e2eKeypair
|
|
534
|
+
// Node has crypto.randomBytes — nacl.box.keyPair auto-detects it
|
|
535
|
+
e2eKeypair = nacl.box.keyPair()
|
|
536
|
+
return e2eKeypair
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function getE2EPubKeyB64() {
|
|
540
|
+
return Buffer.from(ensureE2EKeypair().publicKey).toString('base64')
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
function setPeerE2EPubKey(peerPubKeyB64) {
|
|
544
|
+
if (!peerPubKeyB64 || typeof peerPubKeyB64 !== 'string') return
|
|
545
|
+
try {
|
|
546
|
+
const peerPub = Buffer.from(peerPubKeyB64, 'base64')
|
|
547
|
+
if (peerPub.length !== 32) return
|
|
548
|
+
e2eSharedKey = nacl.box.before(peerPub, ensureE2EKeypair().secretKey)
|
|
549
|
+
e2eNonceCounter = 0
|
|
550
|
+
log('[codepocket] ✓ E2E 加密通道已建立')
|
|
551
|
+
} catch (e) {
|
|
552
|
+
log(`[codepocket] E2E 密钥协商失败: ${e.message}`)
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function nextNonce() {
|
|
557
|
+
e2eNonceCounter++
|
|
558
|
+
const n = new Uint8Array(24)
|
|
559
|
+
n[0] = 0xFF // connector→phone direction tag
|
|
560
|
+
let c = e2eNonceCounter
|
|
561
|
+
for (let i = 6; i >= 1; i--) { n[i] = c & 0xff; c = Math.floor(c / 256) }
|
|
562
|
+
return n
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function e2eEncrypt(obj) {
|
|
566
|
+
if (!e2eSharedKey) return undefined
|
|
567
|
+
try {
|
|
568
|
+
const msgBytes = Buffer.from(JSON.stringify(obj), 'utf8')
|
|
569
|
+
const nonce = nextNonce()
|
|
570
|
+
const cipher = nacl.secretbox(msgBytes, nonce, e2eSharedKey)
|
|
571
|
+
if (!cipher) return undefined
|
|
572
|
+
return { n: Buffer.from(nonce).toString('base64'), c: Buffer.from(cipher).toString('base64') }
|
|
573
|
+
} catch { return undefined }
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
function e2eDecrypt(envelope) {
|
|
577
|
+
if (!envelope || typeof envelope !== 'object') return undefined
|
|
578
|
+
if (typeof envelope.n !== 'string' || typeof envelope.c !== 'string') return undefined
|
|
579
|
+
if (!e2eSharedKey) return undefined
|
|
580
|
+
try {
|
|
581
|
+
const nonce = new Uint8Array(Buffer.from(envelope.n, 'base64'))
|
|
582
|
+
const cipher = new Uint8Array(Buffer.from(envelope.c, 'base64'))
|
|
583
|
+
const plain = nacl.secretbox.open(cipher, nonce, e2eSharedKey)
|
|
584
|
+
if (!plain) return undefined
|
|
585
|
+
return JSON.parse(Buffer.from(plain).toString('utf8'))
|
|
586
|
+
} catch { return undefined }
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// Pack a message for sending: moves sensitive fields into an `enc` envelope.
|
|
590
|
+
// Routing fields (type, requestId, actionId, success, status, ok) stay plaintext.
|
|
591
|
+
function encryptMessage(msg) {
|
|
592
|
+
if (!e2eSharedKey) return msg
|
|
593
|
+
const routing = ['type', 'requestId', 'actionId', 'success', 'status', 'ok', 'reconnected']
|
|
594
|
+
const content = {}
|
|
595
|
+
for (const [k, v] of Object.entries(msg)) {
|
|
596
|
+
if (!routing.includes(k)) content[k] = v
|
|
597
|
+
}
|
|
598
|
+
if (Object.keys(content).length === 0) return msg
|
|
599
|
+
const enc = e2eEncrypt(content)
|
|
600
|
+
if (!enc) return msg
|
|
601
|
+
const out = { type: msg.type }
|
|
602
|
+
for (const k of routing) if (k in msg) out[k] = msg[k]
|
|
603
|
+
out.enc = enc
|
|
604
|
+
return out
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
// Unpack a received message: merges decrypted content back from `enc`.
|
|
608
|
+
function decryptMessage(msg) {
|
|
609
|
+
if (!msg || !msg.enc || !e2eSharedKey) return msg
|
|
610
|
+
const content = e2eDecrypt(msg.enc)
|
|
611
|
+
if (content) {
|
|
612
|
+
delete msg.enc
|
|
613
|
+
Object.assign(msg, content)
|
|
614
|
+
}
|
|
615
|
+
return msg
|
|
616
|
+
}
|
|
617
|
+
|
|
521
618
|
// ── Relay ──
|
|
522
619
|
|
|
523
620
|
function connectRelay() {
|
|
@@ -547,6 +644,7 @@ function connectRelay() {
|
|
|
547
644
|
type: 'register',
|
|
548
645
|
key: relayKey,
|
|
549
646
|
sessionToken: savedSession?.sessionToken || undefined,
|
|
647
|
+
e2ePubKey: getE2EPubKeyB64(),
|
|
550
648
|
}))
|
|
551
649
|
relayPingInterval = setInterval(() => {
|
|
552
650
|
if (relayWs?.readyState === WebSocket.OPEN) {
|
|
@@ -558,6 +656,7 @@ function connectRelay() {
|
|
|
558
656
|
relayWs.on('message', (raw) => {
|
|
559
657
|
let msg
|
|
560
658
|
try { msg = JSON.parse(raw.toString()) } catch { return }
|
|
659
|
+
msg = decryptMessage(msg)
|
|
561
660
|
switch (msg.type) {
|
|
562
661
|
case 'registered': {
|
|
563
662
|
try {
|
|
@@ -572,6 +671,7 @@ function connectRelay() {
|
|
|
572
671
|
break
|
|
573
672
|
}
|
|
574
673
|
case 'client-connected': {
|
|
674
|
+
if (msg.clientE2EPubKey) setPeerE2EPubKey(msg.clientE2EPubKey)
|
|
575
675
|
log('[codepocket] ✓ 小程序已连接')
|
|
576
676
|
break
|
|
577
677
|
}
|
|
@@ -633,7 +733,7 @@ function connectRelay() {
|
|
|
633
733
|
}
|
|
634
734
|
|
|
635
735
|
function sendToRelay(msg) {
|
|
636
|
-
if (relayWs?.readyState === WebSocket.OPEN) relayWs.send(JSON.stringify(msg))
|
|
736
|
+
if (relayWs?.readyState === WebSocket.OPEN) relayWs.send(JSON.stringify(encryptMessage(msg)))
|
|
637
737
|
}
|
|
638
738
|
|
|
639
739
|
function sendSpawnResult(requestId, sessionId, error) {
|
|
@@ -642,12 +742,12 @@ function sendSpawnResult(requestId, sessionId, error) {
|
|
|
642
742
|
: { type: 'spawn-session-result', requestId, success: true, sessionId }
|
|
643
743
|
// Retry sending if relay connection is temporarily down
|
|
644
744
|
if (relayWs?.readyState === WebSocket.OPEN) {
|
|
645
|
-
relayWs.send(JSON.stringify(msg))
|
|
745
|
+
relayWs.send(JSON.stringify(encryptMessage(msg)))
|
|
646
746
|
} else {
|
|
647
747
|
log(`[codepocket] Relay not open, retrying spawn result in 2s...`)
|
|
648
748
|
setTimeout(() => {
|
|
649
749
|
if (relayWs?.readyState === WebSocket.OPEN) {
|
|
650
|
-
relayWs.send(JSON.stringify(msg))
|
|
750
|
+
relayWs.send(JSON.stringify(encryptMessage(msg)))
|
|
651
751
|
} else {
|
|
652
752
|
log(`[codepocket] Failed to send spawn result, relay still down`)
|
|
653
753
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tina_summer/codepocket",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.1",
|
|
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.1",
|
|
24
|
+
"@tina_summer/codepocket-darwin-x64": "2.6.1",
|
|
25
|
+
"@tina_summer/codepocket-linux-x64": "2.6.1",
|
|
26
|
+
"@tina_summer/codepocket-win32-x64": "2.6.1"
|
|
26
27
|
},
|
|
27
28
|
"engines": {
|
|
28
29
|
"node": ">=18"
|