Skip to content

Commit

Permalink
feat: 支持使用代理/节点获取订阅
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed Mar 12, 2024
1 parent ca65e42 commit cd352cd
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 15 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.249",
"version": "2.14.250",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
Expand Down
14 changes: 12 additions & 2 deletions backend/src/restful/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ async function downloadSubscription(req, res) {
}
if (!$arguments.noFlow) {
// forward flow headers
const flowInfo = await getFlowHeaders(url);
const flowInfo = await getFlowHeaders(
url,
undefined,
undefined,
sub.proxy,
);
if (flowInfo) {
res.set('subscription-userinfo', flowInfo);
}
Expand Down Expand Up @@ -243,7 +248,12 @@ async function downloadCollection(req, res) {
}
}
if (!$arguments.noFlow) {
const flowInfo = await getFlowHeaders(url);
const flowInfo = await getFlowHeaders(
url,
undefined,
undefined,
sub.proxy,
);
if (flowInfo) {
res.set('subscription-userinfo', flowInfo);
}
Expand Down
14 changes: 12 additions & 2 deletions backend/src/restful/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ async function compareSub(req, res) {
.filter((i) => i.length)
.map(async (url) => {
try {
return await download(url, sub.ua);
return await download(
url,
sub.ua,
undefined,
sub.proxy,
);
} catch (err) {
errors[url] = err;
$.error(
Expand Down Expand Up @@ -195,7 +200,12 @@ async function compareCollection(req, res) {
.filter((i) => i.length)
.map(async (url) => {
try {
return await download(url, sub.ua);
return await download(
url,
sub.ua,
undefined,
sub.proxy,
);
} catch (err) {
errors[url] = err;
$.error(
Expand Down
7 changes: 6 additions & 1 deletion backend/src/restful/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ async function getFlowInfo(req, res) {
}),
});
} else {
const flowHeaders = await getFlowHeaders(url);
const flowHeaders = await getFlowHeaders(
url,
undefined,
undefined,
sub.proxy,
);
if (!flowHeaders) {
failed(
res,
Expand Down
21 changes: 18 additions & 3 deletions backend/src/restful/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ async function produceArtifact({
.filter((i) => i.length)
.map(async (url) => {
try {
return await download(url, ua || sub.ua);
return await download(
url,
ua || sub.ua,
undefined,
sub.proxy,
);
} catch (err) {
errors[url] = err;
$.error(
Expand Down Expand Up @@ -102,7 +107,12 @@ async function produceArtifact({
.filter((i) => i.length)
.map(async (url) => {
try {
return await download(url, ua || sub.ua);
return await download(
url,
ua || sub.ua,
undefined,
sub.proxy,
);
} catch (err) {
errors[url] = err;
$.error(
Expand Down Expand Up @@ -198,7 +208,12 @@ async function produceArtifact({
.filter((i) => i.length)
.map(async (url) => {
try {
return await download(url, sub.ua);
return await download(
url,
sub.ua,
undefined,
sub.proxy,
);
} catch (err) {
errors[url] = err;
$.error(
Expand Down
17 changes: 13 additions & 4 deletions backend/src/utils/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import $ from '@/core/app';

const tasks = new Map();

export default async function download(rawUrl, ua, timeout) {
export default async function download(rawUrl, ua, timeout, proxy) {
let $arguments = {};
let url = rawUrl.replace(/#noFlow$/, '');
const rawArgs = url.split('#');
Expand Down Expand Up @@ -78,10 +78,15 @@ export default async function download(rawUrl, ua, timeout) {
result = cached;
} else {
$.info(
`Downloading...\nUser-Agent: ${userAgent}\nTimeout: ${requestTimeout}\nURL: ${url}`,
`Downloading...\nUser-Agent: ${userAgent}\nTimeout: ${requestTimeout}\nProxy: ${proxy}\nURL: ${url}`,
);
try {
const { body, headers } = await http.get(url);
const { body, headers } = await http.get({
url,
proxy,
node: proxy,
'policy-descriptor': proxy,
});

if (headers) {
const flowInfo = getFlowField(headers);
Expand Down Expand Up @@ -116,7 +121,11 @@ export default async function download(rawUrl, ua, timeout) {
// 检查订阅有效性

if ($arguments?.validCheck) {
await validCheck(parseFlowHeaders(await getFlowHeaders(url)));
await validCheck(
parseFlowHeaders(
await getFlowHeaders(url, undefined, undefined, proxy),
),
);
}

if (!isNode) {
Expand Down
7 changes: 5 additions & 2 deletions backend/src/utils/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function getFlowField(headers) {
)[0];
return headers[subkey];
}
export async function getFlowHeaders(rawUrl, ua, timeout) {
export async function getFlowHeaders(rawUrl, ua, timeout, proxy) {
let url = rawUrl;
let $arguments = {};
const rawArgs = url.split('#');
Expand Down Expand Up @@ -57,6 +57,9 @@ export async function getFlowHeaders(rawUrl, ua, timeout) {
'User-Agent': userAgent,
},
timeout: requestTimeout,
proxy,
node: proxy,
'policy-descriptor': proxy,
});
flowInfo = getFlowField(headers);
} catch (e) {
Expand Down Expand Up @@ -178,7 +181,7 @@ export function getRmainingDays(opt = {}) {

return daysDiff;
} else {
if (!resetDay) throw new Error('未提供月重置日 resetDay');
if (!resetDay) return;
resetDay = parseInt(resetDay);
if (isNaN(resetDay) || resetDay <= 0 || resetDay > 31)
throw new Error('月重置日应为 1-31 之间的整数');
Expand Down

0 comments on commit cd352cd

Please sign in to comment.