@aivin-labs/cli 1.0.51 → 1.0.60

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.
Files changed (32) hide show
  1. package/bin/cli.mjs +2215 -2211
  2. package/bin/lib/auth.mjs +119 -88
  3. package/bin/lib/context.mjs +58 -5
  4. package/bin/lib/loginHandoff.mjs +87 -0
  5. package/bin/lib/openFile.mjs +29 -4
  6. package/bin/lib/remote/agentContract.mjs +370 -0
  7. package/bin/lib/remote/agentEvidence.mjs +409 -0
  8. package/bin/lib/remote/autoStart.mjs +640 -623
  9. package/bin/lib/remote/client.mjs +55 -17
  10. package/bin/lib/remote/codingAgents.mjs +594 -293
  11. package/bin/lib/remote/constants.mjs +1 -1
  12. package/bin/lib/remote/credentials.mjs +131 -125
  13. package/bin/lib/remote/desktop/castAuth.mjs +44 -19
  14. package/bin/lib/remote/desktop/castConsent.mjs +79 -14
  15. package/bin/lib/remote/desktop/castInput.mjs +3 -0
  16. package/bin/lib/remote/desktop/castTrust.mjs +221 -0
  17. package/bin/lib/remote/desktop/desktopCast.mjs +483 -443
  18. package/bin/lib/remote/desktop/desktopCastCaptureSource.mjs +1 -1
  19. package/bin/lib/remote/desktop/desktopCastSource.mjs +324 -40
  20. package/bin/lib/remote/desktop/desktopWebRtc.mjs +59 -4
  21. package/bin/lib/remote/desktop/strings.mjs +26 -0
  22. package/bin/lib/remote/desktop/traySource.mjs +1512 -1385
  23. package/bin/lib/remote/desktop/webrtcWorker.mjs +4 -0
  24. package/bin/lib/remote/dpapiHelper.mjs +91 -0
  25. package/bin/lib/remote/gui/index.mjs +422 -68
  26. package/bin/lib/remote/gui/windowsHostSource.mjs +2418 -1530
  27. package/bin/lib/remote/humanInput.mjs +7 -0
  28. package/bin/lib/remote/localConfirm.mjs +2 -2
  29. package/bin/lib/remote/tokenStore.mjs +19 -2
  30. package/bin/lib/remote/toolHandlers.mjs +861 -859
  31. package/docs/CLI.md +1606 -1602
  32. package/package.json +1 -1
@@ -1,4 +1,5 @@
1
1
  import net from 'net';
2
+ import os from 'os';
2
3
  import readline from 'readline';
3
4
  import { spawn } from 'child_process';
4
5
  import { createDesktopWebRtc } from './desktopWebRtc.mjs';
@@ -24,6 +25,9 @@ export const DESKTOP_WEBRTC_WORKER_ARG = '__desktop-webrtc-worker';
24
25
  const VIDEO_LIVE_MS = 3000;
25
26
 
