Files
RSSHub/lib/utils/proxy/index.ts
T
dependabot[bot]andTony 873618e99d chore(deps-dev): bump eslint-plugin-unicorn from 65.0.1 to 66.0.0 (#22274)
* chore(deps-dev): bump eslint-plugin-unicorn from 65.0.1 to 66.0.0

Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 65.0.1 to 66.0.0.
- [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases)
- [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v65.0.1...v66.0.0)

---
updated-dependencies:
- dependency-name: eslint-plugin-unicorn
  dependency-version: 66.0.0
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* style: fix unicorn-js/prefer-url-href

* style: oxlint disable

* style: fix unicorn-js/no-useless-else

* style: no-unsafe-string-replacement

* style: fix no-duplicate-loops

* style: fix prefer-number-is-safe-integer

* style: fix prefer-single-array-predicate

* style: fix no-object-methods-with-collections

* style: fix no-useless-boolean-cast

* style: fix no-useless-template-literals

* style: fix no-negated-array-predicate

* style: fix prefer-direct-iteration

* style: fix consistent-optional-chaining

* style: fix prefer-simple-sort-comparator

* style: fix prefer-global-number-constants

* style: fix prefer-type-literal-last

* style: fix require-array-sort-compare.

* style: fix prefer-early-return

* style: fix prefer-short-arrow-method

* style: fix prefer-array-from-map

* style: fix prefer-scoped-selector

* style: fix no-global-object-property-assignment

* style: fix no-unnecessary-global-this

* style: fix no-unreadable-object-destructuring

* style: fix no-invalid-argument-count

* style: fix prefer-smaller-scope

* style: fix prefer-iterator-to-array.

* style: fix prefer-object-iterable-methods

* style: fix typo

* style: fix max-nested-calls

* style: fix no-incorrect-template-string-interpolation

* style: fix no-break-in-nested-loop

* style: fix prefer-await

* style: fix no-computed-property-existence-check

* style: fix prefer-number-coercion

* style: fix no-empty-file

* style: update eslint unicorn v66 recommended preset

* style: add unicorn/prefer-spread to oxlint-disable comments

* style: fix no-array-reduce

* style: delete github/no-then as it's covered by unicorn/prefer-await

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tony <TonyRL@users.noreply.github.com>
2026-06-16 14:43:42 +08:00

145 lines
4.5 KiB
TypeScript

import { HttpsProxyAgent } from 'https-proxy-agent';
import { PacProxyAgent } from 'pac-proxy-agent';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { ProxyAgent } from 'undici';
import { config } from '@/config';
import logger from '@/utils/logger';
import type { MultiProxyResult, ProxyState } from './multi-proxy';
import createMultiProxy from './multi-proxy';
import pacProxy from './pac-proxy';
import unifyProxy from './unify-proxy';
const proxyIsPAC = config.pacUri || config.pacScript;
interface ProxyExport {
agent: PacProxyAgent<string> | HttpsProxyAgent<string> | SocksProxyAgent | null;
dispatcher: ProxyAgent | null;
proxyUri?: string;
proxyObj: Record<string, any>;
proxyUrlHandler?: URL | null;
multiProxy?: MultiProxyResult;
getCurrentProxy: () => ProxyState | null;
markProxyFailed: (proxyUri: string) => void;
getAgentForProxy: (proxyState: ProxyState) => any;
getDispatcherForProxy: (proxyState: ProxyState) => ProxyAgent | null;
}
let proxyUri: string | undefined;
let proxyObj: Record<string, any> = {};
let proxyUrlHandler: URL | null = null;
let multiProxy: MultiProxyResult | undefined;
const createAgentForProxy = (uri: string, proxyObj: Record<string, any>): any => {
if (uri.startsWith('http')) {
return new HttpsProxyAgent(uri, {
headers: {
'proxy-authorization': proxyObj?.auth ? `Basic ${proxyObj.auth}` : undefined,
},
});
}
if (uri.startsWith('socks')) {
return new SocksProxyAgent(uri);
}
return null;
};
const createDispatcherForProxy = (uri: string, proxyObj: Record<string, any>): ProxyAgent | null => {
if (uri.startsWith('http')) {
return new ProxyAgent({
uri,
token: proxyObj?.auth ? `Basic ${proxyObj.auth}` : undefined,
requestTls: {
rejectUnauthorized: process.env.NODE_TLS_REJECT_UNAUTHORIZED !== '0',
},
});
}
return null;
};
if (proxyIsPAC) {
const proxy = pacProxy(config.pacUri, config.pacScript, config.proxy);
proxyUri = proxy.proxyUri;
proxyObj = proxy.proxyObj;
proxyUrlHandler = proxy.proxyUrlHandler;
} else if (config.proxyUris && config.proxyUris.length > 0) {
multiProxy = createMultiProxy(config.proxyUris, config.proxy);
proxyObj = multiProxy.proxyObj;
const currentProxy = multiProxy.getNextProxy();
if (currentProxy) {
proxyUri = currentProxy.uri;
proxyUrlHandler = currentProxy.urlHandler;
}
logger.info(`Multi-proxy initialized with ${config.proxyUris.length} proxies`);
} else {
const proxy = unifyProxy(config.proxyUri, config.proxy);
proxyUri = proxy.proxyUri;
proxyObj = proxy.proxyObj;
proxyUrlHandler = proxy.proxyUrlHandler;
}
let agent: PacProxyAgent<string> | HttpsProxyAgent<string> | SocksProxyAgent | null = null;
let dispatcher: ProxyAgent | null = null;
if (proxyIsPAC && proxyUri) {
agent = new PacProxyAgent(`pac+${proxyUri}`);
} else if (proxyUri) {
agent = createAgentForProxy(proxyUri, proxyObj);
dispatcher = createDispatcherForProxy(proxyUri, proxyObj);
}
const getCurrentProxy = (): ProxyState | null => {
if (multiProxy) {
return multiProxy.getNextProxy();
}
if (proxyUri) {
return {
uri: proxyUri,
isActive: true,
failureCount: 0,
urlHandler: proxyUrlHandler,
};
}
return null;
};
const markProxyFailed = (failedProxyUri: string) => {
if (!multiProxy) {
return;
}
multiProxy.markProxyFailed(failedProxyUri);
const nextProxy = multiProxy.getNextProxy();
if (nextProxy) {
proxyUri = nextProxy.uri;
proxyUrlHandler = nextProxy.urlHandler || null;
agent = createAgentForProxy(nextProxy.uri, proxyObj);
dispatcher = createDispatcherForProxy(nextProxy.uri, proxyObj);
logger.info(`Switched to proxy: ${nextProxy.uri}`);
} else {
logger.warn('No available proxies remaining');
agent = null;
dispatcher = null;
proxyUri = undefined;
}
};
const getAgentForProxy = (proxyState: ProxyState) => createAgentForProxy(proxyState.uri, proxyObj);
const getDispatcherForProxy = (proxyState: ProxyState) => createDispatcherForProxy(proxyState.uri, proxyObj);
const proxyExport: ProxyExport = {
agent,
dispatcher,
proxyUri,
proxyObj,
proxyUrlHandler,
multiProxy,
getCurrentProxy,
markProxyFailed,
getAgentForProxy,
getDispatcherForProxy,
};
export default proxyExport;