Files
RSSHub/lib/utils/proxy/unify-proxy.ts
T
dependabot[bot]andTony 417ab04f06 chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0 (#22310)
* chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0

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

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

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

* style: fix no-useless-coercion

* style: fix prefer-boolean-return

* style: fix no-useless-logical-operand

* style: fix prefer-single-replace

* style: fix consistent-conditional-object-spread

* style: fix no-useless-else

* style: fix prefer-unicode-code-point-escapes

* style: fix operator-assignment

* style: fix prefer-continue

* style: fix prefer-math-constants

* style: fix prefer-hoisting-branch-code

* style: fix prefer-array-slice

* style: fix consistent-class-member-order

* style: fix no-return-array-push

* style: fix prefer-promise-with-resolvers

* style: fix no-array-concat-in-loop

* style: fix no-loop-iterable-mutation

* style: fix no-invalid-argument-count

* style: fix no-duplicate-if-branches

* style: fix prefer-else-if

* style: fix no-unreadable-for-of-expression

* style: fix logical-assignment-operators

* style: fix default-export-style

* style: revert false positve in prefer-type-literal-last in #22274

* style: fix arrow-body-style

* style: add v68 recommended preset

* style: refactor GraphQL queries to use template literals for better readability

---------

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-20 23:11:27 +08:00

116 lines
5.2 KiB
TypeScript

import type { Config } from '@/config';
import logger from '@/utils/logger';
const defaultProtocol = 'http';
const possibleProtocol = ['http', 'https', 'socks', 'socks4', 'socks4a', 'socks5', 'socks5h'];
const unifyProxy = (proxyUri: Config['proxyUri'] | string, proxyObj: Config['proxy']) => {
proxyObj ||= {};
const [oriProxyUri, oriProxyObj] = [proxyUri, proxyObj];
proxyObj = { ...proxyObj };
let proxyUrlHandler: URL | null = null;
// PROXY_URI
if (proxyUri && typeof proxyUri === 'string') {
if (!proxyUri.includes('://')) {
logger.warn(`PROXY_URI contains no protocol, assuming ${defaultProtocol}`);
proxyUri = `${defaultProtocol}://${proxyUri}`;
}
try {
proxyUrlHandler = new URL(proxyUri);
} catch (error: any) {
logger.error(`Parse PROXY_URI error: ${error.stack}`);
}
}
// PROXY_{PROTOCOL,HOST,PORT}
if (proxyObj.protocol || proxyObj.host || proxyObj.port) {
if (proxyUrlHandler) {
logger.warn('PROXY_URI is set, ignoring PROXY_{PROTOCOL,HOST,PORT}');
} else if (proxyObj.host) {
let tempProxyStr = proxyObj.host;
if (tempProxyStr.includes('://')) {
logger.warn('PROXY_HOST contains protocol, ignoring PROXY_PROTOCOL');
} else if (proxyObj.protocol) {
tempProxyStr = `${proxyObj.protocol}://${tempProxyStr}`;
} else {
logger.warn(`PROXY_PROTOCOL is not set, assuming '${defaultProtocol}'`);
tempProxyStr = `${defaultProtocol}://${tempProxyStr}`;
}
try {
proxyUrlHandler = new URL(tempProxyStr);
if (proxyUrlHandler.port && proxyObj.port) {
logger.warn('PROXY_HOST contains port, ignoring PROXY_PORT');
} else if (proxyObj.port) {
if (Number.parseInt(proxyObj.port)) {
proxyUrlHandler.port = proxyObj.port;
} else {
logger.warn('PROXY_PORT is not a number, ignoring');
}
} else {
logger.warn('PROXY_PORT is not set, leaving proxy agent to determine');
}
} catch (error: any) {
logger.error(`Parse PROXY_HOST error: ${error.stack}`);
}
} else {
logger.warn('Either PROXY_{PROTOCOL,PORT} is set, but PROXY_HOST is missing, ignoring');
}
}
// PROXY_AUTH
if (proxyObj.auth && proxyUrlHandler) {
let promptProxyUri = false;
if (proxyUrlHandler.username || proxyUrlHandler.password) {
logger.warn('PROXY_URI contains username and/or password, ignoring PROXY_AUTH');
proxyObj.auth = undefined;
} else if (['http:', 'https:'].includes(proxyUrlHandler.protocol)) {
logger.info('PROXY_AUTH is set and will be used for requests from Node.js. However, requests from Playwright will not use it');
promptProxyUri = true;
} else {
logger.warn(`PROXY_AUTH is only supported by HTTP(S) proxies, but got ${proxyUrlHandler.protocol}, ignoring`);
proxyObj.auth = undefined;
promptProxyUri = true;
}
if (promptProxyUri) {
logger.info('To get rid of this, set PROXY_URI like protocol://username:password@host:port and clear PROXY_{AUTH,PROTOCOL,HOST,PORT}');
}
}
// is proxy enabled and valid?
let isProxyValid = false;
if (proxyUrlHandler) {
const protocol = proxyUrlHandler.protocol.replace(':', '');
if (possibleProtocol.includes(protocol)) {
if (protocol !== 'http' && (proxyUrlHandler.username || proxyUrlHandler.password)) {
logger.warn("PROXY_URI is an HTTPS/SOCKS proxy with authentication, which is not supported by Playwright (ignore if you don't need it)");
logger.info('To get rid of this, consider using an HTTP proxy instead');
}
proxyObj.protocol = protocol;
proxyObj.host = proxyUrlHandler.hostname;
proxyObj.port = proxyUrlHandler.port || undefined;
// Trailing slash can make Chromium reject the proxy URL, trim it.
proxyUri = proxyUrlHandler.href.endsWith('/') ? proxyUrlHandler.href.slice(0, -1) : proxyUrlHandler.href;
isProxyValid = true;
} else {
logger.error(`Unsupported proxy protocol: ${protocol}, expect one of ${possibleProtocol.join(', ')}`);
}
}
if (!isProxyValid) {
if ((oriProxyUri && typeof oriProxyUri === 'string') || oriProxyObj.protocol || oriProxyObj.host || oriProxyObj.port || oriProxyObj.auth) {
logger.error('Proxy is disabled due to misconfiguration');
}
proxyObj.protocol = proxyObj.host = proxyObj.port = proxyObj.auth = undefined;
proxyUri = undefined;
proxyUrlHandler = null;
}
return { proxyUri, proxyObj, proxyUrlHandler };
};
export const unifyProxies = (proxyUris: string[], proxyObj: Config['proxy']) => proxyUris.map((uri) => unifyProxy(uri, proxyObj)).filter((result) => result.proxyUri);
export default unifyProxy;