mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-10-16 16:44:53 +08:00
Since #22401 every socks* PROXY_URI is handed to undici's ProxyAgent, which only accepts socks5: and socks:. A socks5h, socks4 or socks4a URI fell through to its HTTP proxy path and threw "Invalid URL protocol" while the proxy module loaded, so the server never started, even though unify-proxy listed all of them as supported. Pass socks5h to undici as socks5: its SOCKS5 client sends host names to the proxy, so DNS still resolves there, as playwright.ts already assumes. undici has no SOCKS4 support, so drop socks4 and socks4a from the accepted protocols, which logs the supported list and disables the proxy like any other unsupported protocol.
155 lines
5.7 KiB
TypeScript
155 lines
5.7 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
|
|
import type { Config } from '@/config';
|
|
import unifyProxy, { unifyProxies } from '@/utils/proxy/unify-proxy';
|
|
|
|
const emptyProxyObj: Config['proxy'] = {
|
|
protocol: undefined,
|
|
host: undefined,
|
|
port: undefined,
|
|
auth: undefined,
|
|
url_regex: '.*',
|
|
strategy: 'all',
|
|
};
|
|
|
|
const effectiveExpect = ({ proxyUri, proxyObj }, expectUri, expectObj) => {
|
|
expect(proxyUri).toBe(expectUri);
|
|
expect(proxyObj).toEqual(expectObj);
|
|
};
|
|
|
|
describe('unify-proxy', () => {
|
|
const nullExpect = (unified) => effectiveExpect(unified, undefined, emptyProxyObj);
|
|
it('proxy empty', () => {
|
|
nullExpect(unifyProxy('', emptyProxyObj));
|
|
});
|
|
it('proxy-uri invalid', () => {
|
|
nullExpect(unifyProxy('http://inv lid.test', emptyProxyObj));
|
|
});
|
|
it('proxy-uri invalid protocol', () => {
|
|
nullExpect(unifyProxy('ftp://rsshub.proxy', emptyProxyObj));
|
|
});
|
|
it('proxy-obj no host', () => {
|
|
nullExpect(unifyProxy('', { ...emptyProxyObj, protocol: 'http', port: '2333' }));
|
|
});
|
|
it('proxy-obj invalid host', () => {
|
|
nullExpect(unifyProxy('', { ...emptyProxyObj, protocol: 'http', host: 'inv lid.test', port: '2333' }));
|
|
});
|
|
it('proxy-obj invalid protocol', () => {
|
|
nullExpect(unifyProxy('', { ...emptyProxyObj, protocol: 'ftp', host: 'rsshub.proxy', port: '2333' }));
|
|
});
|
|
it('proxy-uri socks4 unsupported', () => {
|
|
nullExpect(unifyProxy('socks4://rsshub.proxy:2333', emptyProxyObj));
|
|
nullExpect(unifyProxy('socks4a://rsshub.proxy:2333', emptyProxyObj));
|
|
});
|
|
|
|
const httpNoPortUri = 'http://rsshub.proxy';
|
|
const httpNoPortObj = { ...emptyProxyObj, protocol: 'http', host: 'rsshub.proxy' };
|
|
const httpNoPortExpect = (unified) => effectiveExpect(unified, httpNoPortUri, httpNoPortObj);
|
|
it('proxy-uri http no port', () => {
|
|
httpNoPortExpect(unifyProxy(httpNoPortUri, emptyProxyObj));
|
|
});
|
|
it('proxy-obj http no port', () => {
|
|
httpNoPortExpect(unifyProxy('', httpNoPortObj));
|
|
});
|
|
|
|
const httpUri = 'http://rsshub.proxy:2333';
|
|
const httpObj = { ...httpNoPortObj, port: '2333' };
|
|
const httpExpect = (unified) => effectiveExpect(unified, httpUri, httpObj);
|
|
it('proxy-uri http', () => {
|
|
httpExpect(unifyProxy(httpUri, emptyProxyObj));
|
|
});
|
|
it('proxy-obj http', () => {
|
|
httpExpect(unifyProxy('', httpObj));
|
|
});
|
|
|
|
const httpsUri = 'https://rsshub.proxy:2333';
|
|
const httpsObj = { ...httpObj, protocol: 'https' };
|
|
const httpsExpect = (unified) => effectiveExpect(unified, httpsUri, httpsObj);
|
|
it('proxy-uri https', () => {
|
|
httpsExpect(unifyProxy(httpsUri, emptyProxyObj));
|
|
});
|
|
it('proxy-obj https', () => {
|
|
httpsExpect(unifyProxy('', httpsObj));
|
|
});
|
|
|
|
const socks5Uri = 'socks5://rsshub.proxy:2333';
|
|
const socks5Obj = { ...httpObj, protocol: 'socks5' };
|
|
const socks5Expect = (unified) => effectiveExpect(unified, socks5Uri, socks5Obj);
|
|
it('proxy-uri socks5', () => {
|
|
socks5Expect(unifyProxy(socks5Uri, emptyProxyObj));
|
|
});
|
|
it('proxy-obj socks5', () => {
|
|
socks5Expect(unifyProxy('', socks5Obj));
|
|
});
|
|
|
|
const overrideObj = { ...emptyProxyObj, protocol: 'http', host: 'over.ride', port: '6666' };
|
|
it('proxy-uri override proxy-obj {PROTOCAL,HOST,PORT}', () => {
|
|
socks5Expect(unifyProxy(socks5Uri, overrideObj));
|
|
});
|
|
|
|
const noProtocolUri = 'rsshub.proxy:2333';
|
|
it('proxy-uri no protocol', () => {
|
|
httpExpect(unifyProxy(noProtocolUri, emptyProxyObj));
|
|
});
|
|
|
|
const noProtocolObj = { ...httpObj, protocol: undefined };
|
|
it('proxy-obj no protocol', () => {
|
|
httpExpect(unifyProxy('', noProtocolObj));
|
|
});
|
|
|
|
const protocolInHostObj = { ...httpObj, host: httpNoPortUri, protocol: undefined };
|
|
it('proxy-obj protocol in host', () => {
|
|
httpExpect(unifyProxy('', protocolInHostObj));
|
|
});
|
|
|
|
const portInHostObj = { ...httpNoPortObj, host: httpUri };
|
|
it('proxy-obj port in host', () => {
|
|
httpExpect(unifyProxy('', portInHostObj));
|
|
});
|
|
|
|
const everythingInHostObj = { ...emptyProxyObj, host: httpUri };
|
|
it('proxy-obj everything in host', () => {
|
|
httpExpect(unifyProxy('', everythingInHostObj));
|
|
});
|
|
|
|
const portBothObj = { ...portInHostObj, port: '6666' };
|
|
it('proxy-obj port in host override proxy-obj port', () => {
|
|
httpExpect(unifyProxy('', portBothObj));
|
|
});
|
|
|
|
const PortNaNObj = { ...httpNoPortObj, port: 'test' };
|
|
it('proxy-obj port NaN', () => {
|
|
httpNoPortExpect(unifyProxy('', PortNaNObj));
|
|
});
|
|
|
|
const httpsAuthUri = 'https://user:pass@rsshub.proxy:2333';
|
|
it('proxy-uri https auth', () => {
|
|
effectiveExpect(unifyProxy(httpsAuthUri, emptyProxyObj), httpsAuthUri, httpsObj);
|
|
});
|
|
|
|
const httpsAuthObj = { ...httpsObj, auth: 'testtest' };
|
|
it('proxy-obj https auth', () => {
|
|
effectiveExpect(unifyProxy('', httpsAuthObj), httpsUri, httpsAuthObj);
|
|
});
|
|
|
|
const socks5AuthUri = 'socks5://user:pass@rsshub.proxy:2333';
|
|
it('proxy-uri socks5 auth', () => {
|
|
effectiveExpect(unifyProxy(socks5AuthUri, emptyProxyObj), socks5AuthUri, socks5Obj);
|
|
});
|
|
|
|
const socks5AuthObj = { ...socks5Obj, auth: 'testtest' };
|
|
it('proxy-obj socks5 auth (invalid)', () => {
|
|
socks5Expect(unifyProxy('', socks5AuthObj));
|
|
});
|
|
|
|
it('proxy-uri user@pass override proxy-obj auth', () => {
|
|
effectiveExpect(unifyProxy(httpsAuthUri, httpsAuthObj), httpsAuthUri, httpsObj);
|
|
});
|
|
|
|
it('unifyProxies filters invalid proxy uris', () => {
|
|
const results = unifyProxies(['http://rsshub.proxy:2333', 'http://inv lid.test'], emptyProxyObj);
|
|
expect(results).toHaveLength(1);
|
|
expect(results[0].proxyUri).toBe('http://rsshub.proxy:2333');
|
|
});
|
|
});
|