Update authorization&cartId.js

This commit is contained in:
XXhaos
2026-04-18 12:09:43 +08:00
committed by GitHub
parent 8375c411eb
commit aa6413babb

View File

@@ -1,16 +1,22 @@
/**
* Surge 脚本:捕获 cart 请求的 Authorization 和 cartId
* - 保留原有功能:始终在 $persistentStore 中维护最新的 authorization 和 cartId
* - 新增功能FIFO 配对写入 cartId_history
* · 若 pending_gamertags 有等候的 gamertag → 取最老一条配对
* · 否则把 cart 快照 push 进 pending_carts 等待
* 并在同一时刻根据最近捕获的 gamertag 写入 cartId_history
*
* 保留原有功能:
* - 始终在 $persistentStore 中维护最新的 authorization 和 cartId
*
* 历史记录逻辑:
* - 读取 gamertag_snapshot由 gamertag 脚本写入,格式 {gamertag, ts}
* - 若快照时间在 GAMERTAG_FRESHNESS_MS 窗口内 → 用它 + 本次 cartId/auth 写入历史
* - 若快照过旧或不存在 → 跳过,不写入(宁可漏记也不错记)
* - 相同 cartId 的记录不重复写入
*/
const pattern = /^https:\/\/cart\.production\.store-web\.dynamics\.com\/v1\.0\/Cart\/eligibilityCheck\?/;
const url = $request.url;
const PAIR_WINDOW_MS = 30000;
const MAX_QUEUE = 20;
// gamertag 快照"新鲜度"窗口:超出此时间认为不可信
const GAMERTAG_FRESHNESS_MS = 10000; // 10 秒
const MAX_HISTORY = 10;
if ($request.method === "PUT" && pattern.test(url)) {
@@ -20,6 +26,7 @@ if ($request.method === "PUT" && pattern.test(url)) {
const cartId = urlObj.searchParams.get('cartId');
const now = Date.now();
// 保留原有:维护最新二元组
if (authorization && authorization !== $persistentStore.read("authorization")) {
$persistentStore.write(authorization, "authorization");
console.log(`Stored authorization: ${authorization}`);
@@ -29,8 +36,9 @@ if ($request.method === "PUT" && pattern.test(url)) {
console.log(`Stored cartId: ${cartId}`);
}
// 尝试写入历史
if (cartId && authorization) {
tryPairFromCart({ cartId, authorization, ts: now });
tryAppendHistory(cartId, authorization, now);
}
$notification.post(
@@ -47,68 +55,74 @@ if ($request.method === "PUT" && pattern.test(url)) {
}
}
function tryPairFromCart(cartSnap) {
const now = cartSnap.ts;
let pendingGT = readQueue("pending_gamertags");
pendingGT = pendingGT.filter(g => (now - g.ts) <= PAIR_WINDOW_MS);
if (pendingGT.length > 0) {
const gtSnap = pendingGT.shift();
writeQueue("pending_gamertags", pendingGT);
appendHistory({
gamertag: gtSnap.gamertag,
cartId: cartSnap.cartId,
authorization: cartSnap.authorization,
timestamp: new Date().toISOString()
});
console.log(`[history] ✅ cart 到达触发配对: gamertag=${gtSnap.gamertag}, cartId=${cartSnap.cartId}`);
$notification.post("Surge 历史记录", "新三元组已记入", `${gtSnap.gamertag}`);
} else {
let pendingCart = readQueue("pending_carts");
pendingCart = pendingCart.filter(c => (now - c.ts) <= PAIR_WINDOW_MS);
pendingCart.push(cartSnap);
if (pendingCart.length > MAX_QUEUE) pendingCart = pendingCart.slice(-MAX_QUEUE);
writeQueue("pending_carts", pendingCart);
console.log(`[history] cart 入队等待,当前 pending_carts 长度: ${pendingCart.length}`);
function tryAppendHistory(cartId, authorization, now) {
// 读取 gamertag 快照
const raw = $persistentStore.read("gamertag_snapshot");
if (!raw) {
console.log(`[history] SKIP: gamertag 快照不存在gamertag 脚本还没捕获过)`);
return;
}
}
function readQueue(key) {
const raw = $persistentStore.read(key);
if (!raw) return [];
let snapshot;
try {
const parsed = JSON.parse(raw);
return Array.isArray(parsed) ? parsed : [];
} catch (e) { return []; }
}
snapshot = JSON.parse(raw);
} catch (e) {
console.log(`[history] SKIP: gamertag 快照解析失败`);
return;
}
function writeQueue(key, arr) {
$persistentStore.write(JSON.stringify(arr), key);
}
const { gamertag, ts } = snapshot || {};
if (!gamertag || !ts) {
console.log(`[history] SKIP: gamertag 快照字段不完整`);
return;
}
function appendHistory(entry) {
// 检查快照新鲜度
const age = now - ts;
console.log(`[history] gamertag 快照: ${gamertag}, 距今 ${age}ms`);
if (age > GAMERTAG_FRESHNESS_MS) {
console.log(`[history] SKIP: gamertag 快照过旧 (${age}ms > ${GAMERTAG_FRESHNESS_MS}ms)`);
return;
}
if (age < 0) {
console.log(`[history] SKIP: gamertag 快照时间异常(在未来)`);
return;
}
// 读取历史
let history = [];
const raw = $persistentStore.read("cartId_history");
if (raw) {
const histRaw = $persistentStore.read("cartId_history");
if (histRaw) {
try {
const parsed = JSON.parse(raw);
const parsed = JSON.parse(histRaw);
if (Array.isArray(parsed)) history = parsed;
} catch (e) { history = []; }
}
// 去重:相同 cartId 的记录不重复写入
if (history.some(h => h.cartId === entry.cartId)) {
console.log(`[history] SKIP: cartId=${entry.cartId} 已存在于历史中`);
// 去重:相同 cartId 不重复记录
if (history.some(h => h.cartId === cartId)) {
console.log(`[history] SKIP: cartId=${cartId} 已存在于历史中`);
return;
}
const entry = {
gamertag,
cartId,
authorization,
timestamp: new Date().toISOString()
};
history.push(entry);
if (history.length > MAX_HISTORY) history = history.slice(-MAX_HISTORY);
$persistentStore.write(JSON.stringify(history), "cartId_history");
console.log(`[history] ✅ 写入成功! gamertag=${gamertag}, cartId=${cartId}, total=${history.length}`);
$notification.post(
"Surge 历史记录",
"新三元组已记入",
`${gamertag}`
);
}
$done({});