Skip to content
8 changes: 5 additions & 3 deletions src/utils/makeProxyAgent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpsProxyAgent } from 'https-proxy-agent';

Check failure on line 1 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Run autofix to sort these imports!
import { SocksProxyAgent } from 'socks-proxy-agent';

import { ProxyAgent } from 'undici'

Check failure on line 4 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`
Expand All @@ -19,11 +19,13 @@
// the end so, we add the protocol constants without the `:` to avoid confusion.
const PROXY_HTTP_PROTOCOL = 'http:';
const PROXY_SOCKS_PROTOCOL = 'socks:';
const PROXY_SOCKS5_PROTOCOL = 'socks5:';

switch (url.protocol) {
case PROXY_HTTP_PROTOCOL:
return new HttpsProxyAgent(url);
case PROXY_SOCKS_PROTOCOL:
case PROXY_SOCKS5_PROTOCOL:
return new SocksProxyAgent(url);
default:
throw new Error(`Unsupported proxy protocol: ${url.protocol}`);
Expand All @@ -45,16 +47,16 @@
return selectProxyAgent(proxyUrl);
}

export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent {
export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent|SocksProxyAgent {

Check failure on line 50 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Replace `|` with `·|·`
let proxyUrl: string

Check failure on line 51 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`
let protocol: string

Check failure on line 52 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`

if (typeof proxy === 'string') {
const url = new URL(proxy)

Check failure on line 55 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`
protocol = url.protocol.replace(':', '')

Check failure on line 56 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`
proxyUrl = proxy

Check failure on line 57 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`
} else {
const { host, password, port, protocol: proto, username } = proxy

Check failure on line 59 in src/utils/makeProxyAgent.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `;`
protocol = (proto || 'http').replace(':', '')

if (protocol === 'socks') {
Expand All @@ -73,10 +75,10 @@
switch (protocol) {
case PROXY_HTTP_PROTOCOL:
case PROXY_HTTPS_PROTOCOL:
return new ProxyAgent(proxyUrl)
case PROXY_SOCKS4_PROTOCOL:
case PROXY_SOCKS5_PROTOCOL:
return new ProxyAgent(proxyUrl)

return new SocksProxyAgent(proxyUrl)
default:
throw new Error(`Unsupported proxy protocol: ${protocol}`)
}
Expand Down
Loading