cursor-switch 1.0.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.
Files changed (63) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +161 -0
  3. package/SECURITY.md +38 -0
  4. package/bin/cswitch.js +35 -0
  5. package/dist/cli.d.ts +6 -0
  6. package/dist/cli.js +850 -0
  7. package/dist/core/account-store.d.ts +25 -0
  8. package/dist/core/account-store.js +182 -0
  9. package/dist/core/cdp-engine.d.ts +75 -0
  10. package/dist/core/cdp-engine.js +320 -0
  11. package/dist/core/config-store.d.ts +38 -0
  12. package/dist/core/config-store.js +100 -0
  13. package/dist/core/cursor-paths.d.ts +22 -0
  14. package/dist/core/cursor-paths.js +287 -0
  15. package/dist/core/dashboard-launcher.d.ts +34 -0
  16. package/dist/core/dashboard-launcher.js +210 -0
  17. package/dist/core/fingerprint.d.ts +7 -0
  18. package/dist/core/fingerprint.js +34 -0
  19. package/dist/core/oauth-receiver.d.ts +31 -0
  20. package/dist/core/oauth-receiver.js +849 -0
  21. package/dist/core/official-api.d.ts +29 -0
  22. package/dist/core/official-api.js +205 -0
  23. package/dist/core/outlook-launcher.d.ts +54 -0
  24. package/dist/core/outlook-launcher.js +671 -0
  25. package/dist/core/process-manager.d.ts +25 -0
  26. package/dist/core/process-manager.js +133 -0
  27. package/dist/core/pure-login-service.d.ts +67 -0
  28. package/dist/core/pure-login-service.js +550 -0
  29. package/dist/core/refund-client.d.ts +102 -0
  30. package/dist/core/refund-client.js +221 -0
  31. package/dist/core/refund-service.d.ts +39 -0
  32. package/dist/core/refund-service.js +384 -0
  33. package/dist/core/sqlite-bridge.d.ts +23 -0
  34. package/dist/core/sqlite-bridge.js +207 -0
  35. package/dist/core/storage-bridge.d.ts +18 -0
  36. package/dist/core/storage-bridge.js +99 -0
  37. package/dist/core/token-parser.d.ts +35 -0
  38. package/dist/core/token-parser.js +106 -0
  39. package/dist/core/update-checker.d.ts +62 -0
  40. package/dist/core/update-checker.js +231 -0
  41. package/dist/index.d.ts +17 -0
  42. package/dist/index.js +17 -0
  43. package/dist/menu.d.ts +12 -0
  44. package/dist/menu.js +2201 -0
  45. package/dist/types.d.ts +60 -0
  46. package/dist/types.js +5 -0
  47. package/dist/utils/browser-utils.d.ts +31 -0
  48. package/dist/utils/browser-utils.js +283 -0
  49. package/dist/utils/clipboard.d.ts +5 -0
  50. package/dist/utils/clipboard.js +48 -0
  51. package/dist/utils/live-menu.d.ts +26 -0
  52. package/dist/utils/live-menu.js +169 -0
  53. package/dist/utils/plan-formatter.d.ts +30 -0
  54. package/dist/utils/plan-formatter.js +153 -0
  55. package/dist/utils/prompt-utils.d.ts +40 -0
  56. package/dist/utils/prompt-utils.js +105 -0
  57. package/dist/utils/security.d.ts +6 -0
  58. package/dist/utils/security.js +27 -0
  59. package/dist/utils/table-renderer.d.ts +7 -0
  60. package/dist/utils/table-renderer.js +88 -0
  61. package/dist/utils/time-formatter.d.ts +13 -0
  62. package/dist/utils/time-formatter.js +83 -0
  63. package/package.json +53 -0
