dsh-account-pool 0.1.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 +362 -0
- package/cordis.patch.yml +26 -0
- package/lib/accounts.js +576 -0
- package/lib/client.js +2049 -0
- package/lib/headers.js +76 -0
- package/lib/index.js +1142 -0
- package/lib/selector.js +343 -0
- package/lib/shim.js +370 -0
- package/lib/tasks.js +429 -0
- package/lib/trae-accounts.js +453 -0
- package/lib/trae-bridge.js +266 -0
- package/lib/trae-storage.js +215 -0
- package/lib/trae-upstream.js +485 -0
- package/lib/upstream.js +610 -0
- package/lib/usage.js +420 -0
- package/package.json +68 -0
package/lib/headers.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 上游 HTTP 请求头:所有出站请求共用一套。
|
|
3
|
+
*
|
|
4
|
+
* 为什么单独成模块:原先 accounts.js 与 upstream.js 各有一份 commonHeaders,
|
|
5
|
+
* 改一处漏一处。出站头是上游风控的判据,必须只有一处定义。
|
|
6
|
+
*
|
|
7
|
+
* 头的来源与理由(对齐 workbuddy2api 网关的 headers.go,那是它验证过的形态):
|
|
8
|
+
* - X-CodeBuddy-Request: 1 官方客户端的风控闸门头,所有 API 请求必带
|
|
9
|
+
* - X-Machine-ID/X-Session-ID 按 uid 稳定派生的设备标识,跨重启不变、
|
|
10
|
+
* 账号间互异;uid 为空时不发(上游不要求)
|
|
11
|
+
* - Accept-Language 按区域切:国内 zh-CN、国际 en-US,与官方客户端一致
|
|
12
|
+
* - Origin/Referer 按区域切,送错域会被 openresty 直接拒
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { createHash } from 'node:crypto'
|
|
16
|
+
|
|
17
|
+
/** CLI 形态 User-Agent——上游按它选通道。 */
|
|
18
|
+
export const CLIENT_UA = 'CLI/2.63.2 CodeBuddy/2.63.2'
|
|
19
|
+
|
|
20
|
+
/** 桌面端 User-Agent:拉国际版模型目录时必须用它才能拿到完整清单。 */
|
|
21
|
+
export const DESKTOP_UA = 'WorkBuddy/5.5.2'
|
|
22
|
+
|
|
23
|
+
/** 官方 IDE 的 User-Agent:/v3/config 用它才返回完整目录。 */
|
|
24
|
+
export const IDE_UA = 'CodeBuddyIDE/4.12.0 CodeBuddy/4.12.0'
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 按 uid 派生一个稳定的设备标识。
|
|
28
|
+
*
|
|
29
|
+
* 与网关同算法(sha256 取前 36 个 hex 字符):同一账号每次得到同一个值,
|
|
30
|
+
* 不同账号互不相同。上游把这些当作设备指纹,随机生成会被判为异常设备。
|
|
31
|
+
*
|
|
32
|
+
* @param {string} uid 账号 uid
|
|
33
|
+
* @param {'machine'|'session'} purpose 用途
|
|
34
|
+
* @returns {string|undefined} uid 为空时返回 undefined(调用方据此不发该头)
|
|
35
|
+
*/
|
|
36
|
+
export function deriveDeviceId(uid, purpose) {
|
|
37
|
+
if (typeof uid !== 'string' || uid === '') return undefined
|
|
38
|
+
return createHash('sha256').update(`wb2a:${purpose}:${uid}`).digest('hex').slice(0, 36)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 按区域返回 Accept-Language。 */
|
|
42
|
+
export function acceptLanguageOf(region) {
|
|
43
|
+
return region === 'global' ? 'en-US' : 'zh-CN'
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 构造所有上游请求共用的头。
|
|
48
|
+
*
|
|
49
|
+
* @param {object} options
|
|
50
|
+
* @param {string} options.origin Origin/Referer 基地址(按区域算好传进来)
|
|
51
|
+
* @param {'cn'|'global'} [options.region] 区域,决定 Accept-Language
|
|
52
|
+
* @param {string} [options.uid] 账号 uid;给了就注入设备头
|
|
53
|
+
* @param {string} [options.userAgent] 覆盖 User-Agent(拉目录时要换)
|
|
54
|
+
* @returns {Record<string,string>}
|
|
55
|
+
*/
|
|
56
|
+
export function commonHeaders({ origin, region = 'cn', uid, userAgent }) {
|
|
57
|
+
const headers = {
|
|
58
|
+
'content-type': 'application/json',
|
|
59
|
+
accept: 'application/json',
|
|
60
|
+
'x-requested-with': 'XMLHttpRequest',
|
|
61
|
+
origin,
|
|
62
|
+
referer: `${origin}/`,
|
|
63
|
+
'user-agent': userAgent ?? CLIENT_UA,
|
|
64
|
+
// 官方客户端风控闸门头:所有 API 请求必带,缺了可能被当机器人。
|
|
65
|
+
'x-codebuddy-request': '1',
|
|
66
|
+
'accept-language': acceptLanguageOf(region),
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// 设备头:按账号稳定派生,账号间互异。uid 缺失时不发。
|
|
70
|
+
const machineId = deriveDeviceId(uid, 'machine')
|
|
71
|
+
const sessionId = deriveDeviceId(uid, 'session')
|
|
72
|
+
if (machineId !== undefined) headers['x-machine-id'] = machineId
|
|
73
|
+
if (sessionId !== undefined) headers['x-session-id'] = sessionId
|
|
74
|
+
|
|
75
|
+
return headers
|
|
76
|
+
}
|