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.
- package/LICENSE +21 -0
- package/README.md +161 -0
- package/SECURITY.md +38 -0
- package/bin/cswitch.js +35 -0
- package/dist/cli.d.ts +6 -0
- package/dist/cli.js +850 -0
- package/dist/core/account-store.d.ts +25 -0
- package/dist/core/account-store.js +182 -0
- package/dist/core/cdp-engine.d.ts +75 -0
- package/dist/core/cdp-engine.js +320 -0
- package/dist/core/config-store.d.ts +38 -0
- package/dist/core/config-store.js +100 -0
- package/dist/core/cursor-paths.d.ts +22 -0
- package/dist/core/cursor-paths.js +287 -0
- package/dist/core/dashboard-launcher.d.ts +34 -0
- package/dist/core/dashboard-launcher.js +210 -0
- package/dist/core/fingerprint.d.ts +7 -0
- package/dist/core/fingerprint.js +34 -0
- package/dist/core/oauth-receiver.d.ts +31 -0
- package/dist/core/oauth-receiver.js +849 -0
- package/dist/core/official-api.d.ts +29 -0
- package/dist/core/official-api.js +205 -0
- package/dist/core/outlook-launcher.d.ts +54 -0
- package/dist/core/outlook-launcher.js +671 -0
- package/dist/core/process-manager.d.ts +25 -0
- package/dist/core/process-manager.js +133 -0
- package/dist/core/pure-login-service.d.ts +67 -0
- package/dist/core/pure-login-service.js +550 -0
- package/dist/core/refund-client.d.ts +102 -0
- package/dist/core/refund-client.js +221 -0
- package/dist/core/refund-service.d.ts +39 -0
- package/dist/core/refund-service.js +384 -0
- package/dist/core/sqlite-bridge.d.ts +23 -0
- package/dist/core/sqlite-bridge.js +207 -0
- package/dist/core/storage-bridge.d.ts +18 -0
- package/dist/core/storage-bridge.js +99 -0
- package/dist/core/token-parser.d.ts +35 -0
- package/dist/core/token-parser.js +106 -0
- package/dist/core/update-checker.d.ts +62 -0
- package/dist/core/update-checker.js +231 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/menu.d.ts +12 -0
- package/dist/menu.js +2201 -0
- package/dist/types.d.ts +60 -0
- package/dist/types.js +5 -0
- package/dist/utils/browser-utils.d.ts +31 -0
- package/dist/utils/browser-utils.js +283 -0
- package/dist/utils/clipboard.d.ts +5 -0
- package/dist/utils/clipboard.js +48 -0
- package/dist/utils/live-menu.d.ts +26 -0
- package/dist/utils/live-menu.js +169 -0
- package/dist/utils/plan-formatter.d.ts +30 -0
- package/dist/utils/plan-formatter.js +153 -0
- package/dist/utils/prompt-utils.d.ts +40 -0
- package/dist/utils/prompt-utils.js +105 -0
- package/dist/utils/security.d.ts +6 -0
- package/dist/utils/security.js +27 -0
- package/dist/utils/table-renderer.d.ts +7 -0
- package/dist/utils/table-renderer.js +88 -0
- package/dist/utils/time-formatter.d.ts +13 -0
- package/dist/utils/time-formatter.js +83 -0
- package/package.json +53 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,850 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Commander 纯命令行入口
|
|
3
|
+
* 支持非交互式 CLI Flags 与一键极速命令
|
|
4
|
+
*/
|
|
5
|
+
import { Command } from 'commander';
|
|
6
|
+
import chalk from 'chalk';
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import { AccountStore } from './core/account-store.js';
|
|
9
|
+
import { resolveCursorPaths } from './core/cursor-paths.js';
|
|
10
|
+
import { StorageBridge } from './core/storage-bridge.js';
|
|
11
|
+
import { SqliteBridge } from './core/sqlite-bridge.js';
|
|
12
|
+
import { ProcessManager } from './core/process-manager.js';
|
|
13
|
+
import { DashboardLauncher } from './core/dashboard-launcher.js';
|
|
14
|
+
import { OutlookLauncher } from './core/outlook-launcher.js';
|
|
15
|
+
import { OAuthReceiver } from './core/oauth-receiver.js';
|
|
16
|
+
import { TokenParser } from './core/token-parser.js';
|
|
17
|
+
import { generateMachineIds, formatMachineIdSummary } from './core/fingerprint.js';
|
|
18
|
+
import { OfficialApiClient } from './core/official-api.js';
|
|
19
|
+
import { renderAccountTable } from './utils/table-renderer.js';
|
|
20
|
+
import { getExpiryStatus } from './utils/time-formatter.js';
|
|
21
|
+
import { maskToken } from './utils/security.js';
|
|
22
|
+
import { formatPlanName, formatPlanBadge } from './utils/plan-formatter.js';
|
|
23
|
+
import { runInteractiveMenu, handleAccountCleanupMenu } from './menu.js';
|
|
24
|
+
import { RefundService } from './core/refund-service.js';
|
|
25
|
+
import { PureLoginService } from './core/pure-login-service.js';
|
|
26
|
+
import { UpdateChecker, CURRENT_VERSION } from './core/update-checker.js';
|
|
27
|
+
import boxen from 'boxen';
|
|
28
|
+
import { copyToClipboard } from './utils/clipboard.js';
|
|
29
|
+
import { safeInput } from './utils/prompt-utils.js';
|
|
30
|
+
export function createCli() {
|
|
31
|
+
const program = new Command();
|
|
32
|
+
program
|
|
33
|
+
.name('cswitch')
|
|
34
|
+
.description('Cursor IDE 极速、安全、纯本地运行的多账号与防封虚拟机器指纹管理工具')
|
|
35
|
+
.version(CURRENT_VERSION)
|
|
36
|
+
.action(async () => {
|
|
37
|
+
// 无参数时默认进入交互式主菜单
|
|
38
|
+
await runInteractiveMenu();
|
|
39
|
+
});
|
|
40
|
+
// 1. current / whoami
|
|
41
|
+
program
|
|
42
|
+
.command('current')
|
|
43
|
+
.alias('whoami')
|
|
44
|
+
.description('查看当前 Cursor IDE 本地实际激活的账号与机器指纹')
|
|
45
|
+
.action(async () => {
|
|
46
|
+
const paths = resolveCursorPaths();
|
|
47
|
+
const storage = new StorageBridge(paths.storageJson);
|
|
48
|
+
const sqlite = new SqliteBridge(paths.sqliteDb);
|
|
49
|
+
const store = new AccountStore();
|
|
50
|
+
const email = storage.readCurrentEmail() || (await sqlite.readEmail()) || '未登录';
|
|
51
|
+
const token = storage.readCurrentToken() || (await sqlite.readToken()) || '';
|
|
52
|
+
const machineIds = storage.readMachineIds();
|
|
53
|
+
const acc = store.getByEmail(email);
|
|
54
|
+
const expiry = getExpiryStatus(acc?.current_period_usage?.billingCycleEnd);
|
|
55
|
+
console.log(chalk.bold.cyan('\n● 当前 Cursor 本地激活态:'));
|
|
56
|
+
console.log(`• 登录邮箱 : ${chalk.bold.white(email)}`);
|
|
57
|
+
console.log(`• 会员级别 : ${formatPlanBadge(acc?.subscription_type)}`);
|
|
58
|
+
console.log(`• 具体到期 : ${chalk.blue(expiry.formattedDate)} (${expiry.countdownText})`);
|
|
59
|
+
console.log(`• 凭据 Token : ${chalk.grey(maskToken(token))}`);
|
|
60
|
+
console.log(`• 机器指纹 : ${chalk.magenta(formatMachineIdSummary(machineIds))}`);
|
|
61
|
+
console.log(`• Cursor进程 : ${ProcessManager.isCursorRunning() ? chalk.green('● 运行中') : chalk.grey('○ 未运行')}\n`);
|
|
62
|
+
});
|
|
63
|
+
// 2. list / ls
|
|
64
|
+
program
|
|
65
|
+
.command('list')
|
|
66
|
+
.alias('ls')
|
|
67
|
+
.description('列出本地保存的 Cursor 账号、高速请求余量与具体到期时间')
|
|
68
|
+
.option('-r, --refresh', '强制直连 Cursor 官方接口刷新最新剩余额度')
|
|
69
|
+
.option('-s, --search <query>', '按邮箱或别名模糊搜索过滤')
|
|
70
|
+
.option('-l, --limit <number>', '限制展示数量 (默认展示前 20 条,使用 -l 0 展示全部)')
|
|
71
|
+
.action(async (options) => {
|
|
72
|
+
const store = new AccountStore();
|
|
73
|
+
let accounts = store.loadAll();
|
|
74
|
+
if (options.search) {
|
|
75
|
+
const q = String(options.search).toLowerCase();
|
|
76
|
+
accounts = accounts.filter((a) => a.email.toLowerCase().includes(q) || (a.username && a.username.toLowerCase().includes(q)));
|
|
77
|
+
}
|
|
78
|
+
if (options.refresh) {
|
|
79
|
+
console.log(chalk.cyan('正在直连 Cursor 官方接口同步最新余量...'));
|
|
80
|
+
for (const acc of accounts) {
|
|
81
|
+
if (acc.token) {
|
|
82
|
+
const res = await OfficialApiClient.fetchUsage(acc.token);
|
|
83
|
+
if (res.success) {
|
|
84
|
+
acc.subscription_type = res.subscriptionType || acc.subscription_type;
|
|
85
|
+
acc.current_period_usage = res.currentPeriodUsage || acc.current_period_usage;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
store.saveAll(accounts);
|
|
90
|
+
}
|
|
91
|
+
const totalCount = accounts.length;
|
|
92
|
+
const limit = options.limit !== undefined ? Number(options.limit) : 20;
|
|
93
|
+
let displayAccounts = accounts;
|
|
94
|
+
if (limit > 0 && accounts.length > limit) {
|
|
95
|
+
// 优先将当前在用的账号排在最前面
|
|
96
|
+
displayAccounts = [
|
|
97
|
+
...accounts.filter((a) => a.is_current),
|
|
98
|
+
...accounts.filter((a) => !a.is_current),
|
|
99
|
+
].slice(0, limit);
|
|
100
|
+
}
|
|
101
|
+
console.log('\n' + renderAccountTable(displayAccounts));
|
|
102
|
+
if (limit > 0 && totalCount > limit) {
|
|
103
|
+
console.log(chalk.grey(` 提示: 当前展示前 ${limit} 个账号 (本地总计 ${totalCount} 个)。查看全部请加参数: cswitch list -l 0 或模糊搜索: cswitch list -s <关键词>\n`));
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
// 3. use / switch
|
|
107
|
+
program
|
|
108
|
+
.command('use <identifier>')
|
|
109
|
+
.alias('switch')
|
|
110
|
+
.description('一键切换到指定账号 (支持输入邮箱、邮箱关键词或序号)')
|
|
111
|
+
.option('--no-restart', '换号后不自动唤起 Cursor')
|
|
112
|
+
.action(async (identifier, options) => {
|
|
113
|
+
const store = new AccountStore();
|
|
114
|
+
let targetAcc = store.getByIndexOrEmail(identifier);
|
|
115
|
+
if (!targetAcc) {
|
|
116
|
+
const parsed = TokenParser.parse(identifier);
|
|
117
|
+
if (parsed.isValid) {
|
|
118
|
+
console.log(chalk.cyan('\n检测到直接传入了 Token 凭据,正在同步并极速换号...'));
|
|
119
|
+
let email = parsed.email;
|
|
120
|
+
if (!email) {
|
|
121
|
+
try {
|
|
122
|
+
email = (await OfficialApiClient.fetchEmail(parsed.token)) || undefined;
|
|
123
|
+
}
|
|
124
|
+
catch { }
|
|
125
|
+
}
|
|
126
|
+
if (!email) {
|
|
127
|
+
email = parsed.sub ? `${parsed.sub.replace('auth0|', '')}@cursor.sh` : `account_${Date.now().toString().slice(-6)}@cursor.sh`;
|
|
128
|
+
}
|
|
129
|
+
const usageRes = await OfficialApiClient.fetchUsage(parsed.token);
|
|
130
|
+
const subType = usageRes.success && usageRes.subscriptionType ? usageRes.subscriptionType : 'free';
|
|
131
|
+
const machineIds = generateMachineIds();
|
|
132
|
+
targetAcc = {
|
|
133
|
+
email,
|
|
134
|
+
token: parsed.token,
|
|
135
|
+
workos_cursor_session_token: parsed.workosSessionToken || parsed.token,
|
|
136
|
+
subscription_type: subType,
|
|
137
|
+
current_period_usage: usageRes.currentPeriodUsage,
|
|
138
|
+
machine_ids: machineIds,
|
|
139
|
+
};
|
|
140
|
+
store.addOrUpdate(targetAcc);
|
|
141
|
+
await copyToClipboard(targetAcc.token);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
console.log(chalk.red(`\n未找到匹配账号或有效 Token: "${identifier}",请使用 cswitch list 查看当前账号列表。\n`));
|
|
145
|
+
process.exit(1);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const paths = resolveCursorPaths();
|
|
149
|
+
const storage = new StorageBridge(paths.storageJson);
|
|
150
|
+
const sqlite = new SqliteBridge(paths.sqliteDb);
|
|
151
|
+
console.log(chalk.cyan(`\n正在执行换号流程,目标: ${chalk.bold.white(targetAcc.email)} ...`));
|
|
152
|
+
if (ProcessManager.isCursorRunning()) {
|
|
153
|
+
console.log(chalk.grey('✔ [1/4] 检测到 Cursor 运行中 -> 发送退出信号,释放 SQLite 独占锁'));
|
|
154
|
+
await ProcessManager.closeCursor();
|
|
155
|
+
}
|
|
156
|
+
storage.createBackup();
|
|
157
|
+
sqlite.createBackup();
|
|
158
|
+
console.log(chalk.grey('✔ [2/4] 创建本地安全快照备份'));
|
|
159
|
+
const machineIds = targetAcc.machine_ids || generateMachineIds();
|
|
160
|
+
storage.writeMachineIds(machineIds);
|
|
161
|
+
console.log(chalk.grey(`✔ [3/4] 换载该账号专属机器指纹: ${formatMachineIdSummary(machineIds)}`));
|
|
162
|
+
const cleanToken = TokenParser.extractTokenPart(targetAcc.token);
|
|
163
|
+
const refreshToken = targetAcc.workos_cursor_session_token || cleanToken;
|
|
164
|
+
storage.writeAuth(targetAcc.email, cleanToken, refreshToken);
|
|
165
|
+
await sqlite.injectAuth(targetAcc.email, cleanToken, refreshToken);
|
|
166
|
+
store.markCurrent(targetAcc.email);
|
|
167
|
+
console.log(chalk.grey('✔ [4/4] 原子注入认证凭据到 SQLite 数据库'));
|
|
168
|
+
console.log(chalk.green(`\n✔ 换号成功!当前已激活: ${chalk.bold.white(targetAcc.email)}\n`));
|
|
169
|
+
if (options.restart !== false && paths.executablePath) {
|
|
170
|
+
ProcessManager.launchCursor(paths.executablePath);
|
|
171
|
+
console.log(chalk.green('✔ Cursor 已在后台启动\n'));
|
|
172
|
+
}
|
|
173
|
+
});
|
|
174
|
+
// 4. add
|
|
175
|
+
program
|
|
176
|
+
.command('add [token]')
|
|
177
|
+
.description('添加 Cursor 账号 (支持直接粘贴 user_xxx::eyJ... / eyJ...、纯净代登或浏览器登录)')
|
|
178
|
+
.option('-p, --pure', '使用海外 S 级纯净住宅 IP 自动代登并提取 Token')
|
|
179
|
+
.option('-b, --browser', '通过浏览器 OAuth 登录并自动截获授权凭据')
|
|
180
|
+
.option('-e, --email <email>', '指定邮箱')
|
|
181
|
+
.action(async (rawToken, options) => {
|
|
182
|
+
const store = new AccountStore();
|
|
183
|
+
const paths = resolveCursorPaths();
|
|
184
|
+
const storage = new StorageBridge(paths.storageJson);
|
|
185
|
+
const sqlite = new SqliteBridge(paths.sqliteDb);
|
|
186
|
+
if (options?.pure) {
|
|
187
|
+
const pureService = new PureLoginService();
|
|
188
|
+
await pureService.runInteractiveFlow(store, storage, sqlite, paths.executablePath);
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
if (options?.browser) {
|
|
192
|
+
console.log(chalk.cyan('\n正在启动本地安全认证监听端口 (127.0.0.1:41890)...'));
|
|
193
|
+
console.log(chalk.grey('已调起系统默认浏览器访问 Cursor 登录页,请在网页中完成登录...'));
|
|
194
|
+
const result = await OAuthReceiver.startBrowserLoginFlow();
|
|
195
|
+
if (result.success && result.token) {
|
|
196
|
+
store.addOrUpdate({
|
|
197
|
+
email: result.email || 'browser_user@cursor.sh',
|
|
198
|
+
token: result.token,
|
|
199
|
+
workos_cursor_session_token: result.workosSessionToken,
|
|
200
|
+
});
|
|
201
|
+
console.log(chalk.green(`\n✔ 登录成功!已自动收录账号: ${result.email || result.token}\n`));
|
|
202
|
+
}
|
|
203
|
+
else if (result.message && !result.message.includes('取消')) {
|
|
204
|
+
console.log(chalk.red(`\n登录失败: ${result.message}\n`));
|
|
205
|
+
}
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
if (!rawToken) {
|
|
209
|
+
console.log(chalk.yellow('\n请提供 Token 或使用选项,例:'));
|
|
210
|
+
console.log(chalk.grey(' cswitch add --pure # 海外纯净住宅 IP 自动代登'));
|
|
211
|
+
console.log(chalk.grey(' cswitch add --browser # 系统浏览器辅助录入'));
|
|
212
|
+
console.log(chalk.grey(' cswitch add "user_xxx::eyJ..." # 直接粘贴 Token\n'));
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
const parsed = TokenParser.parse(rawToken);
|
|
216
|
+
if (!parsed.isValid) {
|
|
217
|
+
console.log(chalk.red('\n未识别到有效的 Token 格式,仅支持 user_xxx::eyJ... 或 eyJ... 格式。\n'));
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
const fallbackEmail = parsed.sub ? `${parsed.sub.replace('auth0|', '')}@cursor.sh` : `account_${Date.now().toString().slice(-6)}@cursor.sh`;
|
|
221
|
+
const email = options?.email || parsed.email || fallbackEmail;
|
|
222
|
+
store.addOrUpdate({
|
|
223
|
+
email,
|
|
224
|
+
token: parsed.token,
|
|
225
|
+
workos_cursor_session_token: parsed.workosSessionToken,
|
|
226
|
+
});
|
|
227
|
+
console.log(chalk.green(`\n✔ 录入成功!账号 ${chalk.bold.white(email)} 已加入本地库。\n`));
|
|
228
|
+
});
|
|
229
|
+
// 4.1 pure (纯净云端代登直达命令)
|
|
230
|
+
program
|
|
231
|
+
.command('pure [account]')
|
|
232
|
+
.alias('cloud-login')
|
|
233
|
+
.description('基于海外 S 级纯净住宅 IP 自动代登 Cursor,自动收发验证码并秒提官方 Token 入库')
|
|
234
|
+
.action(async () => {
|
|
235
|
+
const paths = resolveCursorPaths();
|
|
236
|
+
const storage = new StorageBridge(paths.storageJson);
|
|
237
|
+
const sqlite = new SqliteBridge(paths.sqliteDb);
|
|
238
|
+
const store = new AccountStore();
|
|
239
|
+
const pureService = new PureLoginService();
|
|
240
|
+
await pureService.runInteractiveFlow(store, storage, sqlite, paths.executablePath);
|
|
241
|
+
});
|
|
242
|
+
// 4.2 oauth (全新无痕浏览器官方 OAuth 网页登录)
|
|
243
|
+
program
|
|
244
|
+
.command('oauth')
|
|
245
|
+
.alias('browser-login')
|
|
246
|
+
.description('调起全新独立无痕/隐私浏览器进行 Cursor 官方网页 OAuth 登录,并全自动纳管入库')
|
|
247
|
+
.action(async () => {
|
|
248
|
+
const paths = resolveCursorPaths();
|
|
249
|
+
const storage = new StorageBridge(paths.storageJson);
|
|
250
|
+
const sqlite = new SqliteBridge(paths.sqliteDb);
|
|
251
|
+
const store = new AccountStore();
|
|
252
|
+
const result = await OAuthReceiver.startOfficialOAuthFlow();
|
|
253
|
+
if (!result.success || !result.accessToken) {
|
|
254
|
+
if (result.message) {
|
|
255
|
+
console.log(chalk.yellow(`\n${result.message}\n`));
|
|
256
|
+
}
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
let email = result.email;
|
|
260
|
+
if (!email) {
|
|
261
|
+
const fallback = result.authId
|
|
262
|
+
? `${result.authId.replace('auth0|', '')}@cursor.sh`
|
|
263
|
+
: `account_${Date.now().toString().slice(-6)}@cursor.sh`;
|
|
264
|
+
const inputEmail = await safeInput({
|
|
265
|
+
message: '未能自动解析到邮箱,请输入该账号的登录邮箱:',
|
|
266
|
+
default: fallback,
|
|
267
|
+
});
|
|
268
|
+
email = inputEmail || fallback;
|
|
269
|
+
}
|
|
270
|
+
const machineIds = generateMachineIds();
|
|
271
|
+
console.log(chalk.grey('\n正在向 Cursor 官方同步用量与订阅计划...'));
|
|
272
|
+
const usageRes = await OfficialApiClient.fetchUsage(result.accessToken);
|
|
273
|
+
const existing = store.getByEmail(email);
|
|
274
|
+
const subType = usageRes.success && usageRes.subscriptionType
|
|
275
|
+
? usageRes.subscriptionType
|
|
276
|
+
: existing?.subscription_type || 'free';
|
|
277
|
+
const newAccount = {
|
|
278
|
+
email,
|
|
279
|
+
token: result.accessToken,
|
|
280
|
+
workos_cursor_session_token: result.refreshToken || result.accessToken,
|
|
281
|
+
subscription_type: subType,
|
|
282
|
+
current_period_usage: usageRes.currentPeriodUsage || existing?.current_period_usage,
|
|
283
|
+
machine_ids: machineIds,
|
|
284
|
+
};
|
|
285
|
+
store.addOrUpdate(newAccount);
|
|
286
|
+
const copied = await copyToClipboard(newAccount.token);
|
|
287
|
+
console.log(boxen([
|
|
288
|
+
chalk.bold.green('🎉 官方 OAuth 登录成功!账号已自动收录入库'),
|
|
289
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
290
|
+
` 登录邮箱 : ${chalk.bold.white(newAccount.email)}`,
|
|
291
|
+
` 会员级别 : ${formatPlanBadge(newAccount.subscription_type)}`,
|
|
292
|
+
` 专属指纹 : ${chalk.cyan(formatMachineIdSummary(machineIds))} ${chalk.dim('(独立隔离防封)')}`,
|
|
293
|
+
` 官方凭据 : ${chalk.grey(newAccount.token.slice(0, 16))}...[已加密归档]`,
|
|
294
|
+
` 系统剪贴板: ${copied ? chalk.green('✔ Token 已自动复制到系统剪贴板 (可直接粘贴)') : chalk.yellow('○ 请手动复制')}`,
|
|
295
|
+
].join('\n'), { padding: { top: 0, bottom: 0, left: 1, right: 1 }, borderColor: 'green', borderStyle: 'round' }));
|
|
296
|
+
console.log(chalk.grey(`\n提示: 可随时使用 cswitch use ${newAccount.email} 切换到该账号。\n`));
|
|
297
|
+
});
|
|
298
|
+
// 4.2 token
|
|
299
|
+
program
|
|
300
|
+
.command('token [inputToken]')
|
|
301
|
+
.alias('browser-token')
|
|
302
|
+
.description('提取或录入官方 Token (若本机已登则自动秒提复制,未登调起无痕浏览器 OAuth 登录)')
|
|
303
|
+
.action(async (inputToken) => {
|
|
304
|
+
const paths = resolveCursorPaths();
|
|
305
|
+
const storage = new StorageBridge(paths.storageJson);
|
|
306
|
+
const sqlite = new SqliteBridge(paths.sqliteDb);
|
|
307
|
+
const store = new AccountStore();
|
|
308
|
+
// 如果命令行直接提供了 Token 参数
|
|
309
|
+
if (inputToken && inputToken.trim()) {
|
|
310
|
+
const rawToken = inputToken.trim();
|
|
311
|
+
const parsed = TokenParser.parse(rawToken);
|
|
312
|
+
if (!parsed.isValid) {
|
|
313
|
+
console.log(chalk.red('\n未识别到有效的 Token 格式,仅支持 user_xxx:: 或 eyJ... 格式。\n'));
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
const machineIds = generateMachineIds();
|
|
317
|
+
console.log(chalk.grey('\n正在同步官方用量与订阅计划...'));
|
|
318
|
+
const usageRes = await OfficialApiClient.fetchUsage(parsed.token);
|
|
319
|
+
const fallbackEmail = parsed.sub ? `${parsed.sub.replace('auth0|', '')}@cursor.sh` : `account_${Date.now().toString().slice(-6)}@cursor.sh`;
|
|
320
|
+
const email = parsed.email || fallbackEmail;
|
|
321
|
+
const existing = store.getByEmail(email);
|
|
322
|
+
const newAccount = {
|
|
323
|
+
email,
|
|
324
|
+
token: rawToken,
|
|
325
|
+
workos_cursor_session_token: parsed.workosSessionToken || rawToken,
|
|
326
|
+
subscription_type: usageRes.success && usageRes.subscriptionType ? usageRes.subscriptionType : (existing?.subscription_type || 'free'),
|
|
327
|
+
current_period_usage: usageRes.currentPeriodUsage || existing?.current_period_usage,
|
|
328
|
+
machine_ids: machineIds,
|
|
329
|
+
};
|
|
330
|
+
store.addOrUpdate(newAccount);
|
|
331
|
+
const copied = await copyToClipboard(newAccount.token);
|
|
332
|
+
console.log(boxen([
|
|
333
|
+
chalk.bold.green('✔ 账号 Token 录入成功!已加入本地多账号库'),
|
|
334
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
335
|
+
` 登录邮箱 : ${chalk.bold.white(newAccount.email)}`,
|
|
336
|
+
` 会员级别 : ${formatPlanBadge(newAccount.subscription_type)}`,
|
|
337
|
+
` 专属指纹 : ${chalk.cyan(formatMachineIdSummary(machineIds))}`,
|
|
338
|
+
` 官方凭据 : ${chalk.grey(rawToken.slice(0, 16))}...[已加密保护]`,
|
|
339
|
+
` 系统剪贴板: ${copied ? chalk.green('✔ Token 已自动复制到系统剪贴板 (可直接 Ctrl+V 粘贴)') : chalk.yellow('○ 请手动复制')}`,
|
|
340
|
+
].join('\n'), { padding: { top: 0, bottom: 0, left: 1, right: 1 }, borderColor: 'green', borderStyle: 'round' }));
|
|
341
|
+
console.log(chalk.grey(`\n提示: 可随时使用 cswitch use ${newAccount.email} 切换到该账号。\n`));
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
// 1. 优先智能探测本机已登录态 (0 秒直提,免开浏览器)
|
|
345
|
+
const localEmail = storage.readCurrentEmail() || (await sqlite.readEmail());
|
|
346
|
+
const localToken = storage.readCurrentToken() || (await sqlite.readToken());
|
|
347
|
+
if (localEmail && localToken) {
|
|
348
|
+
console.log(chalk.cyan(`\n✔ 智能检测到本机 Cursor 已登录有效账号: ${chalk.bold.white(localEmail)}`));
|
|
349
|
+
console.log(chalk.grey('正在同步官方用量与订阅计划...'));
|
|
350
|
+
const existing = store.getByEmail(localEmail);
|
|
351
|
+
const usageRes = await OfficialApiClient.fetchUsage(localToken);
|
|
352
|
+
const machineIds = storage.readMachineIds() || existing?.machine_ids || generateMachineIds();
|
|
353
|
+
const account = {
|
|
354
|
+
email: localEmail,
|
|
355
|
+
token: localToken,
|
|
356
|
+
subscription_type: usageRes.success && usageRes.subscriptionType ? usageRes.subscriptionType : (existing?.subscription_type || 'free'),
|
|
357
|
+
current_period_usage: usageRes.currentPeriodUsage || existing?.current_period_usage,
|
|
358
|
+
machine_ids: machineIds,
|
|
359
|
+
is_current: true,
|
|
360
|
+
};
|
|
361
|
+
store.addOrUpdate(account);
|
|
362
|
+
const parsed = TokenParser.parse(localToken);
|
|
363
|
+
const jwtToken = parsed.token || localToken;
|
|
364
|
+
const copied = await copyToClipboard(jwtToken);
|
|
365
|
+
console.log(boxen([
|
|
366
|
+
chalk.bold.green('✔ 本机账号提取成功!已直接纳入本地库 (免开浏览器)'),
|
|
367
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
368
|
+
` 登录邮箱 : ${chalk.bold.white(account.email)}`,
|
|
369
|
+
` 会员级别 : ${formatPlanBadge(account.subscription_type)}`,
|
|
370
|
+
` 专属指纹 : ${chalk.cyan(formatMachineIdSummary(machineIds))}`,
|
|
371
|
+
` 系统剪贴板: ${copied ? chalk.green('✔ Token 已自动复制到系统剪贴板 (可直接 Ctrl+V 粘贴)') : chalk.yellow('○ 请手动复制')}`,
|
|
372
|
+
].join('\n'), { padding: { top: 0, bottom: 0, left: 1, right: 1 }, borderColor: 'green', borderStyle: 'round' }));
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
// 2. 本机未登录,调起全新无痕浏览器执行官方 OAuth 登录
|
|
376
|
+
console.log(chalk.cyan('\n⚡ 本机未检测到登录态,正在调起无痕浏览器执行官方 OAuth 登录...\n'));
|
|
377
|
+
const oauthRes = await OAuthReceiver.startOfficialOAuthFlow();
|
|
378
|
+
if (!oauthRes.success || !oauthRes.accessToken) {
|
|
379
|
+
if (oauthRes.message) {
|
|
380
|
+
console.log(chalk.yellow(`\n${oauthRes.message}\n`));
|
|
381
|
+
}
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
let email = oauthRes.email;
|
|
385
|
+
if (!email) {
|
|
386
|
+
const fallback = oauthRes.authId
|
|
387
|
+
? `${oauthRes.authId.replace('auth0|', '')}@cursor.sh`
|
|
388
|
+
: `account_${Date.now().toString().slice(-6)}@cursor.sh`;
|
|
389
|
+
const inputEmail = await safeInput({
|
|
390
|
+
message: '未能自动解析到邮箱,请输入该账号的登录邮箱:',
|
|
391
|
+
default: fallback,
|
|
392
|
+
});
|
|
393
|
+
email = inputEmail || fallback;
|
|
394
|
+
}
|
|
395
|
+
const machineIds = generateMachineIds();
|
|
396
|
+
console.log(chalk.grey('\n正在向 Cursor 官方同步用量与订阅计划...'));
|
|
397
|
+
const usageRes = await OfficialApiClient.fetchUsage(oauthRes.accessToken);
|
|
398
|
+
const existing = store.getByEmail(email);
|
|
399
|
+
const subType = usageRes.success && usageRes.subscriptionType
|
|
400
|
+
? usageRes.subscriptionType
|
|
401
|
+
: existing?.subscription_type || 'free';
|
|
402
|
+
const newAccount = {
|
|
403
|
+
email,
|
|
404
|
+
token: oauthRes.accessToken,
|
|
405
|
+
workos_cursor_session_token: oauthRes.refreshToken || oauthRes.accessToken,
|
|
406
|
+
subscription_type: subType,
|
|
407
|
+
current_period_usage: usageRes.currentPeriodUsage || existing?.current_period_usage,
|
|
408
|
+
machine_ids: machineIds,
|
|
409
|
+
};
|
|
410
|
+
store.addOrUpdate(newAccount);
|
|
411
|
+
const copied = await copyToClipboard(newAccount.token);
|
|
412
|
+
console.log(boxen([
|
|
413
|
+
chalk.bold.green('🎉 官方 OAuth 登录成功!账号已自动收录入库'),
|
|
414
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
415
|
+
` 登录邮箱 : ${chalk.bold.white(newAccount.email)}`,
|
|
416
|
+
` 会员级别 : ${formatPlanBadge(newAccount.subscription_type)}`,
|
|
417
|
+
` 专属指纹 : ${chalk.cyan(formatMachineIdSummary(machineIds))} ${chalk.dim('(独立隔离防封)')}`,
|
|
418
|
+
` 官方凭据 : ${chalk.grey(newAccount.token.slice(0, 16))}...[已加密归档]`,
|
|
419
|
+
` 系统剪贴板: ${copied ? chalk.green('✔ Token 已自动复制到系统剪贴板 (可直接粘贴)') : chalk.yellow('○ 请手动复制')}`,
|
|
420
|
+
].join('\n'), { padding: { top: 0, bottom: 0, left: 1, right: 1 }, borderColor: 'green', borderStyle: 'round' }));
|
|
421
|
+
console.log(chalk.grey(`\n提示: 可随时使用 cswitch use ${newAccount.email} 切换到该账号。\n`));
|
|
422
|
+
});
|
|
423
|
+
// 4.3 copy (一键拷贝指定账号或当前在用账号 Token 到剪贴板)
|
|
424
|
+
program
|
|
425
|
+
.command('copy [email]')
|
|
426
|
+
.alias('cp')
|
|
427
|
+
.description('一键复制指定账号或当前在用账号的 Token 到系统剪贴板')
|
|
428
|
+
.action(async (targetEmail) => {
|
|
429
|
+
const paths = resolveCursorPaths();
|
|
430
|
+
const storage = new StorageBridge(paths.storageJson);
|
|
431
|
+
const sqlite = new SqliteBridge(paths.sqliteDb);
|
|
432
|
+
const store = new AccountStore();
|
|
433
|
+
let email = targetEmail;
|
|
434
|
+
let tokenToCopy = '';
|
|
435
|
+
if (email) {
|
|
436
|
+
const acc = store.getByEmail(email);
|
|
437
|
+
if (!acc) {
|
|
438
|
+
console.log(chalk.red(`\n未在本地库中找到邮箱为 ${email} 的账号\n`));
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
tokenToCopy = acc.token;
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
tokenToCopy = storage.readCurrentToken() || (await sqlite.readToken()) || '';
|
|
445
|
+
email = storage.readCurrentEmail() || (await sqlite.readEmail()) || '当前账号';
|
|
446
|
+
if (!tokenToCopy) {
|
|
447
|
+
const currentAcc = store.getCurrentAccount();
|
|
448
|
+
if (currentAcc) {
|
|
449
|
+
tokenToCopy = currentAcc.token;
|
|
450
|
+
email = currentAcc.email;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
if (!tokenToCopy) {
|
|
455
|
+
console.log(chalk.yellow('\n未检测到有效 Token,请先登录或使用 cswitch token 进行提取。\n'));
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
const parsed = TokenParser.parse(tokenToCopy);
|
|
459
|
+
const jwtToken = parsed.token || tokenToCopy;
|
|
460
|
+
const success = await copyToClipboard(jwtToken);
|
|
461
|
+
console.log(boxen([
|
|
462
|
+
chalk.bold.green('✔ Token 已成功复制到系统剪贴板!'),
|
|
463
|
+
chalk.dim('────────────────────────────────────────────────'),
|
|
464
|
+
` 账号邮箱 : ${chalk.bold.white(email)}`,
|
|
465
|
+
` 凭据类型 : ${chalk.cyan('原生 Bearer JWT (Access Token)')}`,
|
|
466
|
+
` 凭据预览 : ${chalk.dim(jwtToken.slice(0, 16))}...${chalk.dim(jwtToken.slice(-12))} ${chalk.grey(`(共 ${jwtToken.length} 字符)`)}`,
|
|
467
|
+
` 操作提示 : ${chalk.yellow('直接使用 Ctrl + V (或 Cmd + V) 粘贴即可')}`,
|
|
468
|
+
].join('\n'), { padding: { top: 0, bottom: 0, left: 1, right: 1 }, borderColor: 'green', borderStyle: 'round' }));
|
|
469
|
+
if (!success) {
|
|
470
|
+
console.log(chalk.yellow('提示: 自动写入剪贴板未生效,以下为 Token 明文,可手动选中复制:'));
|
|
471
|
+
console.log(chalk.grey(jwtToken) + '\n');
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
// 5. capture
|
|
475
|
+
program
|
|
476
|
+
.command('capture [alias]')
|
|
477
|
+
.description('一键收录当前电脑 Cursor IDE 正在登录的有效账号')
|
|
478
|
+
.action(async (alias) => {
|
|
479
|
+
const paths = resolveCursorPaths();
|
|
480
|
+
const storage = new StorageBridge(paths.storageJson);
|
|
481
|
+
const sqlite = new SqliteBridge(paths.sqliteDb);
|
|
482
|
+
const store = new AccountStore();
|
|
483
|
+
const email = storage.readCurrentEmail() || (await sqlite.readEmail());
|
|
484
|
+
const token = storage.readCurrentToken() || (await sqlite.readToken());
|
|
485
|
+
if (!email || !token) {
|
|
486
|
+
console.log(chalk.yellow('\n未能从本地 Cursor 发现登录数据,请确认 Cursor 已登录。\n'));
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
const machineIds = storage.readMachineIds();
|
|
490
|
+
store.addOrUpdate({
|
|
491
|
+
email,
|
|
492
|
+
token,
|
|
493
|
+
username: alias || null,
|
|
494
|
+
machine_ids: machineIds,
|
|
495
|
+
is_current: true,
|
|
496
|
+
});
|
|
497
|
+
console.log(chalk.green(`\n✔ 成功收录本机在用账号: ${chalk.bold.white(email)}`));
|
|
498
|
+
console.log(chalk.grey(`• 绑定硬件指纹: ${formatMachineIdSummary(machineIds)}\n`));
|
|
499
|
+
});
|
|
500
|
+
// 6. dash / dashboard
|
|
501
|
+
program
|
|
502
|
+
.command('dash [identifier]')
|
|
503
|
+
.alias('dashboard')
|
|
504
|
+
.description('免密直达 Cursor 官网后台控制台 (使用最新独立隔离浏览器 + 原生 CDP 自动注入 Token 秒登)')
|
|
505
|
+
.option('-t, --token <token>', '直接指定 Token 凭据免密打开控制台')
|
|
506
|
+
.action(async (identifier, options) => {
|
|
507
|
+
if (options?.token) {
|
|
508
|
+
console.log(chalk.cyan('\n正在使用指定 Token 调起独立浏览器并免密直达 Cursor 控制台...'));
|
|
509
|
+
await DashboardLauncher.openDashboardWithToken(options.token, identifier || 'Direct Token User');
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
const store = new AccountStore();
|
|
513
|
+
let targetAcc = identifier ? store.getByIndexOrEmail(identifier) : store.loadAll().find((a) => a.is_current);
|
|
514
|
+
if (!targetAcc && identifier) {
|
|
515
|
+
// 尝试解析是否直接传入了 Token (user_xxx:: 或 eyJ...)
|
|
516
|
+
const parsed = TokenParser.parse(identifier);
|
|
517
|
+
if (parsed.isValid) {
|
|
518
|
+
process.stdout.write(chalk.grey('\n正在解析 Token 并同步账号信息... '));
|
|
519
|
+
let email;
|
|
520
|
+
try {
|
|
521
|
+
const fetchedEmail = await OfficialApiClient.fetchEmail(parsed.token);
|
|
522
|
+
if (fetchedEmail)
|
|
523
|
+
email = fetchedEmail;
|
|
524
|
+
}
|
|
525
|
+
catch { }
|
|
526
|
+
if (!email) {
|
|
527
|
+
email = parsed.email || (parsed.sub ? `${parsed.sub.replace('auth0|', '')}@cursor.sh` : undefined);
|
|
528
|
+
}
|
|
529
|
+
if (!email) {
|
|
530
|
+
email = `token_user_${Date.now().toString().slice(-4)}@cursor.sh`;
|
|
531
|
+
}
|
|
532
|
+
const usageRes = await OfficialApiClient.fetchUsage(parsed.token);
|
|
533
|
+
const existing = store.getByEmail(email);
|
|
534
|
+
const subType = usageRes.success && usageRes.subscriptionType
|
|
535
|
+
? usageRes.subscriptionType
|
|
536
|
+
: existing?.subscription_type || 'free';
|
|
537
|
+
targetAcc = {
|
|
538
|
+
email,
|
|
539
|
+
token: parsed.token,
|
|
540
|
+
workos_cursor_session_token: parsed.workosSessionToken || parsed.token,
|
|
541
|
+
subscription_type: subType,
|
|
542
|
+
current_period_usage: usageRes.currentPeriodUsage || existing?.current_period_usage,
|
|
543
|
+
machine_ids: existing?.machine_ids || generateMachineIds(),
|
|
544
|
+
};
|
|
545
|
+
if (!existing) {
|
|
546
|
+
store.addOrUpdate(targetAcc);
|
|
547
|
+
console.log(chalk.grey('✔ 已自动纳管录入本地多账号库'));
|
|
548
|
+
}
|
|
549
|
+
else {
|
|
550
|
+
console.log(chalk.green(`✔ ${email} [${formatPlanName(subType)}]`));
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
if (!targetAcc) {
|
|
555
|
+
targetAcc = store.loadAll()[0];
|
|
556
|
+
}
|
|
557
|
+
if (!targetAcc) {
|
|
558
|
+
console.log(chalk.yellow('\n账号库为空,且未输入有效账号标识或 Token。\n'));
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
console.log(chalk.cyan(`\n正在调起全新独立浏览器并免密直达 Cursor 控制台 (${targetAcc.email})...`));
|
|
562
|
+
await DashboardLauncher.openDashboard(targetAcc);
|
|
563
|
+
});
|
|
564
|
+
// 6.5 mail / outlook (打开独立浏览器自动登录 Outlook 邮箱)
|
|
565
|
+
program
|
|
566
|
+
.command('mail [identifier]')
|
|
567
|
+
.alias('outlook')
|
|
568
|
+
.description('打开全新的独立浏览器实例并自动登录 Outlook 邮箱 (遇辅助邮箱/安全验证自动停下交接)')
|
|
569
|
+
.option('-p, --password <pwd>', '指定邮箱密码 (不传则优先读取已保存密码或在网页手动输入)')
|
|
570
|
+
.option('-d, --dash', '同时在打开的独立浏览器新标签页中免密直达 Cursor 官网控制台')
|
|
571
|
+
.option('-t, --token <token>', '指定 Cursor Token 并在浏览器新标签页中免密直达控制台')
|
|
572
|
+
.action(async (identifier, options) => {
|
|
573
|
+
const store = new AccountStore();
|
|
574
|
+
let email = '';
|
|
575
|
+
let emailPassword = options?.password ? String(options.password).trim() : '';
|
|
576
|
+
let cursorToken = options?.token ? String(options.token).trim() : '';
|
|
577
|
+
if (identifier) {
|
|
578
|
+
// 尝试按多段式解析 (支持 email----password 等格式)
|
|
579
|
+
const pureLogin = new PureLoginService();
|
|
580
|
+
const parsed = pureLogin.parseAccountString(identifier);
|
|
581
|
+
if (parsed.email) {
|
|
582
|
+
email = parsed.email;
|
|
583
|
+
if (!emailPassword && parsed.emailPassword) {
|
|
584
|
+
emailPassword = parsed.emailPassword;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
else {
|
|
588
|
+
// 尝试从已有账号库匹配
|
|
589
|
+
const targetAcc = store.getByIndexOrEmail(identifier);
|
|
590
|
+
if (targetAcc) {
|
|
591
|
+
email = targetAcc.email;
|
|
592
|
+
if (!emailPassword && targetAcc.email_password) {
|
|
593
|
+
emailPassword = targetAcc.email_password;
|
|
594
|
+
}
|
|
595
|
+
if (!cursorToken && (targetAcc.workos_cursor_session_token || targetAcc.token)) {
|
|
596
|
+
cursorToken = targetAcc.workos_cursor_session_token || targetAcc.token;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
else {
|
|
600
|
+
email = identifier.trim();
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
else {
|
|
605
|
+
// 未传 identifier,优先使用当前在用账号,或列表中第一个账号
|
|
606
|
+
const currentAcc = store.loadAll().find((a) => a.is_current) || store.loadAll()[0];
|
|
607
|
+
if (currentAcc) {
|
|
608
|
+
email = currentAcc.email;
|
|
609
|
+
if (!emailPassword && currentAcc.email_password) {
|
|
610
|
+
emailPassword = currentAcc.email_password;
|
|
611
|
+
}
|
|
612
|
+
if (!cursorToken && (currentAcc.workos_cursor_session_token || currentAcc.token)) {
|
|
613
|
+
cursorToken = currentAcc.workos_cursor_session_token || currentAcc.token;
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
if (!email) {
|
|
618
|
+
console.log(chalk.yellow('\n未检测到可用邮箱,请使用 "cswitch mail <email>" 或直接进入 cswitch 主菜单。\n'));
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
// 若指定了 -d 但未指定 token,尝试从库中寻找匹配账号的 token
|
|
622
|
+
if (options?.dash && !cursorToken) {
|
|
623
|
+
const matched = store.getByEmail(email) || store.loadAll().find((a) => a.email.toLowerCase() === email.toLowerCase());
|
|
624
|
+
if (matched?.token) {
|
|
625
|
+
cursorToken = matched.workos_cursor_session_token || matched.token;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
const shouldOpenDash = Boolean(options?.dash || (options?.token && cursorToken));
|
|
629
|
+
console.log(chalk.cyan(`\n正在调起全新独立浏览器并自动登录 ${email}...`));
|
|
630
|
+
if (shouldOpenDash && cursorToken) {
|
|
631
|
+
console.log(chalk.green(` [联动] 同时在浏览器新标签页免密打开 Cursor 控制台 (Token: ${cursorToken.slice(0, 15)}...)`));
|
|
632
|
+
}
|
|
633
|
+
const res = await OutlookLauncher.openMailbox({
|
|
634
|
+
email,
|
|
635
|
+
emailPassword,
|
|
636
|
+
openDashboard: shouldOpenDash,
|
|
637
|
+
cursorToken,
|
|
638
|
+
});
|
|
639
|
+
OutlookLauncher.printResultCard(res);
|
|
640
|
+
});
|
|
641
|
+
// 7. refund / aozai / seat (奥仔退款)
|
|
642
|
+
program
|
|
643
|
+
.command('refund [identifier]')
|
|
644
|
+
.alias('aozai')
|
|
645
|
+
.alias('seat')
|
|
646
|
+
.description('奥仔退款:执行席位退款,账号退出并恢复为 Free 免费状态')
|
|
647
|
+
.option('-c, --card <code>', '使用指定席位卡密 (不传则使用已保存卡密或提示输入)')
|
|
648
|
+
.option('-t, --token <token>', '直接指定 Cursor Session Token 执行退款')
|
|
649
|
+
.option('-s, --status', '查询当前卡密剩余点数与扣点费率')
|
|
650
|
+
.option('-u, --usage', '查看卡密历史使用与退款流水明细')
|
|
651
|
+
.option('--set-card <code>', '保存默认席位卡密到本地配置')
|
|
652
|
+
.option('--api-base <url>', '自定义奥仔退款服务 API 网关地址')
|
|
653
|
+
.option('-y, --yes', '自动化免确认模式 (自动同意扣费与重启 Cursor)')
|
|
654
|
+
.action(async (identifier, options) => {
|
|
655
|
+
const refundService = new RefundService(options?.apiBase);
|
|
656
|
+
if (options?.setCard) {
|
|
657
|
+
refundService.getConfigStore().setSeatCardCode(options.setCard);
|
|
658
|
+
console.log(chalk.green(`\n✔ 默认席位卡密已保存: ${options.setCard}\n`));
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
if (options?.status) {
|
|
662
|
+
const cardRes = await refundService.ensureCardAuth(options?.card, true);
|
|
663
|
+
if (!cardRes)
|
|
664
|
+
return;
|
|
665
|
+
const balRes = await refundService.getClient().getBalance(cardRes.auth.token);
|
|
666
|
+
const b = balRes.data || cardRes.auth;
|
|
667
|
+
console.log(chalk.bold.cyan('\n● 奥仔席位退款卡密状态:'));
|
|
668
|
+
console.log(`• 卡密编号 : ${chalk.white(cardRes.cardCode)}`);
|
|
669
|
+
console.log(`• 剩余点数 : ${chalk.bold.green(b.remaining)} 点`);
|
|
670
|
+
console.log(`• 已用点数 : ${chalk.yellow(b.used)} 点`);
|
|
671
|
+
console.log(`• 单次扣点 : ${chalk.magenta(b.pointsPerOp)} 点`);
|
|
672
|
+
console.log(`• 资费开通 : ${b.apiAllowed ? chalk.green('已开通') : chalk.yellow('待确认')}\n`);
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
if (options?.usage) {
|
|
676
|
+
await refundService.printUsageHistory(options?.card);
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
let targetAcc;
|
|
680
|
+
const store = new AccountStore();
|
|
681
|
+
if (identifier) {
|
|
682
|
+
targetAcc = store.getByIndexOrEmail(identifier);
|
|
683
|
+
if (!targetAcc) {
|
|
684
|
+
console.log(chalk.red(`\n未找到匹配账号: "${identifier}"\n`));
|
|
685
|
+
process.exit(1);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
else if (!options?.token) {
|
|
689
|
+
targetAcc = store.loadAll().find((a) => a.is_current) || store.loadAll()[0];
|
|
690
|
+
}
|
|
691
|
+
await refundService.runRefundForAccount({
|
|
692
|
+
account: targetAcc,
|
|
693
|
+
sessionToken: options?.token,
|
|
694
|
+
cardCode: options?.card,
|
|
695
|
+
autoYes: Boolean(options?.yes),
|
|
696
|
+
});
|
|
697
|
+
});
|
|
698
|
+
// 8. id
|
|
699
|
+
const idCmd = program.command('id').description('Cursor 机器硬件指纹 (Machine ID) 管理');
|
|
700
|
+
idCmd
|
|
701
|
+
.command('status')
|
|
702
|
+
.description('查看当前 Cursor 的硬件指纹')
|
|
703
|
+
.action(() => {
|
|
704
|
+
const paths = resolveCursorPaths();
|
|
705
|
+
const storage = new StorageBridge(paths.storageJson);
|
|
706
|
+
const ids = storage.readMachineIds();
|
|
707
|
+
console.log(chalk.bold.magenta('\n● 当前 Cursor 机器硬件指纹:'));
|
|
708
|
+
console.log(`• devDeviceId : ${chalk.white(ids['telemetry.devDeviceId'] || '--')}`);
|
|
709
|
+
console.log(`• sqmId : ${chalk.white(ids['telemetry.sqmId'] || '--')}`);
|
|
710
|
+
console.log(`• macMachineId : ${chalk.white(maskToken(ids['telemetry.macMachineId']))}`);
|
|
711
|
+
console.log(`• machineId : ${chalk.white(maskToken(ids['telemetry.machineId']))}\n`);
|
|
712
|
+
});
|
|
713
|
+
idCmd
|
|
714
|
+
.command('reset')
|
|
715
|
+
.description('一键备份并重置全套崭新的随机机器码')
|
|
716
|
+
.action(() => {
|
|
717
|
+
const paths = resolveCursorPaths();
|
|
718
|
+
const storage = new StorageBridge(paths.storageJson);
|
|
719
|
+
const newIds = generateMachineIds();
|
|
720
|
+
storage.createBackup();
|
|
721
|
+
storage.writeMachineIds(newIds);
|
|
722
|
+
console.log(chalk.green('\n✔ 硬件指纹已重置为全新随机硬件码!旧指纹已安全备份。\n'));
|
|
723
|
+
});
|
|
724
|
+
// 8. remove / rm
|
|
725
|
+
program
|
|
726
|
+
.command('remove [identifier]')
|
|
727
|
+
.alias('rm')
|
|
728
|
+
.description('从本地库移除指定账号 (若不传参则进入交互式多账号清理管理)')
|
|
729
|
+
.action(async (identifier) => {
|
|
730
|
+
const paths = resolveCursorPaths();
|
|
731
|
+
const storage = new StorageBridge(paths.storageJson);
|
|
732
|
+
const sqlite = new SqliteBridge(paths.sqliteDb);
|
|
733
|
+
const store = new AccountStore();
|
|
734
|
+
if (!identifier) {
|
|
735
|
+
await handleAccountCleanupMenu(store, storage, sqlite);
|
|
736
|
+
return;
|
|
737
|
+
}
|
|
738
|
+
const acc = store.getByIndexOrEmail(identifier);
|
|
739
|
+
if (!acc) {
|
|
740
|
+
console.log(chalk.red(`\n未找到匹配账号: "${identifier}"\n`));
|
|
741
|
+
return;
|
|
742
|
+
}
|
|
743
|
+
store.remove(acc.email);
|
|
744
|
+
console.log(chalk.green(`\n✔ 账号 ${acc.email} 已成功移除。\n`));
|
|
745
|
+
});
|
|
746
|
+
// 9. import / export
|
|
747
|
+
program
|
|
748
|
+
.command('import <filePath>')
|
|
749
|
+
.description('从文本或 JSON 文件批量导入账号')
|
|
750
|
+
.action((filePath) => {
|
|
751
|
+
if (!fs.existsSync(filePath)) {
|
|
752
|
+
console.log(chalk.red(`\n文件不存在: ${filePath}\n`));
|
|
753
|
+
return;
|
|
754
|
+
}
|
|
755
|
+
const store = new AccountStore();
|
|
756
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
757
|
+
let count = 0;
|
|
758
|
+
if (filePath.endsWith('.json')) {
|
|
759
|
+
const arr = JSON.parse(content);
|
|
760
|
+
if (Array.isArray(arr)) {
|
|
761
|
+
for (const item of arr) {
|
|
762
|
+
if (item.email && item.token) {
|
|
763
|
+
store.addOrUpdate(item);
|
|
764
|
+
count++;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
else {
|
|
770
|
+
const lines = content.split('\n');
|
|
771
|
+
for (const line of lines) {
|
|
772
|
+
const parsed = TokenParser.parse(line);
|
|
773
|
+
if (parsed.isValid && parsed.email) {
|
|
774
|
+
store.addOrUpdate({
|
|
775
|
+
email: parsed.email,
|
|
776
|
+
token: parsed.token,
|
|
777
|
+
workos_cursor_session_token: parsed.workosSessionToken,
|
|
778
|
+
});
|
|
779
|
+
count++;
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
console.log(chalk.green(`\n✔ 导入完成!共新增/更新 ${count} 个账号。\n`));
|
|
784
|
+
});
|
|
785
|
+
program
|
|
786
|
+
.command('export [targetPath]')
|
|
787
|
+
.description('导出所有账号备份为 JSON 文件')
|
|
788
|
+
.action((targetPath) => {
|
|
789
|
+
const store = new AccountStore();
|
|
790
|
+
const accounts = store.loadAll();
|
|
791
|
+
const outPath = targetPath || `cursor_accounts_backup_${Date.now()}.json`;
|
|
792
|
+
fs.writeFileSync(outPath, JSON.stringify(accounts, null, 2), 'utf8');
|
|
793
|
+
console.log(chalk.green.bold(`\n✔ 成功导出 ${accounts.length} 个账号至: ${outPath}\n`));
|
|
794
|
+
});
|
|
795
|
+
// 10. update / upgrade
|
|
796
|
+
program
|
|
797
|
+
.command('update')
|
|
798
|
+
.alias('upgrade')
|
|
799
|
+
.description('在线检查最新版本并执行一键跨平台自升级')
|
|
800
|
+
.option('-c, --check', '仅检查是否有可用新版本,不执行升级')
|
|
801
|
+
.option('-y, --yes', '跳过确认提示,直接自动执行升级')
|
|
802
|
+
.option('-m, --mirror', '使用国内 npmmirror 镜像源加速下载')
|
|
803
|
+
.action(async (options) => {
|
|
804
|
+
const checker = new UpdateChecker();
|
|
805
|
+
console.log(chalk.cyan('正在从官方源及国内加速镜像检查最新版本...'));
|
|
806
|
+
const res = await checker.check(true);
|
|
807
|
+
if (!res.hasUpdate) {
|
|
808
|
+
console.log(chalk.green(`\n✔ 当前已是最新版本 (v${res.currentVersion}),无需更新。\n`));
|
|
809
|
+
return;
|
|
810
|
+
}
|
|
811
|
+
console.log(boxen([
|
|
812
|
+
chalk.bold.yellow('🎉 发现 Cursor-Switch 崭新版本!'),
|
|
813
|
+
'',
|
|
814
|
+
`• 当前本地版本 : ${chalk.grey('v' + res.currentVersion)}`,
|
|
815
|
+
`• 线上发布版本 : ${chalk.bold.green('v' + res.latestVersion)}`,
|
|
816
|
+
`• 推荐包管理器 : ${chalk.cyan(checker.detectPackageManager(options.mirror).name)}`,
|
|
817
|
+
].join('\n'), { padding: 1, borderColor: 'green', borderStyle: 'round' }));
|
|
818
|
+
if (options.check) {
|
|
819
|
+
return;
|
|
820
|
+
}
|
|
821
|
+
let proceed = !!options.yes;
|
|
822
|
+
if (!proceed) {
|
|
823
|
+
const { safeConfirm } = await import('./utils/prompt-utils.js');
|
|
824
|
+
proceed = await safeConfirm({
|
|
825
|
+
message: `是否立即使用 ${checker.detectPackageManager(options.mirror).name} 执行升级?`,
|
|
826
|
+
default: true,
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
if (!proceed) {
|
|
830
|
+
console.log(chalk.grey('\n已取消升级。\n'));
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
console.log(chalk.cyan('\n正在下载并安装升级包,请稍候...'));
|
|
834
|
+
const upgradeResult = await checker.performUpgrade({
|
|
835
|
+
useMirror: !!options.mirror,
|
|
836
|
+
onProgress: (line) => {
|
|
837
|
+
console.log(chalk.grey(` > ${line}`));
|
|
838
|
+
},
|
|
839
|
+
});
|
|
840
|
+
if (upgradeResult.success) {
|
|
841
|
+
console.log(chalk.green.bold(`\n✔ 升级成功!当前已更新至最新版本。`));
|
|
842
|
+
console.log(chalk.grey('提示: 请重启终端或直接重新运行 cswitch 生效。\n'));
|
|
843
|
+
}
|
|
844
|
+
else {
|
|
845
|
+
console.log(chalk.red.bold(`\n✖ 自动升级遇到错误: ${upgradeResult.error}`));
|
|
846
|
+
console.log(chalk.yellow(`您也可以手动运行以下命令升级:\n npm install -g cursor-switch@latest\n`));
|
|
847
|
+
}
|
|
848
|
+
});
|
|
849
|
+
return program;
|
|
850
|
+
}
|