feat: 流量和同步配置也使用默认代理/策略

This commit is contained in:
xream 2024-10-07 18:34:39 +08:00
parent e2346d16a2
commit f3200aea8c
No known key found for this signature in database
GPG Key ID: 1D2C5225471789F9
3 changed files with 73 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "sub-store", "name": "sub-store",
"version": "2.14.387", "version": "2.14.388",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js", "main": "src/main.js",
"scripts": { "scripts": {

View File

@ -10,7 +10,13 @@ export function getFlowField(headers) {
)[0]; )[0];
return headers[subkey]; 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 url = flowUrl || rawUrl || '';
let $arguments = {}; let $arguments = {};
const rawArgs = url.split('#'); const rawArgs = url.split('#');
@ -41,7 +47,9 @@ export async function getFlowHeaders(rawUrl, ua, timeout, proxy, flowUrl) {
// $.info(`使用缓存的流量信息: ${url}`); // $.info(`使用缓存的流量信息: ${url}`);
flowInfo = cached; flowInfo = cached;
} else { } else {
const { defaultFlowUserAgent, defaultTimeout } = $.read(SETTINGS_KEY); const { defaultProxy, defaultFlowUserAgent, defaultTimeout } =
$.read(SETTINGS_KEY);
const proxy = customProxy || defaultProxy;
const userAgent = const userAgent =
ua || ua ||
defaultFlowUserAgent || defaultFlowUserAgent ||
@ -67,7 +75,7 @@ export async function getFlowHeaders(rawUrl, ua, timeout, proxy, flowUrl) {
$.info( $.info(
`使用 HEAD 方法从响应头获取流量信息: ${url}, User-Agent: ${ `使用 HEAD 方法从响应头获取流量信息: ${url}, User-Agent: ${
userAgent || '' userAgent || ''
}`, }, Proxy: ${proxy}`,
); );
const { headers } = await http.head({ const { headers } = await http.head({
url: url url: url
@ -97,14 +105,14 @@ export async function getFlowHeaders(rawUrl, ua, timeout, proxy, flowUrl) {
$.error( $.error(
`使用 HEAD 方法从响应头获取流量信息失败: ${url}, User-Agent: ${ `使用 HEAD 方法从响应头获取流量信息失败: ${url}, User-Agent: ${
userAgent || '' userAgent || ''
}: ${e.message ?? e}`, }, Proxy: ${proxy}: ${e.message ?? e}`,
); );
} }
if (!flowInfo) { if (!flowInfo) {
$.info( $.info(
`使用 GET 方法获取流量信息: ${url}, User-Agent: ${ `使用 GET 方法获取流量信息: ${url}, User-Agent: ${
userAgent || '' userAgent || ''
}`, }, Proxy: ${proxy}`,
); );
const { headers } = await http.get({ const { headers } = await http.get({
url: url url: url
@ -113,8 +121,21 @@ export async function getFlowHeaders(rawUrl, ua, timeout, proxy, flowUrl) {
.filter((i) => i.length)[0], .filter((i) => i.length)[0],
headers: { headers: {
'User-Agent': userAgent, 'User-Agent': userAgent,
...(isStash && proxy
? {
'X-Stash-Selected-Proxy':
encodeURIComponent(proxy),
}
: {}),
...(isShadowRocket && proxy
? { 'X-Surge-Policy': proxy }
: {}),
}, },
timeout: requestTimeout, timeout: requestTimeout,
...(proxy ? { proxy } : {}),
...(isLoon && proxy ? { node: proxy } : {}),
...(isQX && proxy ? { opts: { policy: proxy } } : {}),
...(proxy ? getPolicyDescriptor(proxy) : {}),
}); });
flowInfo = getFlowField(headers); flowInfo = getFlowField(headers);
} }

View File

@ -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 * Gist backup
*/ */
export default class Gist { export default class Gist {
constructor({ token, key, syncPlatform }) { constructor({ token, key, syncPlatform }) {
const { isStash, isLoon, isShadowRocket, isQX } = ENV();
const { defaultProxy: proxy, defaultTimeout: timeout } =
$.read(SETTINGS_KEY);
if (syncPlatform === 'gitlab') { if (syncPlatform === 'gitlab') {
this.headers = { this.headers = {
'PRIVATE-TOKEN': `${token}`, 'PRIVATE-TOKEN': `${token}`,
@ -13,7 +20,25 @@ export default class Gist {
}; };
this.http = HTTP({ this.http = HTTP({
baseURL: 'https://gitlab.com/api/v4', 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: { events: {
onResponse: (resp) => { onResponse: (resp) => {
if (/^[45]/.test(String(resp.statusCode))) { if (/^[45]/.test(String(resp.statusCode))) {
@ -35,7 +60,25 @@ export default class Gist {
}; };
this.http = HTTP({ this.http = HTTP({
baseURL: 'https://api.github.com', 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: { events: {
onResponse: (resp) => { onResponse: (resp) => {
if (/^[45]/.test(String(resp.statusCode))) { if (/^[45]/.test(String(resp.statusCode))) {