@dsh-mobile/mobile-remote 3.3.0 → 3.4.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/FAQ.md +2 -2
- package/LICENSE +0 -0
- package/README.md +13 -8
- package/docs/PRIVACY-POLICY.en.md +26 -18
- package/docs/PRIVACY-POLICY.md +26 -18
- package/docs/SUPPORT.md +1 -1
- package/docs/TERMS-OF-SERVICE.en.md +5 -3
- package/docs/TERMS-OF-SERVICE.md +5 -3
- package/docs/USER-GUIDE.zh-CN.md +7 -6
- package/lib/account-usage-query.js +60 -60
- package/lib/account-usage.js +10 -10
- package/lib/api-helpers.js +114 -110
- package/lib/assistant-prefs.js +119 -76
- package/lib/auto-config.js +65 -65
- package/lib/backup.js +159 -123
- package/lib/break-reminder.js +45 -45
- package/lib/client.js +336 -138
- package/lib/config.js +234 -218
- package/lib/git-read-bundled-provider.js +27 -7
- package/lib/git-read-service.js +5 -5
- package/lib/host-compat.js +84 -84
- package/lib/http-util.js +17 -17
- package/lib/index.js +1670 -1511
- package/lib/memory/context.js +45 -40
- package/lib/memory/decay.js +93 -93
- package/lib/memory/extract.js +77 -77
- package/lib/memory/routes.js +33 -33
- package/lib/memory/store.js +239 -192
- package/lib/notify-copy.js +93 -93
- package/lib/permission-guide.js +86 -86
- package/lib/persona-cards.js +73 -61
- package/lib/phone-action-args.js +175 -0
- package/lib/phone-actions.js +227 -0
- package/lib/phone-announce.js +58 -0
- package/lib/proactive-material.js +42 -42
- package/lib/proactive.js +122 -119
- package/lib/protected-paths.js +205 -45
- package/lib/protocol.js +62 -23
- package/lib/push.js +29 -29
- package/lib/rate-limit.js +16 -16
- package/lib/relay/connector.js +334 -81
- package/lib/relay/crypto.js +15 -15
- package/lib/relay/devices.js +64 -32
- package/lib/relay/fingerprint.js +34 -34
- package/lib/relay/identity.js +26 -26
- package/lib/relay/mux.js +60 -46
- package/lib/relay/noise.js +40 -40
- package/lib/relay/on-demand.js +36 -31
- package/lib/relay/p2p.js +239 -0
- package/lib/relay/pairing.js +54 -15
- package/lib/relay/phone.js +63 -39
- package/lib/relay/serve.js +21 -21
- package/lib/relay/signal.js +69 -0
- package/lib/relay/ws-server.js +245 -0
- package/lib/reminders.js +15 -15
- package/lib/rpc-adapter.js +43 -43
- package/lib/session-list.js +31 -31
- package/lib/session-rotation.js +95 -95
- package/lib/session-title-cache.js +35 -35
- package/lib/session-title-refresh.js +51 -51
- package/lib/timeline.js +140 -140
- package/lib/turn-outline.js +104 -104
- package/package.json +11 -18
package/lib/protected-paths.js
CHANGED
|
@@ -1,39 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
+
import { lstatSync, readdirSync, realpathSync, statSync, unlinkSync } from "node:fs";
|
|
21
28
|
import { homedir } from "node:os";
|
|
22
|
-
import { dirname, join, resolve, sep } from "node:path";
|
|
29
|
+
import { basename, dirname, join, resolve, sep } from "node:path";
|
|
23
30
|
import { mobileRemoteStateDir } from "./auto-config.js";
|
|
24
31
|
|
|
25
32
|
const caseInsensitive = process.platform === "darwin" || process.platform === "win32";
|
|
26
33
|
const norm = (p) => (caseInsensitive ? p.normalize("NFC").toLowerCase() : p.normalize("NFC"));
|
|
27
34
|
const idOf = (st) => `${st.dev}:${st.ino}`;
|
|
28
35
|
|
|
29
|
-
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
30
42
|
export function realPathOf(path) {
|
|
31
43
|
let current = resolve(path);
|
|
32
44
|
const rest = [];
|
|
33
45
|
for (;;) {
|
|
34
46
|
try {
|
|
35
47
|
return join(realpathSync(current), ...rest.reverse());
|
|
36
|
-
} catch {
|
|
48
|
+
} catch (err) {
|
|
49
|
+
if (err?.code !== "ENOENT" && err?.code !== "ENOTDIR") {
|
|
50
|
+
throw Object.assign(new Error("path-unresolvable"), { code: "path-unresolvable", cause: err?.code });
|
|
51
|
+
}
|
|
37
52
|
const parent = dirname(current);
|
|
38
53
|
if (parent === current) return resolve(path);
|
|
39
54
|
rest.push(current.slice(parent.length).replace(/^[\\/]+/, ""));
|
|
@@ -42,40 +57,78 @@ export function realPathOf(path) {
|
|
|
42
57
|
}
|
|
43
58
|
}
|
|
44
59
|
|
|
45
|
-
|
|
60
|
+
|
|
46
61
|
export function protectedRoots(env = process.env, home = homedir()) {
|
|
47
62
|
const dshHome = typeof env.DSH_HOME === "string" && env.DSH_HOME.trim() !== "" ? env.DSH_HOME : join(home, ".dsh");
|
|
48
63
|
return [dshHome, mobileRemoteStateDir(env, home)].map((root) => realPathOf(root));
|
|
49
64
|
}
|
|
50
65
|
|
|
51
|
-
|
|
66
|
+
|
|
52
67
|
function rootIds(roots) {
|
|
53
68
|
const ids = new Set();
|
|
54
69
|
for (const root of roots) {
|
|
55
70
|
try {
|
|
56
71
|
ids.add(idOf(statSync(root)));
|
|
57
72
|
} catch {
|
|
58
|
-
|
|
73
|
+
|
|
59
74
|
}
|
|
60
75
|
}
|
|
61
76
|
return ids;
|
|
62
77
|
}
|
|
63
78
|
|
|
64
|
-
|
|
79
|
+
const DEVICE_ROOTS = ["/dev", "/proc"];
|
|
80
|
+
const underDevice = (p) => {
|
|
81
|
+
const f = norm(p);
|
|
82
|
+
return DEVICE_ROOTS.some((d) => f === d || f.startsWith(`${d}/`));
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
65
89
|
export function isDeviceOrProcPath(path) {
|
|
66
90
|
if (process.platform === "win32") return false;
|
|
67
|
-
|
|
68
|
-
|
|
91
|
+
if (underDevice(resolve(path))) return true;
|
|
92
|
+
let real;
|
|
93
|
+
try {
|
|
94
|
+
real = realPathOf(path);
|
|
95
|
+
} catch {
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
if (underDevice(real)) return true;
|
|
99
|
+
const ids = new Set();
|
|
100
|
+
for (const d of DEVICE_ROOTS) {
|
|
101
|
+
try {
|
|
102
|
+
ids.add(idOf(statSync(d)));
|
|
103
|
+
} catch {
|
|
104
|
+
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
for (let current = real; ; ) {
|
|
108
|
+
try {
|
|
109
|
+
if (ids.has(idOf(statSync(current)))) return true;
|
|
110
|
+
} catch {
|
|
111
|
+
|
|
112
|
+
}
|
|
113
|
+
const parent = dirname(current);
|
|
114
|
+
if (parent === current) return false;
|
|
115
|
+
current = parent;
|
|
116
|
+
}
|
|
69
117
|
}
|
|
70
118
|
|
|
71
|
-
|
|
119
|
+
|
|
72
120
|
export function isProtectedPath(target, roots = protectedRoots()) {
|
|
73
121
|
if (isDeviceOrProcPath(target)) return true;
|
|
74
|
-
|
|
75
|
-
|
|
122
|
+
let real;
|
|
123
|
+
try {
|
|
124
|
+
real = realPathOf(target);
|
|
125
|
+
} catch {
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
|
|
76
129
|
const r = norm(real);
|
|
77
130
|
if (roots.some((root) => r === norm(root) || r.startsWith(norm(root).endsWith(sep) ? norm(root) : `${norm(root)}${sep}`))) return true;
|
|
78
|
-
|
|
131
|
+
|
|
79
132
|
const ids = rootIds(roots);
|
|
80
133
|
if (ids.size === 0) return false;
|
|
81
134
|
let current = real;
|
|
@@ -83,7 +136,7 @@ export function isProtectedPath(target, roots = protectedRoots()) {
|
|
|
83
136
|
try {
|
|
84
137
|
if (ids.has(idOf(statSync(current)))) return true;
|
|
85
138
|
} catch {
|
|
86
|
-
|
|
139
|
+
|
|
87
140
|
}
|
|
88
141
|
const parent = dirname(current);
|
|
89
142
|
if (parent === current) return false;
|
|
@@ -91,13 +144,13 @@ export function isProtectedPath(target, roots = protectedRoots()) {
|
|
|
91
144
|
}
|
|
92
145
|
}
|
|
93
146
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
101
154
|
export function protectedFileIds(roots = protectedRoots()) {
|
|
102
155
|
const [dshHome, stateDir] = roots;
|
|
103
156
|
const ids = rootIds(roots);
|
|
@@ -115,7 +168,7 @@ export function protectedFileIds(roots = protectedRoots()) {
|
|
|
115
168
|
ids.add(idOf(st));
|
|
116
169
|
if (st.isDirectory() && depth > 0) walk(path, depth - 1);
|
|
117
170
|
} catch {
|
|
118
|
-
|
|
171
|
+
|
|
119
172
|
}
|
|
120
173
|
}
|
|
121
174
|
};
|
|
@@ -124,7 +177,114 @@ export function protectedFileIds(roots = protectedRoots()) {
|
|
|
124
177
|
return ids;
|
|
125
178
|
}
|
|
126
179
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
180
|
+
|
|
181
|
+
const SKIP_DIRS = new Set(["node_modules", ".pnpm-store", ".git"]);
|
|
182
|
+
|
|
183
|
+
export const FULL_WALK_LIMIT = 100_000;
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
export function allProtectedIds(roots = protectedRoots(), limit = FULL_WALK_LIMIT) {
|
|
191
|
+
const ids = rootIds(roots);
|
|
192
|
+
let seen = 0;
|
|
193
|
+
const stack = roots.filter(Boolean).map((r) => [r, 0]);
|
|
194
|
+
while (stack.length) {
|
|
195
|
+
const [dir, depth] = stack.pop();
|
|
196
|
+
let entries;
|
|
197
|
+
try {
|
|
198
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
199
|
+
} catch {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
for (const entry of entries) {
|
|
203
|
+
if (++seen > limit) return null;
|
|
204
|
+
const path = join(dir, entry.name);
|
|
205
|
+
try {
|
|
206
|
+
const st = lstatSync(path);
|
|
207
|
+
ids.add(idOf(st));
|
|
208
|
+
if (st.isDirectory() && depth < 32 && !SKIP_DIRS.has(entry.name)) stack.push([path, depth + 1]);
|
|
209
|
+
} catch {
|
|
210
|
+
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return ids;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
export function isProtectedFile(st, ids) {
|
|
222
|
+
if (!st) return false;
|
|
223
|
+
const id = idOf(st);
|
|
224
|
+
if ((ids ?? protectedFileIds()).has(id)) return true;
|
|
225
|
+
if (ids === undefined && st.nlink > 1 && !st.isDirectory()) {
|
|
226
|
+
const all = allProtectedIds();
|
|
227
|
+
return all === null || all.has(id);
|
|
228
|
+
}
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
export function isStillAt(target, st, roots = protectedRoots()) {
|
|
238
|
+
try {
|
|
239
|
+
const real = realPathOf(target);
|
|
240
|
+
if (isProtectedPath(real, roots)) return false;
|
|
241
|
+
return idOf(statSync(real)) === idOf(st);
|
|
242
|
+
} catch {
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
export function removeCreatedEntry(target, st, roots = protectedRoots()) {
|
|
253
|
+
if (!st) return false;
|
|
254
|
+
const name = basename(target);
|
|
255
|
+
const id = idOf(st);
|
|
256
|
+
const dirs = [dirname(resolve(target))];
|
|
257
|
+
try {
|
|
258
|
+
dirs.push(dirname(realPathOf(target)));
|
|
259
|
+
} catch {
|
|
260
|
+
|
|
261
|
+
}
|
|
262
|
+
const stack = roots.filter(Boolean).map((r) => [r, 0]);
|
|
263
|
+
let seen = 0;
|
|
264
|
+
while (stack.length && seen < FULL_WALK_LIMIT) {
|
|
265
|
+
const [dir, depth] = stack.pop();
|
|
266
|
+
dirs.push(dir);
|
|
267
|
+
let entries;
|
|
268
|
+
try {
|
|
269
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
270
|
+
} catch {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
for (const entry of entries) {
|
|
274
|
+
seen++;
|
|
275
|
+
if (entry.isDirectory() && depth < 32 && !SKIP_DIRS.has(entry.name)) stack.push([join(dir, entry.name), depth + 1]);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
for (const dir of dirs) {
|
|
279
|
+
const candidate = join(dir, name);
|
|
280
|
+
try {
|
|
281
|
+
if (idOf(lstatSync(candidate)) === id) {
|
|
282
|
+
unlinkSync(candidate);
|
|
283
|
+
return true;
|
|
284
|
+
}
|
|
285
|
+
} catch {
|
|
286
|
+
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return false;
|
|
130
290
|
}
|
package/lib/protocol.js
CHANGED
|
@@ -1,35 +1,74 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
19
21
|
|
|
20
22
|
export const PROTOCOL = Object.freeze({ min: 1, max: 1 });
|
|
21
23
|
|
|
22
24
|
export const FEATURES = Object.freeze([
|
|
23
|
-
|
|
25
|
+
|
|
24
26
|
"persona-cards",
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
"lan-noise",
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
"p2p-signal",
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
"standby",
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
const dynamicFeatures = new Map();
|
|
46
|
+
|
|
47
|
+
export const DYNAMIC_FEATURES = Object.freeze([
|
|
48
|
+
|
|
49
|
+
"p2p",
|
|
25
50
|
]);
|
|
51
|
+
export function registerFeature(name) {
|
|
52
|
+
if (!DYNAMIC_FEATURES.includes(name)) throw new Error(`not a dynamic feature: ${name}`);
|
|
53
|
+
dynamicFeatures.set(name, (dynamicFeatures.get(name) ?? 0) + 1);
|
|
54
|
+
let done = false;
|
|
55
|
+
return () => {
|
|
56
|
+
if (done) return;
|
|
57
|
+
done = true;
|
|
58
|
+
const n = (dynamicFeatures.get(name) ?? 1) - 1;
|
|
59
|
+
if (n <= 0) dynamicFeatures.delete(name);
|
|
60
|
+
else dynamicFeatures.set(name, n);
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const PHONE_ACTIONS_FEATURE = "phone-actions";
|
|
65
|
+
|
|
26
66
|
|
|
27
|
-
/** Apps older than this can't work with this plugin (none so far). */
|
|
28
67
|
export const MIN_APP_VERSION = "3.0.0";
|
|
29
68
|
|
|
30
|
-
|
|
31
|
-
export const protocolInfo = () => ({
|
|
69
|
+
|
|
70
|
+
export const protocolInfo = (extraFeatures = []) => ({
|
|
32
71
|
protocol: { min: PROTOCOL.min, max: PROTOCOL.max },
|
|
33
|
-
features: [...FEATURES],
|
|
72
|
+
features: [...FEATURES, ...dynamicFeatures.keys(), ...extraFeatures],
|
|
34
73
|
minAppVersion: MIN_APP_VERSION,
|
|
35
74
|
});
|
package/lib/push.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
20
|
export function ntfyPublish(url, payload = {}) {
|
|
21
21
|
const parsed = new URL(url);
|
|
22
22
|
const segments = parsed.pathname.split("/").filter((segment) => segment !== "");
|
|
@@ -28,33 +28,33 @@ export function ntfyPublish(url, payload = {}) {
|
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
|
|
32
32
|
export async function pushToChannel(logger, target, kind, kindLabel, title, detail, sessionId) {
|
|
33
33
|
const desp = detail || "详情请在 DSH Remote App 中查看";
|
|
34
34
|
let url = target.url;
|
|
35
35
|
const headers = { "user-agent": "dsh-mobile-remote" };
|
|
36
36
|
let init;
|
|
37
37
|
if (target.format === "serverchan") {
|
|
38
|
-
|
|
38
|
+
|
|
39
39
|
const params = new URLSearchParams({ title: `${kindLabel} · ${title}`, desp });
|
|
40
40
|
init = { method: "POST", headers: { ...headers, "content-type": "application/x-www-form-urlencoded" }, body: params.toString() };
|
|
41
41
|
} else if (target.format === "ntfy") {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
45
|
const publish = ntfyPublish(url, { title: `${kindLabel} · ${title}`, message: desp });
|
|
46
46
|
url = publish.url;
|
|
47
47
|
init = { method: "POST", headers: { ...headers, "content-type": "application/json" }, body: publish.body };
|
|
48
48
|
} else if (target.format === "bark") {
|
|
49
49
|
init = { method: "POST", headers: { ...headers, "content-type": "application/json" }, body: JSON.stringify({ title: `${kindLabel} · ${title}`, body: desp }) };
|
|
50
50
|
} else if (target.format === "wecom") {
|
|
51
|
-
|
|
51
|
+
|
|
52
52
|
init = { method: "POST", headers: { ...headers, "content-type": "application/json" }, body: JSON.stringify({ msgtype: "text", text: { content: `${kindLabel} · ${title}\n${desp}` } }) };
|
|
53
53
|
} else {
|
|
54
54
|
init = { method: "POST", headers: { ...headers, "content-type": "application/json" }, body: JSON.stringify({ kind, title, detail: desp, sessionId, time: Date.now() }) };
|
|
55
55
|
}
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
|
|
57
|
+
|
|
58
58
|
const pushOnce = async () => {
|
|
59
59
|
try {
|
|
60
60
|
return await fetch(url, { ...init, signal: AbortSignal.timeout(15_000) });
|
|
@@ -65,8 +65,8 @@ export async function pushToChannel(logger, target, kind, kindLabel, title, deta
|
|
|
65
65
|
};
|
|
66
66
|
const response = await pushOnce();
|
|
67
67
|
if (!response.ok) {
|
|
68
|
-
|
|
69
|
-
|
|
68
|
+
|
|
69
|
+
|
|
70
70
|
const text = (await response.text().catch(() => "")).trim().slice(0, 300);
|
|
71
71
|
throw new Error(`HTTP ${response.status}${text ? `: ${text}` : "(空响应体)"}`);
|
|
72
72
|
}
|
package/lib/rate-limit.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
9
|
export function createLoginRateLimiter(overrides) {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
|
|
11
|
+
|
|
12
12
|
const rateLimitCfg = { maxFailures: 10, windowMs: 60_000, blockMs: 60_000, ...(overrides ?? {}) };
|
|
13
|
-
const rateBuckets = new Map();
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
const rateBuckets = new Map();
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
18
|
const rateBlocked = (ip) => {
|
|
19
19
|
const b = rateBuckets.get(ip);
|
|
20
20
|
if (b === undefined || b.blockedUntil === undefined) return false;
|
|
@@ -31,7 +31,7 @@ export function createLoginRateLimiter(overrides) {
|
|
|
31
31
|
}
|
|
32
32
|
b.count++;
|
|
33
33
|
if (b.count >= rateLimitCfg.maxFailures) b.blockedUntil = now + rateLimitCfg.blockMs;
|
|
34
|
-
|
|
34
|
+
|
|
35
35
|
if (rateBuckets.size > 512) {
|
|
36
36
|
const oldest = [...rateBuckets.entries()]
|
|
37
37
|
.sort((x, y) => x[1].windowStart - y[1].windowStart)
|