@yanzc/claude-pro 0.11.0 → 0.13.0
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 +56 -36
- package/package.json +1 -1
- package/src/agent.js +4 -3
- package/src/browser/session.js +21 -1
- package/src/commands.js +198 -33
- package/src/config.js +17 -2
- package/src/gateway.js +33 -0
- package/src/live.js +148 -0
- package/src/main.js +5 -3
- package/src/peers.js +41 -4
- package/src/providers.js +25 -6
- package/src/remote.js +82 -268
- package/src/runtime.js +36 -11
- package/src/selfcheck.js +3 -3
- package/src/session.js +128 -8
- package/src/supervisor.js +1 -1
- package/src/tools/bash.js +21 -12
- package/src/tools/browser.js +14 -8
- package/src/tools/code.js +18 -11
- package/src/tools/restart.js +1 -1
- package/src/ui/dialogs.js +23 -30
- package/src/ui/editor.js +65 -169
- package/src/ui/repl.js +467 -474
- package/src/ui/screen.js +47 -674
- package/src/updater.js +5 -4
- package/src/web/api.js +881 -0
- package/src/web/app.css +530 -0
- package/src/web/app.js +2188 -0
- package/src/web/data.js +436 -0
- package/src/web/host.js +332 -0
- package/src/web/login.html +67 -0
- package/src/web/logo.svg +1 -0
- package/src/web/page.html +44 -0
- package/src/web/remote-access.js +232 -0
- package/src/web/server.js +476 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
// 远程访问 of the web console: https://<通讯号>.7bit.fun reaches this computer's console through the gateway (a
|
|
2
|
+
// WebSocket the console's server keeps open, see TunnelClient), and asks for a password first. The password is checked
|
|
3
|
+
// here (the gateway never knows it): its scrypt hash, the logged-in devices and the recent logins are kept in
|
|
4
|
+
// CONFIG_DIR/web-remote.json, readable by the user alone.
|
|
5
|
+
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import os from 'node:os';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import http from 'node:http';
|
|
10
|
+
import crypto from 'node:crypto';
|
|
11
|
+
import { CONFIG_DIR, VERSION } from '../config.js';
|
|
12
|
+
|
|
13
|
+
export const remoteFile = () => path.join(CONFIG_DIR, 'web-remote.json');
|
|
14
|
+
export const MIN_PASSWORD = 10;
|
|
15
|
+
const SESSION_DAYS = 30;
|
|
16
|
+
const LOGINS_KEPT = 30;
|
|
17
|
+
|
|
18
|
+
export function readRemote() {
|
|
19
|
+
try {
|
|
20
|
+
const data = JSON.parse(fs.readFileSync(remoteFile(), 'utf8'));
|
|
21
|
+
return data && typeof data === 'object' ? data : {};
|
|
22
|
+
} catch { return {}; }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function writeRemote(mutate) {
|
|
26
|
+
const data = readRemote();
|
|
27
|
+
mutate(data);
|
|
28
|
+
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
29
|
+
const tmp = `${remoteFile()}.${process.pid}.tmp`;
|
|
30
|
+
fs.writeFileSync(tmp, JSON.stringify(data, null, 2), { mode: 0o600 });
|
|
31
|
+
fs.renameSync(tmp, remoteFile());
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const scrypt = (password, salt) => crypto.scryptSync(String(password), Buffer.from(salt, 'base64'), 32, { N: 1 << 15, r: 8, p: 1, maxmem: 64 * 1024 * 1024 }).toString('base64');
|
|
36
|
+
|
|
37
|
+
// A new password logs every device out.
|
|
38
|
+
export function setPassword(password) {
|
|
39
|
+
if (String(password || '').length < MIN_PASSWORD) throw new Error(`密码至少 ${MIN_PASSWORD} 位`);
|
|
40
|
+
const salt = crypto.randomBytes(16).toString('base64');
|
|
41
|
+
return writeRemote((d) => { d.salt = salt; d.hash = scrypt(password, salt); d.sessions = {}; });
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function hasPassword(data = readRemote()) { return Boolean(data.salt && data.hash); }
|
|
45
|
+
|
|
46
|
+
export function checkPassword(password, data = readRemote()) {
|
|
47
|
+
if (!hasPassword(data)) return false;
|
|
48
|
+
const got = Buffer.from(scrypt(password, data.salt), 'base64');
|
|
49
|
+
const want = Buffer.from(data.hash, 'base64');
|
|
50
|
+
return got.length === want.length && crypto.timingSafeEqual(got, want);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// This computer's id among the account's computers (the gateway lists them by it).
|
|
54
|
+
export function computerId() {
|
|
55
|
+
const data = readRemote();
|
|
56
|
+
if (/^[\w-]{6,64}$/.test(data.computer || '')) return data.computer;
|
|
57
|
+
const id = crypto.randomBytes(8).toString('hex');
|
|
58
|
+
writeRemote((d) => { d.computer = id; });
|
|
59
|
+
return id;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function setEnabled(on) { return writeRemote((d) => { d.enabled = Boolean(on); }); }
|
|
63
|
+
|
|
64
|
+
// ---------------------------------------------------------------- logged-in devices
|
|
65
|
+
|
|
66
|
+
const hashToken = (secret) => crypto.createHash('sha256').update(secret).digest('base64url');
|
|
67
|
+
|
|
68
|
+
// A device that typed the password: the cookie's value ("<id>.<secret>"; only the secret's hash is kept).
|
|
69
|
+
export function newSession({ remember, ip, ua }) {
|
|
70
|
+
const id = crypto.randomBytes(9).toString('base64url');
|
|
71
|
+
const secret = crypto.randomBytes(24).toString('base64url');
|
|
72
|
+
const now = Date.now();
|
|
73
|
+
writeRemote((d) => {
|
|
74
|
+
d.sessions = Object.fromEntries(Object.entries(d.sessions || {}).filter(([, s]) => s.expires > now));
|
|
75
|
+
d.sessions[id] = { hash: hashToken(secret), created: now, expires: now + (remember ? SESSION_DAYS : 1) * 86_400_000, remember: Boolean(remember), ip, ua: String(ua || '').slice(0, 200), lastSeen: now };
|
|
76
|
+
});
|
|
77
|
+
return { token: `${id}.${secret}`, maxAge: remember ? SESSION_DAYS * 86400 : null };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const seenWritten = new Map(); // id -> when lastSeen was last written (at most every 10 minutes)
|
|
81
|
+
export function validSession(token) {
|
|
82
|
+
const [id, secret] = String(token || '').split('.');
|
|
83
|
+
if (!id || !secret) return null;
|
|
84
|
+
const s = readRemote().sessions?.[id];
|
|
85
|
+
if (!s || s.expires < Date.now()) return null;
|
|
86
|
+
const a = Buffer.from(hashToken(secret)), b = Buffer.from(String(s.hash || ''));
|
|
87
|
+
if (a.length !== b.length || !crypto.timingSafeEqual(a, b)) return null;
|
|
88
|
+
if (Date.now() - (seenWritten.get(id) || 0) > 600_000) {
|
|
89
|
+
seenWritten.set(id, Date.now());
|
|
90
|
+
try { writeRemote((d) => { if (d.sessions?.[id]) d.sessions[id].lastSeen = Date.now(); }); } catch { /* next time */ }
|
|
91
|
+
}
|
|
92
|
+
return { id, ...s };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function endSession(token) {
|
|
96
|
+
const [id] = String(token || '').split('.');
|
|
97
|
+
if (id) writeRemote((d) => { if (d.sessions) delete d.sessions[id]; });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function endAllSessions() { writeRemote((d) => { d.sessions = {}; }); }
|
|
101
|
+
|
|
102
|
+
export function noteLogin({ ip, ua, ok }) {
|
|
103
|
+
try {
|
|
104
|
+
writeRemote((d) => {
|
|
105
|
+
d.logins = [{ at: Date.now(), ip, ua: String(ua || '').slice(0, 200), ok: Boolean(ok) }, ...(d.logins || [])].slice(0, LOGINS_KEPT);
|
|
106
|
+
});
|
|
107
|
+
} catch { /* not recorded */ }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Brute force: 5 wrong passwords from an address lock it for 15 minutes; 20 in an hour from anywhere lock all logins
|
|
111
|
+
// for the rest of that hour.
|
|
112
|
+
const failures = new Map(); // ip -> {count, until}
|
|
113
|
+
let hourly = { start: Date.now(), count: 0 };
|
|
114
|
+
export function loginBlocked(ip) {
|
|
115
|
+
const now = Date.now();
|
|
116
|
+
if (now - hourly.start > 3600_000) hourly = { start: now, count: 0 };
|
|
117
|
+
if (hourly.count >= 20) return Math.ceil((hourly.start + 3600_000 - now) / 60_000);
|
|
118
|
+
const f = failures.get(ip);
|
|
119
|
+
if (f && f.until > now) return Math.ceil((f.until - now) / 60_000);
|
|
120
|
+
return 0;
|
|
121
|
+
}
|
|
122
|
+
export function loginFailed(ip) {
|
|
123
|
+
hourly.count++;
|
|
124
|
+
const f = failures.get(ip) || { count: 0, until: 0 };
|
|
125
|
+
f.count++;
|
|
126
|
+
if (f.count >= 5) { f.until = Date.now() + 15 * 60_000; f.count = 0; }
|
|
127
|
+
failures.set(ip, f);
|
|
128
|
+
}
|
|
129
|
+
export function loginPassed(ip) { failures.delete(ip); }
|
|
130
|
+
|
|
131
|
+
// ---------------------------------------------------------------- the tunnel to the gateway
|
|
132
|
+
|
|
133
|
+
// The console's end of the tunnel (see the gateway's tunnel.mjs): each request the gateway passes on is made to this
|
|
134
|
+
// server itself on 127.0.0.1 with `secret` in a header (it never leaves this process, so nothing else can claim to be
|
|
135
|
+
// a remote request), and the answer goes back as it comes.
|
|
136
|
+
export class TunnelClient {
|
|
137
|
+
constructor({ gatewayUrl, apiKey, localPort, secret, onState = () => {} }) {
|
|
138
|
+
this.wsUrl = `${String(gatewayUrl).replace(/\/+$/, '').replace(/^http/, 'ws')}/v1/tunnel`;
|
|
139
|
+
this.apiKey = apiKey;
|
|
140
|
+
this.localPort = localPort;
|
|
141
|
+
this.secret = secret;
|
|
142
|
+
this.onState = onState;
|
|
143
|
+
this.state = { status: 'connecting', host: null, error: null, since: null };
|
|
144
|
+
this.local = new Map(); // id -> the request to this server
|
|
145
|
+
this.closed = false;
|
|
146
|
+
this.wait = 1000;
|
|
147
|
+
this.open();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
set(patch) { this.state = { ...this.state, ...patch }; try { this.onState(this.state); } catch { /* ignore */ } }
|
|
151
|
+
|
|
152
|
+
open() {
|
|
153
|
+
if (this.closed) return;
|
|
154
|
+
let ws;
|
|
155
|
+
try { ws = new WebSocket(this.wsUrl); } catch (e) { this.retry(e.message); return; }
|
|
156
|
+
this.ws = ws;
|
|
157
|
+
let ready = false;
|
|
158
|
+
ws.onopen = () => ws.send(JSON.stringify({ t: 'hello', key: this.apiKey, computer: computerId(), machine: machineLabel(), version: VERSION }));
|
|
159
|
+
ws.onmessage = (event) => {
|
|
160
|
+
let msg;
|
|
161
|
+
try { msg = JSON.parse(String(event.data)); } catch { return; }
|
|
162
|
+
if (msg.t === 'ready') { ready = true; this.wait = 1000; this.set({ status: 'online', host: msg.host, error: null, since: Date.now() }); return; }
|
|
163
|
+
if (msg.t === 'error') { this.fatal = msg.message; this.set({ status: 'error', error: msg.message }); return; }
|
|
164
|
+
this.fromGateway(msg);
|
|
165
|
+
};
|
|
166
|
+
ws.onclose = (event) => {
|
|
167
|
+
for (const req of this.local.values()) req.destroy();
|
|
168
|
+
this.local.clear();
|
|
169
|
+
if (this.closed) return;
|
|
170
|
+
// refused for good (a bad key, a 通讯号 that is not a host name): tried again only slowly
|
|
171
|
+
if (event.code === 4003 || event.code === 4005) { this.wait = 5 * 60_000; this.set({ status: 'error', error: this.fatal || '网关拒绝了这台电脑' }); }
|
|
172
|
+
else if (!ready) this.set({ status: 'connecting', error: this.state.error });
|
|
173
|
+
else this.set({ status: 'connecting' });
|
|
174
|
+
this.retry();
|
|
175
|
+
};
|
|
176
|
+
ws.onerror = () => {};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
retry(error) {
|
|
180
|
+
if (this.closed) return;
|
|
181
|
+
if (error) this.set({ status: 'connecting', error });
|
|
182
|
+
clearTimeout(this.timer);
|
|
183
|
+
this.timer = setTimeout(() => this.open(), this.wait);
|
|
184
|
+
this.timer.unref?.();
|
|
185
|
+
this.wait = Math.min(this.wait * 2, 60_000);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
send(msg) { if (this.ws?.readyState === 1) this.ws.send(JSON.stringify(msg)); }
|
|
189
|
+
|
|
190
|
+
fromGateway(msg) {
|
|
191
|
+
if (msg.t === 'req') return this.start(msg);
|
|
192
|
+
const req = this.local.get(msg.id);
|
|
193
|
+
if (!req) return;
|
|
194
|
+
if (msg.t === 'body') req.write(Buffer.from(String(msg.data || ''), 'base64'));
|
|
195
|
+
else if (msg.t === 'end') req.end();
|
|
196
|
+
else if (msg.t === 'abort') { req.destroy(); this.local.delete(msg.id); }
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
start(msg) {
|
|
200
|
+
const headers = { ...(msg.headers || {}) };
|
|
201
|
+
for (const k of Object.keys(headers)) if (/^x-cpweb-/i.test(k)) delete headers[k];
|
|
202
|
+
headers.host = `127.0.0.1:${this.localPort}`;
|
|
203
|
+
headers['x-cpweb-tunnel'] = this.secret;
|
|
204
|
+
headers['x-cpweb-ip'] = String(msg.ip || '');
|
|
205
|
+
headers['x-cpweb-host'] = String(msg.host || this.state.host || '');
|
|
206
|
+
headers['x-cpweb-origin'] = String(msg.origin || `https://${headers['x-cpweb-host']}`);
|
|
207
|
+
const req = http.request({ host: '127.0.0.1', port: this.localPort, method: msg.method, path: msg.path, headers }, (res) => {
|
|
208
|
+
this.send({ t: 'res', id: msg.id, status: res.statusCode, headers: res.headers });
|
|
209
|
+
res.on('data', (chunk) => {
|
|
210
|
+
for (let i = 0; i < chunk.length; i += 256 * 1024) this.send({ t: 'body', id: msg.id, data: chunk.subarray(i, i + 256 * 1024).toString('base64') });
|
|
211
|
+
});
|
|
212
|
+
res.on('end', () => { this.send({ t: 'end', id: msg.id }); this.local.delete(msg.id); });
|
|
213
|
+
res.on('error', () => { this.send({ t: 'end', id: msg.id }); this.local.delete(msg.id); });
|
|
214
|
+
});
|
|
215
|
+
req.on('error', () => { this.send({ t: 'res', id: msg.id, status: 502, headers: { 'content-type': 'text/plain; charset=utf-8' } }); this.send({ t: 'end', id: msg.id }); this.local.delete(msg.id); });
|
|
216
|
+
this.local.set(msg.id, req);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
close() {
|
|
220
|
+
this.closed = true;
|
|
221
|
+
clearTimeout(this.timer);
|
|
222
|
+
for (const req of this.local.values()) req.destroy();
|
|
223
|
+
this.local.clear();
|
|
224
|
+
try { this.ws?.close(); } catch { /* gone */ }
|
|
225
|
+
this.set({ status: 'off', host: null });
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function machineLabel() {
|
|
230
|
+
const raw = process.env.CLAUDE_PRO_MACHINE_NAME || os.hostname() || 'computer';
|
|
231
|
+
return String(raw).replace(/[^\p{L}\p{M}\p{N}_.-]+/gu, '-').replace(/^[-.]+|[-.]+$/g, '').slice(0, 40) || 'computer';
|
|
232
|
+
}
|