Skip to content

Commit

Permalink
feat: 规范化 subscription-userinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed Feb 27, 2025
1 parent 6efb19c commit fc0aed0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 10 deletions.
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.16.57",
"version": "2.16.58",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
"main": "src/main.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions backend/src/core/proxy-utils/processors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
validCheck,
flowTransfer,
getRmainingDays,
normalizeFlowHeader,
} from '@/utils/flow';

function isObject(item) {
Expand Down Expand Up @@ -1083,6 +1084,7 @@ function createDynamicFunction(name, script, $arguments, $options) {
flowTransfer,
validCheck,
getRmainingDays,
normalizeFlowHeader,
};
if ($.env.isLoon) {
return new Function(
Expand Down
16 changes: 9 additions & 7 deletions backend/src/restful/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { ProxyUtils } from '@/core/proxy-utils';
import { COLLECTIONS_KEY, SUBS_KEY } from '@/constants';
import { findByName } from '@/utils/database';
import { getFlowHeaders } from '@/utils/flow';
import { getFlowHeaders, normalizeFlowHeader } from '@/utils/flow';
import $ from '@/core/app';
import { failed } from '@/restful/response';
import { InternalServerError, ResourceNotFoundError } from '@/restful/errors';
Expand Down Expand Up @@ -259,7 +259,10 @@ async function downloadSubscription(req, res) {
$arguments.flowUrl,
);
if (flowInfo) {
res.set('subscription-userinfo', flowInfo);
res.set(
'subscription-userinfo',
normalizeFlowHeader(flowInfo),
);
}
}
} catch (err) {
Expand Down Expand Up @@ -293,10 +296,9 @@ async function downloadSubscription(req, res) {
}
res.set(
'subscription-userinfo',
[subUserInfo, flowInfo]
.filter((i) => i)
.join('; ')
.replace(/\s*;\s*;\s*/g, ';'),
normalizeFlowHeader(
[subUserInfo, flowInfo].filter((i) => i).join(';'),
),
);
}

Expand Down Expand Up @@ -556,7 +558,7 @@ async function downloadCollection(req, res) {
if (subUserInfo) {
res.set(
'subscription-userinfo',
subUserInfo.replace(/\s*;\s*;\s*/g, ';'),
normalizeFlowHeader(subUserInfo),
);
}
if (platform === 'JSON') {
Expand Down
4 changes: 2 additions & 2 deletions backend/src/restful/file.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { deleteByName, findByName, updateByName } from '@/utils/database';
import { getFlowHeaders } from '@/utils/flow';
import { getFlowHeaders, normalizeFlowHeader } from '@/utils/flow';
import { FILES_KEY } from '@/constants';
import { failed, success } from '@/restful/response';
import $ from '@/core/app';
Expand Down Expand Up @@ -148,7 +148,7 @@ async function getFile(req, res) {
if (flowInfo) {
res.set(
'subscription-userinfo',
flowInfo.replace(/\s*;\s*;\s*/g, ';'),
normalizeFlowHeader(flowInfo),
);
}
}
Expand Down
38 changes: 38 additions & 0 deletions backend/src/utils/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,41 @@ export function getRmainingDays(opt = {}) {
$.error(`getRmainingDays failed: ${e.message ?? e}`);
}
}

export function normalizeFlowHeader(flowHeaders) {
try {
// 使用 Map 保持顺序并处理重复键
const kvMap = new Map();

flowHeaders
.split(';')
.map((p) => p.trim())
.filter(Boolean)
.forEach((pair) => {
const eqIndex = pair.indexOf('=');
if (eqIndex === -1) return;

const key = pair.slice(0, eqIndex).trim();
const encodedValue = pair.slice(eqIndex + 1).trim();

// 只保留第一个出现的 key
if (!kvMap.has(key)) {
try {
// 解码 URI 组件并保留原始值作为 fallback
const decodedValue = decodeURIComponent(encodedValue);
kvMap.set(key, decodedValue);
} catch (e) {
kvMap.set(key, encodedValue);
}
}
});

// 拼接标准化字符串
return Array.from(kvMap.entries())
.map(([k, v]) => `${k}=${encodeURIComponent(v)}`) // 重新编码保持兼容性
.join(';');
} catch (e) {
$.error(`normalizeFlowHeader failed: ${e.message ?? e}`);
return flowHeaders;
}
}

0 comments on commit fc0aed0

Please sign in to comment.