@jx3box/jx3box-ui 2.4.28 → 2.4.29
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/package.json
CHANGED
package/src/utils/client-stat.js
CHANGED
|
@@ -2,6 +2,7 @@ import { $cms } from "@jx3box/jx3box-common/js/api";
|
|
|
2
2
|
import { isMiniProgram, isApp } from "@jx3box/jx3box-common/js/utils";
|
|
3
3
|
import User from "@jx3box/jx3box-common/js/user";
|
|
4
4
|
import { applyJx3boxClientStatRules } from "./client-stat-business";
|
|
5
|
+
import { DEFAULT_BLOCK_STORAGE_KEY } from "./traffic-analytics";
|
|
5
6
|
import packageInfo from "../../package.json";
|
|
6
7
|
|
|
7
8
|
const INSTANCE_ID_KEY = "jx3box:device_id";
|
|
@@ -15,8 +16,8 @@ const STARTUP_DELAY_MAX = 15 * 1000;
|
|
|
15
16
|
|
|
16
17
|
let reportTask = null;
|
|
17
18
|
let scheduledTimer = null;
|
|
18
|
-
let
|
|
19
|
-
|
|
19
|
+
let pendingIdentity = null;
|
|
20
|
+
const PERMISSION_KEY = "jx3box:client-stat-permission:v1";
|
|
20
21
|
const TRAFFIC_PERMISSION_MAX_AGE = 5 * 60 * 1000;
|
|
21
22
|
|
|
22
23
|
function compactPayload(payload) {
|
|
@@ -222,11 +223,28 @@ function getReportSignature() {
|
|
|
222
223
|
}
|
|
223
224
|
|
|
224
225
|
export async function reportClientStat({ force = false } = {}) {
|
|
225
|
-
|
|
226
|
+
const today = chinaDate();
|
|
227
|
+
const signature = getReportSignature();
|
|
228
|
+
const instance = ensureClientInstanceId();
|
|
229
|
+
const identity = JSON.stringify([today, signature, instance]);
|
|
230
|
+
if (reportTask) {
|
|
231
|
+
if (identity === pendingIdentity) return reportTask;
|
|
232
|
+
await reportTask.catch(() => null);
|
|
233
|
+
return reportClientStat({ force });
|
|
234
|
+
}
|
|
226
235
|
|
|
236
|
+
pendingIdentity = identity;
|
|
227
237
|
reportTask = (async () => {
|
|
228
|
-
|
|
229
|
-
|
|
238
|
+
let cached;
|
|
239
|
+
try {
|
|
240
|
+
cached = JSON.parse(localStorage.getItem(PERMISSION_KEY));
|
|
241
|
+
} catch (_) { /* 旧值损坏时重新向服务端确认。 */ }
|
|
242
|
+
if (cached?.identity === identity && cached.until > Date.now()) {
|
|
243
|
+
if (!cached.gate) return { reported: false, reason: "cooldown" };
|
|
244
|
+
if (cached.gate.traffic_allowed === false || !localStorage.getItem(DEFAULT_BLOCK_STORAGE_KEY)) {
|
|
245
|
+
return { reported: false, data: cached.gate };
|
|
246
|
+
}
|
|
247
|
+
}
|
|
230
248
|
if (
|
|
231
249
|
!force &&
|
|
232
250
|
localStorage.getItem(REPORT_DATE_KEY) === today &&
|
|
@@ -235,14 +253,24 @@ export async function reportClientStat({ force = false } = {}) {
|
|
|
235
253
|
return { reported: false, reason: "already_reported" };
|
|
236
254
|
}
|
|
237
255
|
|
|
238
|
-
const payload = buildClientStatPayload(
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
256
|
+
const payload = buildClientStatPayload(instance);
|
|
257
|
+
let data;
|
|
258
|
+
try {
|
|
259
|
+
const response = await $cms({ mute: true }).post("/api/cms/system/stat/heartbeat", payload, {
|
|
260
|
+
timeout: 10000,
|
|
261
|
+
skipForbiddenSessionClear: true,
|
|
262
|
+
});
|
|
263
|
+
data = response?.data?.data;
|
|
264
|
+
} finally {
|
|
265
|
+
const gate = typeof data?.traffic_allowed === "boolean"
|
|
266
|
+
? { traffic_allowed: data.traffic_allowed, blocked: data.blocked || null }
|
|
267
|
+
: null;
|
|
268
|
+
const ttl = gate ? TRAFFIC_PERMISSION_MAX_AGE : 60000 + Math.floor(Math.random() * 60000);
|
|
269
|
+
localStorage.setItem(PERMISSION_KEY, JSON.stringify({ identity, gate, until: Date.now() + ttl }));
|
|
270
|
+
}
|
|
243
271
|
localStorage.setItem(REPORT_DATE_KEY, today);
|
|
244
272
|
localStorage.setItem(REPORT_SIGNATURE_KEY, signature);
|
|
245
|
-
return { reported: true, data:
|
|
273
|
+
return { reported: true, data: data || null, payload };
|
|
246
274
|
})().finally(() => {
|
|
247
275
|
reportTask = null;
|
|
248
276
|
});
|
|
@@ -250,22 +278,15 @@ export async function reportClientStat({ force = false } = {}) {
|
|
|
250
278
|
return reportTask;
|
|
251
279
|
}
|
|
252
280
|
|
|
253
|
-
export async function resolveClientTrafficPermission(
|
|
254
|
-
if (!force && trafficPermission && Date.now() - trafficPermissionCheckedAt < TRAFFIC_PERMISSION_MAX_AGE) {
|
|
255
|
-
return trafficPermission;
|
|
256
|
-
}
|
|
281
|
+
export async function resolveClientTrafficPermission() {
|
|
257
282
|
try {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
if (
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
}
|
|
265
|
-
} catch (error) {
|
|
266
|
-
// Traffic remains fail-closed when heartbeat cannot confirm permission.
|
|
283
|
+
let result = await reportClientStat({ force: true });
|
|
284
|
+
// 并发的普通日检可能只返回“今天已报”;此时仍需取得显式许可。
|
|
285
|
+
if (result.reason === "already_reported") result = await reportClientStat({ force: true });
|
|
286
|
+
return typeof result.data?.traffic_allowed === "boolean" ? result.data : null;
|
|
287
|
+
} catch (_) {
|
|
288
|
+
return null;
|
|
267
289
|
}
|
|
268
|
-
return null;
|
|
269
290
|
}
|
|
270
291
|
|
|
271
292
|
function isLocalhost() {
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const assert = require("node:assert/strict");
|
|
2
|
+
const test = require("node:test");
|
|
3
|
+
const fs = require("node:fs");
|
|
4
|
+
const path = require("node:path");
|
|
5
|
+
const vm = require("node:vm");
|
|
6
|
+
|
|
7
|
+
test("reuses a confirmed permission across page loads and cools down failed requests per identity", async () => {
|
|
8
|
+
const values = new Map([["jx3box:device_id", "fixture-id"]]);
|
|
9
|
+
let uid = 0;
|
|
10
|
+
let requests = 0;
|
|
11
|
+
let fail = false;
|
|
12
|
+
const source = fs.readFileSync(path.join(__dirname, "../src/utils/client-stat.js"), "utf8")
|
|
13
|
+
.replace(/^import .+;$/gm, "").replace(/^export /gm, "");
|
|
14
|
+
function load() {
|
|
15
|
+
const context = {
|
|
16
|
+
window: { location: { hostname: "test.jx3box.com", search: "" } },
|
|
17
|
+
document: {}, URLSearchParams, packageInfo: { version: "test" },
|
|
18
|
+
DEFAULT_BLOCK_STORAGE_KEY: "block",
|
|
19
|
+
User: { isLogin: () => !!uid, getInfo: () => ({ uid }) },
|
|
20
|
+
localStorage: { getItem: k => values.get(k) ?? null, setItem: (k, v) => values.set(k, v) },
|
|
21
|
+
isApp: () => false, isMiniProgram: () => false, applyJx3boxClientStatRules: v => v,
|
|
22
|
+
$cms: () => ({ post: async () => {
|
|
23
|
+
requests++;
|
|
24
|
+
if (fail) throw Error("offline");
|
|
25
|
+
return { data: { data: { traffic_allowed: true } } };
|
|
26
|
+
} }),
|
|
27
|
+
};
|
|
28
|
+
return vm.runInNewContext(source + "\nresolveClientTrafficPermission;", context);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
assert.equal((await load()()).traffic_allowed, true);
|
|
32
|
+
assert.equal((await load()()).traffic_allowed, true);
|
|
33
|
+
assert.equal(requests, 1);
|
|
34
|
+
values.set("block", "robot");
|
|
35
|
+
fail = true;
|
|
36
|
+
assert.equal(await load()(), null);
|
|
37
|
+
assert.equal(await load()(), null);
|
|
38
|
+
assert.equal(requests, 2);
|
|
39
|
+
uid = 10;
|
|
40
|
+
fail = false;
|
|
41
|
+
assert.equal((await load()()).traffic_allowed, true);
|
|
42
|
+
assert.equal(requests, 3);
|
|
43
|
+
});
|