@keyou007/agent-bot 0.1.27-alpha.0 → 0.1.27-alpha.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.
@@ -18,7 +18,7 @@ import type { TerminalCommandState } from "./TerminalCommandTracker.js";
18
18
  const require = createRequire(import.meta.url);
19
19
  const AUTH_TTL = 5 * 60_000;
20
20
  const IDLE_TTL = 30 * 60_000;
21
- const DISCONNECT_TTL = 3 * 60_000;
21
+ const DISCONNECT_TTL = 30 * 60_000;
22
22
  const FOREGROUND_TTL = 90_000;
23
23
  const MAX_TTL = 8 * 60 * 60_000;
24
24
  const KEY = z.string().regex(/^[a-f0-9]{64}$/u);
@@ -221,7 +221,7 @@ export class TerminalServer {
221
221
  const expire = () => {
222
222
  if (session.state !== "running" || session.socket || session.disconnectedAt === undefined) return;
223
223
  const remaining = DISCONNECT_TTL - (this.now() - session.disconnectedAt);
224
- if (remaining <= 0) this.disposeSession(session, "网页已断开 3 分钟,Shell 已自动关闭");
224
+ if (remaining <= 0) this.disposeSession(session, "网页已断开 30 分钟,Shell 已自动关闭");
225
225
  else { session.disconnectTimer = setTimeout(expire, remaining); session.disconnectTimer.unref(); }
226
226
  };
227
227
  session.disconnectTimer = setTimeout(expire, DISCONNECT_TTL); session.disconnectTimer.unref();
@@ -303,7 +303,12 @@ export class TerminalServer {
303
303
  body = fs.readFileSync(path.join(path.dirname(require.resolve("@xterm/xterm")), "../css/xterm.css"), "utf8"); type = "text/css";
304
304
  } else {
305
305
  const resumeId = this.resumeId(url.pathname);
306
- if (this.launchJob(url.pathname) || resumeId) { body = terminalPage(this.basePath, Boolean(resumeId)); type = "text/html; charset=utf-8"; }
306
+ if (this.launchJob(url.pathname) || resumeId) {
307
+ const session = resumeId ? this.sessions.get(resumeId) : undefined;
308
+ body = terminalPage(this.basePath, Boolean(resumeId), session?.state === "running" && Boolean(session.pty),
309
+ (this.options.password?.() ?? DEFAULT_TERMINAL_PASSWORD) === DEFAULT_TERMINAL_PASSWORD);
310
+ type = "text/html; charset=utf-8";
311
+ }
307
312
  }
308
313
  this.headers(res); res.statusCode = body === undefined ? 404 : 200; res.setHeader("Content-Type", type);
309
314
  res.end(req.method === "HEAD" ? undefined : body ?? "Not found."); return;
@@ -317,27 +322,31 @@ export class TerminalServer {
317
322
  if (!result.success) { this.json(res, 400, { error: "终端连接参数无效。" }); return; }
318
323
  const parsed = result.data;
319
324
  const now = this.now();
320
- const address = req.socket.remoteAddress ?? "unknown";
321
- while (this.passwordFailures[0] && this.passwordFailures[0].at <= now - 60_000) this.passwordFailures.shift();
322
- if (this.passwordFailures.length >= 50 || this.passwordFailures.filter((failure) => failure.address === address).length >= 5) {
323
- this.json(res, 429, { error: "密码尝试过于频繁,请一分钟后重试。" }); return;
324
- }
325
- const passwordHash = hash(this.options.password?.() ?? DEFAULT_TERMINAL_PASSWORD);
326
- if (parsed.password === undefined || !timingSafeEqual(hash(parsed.password), passwordHash)) {
327
- this.passwordFailures.push({ address, at: now });
328
- this.json(res, 401, { error: "终端密码错误,请重新输入。" }); return;
329
- }
330
325
  const resumeId = this.resumeId(parsed.launch);
331
326
  const jobId = this.launchJob(parsed.launch);
332
327
  const ownerId = this.options.ownerId();
333
- if ((!jobId && !resumeId) || !ownerId) throw new Error("终端链接无效,或尚未配置飞书所有者。");
334
328
  const existing = resumeId ? this.sessions.get(resumeId) : undefined;
335
329
  if (resumeId && (!existing || !this.canReopen(existing) || existing.ownerId !== ownerId)) {
336
330
  throw new Error("原终端尚未授权、已结束或服务已重启;请从原命令卡片创建新终端。");
337
331
  }
332
+ // A live Shell's recovery link grants reattachment, never a new Shell.
333
+ const resumeRunning = existing?.state === "running" && Boolean(existing.pty) && existing.ownerId === ownerId;
334
+ const passwordHash = hash(this.options.password?.() ?? DEFAULT_TERMINAL_PASSWORD);
335
+ if (!resumeRunning) {
336
+ const address = req.socket.remoteAddress ?? "unknown";
337
+ while (this.passwordFailures[0] && this.passwordFailures[0].at <= now - 60_000) this.passwordFailures.shift();
338
+ if (this.passwordFailures.length >= 50 || this.passwordFailures.filter((failure) => failure.address === address).length >= 5) {
339
+ this.json(res, 429, { error: "密码尝试过于频繁,请一分钟后重试。" }); return;
340
+ }
341
+ if (parsed.password === undefined || !timingSafeEqual(hash(parsed.password), passwordHash)) {
342
+ this.passwordFailures.push({ address, at: now });
343
+ this.json(res, 401, { error: "终端密码错误,请重新输入。" }); return;
344
+ }
345
+ }
346
+ if ((!jobId && !resumeId) || !ownerId) throw new Error("终端链接无效,或尚未配置飞书所有者。");
338
347
  if (!existing || existing.state === "approved") this.assertCanStart();
339
348
  while (this.requests[0] !== undefined && this.requests[0] < now - 3600_000) this.requests.shift();
340
- if (this.requests.length >= 20 || (this.requests.length > 0 && now - this.requests.at(-1)! < 10_000)
349
+ if (this.requests.length >= 20 || (!resumeRunning && this.requests.length > 0 && now - this.requests.at(-1)! < 10_000)
341
350
  || (!existing && [...this.sessions.values()].filter((s) => s.state !== "closed").length >= 3)) {
342
351
  this.json(res, 429, { error: "终端请求过于频繁或已达 3 个会话上限,请稍后重试。" }); return;
343
352
  }
@@ -500,7 +509,7 @@ export class TerminalServer {
500
509
  this.disposeSession(s, "终端已超时或授权已失效,请重新连接");
501
510
  }
502
511
  if (s.state === "running" && s.disconnectedAt !== undefined && now - s.disconnectedAt >= DISCONNECT_TTL) {
503
- this.disposeSession(s, "网页已断开 3 分钟,Shell 已自动关闭");
512
+ this.disposeSession(s, "网页已断开 30 分钟,Shell 已自动关闭");
504
513
  }
505
514
  if (s.state === "running" && now - Math.max(s.lastActivity, s.foregroundUntil ?? 0) >= IDLE_TTL) {
506
515
  this.disposeSession(s, "连续 30 分钟无输入或输出,Shell 已自动关闭");