Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
165 lines (151 loc) · 4.65 KB
/
Copy pathmain.js
File metadata and controls
165 lines (151 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import md5 from "tinyhmacmd5";
/**
* @typedef {{
* body: any,
* code: string,
* msg: string | null,
* status: number,
* }} CCWResult
*
* @typedef {Omit<Response, 'json'> & {
* json(): Promise<CCWResult>
* }} CCWFetchResponse
*/
class CCWFetch {
/**
* 强制使用指定token。
*/
token = ''
/**
* 是否允许使用 guest-id。
* 如果该值为 false,则在未登录时不会尝试使用 guest-id。
* 仅在 token 属性为空时有效。
*/
allowGuestId = true
/**
* [内部]
* 签名密钥。
* 使用 healthCheck 以更新签名密钥。
*/
_hmacKey = ''
/**
* [内部]
* 是否使用guest-id。
*/
_usingGuestId = false
/**
* [内部]
* guest-id。
* 使用 healthCheck 以生成 guest-id。
*/
_guestId = ''
/**
* 更新签名密钥。
* 失败会抛出错误。
*/
async healthCheck(url = "https://community-web.ccw.site/health/check") {
this._hmacKey = ''
// 先尝试用token请求
this._usingGuestId = false
let response = await this.POST(url)
if (response.status != 200) {
throw Error(`CCWFetch healthCheck: HTTP ${response.status} ${response.statusText}`)
}
let result = await response.json()
if (result.status != 200) {
if (!this.token && this.allowGuestId) {
// 未指定token,且未登录,尝试用guest-id请求。
// 随机生成guest-id,反追踪。
let hex = ''
for (; hex.length < 32;)
hex += (0 | Math.random() * 16).toString(16);
this._guestId = hex
this._usingGuestId = true
response = await this.POST(url)
if (response.status != 200) {
throw Error(`CCWFetch healthCheck: HTTP ${response.status} ${response.statusText}`)
}
result = await response.json()
}
if (result.status != 200) {
throw Error(`CCWFetch healthCheck: ${result.msg}`)
}
}
/**
* @type {{
* name: string,
* status: string,
* traceId: string,
* }[]}
*/
const body = result.body
this._hmacKey = body.reduce((p, { traceId: v }) => v[parseInt(v[0], 16) + 1] + p, "")
}
/**
* 请求 CCW API 。
* 如果有 body 但还没获取 hmacKey,会自动调用 healthCheck 函数。
*
* @param {string} method
* @param {string | URL} url
* @param {string | {[key: string]: any} | null} [body]
* @param {RequestInit} [init]
* @returns {Promise<CCWFetchResponse>}
*/
async fetch(method, url, body, init) {
init = (
init == null
? {}
: Object.assign({}, init)
)
init.method = method
init.headers = new Headers(init.headers ?? {})
init.headers.has("content-type") || init.headers.set("content-type", "application/json");
if (this.token) {
// token
init.headers.set("token", this.token);
} else if (this._usingGuestId && this.allowGuestId) {
// guest
init.headers.set("guest-id", this._guestId)
} else {
// user
init.credentials ??= "include"
if (navigator.userAgent.includes("gandi-desktop")) try {
// gandi desktop
/** @type {{ token?: string, userId?: string }} */
//@ts-ignore
const { token } = electron.ipcRenderer.sendSync("auth:get-token");
if (token)
init.headers.set("token", token);
} catch (e) {
console.error(e)
}
}
if (body) {
if (typeof body == 'object')
body = JSON.stringify(body);
init.body = body
if (!this._hmacKey)
await this.healthCheck();
const b = "" + Date.now()
init.headers.set("b", b)
init.headers.set("a", md5("ccw" + body + b, this._hmacKey))
}
return fetch(url, init);
}
/**
* @param {string} url
* @param {string | {[key: string]: any} | null} [body]
* @param {RequestInit} [init]
*/
POST(url, body, init) {
return this.fetch("POST", url, body, init)
}
/**
* @param {string} url
* @param {RequestInit} [init]
*/
GET(url, init) {
return this.fetch("GET", url, null, init)
}
}
export default CCWFetch