From f3200aea8c70550444966a38d397b71803aac285 Mon Sep 17 00:00:00 2001 From: xream Date: Mon, 7 Oct 2024 18:34:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B5=81=E9=87=8F=E5=92=8C=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E9=85=8D=E7=BD=AE=E4=B9=9F=E4=BD=BF=E7=94=A8=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E4=BB=A3=E7=90=86/=E7=AD=96=E7=95=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- backend/src/utils/flow.js | 31 +++++++++++++++++++++---- backend/src/utils/gist.js | 49 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 73 insertions(+), 9 deletions(-) diff --git a/backend/package.json b/backend/package.json index 4cd353ee8..e3e06f838 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.14.387", + "version": "2.14.388", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "main": "src/main.js", "scripts": { diff --git a/backend/src/utils/flow.js b/backend/src/utils/flow.js index 84d53acba..46c8f77df 100644 --- a/backend/src/utils/flow.js +++ b/backend/src/utils/flow.js @@ -10,7 +10,13 @@ export function getFlowField(headers) { )[0]; return headers[subkey]; } -export async function getFlowHeaders(rawUrl, ua, timeout, proxy, flowUrl) { +export async function getFlowHeaders( + rawUrl, + ua, + timeout, + customProxy, + flowUrl, +) { let url = flowUrl || rawUrl || ''; let $arguments = {}; const rawArgs = url.split('#'); @@ -41,7 +47,9 @@ export async function getFlowHeaders(rawUrl, ua, timeout, proxy, flowUrl) { // $.info(`使用缓存的流量信息: ${url}`); flowInfo = cached; } else { - const { defaultFlowUserAgent, defaultTimeout } = $.read(SETTINGS_KEY); + const { defaultProxy, defaultFlowUserAgent, defaultTimeout } = + $.read(SETTINGS_KEY); + const proxy = customProxy || defaultProxy; const userAgent = ua || defaultFlowUserAgent || @@ -67,7 +75,7 @@ export async function getFlowHeaders(rawUrl, ua, timeout, proxy, flowUrl) { $.info( `使用 HEAD 方法从响应头获取流量信息: ${url}, User-Agent: ${ userAgent || '' - }`, + }, Proxy: ${proxy}`, ); const { headers } = await http.head({ url: url @@ -97,14 +105,14 @@ export async function getFlowHeaders(rawUrl, ua, timeout, proxy, flowUrl) { $.error( `使用 HEAD 方法从响应头获取流量信息失败: ${url}, User-Agent: ${ userAgent || '' - }: ${e.message ?? e}`, + }, Proxy: ${proxy}: ${e.message ?? e}`, ); } if (!flowInfo) { $.info( `使用 GET 方法获取流量信息: ${url}, User-Agent: ${ userAgent || '' - }`, + }, Proxy: ${proxy}`, ); const { headers } = await http.get({ url: url @@ -113,8 +121,21 @@ export async function getFlowHeaders(rawUrl, ua, timeout, proxy, flowUrl) { .filter((i) => i.length)[0], headers: { 'User-Agent': userAgent, + ...(isStash && proxy + ? { + 'X-Stash-Selected-Proxy': + encodeURIComponent(proxy), + } + : {}), + ...(isShadowRocket && proxy + ? { 'X-Surge-Policy': proxy } + : {}), }, timeout: requestTimeout, + ...(proxy ? { proxy } : {}), + ...(isLoon && proxy ? { node: proxy } : {}), + ...(isQX && proxy ? { opts: { policy: proxy } } : {}), + ...(proxy ? getPolicyDescriptor(proxy) : {}), }); flowInfo = getFlowField(headers); } diff --git a/backend/src/utils/gist.js b/backend/src/utils/gist.js index 78b107508..317603e79 100644 --- a/backend/src/utils/gist.js +++ b/backend/src/utils/gist.js @@ -1,10 +1,17 @@ -import { HTTP } from '@/vendor/open-api'; +import { HTTP, ENV } from '@/vendor/open-api'; +import { getPolicyDescriptor } from '@/utils'; +import $ from '@/core/app'; +import { SETTINGS_KEY } from '@/constants'; /** * Gist backup */ export default class Gist { constructor({ token, key, syncPlatform }) { + const { isStash, isLoon, isShadowRocket, isQX } = ENV(); + const { defaultProxy: proxy, defaultTimeout: timeout } = + $.read(SETTINGS_KEY); + if (syncPlatform === 'gitlab') { this.headers = { 'PRIVATE-TOKEN': `${token}`, @@ -13,7 +20,25 @@ export default class Gist { }; this.http = HTTP({ baseURL: 'https://gitlab.com/api/v4', - headers: { ...this.headers }, + headers: { + ...this.headers, + ...(isStash && proxy + ? { + 'X-Stash-Selected-Proxy': + encodeURIComponent(proxy), + } + : {}), + ...(isShadowRocket && proxy + ? { 'X-Surge-Policy': proxy } + : {}), + }, + + ...(proxy ? { proxy } : {}), + ...(isLoon && proxy ? { node: proxy } : {}), + ...(isQX && proxy ? { opts: { policy: proxy } } : {}), + ...(proxy ? getPolicyDescriptor(proxy) : {}), + timeout, + events: { onResponse: (resp) => { if (/^[45]/.test(String(resp.statusCode))) { @@ -35,7 +60,25 @@ export default class Gist { }; this.http = HTTP({ baseURL: 'https://api.github.com', - headers: { ...this.headers }, + headers: { + ...this.headers, + ...(isStash && proxy + ? { + 'X-Stash-Selected-Proxy': + encodeURIComponent(proxy), + } + : {}), + ...(isShadowRocket && proxy + ? { 'X-Surge-Policy': proxy } + : {}), + }, + + ...(proxy ? { proxy } : {}), + ...(isLoon && proxy ? { node: proxy } : {}), + ...(isQX && proxy ? { opts: { policy: proxy } } : {}), + ...(proxy ? getPolicyDescriptor(proxy) : {}), + timeout, + events: { onResponse: (resp) => { if (/^[45]/.test(String(resp.statusCode))) {