@@ -0,0 +1,849 @@
1
+ /**
2
+ * 本地认证回调监听服务 (Local OAuth Receiver)
3
+ * 启动临时 HTTP 监听,支持跨浏览器 (Chrome/Edge/Safari/Firefox/Arc) 登录与凭据捕获
4
+ * 具备端口冲突自愈、CORS 支持、一键控制台抓取脚本、书签栏抓取与手动辅助提交
5
+ */
6
+ import http from 'node:http';
7
+ import readline from 'node:readline';
8
+ import crypto from 'node:crypto';
9
+ import chalk from 'chalk';
10
+ import boxen from 'boxen';
11
+ import { TokenParser } from './token-parser.js';
12
+ import { openBrowserUrl, openIncognitoBrowserUrl } from '../utils/browser-utils.js';
13
+ import { OfficialApiClient } from './official-api.js';
14
+ export class OAuthReceiver {
15
+ /**
16
+ * 调起无痕新浏览器实例,通过官方原生 PKCE 端点执行全自动 OAuth 登录与秒级回传
17
+ */
18
+ static async startOfficialOAuthFlow() {
19
+ const verifier = crypto.randomBytes(32).toString('base64url');
20
+ const challenge = crypto.createHash('sha256').update(verifier).digest('base64url');
21
+ const uuid = crypto.randomUUID();
22
+ const loginUrl = `https://cursor.com/loginDeepControl?challenge=${challenge}&uuid=${uuid}&mode=login&supportsSelectedTeamLogin=true`;
23
+ const pollUrl = `https://api2.cursor.sh/auth/poll?uuid=${uuid}&verifier=${verifier}`;
24
+ console.clear();
25
+ console.log(chalk.bold.cyan('\n● Cursor 官方网页 OAuth 登录 (无痕新实例 · 官方通道)\n'));
26
+ console.log(boxen([
27
+ chalk.bold.green('⚡ 正在调起全新独立无痕/隐私浏览器窗口...'),
28
+ chalk.dim('──────────────────────────────────────────────────────────────────'),
29
+ ` 授权网址 : ${chalk.bold.yellow(loginUrl)}`,
30
+ ` 窗口模式 : ${chalk.cyan('全新无痕/隐私实例')} ${chalk.dim('(纯净未登录态,隔离历史 Cookie 与旧账号)')}`,
31
+ ` 监听状态 : ${chalk.green('● 官方握手长轮询中')} ${chalk.dim('(等待网页端完成登录)')}`,
32
+ chalk.dim('──────────────────────────────────────────────────────────────────'),
33
+ ' 💡 操作指南:',
34
+ ' 1. 浏览器已自动弹出官方登录页面,支持 Google / GitHub / 邮箱登录',
35
+ ' 2. 网页端登录成功后,官方服务端将秒级自动将 Token 回传至本终端!',
36
+ ' 3. 无需手动复制任何代码或 Token,全自动纳管入库并同步余量与会员计划',
37
+ ' 4. 随时按 [0]、[q] 或 [Esc] 即可取消并安全返回',
38
+ ].join('\n'), {
39
+ padding: { top: 0, bottom: 0, left: 1, right: 1 },
40
+ borderColor: 'cyan',
41
+ borderStyle: 'round',
42
+ }));
43
+ // 调起无痕浏览器
44
+ await openIncognitoBrowserUrl(loginUrl);
45
+ return new Promise((resolve) => {
46
+ let isDone = false;
47
+ let cleanupStdin = () => { };
48
+ let pollTimer = null;
49
+ let spinnerTimer = null;
50
+ let elapsedSeconds = 0;
51
+ const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
52
+ let spinnerIdx = 0;
53
+ const finish = (result) => {
54
+ if (isDone)
55
+ return;
56
+ isDone = true;
57
+ if (pollTimer)
58
+ clearInterval(pollTimer);
59
+ if (spinnerTimer)
60
+ clearInterval(spinnerTimer);
61
+ cleanupStdin();
62
+ process.stdout.write('\r\x1b[K\x1b[?25h'); // 清理终端走字行并 100% 恢复光标可见
63
+ resolve(result);
64
+ };
65
+ // 终端单键即时退出监听
66
+ if (process.stdin.isTTY) {
67
+ readline.emitKeypressEvents(process.stdin);
68
+ const wasRaw = process.stdin.isRaw;
69
+ try {
70
+ process.stdin.setRawMode(true);
71
+ }
72
+ catch { }
73
+ process.stdin.resume();
74
+ const onKey = (str, key) => {
75
+ if (!key)
76
+ return;
77
+ if (key.ctrl && key.name === 'c') {
78
+ process.stdout.write('\x1b[?25h\n');
79
+ finish({ success: false, message: '用户已终止程序' });
80
+ process.exit(0);
81
+ }
82
+ if (key.name === 'escape' ||
83
+ key.name === 'q' ||
84
+ key.name === 'return' ||
85
+ key.name === 'enter' ||
86
+ str === '0') {
87
+ finish({ success: false, message: '已取消 OAuth 登录操作' });
88
+ }
89
+ };
90
+ process.stdin.on('keypress', onKey);
91
+ cleanupStdin = () => {
92
+ process.stdin.removeListener('keypress', onKey);
93
+ try {
94
+ if (!wasRaw) {
95
+ process.stdin.setRawMode(false);
96
+ }
97
+ }
98
+ catch { }
99
+ try {
100
+ process.stdin.pause();
101
+ }
102
+ catch { }
103
+ process.stdout.write('\x1b[?25h');
104
+ };
105
+ }
106
+ // 实时走字动画与时间提示
107
+ spinnerTimer = setInterval(() => {
108
+ if (isDone)
109
+ return;
110
+ elapsedSeconds++;
111
+ const frame = chalk.cyan(spinnerFrames[spinnerIdx++ % spinnerFrames.length]);
112
+ process.stdout.write(`\r ${frame} 正在等待网页端授权回传... (已等待 ${chalk.bold.yellow(elapsedSeconds)}s, 按 0 或 q 取消)`);
113
+ }, 1000);
114
+ // 轮询官方端点 (防重入保护)
115
+ let isPolling = false;
116
+ const poll = async () => {
117
+ if (isDone || isPolling)
118
+ return;
119
+ isPolling = true;
120
+ try {
121
+ const res = await fetch(pollUrl, {
122
+ headers: {
123
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
124
+ Accept: 'application/json',
125
+ },
126
+ signal: AbortSignal.timeout(4000),
127
+ });
128
+ if (res.status === 200) {
129
+ const data = (await res.json());
130
+ if (data && data.accessToken) {
131
+ process.stdout.write('\r\x1b[K');
132
+ console.log(chalk.green('\n✔ 官方授权完成!已成功捕获登录凭据'));
133
+ // 尝试向官方接口查询精准真实邮箱
134
+ let email;
135
+ try {
136
+ process.stdout.write(chalk.grey('正在向官方端点同步账号邮箱信息... '));
137
+ const fetched = await OfficialApiClient.fetchEmail(data.accessToken);
138
+ if (fetched) {
139
+ email = fetched;
140
+ console.log(chalk.green(`✔ ${email}`));
141
+ }
142
+ else {
143
+ console.log(chalk.grey('○ 准备解析 Token Payload'));
144
+ }
145
+ }
146
+ catch {
147
+ console.log(chalk.grey('○ 准备解析 Token Payload'));
148
+ }
149
+ if (!email) {
150
+ const parsed = TokenParser.parse(data.accessToken);
151
+ email = parsed.email || (parsed.sub ? `${parsed.sub.replace('auth0|', '')}@cursor.sh` : undefined);
152
+ }
153
+ finish({
154
+ success: true,
155
+ accessToken: data.accessToken,
156
+ refreshToken: data.refreshToken,
157
+ authId: data.authId,
158
+ selectedTeamId: data.selectedTeamId,
159
+ email,
160
+ });
161
+ return;
162
+ }
163
+ }
164
+ else if (res.status === 403) {
165
+ finish({
166
+ success: false,
167
+ message: '登录被 Cursor 官方策略拦截 (HTTP 403)',
168
+ });
169
+ return;
170
+ }
171
+ }
172
+ catch {
173
+ // 网络抖动或超时,继续轮询
174
+ }
175
+ finally {
176
+ isPolling = false;
177
+ }
178
+ };
179
+ // 每 1000ms 轮询一次
180
+ pollTimer = setInterval(poll, 1000);
181
+ // 5 分钟安全超时
182
+ setTimeout(() => {
183
+ finish({
184
+ success: false,
185
+ message: '登录超时 (5分钟未收到官方授权回传),已自动退出',
186
+ });
187
+ }, 5 * 60 * 1000);
188
+ });
189
+ }
190
+ /**
191
+ * 启动本地监听服务并调起浏览器协助登录
192
+ */
193
+ static async startBrowserLoginFlow(defaultPort = 41890) {
194
+ return new Promise((resolve) => {
195
+ let isResolved = false;
196
+ let currentPort = defaultPort;
197
+ let cleanupStdin = () => { };
198
+ const createAndListen = (portToTry) => {
199
+ const server = http.createServer((req, res) => {
200
+ // 开启跨域允许与完全禁用浏览器缓存
201
+ res.setHeader('Access-Control-Allow-Origin', '*');
202
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
203
+ res.setHeader('Access-Control-Allow-Headers', '*');
204
+ res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0');
205
+ res.setHeader('Pragma', 'no-cache');
206
+ res.setHeader('Expires', '0');
207
+ if (req.method === 'OPTIONS') {
208
+ res.writeHead(204);
209
+ res.end();
210
+ return;
211
+ }
212
+ const url = new URL(req.url || '/', `http://127.0.0.1:${portToTry}`);
213
+ // 检查用户在网页端是否点击了取消
214
+ const action = url.searchParams.get('action');
215
+ if (action === 'cancel') {
216
+ res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
217
+ res.end(`
218
+ <!DOCTYPE html>
219
+ <html lang="zh-CN">
220
+ <head>
221
+ <meta charset="utf-8">
222
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
223
+ <title>Cursor-Switch 已取消操作</title>
224
+ <style>
225
+ body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: #080c14; color: #f8fafc; display: flex; align-items: center; justify-content: center; min-height: 100vh; margin: 0; padding: 20px; }
226
+ .card { text-align: center; border: 1px solid rgba(255, 255, 255, 0.08); padding: 40px; border-radius: 20px; background: #111827; box-shadow: 0 20px 50px rgba(0,0,0,0.6); max-width: 440px; width: 100%; }
227
+ h2 { color: #f1f5f9; margin: 0 0 12px 0; font-size: 20px; font-weight: 700; }
228
+ p { color: #94a3b8; font-size: 14px; line-height: 1.6; margin: 0 0 24px 0; }
229
+ .btn-close { background: #334155; color: #fff; border: none; border-radius: 8px; padding: 10px 24px; font-size: 13px; font-weight: 600; cursor: pointer; transition: background 0.2s; }
230
+ .btn-close:hover { background: #475569; }
231
+ </style>
232
+ </head>
233
+ <body>
234
+ <div class="card">
235
+ <div style="font-size: 36px; margin-bottom: 14px;">👋</div>
236
+ <h2>已取消本次提取操作</h2>
237
+ <p>本地监听服务已安全关闭。<br>您可以关闭此标签页并切回命令行终端。</p>
238
+ <button type="button" class="btn-close" onclick="window.close()">✕ 关闭此标签页</button>
239
+ </div>
240
+ <script>setTimeout(() => { window.close(); }, 2000);</script>
241
+ </body>
242
+ </html>
243
+ `);
244
+ cleanupStdin();
245
+ if (!isResolved) {
246
+ isResolved = true;
247
+ try {
248
+ server.close();
249
+ }
250
+ catch { }
251
+ console.log(chalk.yellow('\n✔ 网页端已取消操作,正在安全返回...\n'));
252
+ resolve({ success: false, message: '用户已取消' });
253
+ }
254
+ return;
255
+ }
256
+ // 处理提交或回调中的 token 参数
257
+ const rawToken = url.searchParams.get('token') || url.searchParams.get('session');
258
+ const rawEmail = url.searchParams.get('email');
259
+ if (rawToken) {
260
+ const parsed = TokenParser.parse(rawToken);
261
+ res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
262
+ res.end(`
263
+ <!DOCTYPE html>
264
+ <html lang="zh-CN">
265
+ <head>
266
+ <meta charset="utf-8">
267
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
268
+ <title>Cursor-Switch 凭据捕获成功</title>
269
+ <style>
270
+ * { box-sizing: border-box; margin: 0; padding: 0; }
271
+ body {
272
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
273
+ background: #080c14;
274
+ color: #f8fafc;
275
+ display: flex;
276
+ align-items: center;
277
+ justify-content: center;
278
+ min-height: 100vh;
279
+ padding: 20px;
280
+ }
281
+ .card {
282
+ text-align: center;
283
+ border: 1px solid rgba(255, 255, 255, 0.08);
284
+ padding: 44px 36px;
285
+ border-radius: 20px;
286
+ background: #111827;
287
+ box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
288
+ max-width: 480px;
289
+ width: 100%;
290
+ }
291
+ .icon-circle {
292
+ width: 64px;
293
+ height: 64px;
294
+ background: rgba(16, 185, 129, 0.15);
295
+ border: 1px solid rgba(16, 185, 129, 0.3);
296
+ border-radius: 50%;
297
+ display: flex;
298
+ align-items: center;
299
+ justify-content: center;
300
+ margin: 0 auto 20px;
301
+ color: #34d399;
302
+ font-size: 32px;
303
+ box-shadow: 0 0 25px rgba(16, 185, 129, 0.25);
304
+ }
305
+ h2 { color: #ffffff; margin: 0 0 10px 0; font-size: 22px; font-weight: 700; }
306
+ p { color: #94a3b8; font-size: 14px; line-height: 1.6; margin: 0 0 22px 0; }
307
+ .email-box {
308
+ background: rgba(0, 0, 0, 0.25);
309
+ border: 1px solid rgba(255, 255, 255, 0.06);
310
+ border-radius: 10px;
311
+ padding: 10px 14px;
312
+ font-size: 13px;
313
+ color: #e2e8f0;
314
+ margin-bottom: 22px;
315
+ word-break: break-all;
316
+ }
317
+ .btn-close {
318
+ background: #2563eb;
319
+ color: #fff;
320
+ border: none;
321
+ border-radius: 8px;
322
+ padding: 10px 24px;
323
+ font-size: 13px;
324
+ font-weight: 600;
325
+ cursor: pointer;
326
+ transition: background 0.2s;
327
+ }
328
+ .btn-close:hover { background: #1d4ed8; }
329
+ .countdown { font-size: 12px; color: #64748b; margin-top: 14px; }
330
+ </style>
331
+ </head>
332
+ <body>
333
+ <div class="card">
334
+ <div class="icon-circle">✔</div>
335
+ <h2>🎉 官方 Token 捕获成功!</h2>
336
+ <p>凭据已安全传输回终端,账号已自动纳管入库。<br>终端即将自动为您激活账号与专属防封指纹。</p>
337
+ ${rawEmail || parsed.email ? `<div class="email-box">已成功收录: <b>${rawEmail || parsed.email}</b></div>` : ''}
338
+ <button type="button" class="btn-close" onclick="window.close()">✕ 关闭此标签页</button>
339
+ <div class="countdown" id="cd">本页面将在 3 秒后尝试自动关闭...</div>
340
+ </div>
341
+ <script>
342
+ let s = 3;
343
+ const timer = setInterval(() => {
344
+ s--;
345
+ const el = document.getElementById('cd');
346
+ if (el) el.textContent = '本页面将在 ' + s + ' 秒后尝试自动关闭...';
347
+ if (s <= 0) {
348
+ clearInterval(timer);
349
+ window.close();
350
+ }
351
+ }, 1000);
352
+ </script>
353
+ </body>
354
+ </html>
355
+ `);
356
+ cleanupStdin();
357
+ if (!isResolved) {
358
+ isResolved = true;
359
+ server.close();
360
+ resolve({
361
+ success: true,
362
+ token: parsed.token,
363
+ email: rawEmail || parsed.email,
364
+ workosSessionToken: parsed.workosSessionToken,
365
+ });
366
+ }
367
+ return;
368
+ }
369
+ // 浏览器访问时的引导提取与提交页面
370
+ res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
371
+ res.end(`
372
+ <!DOCTYPE html>
373
+ <html lang="zh-CN">
374
+ <head>
375
+ <meta charset="utf-8">
376
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
377
+ <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
378
+ <meta http-equiv="Pragma" content="no-cache">
379
+ <meta http-equiv="Expires" content="0">
380
+ <title>Cursor-Switch 浏览器凭据同步助手</title>
381
+ <style>
382
+ * { box-sizing: border-box; margin: 0; padding: 0; }
383
+ body {
384
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
385
+ background: #080c14;
386
+ color: #e2e8f0;
387
+ min-height: 100vh;
388
+ display: flex;
389
+ align-items: center;
390
+ justify-content: center;
391
+ padding: 24px;
392
+ line-height: 1.5;
393
+ }
394
+ .wrapper {
395
+ max-width: 540px;
396
+ width: 100%;
397
+ background: #111827;
398
+ border: 1px solid rgba(255, 255, 255, 0.08);
399
+ border-radius: 20px;
400
+ box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
401
+ overflow: hidden;
402
+ }
403
+ .header {
404
+ padding: 24px 28px 20px;
405
+ background: linear-gradient(180deg, rgba(30, 41, 59, 0.4) 0%, rgba(17, 24, 39, 0) 100%);
406
+ border-bottom: 1px solid rgba(255, 255, 255, 0.06);
407
+ }
408
+ .header-top {
409
+ display: flex;
410
+ align-items: center;
411
+ justify-content: space-between;
412
+ margin-bottom: 8px;
413
+ }
414
+ .brand {
415
+ display: flex;
416
+ align-items: center;
417
+ gap: 10px;
418
+ }
419
+ .brand-icon {
420
+ width: 32px;
421
+ height: 32px;
422
+ background: linear-gradient(135deg, #0ea5e9 0%, #3b82f6 100%);
423
+ border-radius: 9px;
424
+ display: flex;
425
+ align-items: center;
426
+ justify-content: center;
427
+ font-size: 18px;
428
+ box-shadow: 0 0 15px rgba(59, 130, 246, 0.4);
429
+ }
430
+ .brand-title {
431
+ font-size: 18px;
432
+ font-weight: 700;
433
+ color: #ffffff;
434
+ }
435
+ .status-pill {
436
+ display: flex;
437
+ align-items: center;
438
+ gap: 6px;
439
+ background: rgba(16, 185, 129, 0.12);
440
+ border: 1px solid rgba(16, 185, 129, 0.3);
441
+ color: #34d399;
442
+ font-size: 12px;
443
+ font-weight: 500;
444
+ padding: 4px 10px;
445
+ border-radius: 9999px;
446
+ }
447
+ .status-dot {
448
+ width: 7px;
449
+ height: 7px;
450
+ background: #10b981;
451
+ border-radius: 50%;
452
+ box-shadow: 0 0 8px #10b981;
453
+ animation: pulse 2s infinite;
454
+ }
455
+ @keyframes pulse {
456
+ 0%, 100% { opacity: 1; transform: scale(1); }
457
+ 50% { opacity: 0.4; transform: scale(0.85); }
458
+ }
459
+ .header-desc {
460
+ font-size: 13px;
461
+ color: #94a3b8;
462
+ }
463
+ .content {
464
+ padding: 24px 28px;
465
+ }
466
+ .step-card {
467
+ background: #161f30;
468
+ border: 1px solid rgba(255, 255, 255, 0.06);
469
+ border-radius: 14px;
470
+ padding: 18px 20px;
471
+ margin-bottom: 18px;
472
+ }
473
+ .step-header {
474
+ display: flex;
475
+ align-items: center;
476
+ gap: 12px;
477
+ margin-bottom: 12px;
478
+ }
479
+ .step-badge {
480
+ width: 24px;
481
+ height: 24px;
482
+ background: #3b82f6;
483
+ color: #fff;
484
+ font-size: 12px;
485
+ font-weight: 700;
486
+ border-radius: 50%;
487
+ display: flex;
488
+ align-items: center;
489
+ justify-content: center;
490
+ flex-shrink: 0;
491
+ }
492
+ .step-title {
493
+ font-size: 15px;
494
+ font-weight: 600;
495
+ color: #f1f5f9;
496
+ }
497
+ .step-subtext {
498
+ font-size: 12px;
499
+ color: #64748b;
500
+ margin-left: auto;
501
+ }
502
+ .step-row {
503
+ display: flex;
504
+ align-items: center;
505
+ justify-content: space-between;
506
+ gap: 14px;
507
+ background: rgba(0, 0, 0, 0.2);
508
+ border: 1px solid rgba(255, 255, 255, 0.04);
509
+ padding: 12px 14px;
510
+ border-radius: 10px;
511
+ }
512
+ .step-row-text {
513
+ font-size: 13px;
514
+ color: #cbd5e1;
515
+ }
516
+ .btn-login {
517
+ background: #2563eb;
518
+ color: #ffffff;
519
+ text-decoration: none;
520
+ font-size: 13px;
521
+ font-weight: 600;
522
+ padding: 8px 16px;
523
+ border-radius: 8px;
524
+ display: inline-flex;
525
+ align-items: center;
526
+ gap: 6px;
527
+ white-space: nowrap;
528
+ transition: all 0.2s;
529
+ }
530
+ .btn-login:hover {
531
+ background: #1d4ed8;
532
+ transform: translateY(-1px);
533
+ }
534
+ .input-form {
535
+ display: flex;
536
+ gap: 10px;
537
+ }
538
+ .token-input {
539
+ flex: 1;
540
+ background: #0a0e17;
541
+ border: 1px solid #334155;
542
+ border-radius: 10px;
543
+ padding: 12px 14px;
544
+ color: #fff;
545
+ font-size: 13px;
546
+ outline: none;
547
+ transition: all 0.2s;
548
+ }
549
+ .token-input:focus {
550
+ border-color: #3b82f6;
551
+ box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.2);
552
+ }
553
+ .btn-submit {
554
+ background: #059669;
555
+ color: #ffffff;
556
+ border: none;
557
+ font-size: 13px;
558
+ font-weight: 600;
559
+ padding: 0 20px;
560
+ border-radius: 10px;
561
+ cursor: pointer;
562
+ white-space: nowrap;
563
+ transition: all 0.2s;
564
+ }
565
+ .btn-submit:hover {
566
+ background: #047857;
567
+ transform: translateY(-1px);
568
+ }
569
+ .assistant-card {
570
+ background: rgba(0, 0, 0, 0.2);
571
+ border: 1px dashed rgba(255, 255, 255, 0.1);
572
+ border-radius: 12px;
573
+ padding: 14px 16px;
574
+ margin-top: 14px;
575
+ }
576
+ .assistant-label {
577
+ font-size: 12px;
578
+ font-weight: 600;
579
+ color: #38bdf8;
580
+ margin-bottom: 6px;
581
+ display: flex;
582
+ align-items: center;
583
+ gap: 6px;
584
+ }
585
+ .assistant-desc {
586
+ font-size: 12px;
587
+ color: #94a3b8;
588
+ line-height: 1.5;
589
+ margin-bottom: 10px;
590
+ }
591
+ .copy-btn {
592
+ background: #1e293b;
593
+ color: #e2e8f0;
594
+ border: 1px solid rgba(255, 255, 255, 0.1);
595
+ font-size: 12px;
596
+ font-weight: 600;
597
+ padding: 9px 14px;
598
+ border-radius: 8px;
599
+ cursor: pointer;
600
+ transition: all 0.2s;
601
+ width: 100%;
602
+ display: flex;
603
+ align-items: center;
604
+ justify-content: center;
605
+ gap: 6px;
606
+ }
607
+ .copy-btn:hover {
608
+ background: #334155;
609
+ border-color: rgba(255, 255, 255, 0.2);
610
+ }
611
+ .copy-btn.copied {
612
+ background: #059669;
613
+ color: #fff;
614
+ border-color: #059669;
615
+ }
616
+ .cancel-link {
617
+ color: #64748b;
618
+ font-size: 12px;
619
+ text-decoration: none;
620
+ padding: 6px 14px;
621
+ border-radius: 6px;
622
+ border: 1px solid rgba(255, 255, 255, 0.06);
623
+ display: inline-flex;
624
+ align-items: center;
625
+ gap: 4px;
626
+ transition: all 0.2s;
627
+ }
628
+ .cancel-link:hover {
629
+ color: #cbd5e1;
630
+ border-color: rgba(255, 255, 255, 0.2);
631
+ }
632
+ .toast {
633
+ position: fixed;
634
+ top: 24px;
635
+ left: 50%;
636
+ transform: translateX(-50%) translateY(-20px);
637
+ background: #1e293b;
638
+ color: #f8fafc;
639
+ border: 1px solid rgba(255, 255, 255, 0.15);
640
+ padding: 10px 20px;
641
+ border-radius: 30px;
642
+ font-size: 13px;
643
+ font-weight: 500;
644
+ box-shadow: 0 10px 30px rgba(0,0,0,0.5);
645
+ opacity: 0;
646
+ pointer-events: none;
647
+ transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
648
+ z-index: 9999;
649
+ display: flex;
650
+ align-items: center;
651
+ gap: 8px;
652
+ }
653
+ .toast.show {
654
+ opacity: 1;
655
+ transform: translateX(-50%) translateY(0);
656
+ }
657
+ </style>
658
+ </head>
659
+ <body>
660
+ <div class="wrapper">
661
+ <div class="header">
662
+ <div class="header-top">
663
+ <div class="brand">
664
+ <div class="brand-icon">⚡</div>
665
+ <div class="brand-title">Cursor-Switch 凭据同步助手</div>
666
+ </div>
667
+ <div class="status-pill">
668
+ <span class="status-dot"></span>
669
+ <span>终端等待接收中</span>
670
+ </div>
671
+ </div>
672
+ <p class="header-desc">只需简单两步,即可将浏览器中的 Cursor 登录凭据安全同步至本地终端:</p>
673
+ </div>
674
+
675
+ <div class="content">
676
+ <!-- 步骤 1 -->
677
+ <div class="step-card">
678
+ <div class="step-header">
679
+ <div class="step-badge">1</div>
680
+ <div class="step-title">确认浏览器已登录官网</div>
681
+ <div class="step-subtext">若已登录请直接看第 2 步</div>
682
+ </div>
683
+ <div class="step-row">
684
+ <div class="step-row-text">在当前浏览器中打开 Cursor 官网并完成账号登录</div>
685
+ <a href="https://cursor.com/login" target="_blank" class="btn-login">
686
+ <span>打开官网登录</span>
687
+ <span>↗</span>
688
+ </a>
689
+ </div>
690
+ </div>
691
+
692
+ <!-- 步骤 2 -->
693
+ <div class="step-card">
694
+ <div class="step-header">
695
+ <div class="step-badge">2</div>
696
+ <div class="step-title">同步 Token 凭据给终端</div>
697
+ <div class="step-subtext">直接输入即可</div>
698
+ </div>
699
+
700
+ <form method="GET" action="/" class="input-form">
701
+ <input type="text" name="token" class="token-input" placeholder="请在此粘贴 Token (支持 user_xxx:: 或 eyJ...)" required autofocus autocomplete="off" spellcheck="false" />
702
+ <button type="submit" class="btn-submit">立即同步 🚀</button>
703
+ </form>
704
+
705
+ <!-- 极客辅助:不想手动复制 -->
706
+ <div class="assistant-card">
707
+ <div class="assistant-label">💡 极客自动提取(不想手动找 Cookie?):</div>
708
+ <p class="assistant-desc">切到已登录的 cursor.com 网页,按 <b>F12</b> 打开控制台(Console)粘贴此代码回车,凭据将全自动秒传回终端:</p>
709
+ <button type="button" class="copy-btn" id="copyBtn" onclick="onCopyScript()">
710
+ 📋 复制控制台一键抓取代码
711
+ </button>
712
+ </div>
713
+ </div>
714
+
715
+ <!-- 底部取消 -->
716
+ <div style="text-align: center; margin-top: 14px;">
717
+ <a href="/?action=cancel" class="cancel-link">
718
+ ✕ 取消本次操作并返回终端
719
+ </a>
720
+ </div>
721
+ </div>
722
+ </div>
723
+
724
+ <div class="toast" id="toast"></div>
725
+
726
+ <script>
727
+ function showToast(msg, icon = '💡') {
728
+ const t = document.getElementById('toast');
729
+ t.innerHTML = '<span>' + icon + '</span><span>' + msg + '</span>';
730
+ t.classList.add('show');
731
+ setTimeout(() => { t.classList.remove('show'); }, 3500);
732
+ }
733
+
734
+ function onCopyScript() {
735
+ const code = "fetch('http://127.0.0.1:${portToTry}/?token=' + encodeURIComponent((document.cookie.match(/WorkosCursorSessionToken=([^;]+)/)||[,''])[1])).then(() => alert('🎉 凭据已成功同步回终端!请返回命令行查看。')).catch(() => alert('未找到登录 Cookie,请先在 cursor.com 登录!'));";
736
+ navigator.clipboard.writeText(code).then(() => {
737
+ const btn = document.getElementById('copyBtn');
738
+ btn.classList.add('copied');
739
+ btn.innerHTML = '✔ 代码已复制到剪贴板!';
740
+ showToast('代码已复制!请切到 cursor.com 网页按 F12 粘贴并回车即可。', '📋');
741
+ setTimeout(() => {
742
+ btn.classList.remove('copied');
743
+ btn.innerHTML = '📋 复制控制台一键抓取代码';
744
+ }, 3500);
745
+ }).catch(() => {
746
+ prompt('请手动复制以下代码:', code);
747
+ });
748
+ }
749
+ </script>
750
+ </body>
751
+ </html>
752
+ `);
753
+ });
754
+ server.on('error', (err) => {
755
+ cleanupStdin();
756
+ if (err.code === 'EADDRINUSE' && portToTry < defaultPort + 10) {
757
+ currentPort = portToTry + 1;
758
+ createAndListen(currentPort);
759
+ }
760
+ else {
761
+ if (!isResolved) {
762
+ isResolved = true;
763
+ resolve({
764
+ success: false,
765
+ message: `端口监听失败: ${err.message}`,
766
+ });
767
+ }
768
+ }
769
+ });
770
+ server.listen(portToTry, '127.0.0.1', async () => {
771
+ currentPort = portToTry;
772
+ const helperUrl = `http://127.0.0.1:${currentPort}/?_ts=${Date.now()}`;
773
+ console.log(chalk.cyan(`\n正在启动本地安全认证监听服务 (${helperUrl})...`));
774
+ console.log(chalk.grey('已尝试调起系统默认浏览器。若未自动弹出,可在任意浏览器访问下方地址:'));
775
+ console.log(chalk.bold.yellow(`👉 ${helperUrl}\n`));
776
+ console.log(chalk.dim(' 💡 提示: 在终端随时按 [Esc]、[q]、[0] 或 [回车] 即可立即停止监听并返回\n'));
777
+ // 终端单键即时监听,支持 Esc, q, Enter, 0
778
+ if (process.stdin.isTTY) {
779
+ readline.emitKeypressEvents(process.stdin);
780
+ const wasRaw = process.stdin.isRaw;
781
+ try {
782
+ process.stdin.setRawMode(true);
783
+ }
784
+ catch { }
785
+ process.stdin.resume();
786
+ const onKey = (str, key) => {
787
+ if (!key)
788
+ return;
789
+ if (key.ctrl && key.name === 'c') {
790
+ cleanupStdin();
791
+ process.stdout.write('\x1b[?25h\n');
792
+ process.exit(0);
793
+ }
794
+ if (key.name === 'escape' ||
795
+ key.name === 'q' ||
796
+ key.name === 'return' ||
797
+ key.name === 'enter' ||
798
+ str === '0') {
799
+ cleanupStdin();
800
+ if (!isResolved) {
801
+ isResolved = true;
802
+ try {
803
+ server.close();
804
+ }
805
+ catch { }
806
+ console.log(chalk.yellow('\n✔ 已停止监听,正在返回...\n'));
807
+ resolve({ success: false, message: '用户已取消' });
808
+ }
809
+ }
810
+ };
811
+ process.stdin.on('keypress', onKey);
812
+ cleanupStdin = () => {
813
+ process.stdin.removeListener('keypress', onKey);
814
+ try {
815
+ if (!wasRaw) {
816
+ process.stdin.setRawMode(false);
817
+ }
818
+ }
819
+ catch { }
820
+ try {
821
+ process.stdin.pause();
822
+ }
823
+ catch { }
824
+ process.stdout.write('\x1b[?25h');
825
+ };
826
+ }
827
+ // 调起系统默认浏览器访问本地认证助手页
828
+ await openBrowserUrl(helperUrl);
829
+ });
830
+ // 5 分钟超时保护
831
+ setTimeout(() => {
832
+ cleanupStdin();
833
+ if (!isResolved) {
834
+ isResolved = true;
835
+ try {
836
+ server.close();
837
+ }
838
+ catch { }
839
+ resolve({
840
+ success: false,
841
+ message: '登录超时(5分钟未接收到回调),已自动关闭监听',
842
+ });
843
+ }
844
+ }, 5 * 60 * 1000);
845
+ };
846
+ createAndListen(defaultPort);
847
+ });
848
+ }
849
+ }