26
27
  export async function runDesktopWebRtcWorker({ input = process.stdin, output = process.stdout } = {}) {
28
+ // Bộ mã hoá hình chạy bằng CPU: máy bận mà ở mức thường thì có lượt đứng hình vài giây (đo 13/11/2026, 24 luồng đốt CPU: trung vị
29
+ // 104 ms, lượt tệ nhất 3 s; trên mức thường: 94 ms, tệ nhất 106 ms). Không lên High để không giành ứng dụng người dùng đang dùng.
30
+ try { os.setPriority(process.pid, os.constants.priority.PRIORITY_ABOVE_NORMAL); } catch { /* không đổi được thì chạy mức thường */ }
27
31
  const send = (msg) => { try { output.write(JSON.stringify(msg) + '\n'); } catch { /* daemon đã đóng */ } };
28
32
  let lastRawAt = 0;
29
33
  let lastState = '';
@@ -0,0 +1,91 @@
1
+ import { createHash } from 'crypto';
2
+ import { execFileSync } from 'child_process';
3
+ import fs from 'fs';
4
+ import path from 'path';
5
+ import { REMOTE_CONFIG_DIR } from './constants.mjs';
6
+
7
+ /**
8
+ * DPAPI nhanh trên Windows (13/11/2026): tệp exe nhỏ biên dịch một lần bằng csc của .NET Framework có sẵn trong Windows, đặt trong
9
+ * `~/.aivin/bin` (thư mục bị chặn với mọi công cụ từ xa). Mỗi lần giải ~0,1 s thay vì ~1–4 s của PowerShell — đủ nhanh để CLI giải khoá
10
+ * API ở MỖI lệnh. Không biên dịch được (máy chặn csc, diệt virus xoá tệp) → `null`, tokenStore lùi về PowerShell như cũ.
11
+ * Định dạng bản mã giống hệt bản PowerShell (UTF-8, entropy, CurrentUser, base64) nên đọc được bản mã cũ.
12
+ */
13
+
14
+ const SOURCE = `using System; using System.Text; using System.Security.Cryptography;
15
+ class AivinDpapi {
16
+ static int Main(string[] a) {
17
+ try {
18
+ if (a.Length < 2) return 2;
19
+ string input = Console.In.ReadToEnd();
20
+ byte[] entropy = Encoding.UTF8.GetBytes(a[1]);
21
+ if (a[0] == "protect") {
22
+ Console.Out.Write(Convert.ToBase64String(ProtectedData.Protect(Encoding.UTF8.GetBytes(input), entropy, DataProtectionScope.CurrentUser)));
23
+ return 0;
24
+ }
25
+ if (a[0] == "unprotect") {
26
+ Console.Out.Write(Encoding.UTF8.GetString(ProtectedData.Unprotect(Convert.FromBase64String(input.Trim()), entropy, DataProtectionScope.CurrentUser)));
27
+ return 0;
28
+ }
29
+ return 2;
30
+ } catch { return 1; }
31
+ }
32
+ }
33
+ `;
34
+
35
+ const VERSION = createHash('sha256').update(SOURCE).digest('hex').slice(0, 10);
36
+ const BIN_DIR = path.join(REMOTE_CONFIG_DIR, 'bin');
37
+ export const DPAPI_HELPER_PATH = path.join(BIN_DIR, `aivin-dpapi-${VERSION}.exe`);
38
+ const FAILED_MARK = path.join(BIN_DIR, `aivin-dpapi-${VERSION}.failed`);
39
+ /** Biên dịch hỏng thì thôi thử lại trong ngần này — không bắt mọi lệnh chờ csc. */
40
+ const RETRY_AFTER_FAIL_MS = 24 * 3600_000;
41
+ const RUN_TIMEOUT_MS = 10_000;
42
+
43
+ export const DpapiMode = Object.freeze({ PROTECT: 'protect', UNPROTECT: 'unprotect' });
44
+
45
+ let resolved; // undefined = chưa dò; null = không dùng được; chuỗi = đường dẫn exe
46
+
47
+ function cscPath(env = process.env) {
48
+ const windir = env.WINDIR || env.SystemRoot || 'C:\\Windows';
49
+ return ['Framework64', 'Framework'].map((d) => path.join(windir, 'Microsoft.NET', d, 'v4.0.30319', 'csc.exe')).find((p) => fs.existsSync(p)) || null;
50
+ }
51
+
52
+ function recentlyFailed() {
53
+ try { return Date.now() - fs.statSync(FAILED_MARK).mtimeMs < RETRY_AFTER_FAIL_MS; } catch { return false; }
54
+ }
55
+
56
+ function build() {
57
+ const csc = cscPath();
58
+ if (!csc) return null;
59
+ fs.mkdirSync(BIN_DIR, { recursive: true });
60
+ const src = path.join(BIN_DIR, `aivin-dpapi-${VERSION}.cs`);
61
+ const tmp = `${DPAPI_HELPER_PATH}.${process.pid}.tmp`;
62
+ try {
63
+ fs.writeFileSync(src, SOURCE, 'utf8');
64
+ execFileSync(csc, ['-nologo', '-target:exe', '-optimize+', '-r:System.Security.dll', `-out:${tmp}`, src], { timeout: 60_000, windowsHide: true, stdio: 'ignore' });
65
+ fs.renameSync(tmp, DPAPI_HELPER_PATH);
66
+ return DPAPI_HELPER_PATH;
67
+ } catch {
68
+ try { fs.rmSync(tmp, { force: true }); } catch { /* không có thì thôi */ }
69
+ try { fs.writeFileSync(FAILED_MARK, new Date().toISOString()); } catch { /* chỉ là dấu */ }
70
+ return fs.existsSync(DPAPI_HELPER_PATH) ? DPAPI_HELPER_PATH : null;
71
+ } finally {
72
+ try { fs.rmSync(src, { force: true }); } catch { /* không có thì thôi */ }
73
+ }
74
+ }
75
+
76
+ /** Đường dẫn exe (biên dịch lần đầu nếu cần); `null` khi không phải Windows hoặc không dựng được. */
77
+ export function dpapiHelper({ platform = process.platform } = {}) {
78
+ if (resolved !== undefined) return resolved;
79
+ if (platform !== 'win32' || process.env.AIVIN_DPAPI_HELPER === '0') return (resolved = null);
80
+ if (fs.existsSync(DPAPI_HELPER_PATH)) return (resolved = DPAPI_HELPER_PATH);
81
+ if (recentlyFailed()) return (resolved = null);
82
+ return (resolved = build());
83
+ }
84
+
85
+ /** Chạy exe; lỗi (bản mã của máy / tài khoản khác…) → ném lỗi như nhánh PowerShell. */
86
+ export function runDpapiHelper(exe, mode, input, entropy) {
87
+ return execFileSync(exe, [mode, entropy], { input, encoding: 'utf8', timeout: RUN_TIMEOUT_MS, windowsHide: true, stdio: ['pipe', 'pipe', 'pipe'] }).trim();
88
+ }
89
+
90
+ /** Chỉ cho test: quên kết quả dò. */
91
+ export function resetDpapiHelperForTest() { resolved = undefined; }