Skip to content

Commit

Permalink
feat: 文件支持设置 查询流量信息订阅链接. 服务器版中使用此链接可在响应中传递订阅流量信息
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoyueLee committed Jun 28, 2024
1 parent bfe072c commit f524920
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 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.14.344",
"version": "2.14.345",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
Expand Down
39 changes: 38 additions & 1 deletion backend/src/restful/file.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { deleteByName, findByName, updateByName } from '@/utils/database';
import { getFlowHeaders } from '@/utils/flow';
import { FILES_KEY } from '@/constants';
import { failed, success } from '@/restful/response';
import $ from '@/core/app';
Expand Down Expand Up @@ -50,7 +51,15 @@ async function getFile(req, res) {
name = decodeURIComponent(name);

$.info(`正在下载文件:${name}`);
let { url, ua, content, mergeSources, ignoreFailedRemoteFile } = req.query;
let {
url,
subInfoUrl,
subInfoUserAgent,
ua,
content,
mergeSources,
ignoreFailedRemoteFile,
} = req.query;
if (url) {
url = decodeURIComponent(url);
$.info(`指定远程文件 URL: ${url}`);
Expand All @@ -59,6 +68,14 @@ async function getFile(req, res) {
ua = decodeURIComponent(ua);
$.info(`指定远程文件 User-Agent: ${ua}`);
}
if (subInfoUrl) {
subInfoUrl = decodeURIComponent(subInfoUrl);
$.info(`指定获取流量的 subInfoUrl: ${subInfoUrl}`);
}
if (subInfoUserAgent) {
subInfoUserAgent = decodeURIComponent(subInfoUserAgent);
$.info(`指定获取流量的 subInfoUserAgent: ${subInfoUserAgent}`);
}
if (content) {
content = decodeURIComponent(content);
$.info(`指定本地文件: ${content}`);
Expand Down Expand Up @@ -86,6 +103,26 @@ async function getFile(req, res) {
ignoreFailedRemoteFile,
});

try {
subInfoUrl = subInfoUrl || file.subInfoUrl;
if (subInfoUrl) {
// forward flow headers
const flowInfo = await getFlowHeaders(
subInfoUrl,
subInfoUserAgent || file.subInfoUserAgent,
);
if (flowInfo) {
res.set('subscription-userinfo', flowInfo);
}
}
} catch (err) {
$.error(
`文件 ${name} 获取流量信息时发生错误: ${JSON.stringify(
err,
)}`,
);
}

res.set('Content-Type', 'text/plain; charset=utf-8').send(
output ?? '',
);
Expand Down

0 comments on commit f524920

Please sign in to comment.