mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-19 20:02:20 -06:00
Merge pull request #1793 from henrybarreto/feat/add-support-socks-proxy
feat: add support to socks proxy
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { HttpsProxyAgent } from 'https-proxy-agent';
|
||||
import { SocksProxyAgent } from 'socks-proxy-agent';
|
||||
|
||||
type Proxy = {
|
||||
host: string;
|
||||
@@ -8,9 +9,28 @@ type Proxy = {
|
||||
username?: string;
|
||||
};
|
||||
|
||||
export function makeProxyAgent(proxy: Proxy | string) {
|
||||
function selectProxyAgent(proxyUrl: string): HttpsProxyAgent<string> | SocksProxyAgent {
|
||||
const url = new URL(proxyUrl);
|
||||
|
||||
// NOTE: The following constants are not used in the function but are defined for clarity.
|
||||
// When a proxy URL is used to build the URL object, the protocol returned by procotol's property contains a `:` at
|
||||
// the end so, we add the protocol constants without the `:` to avoid confusion.
|
||||
const PROXY_HTTP_PROTOCOL = 'http:';
|
||||
const PROXY_SOCKS_PROTOCOL = 'socks:';
|
||||
|
||||
switch (url.protocol) {
|
||||
case PROXY_HTTP_PROTOCOL:
|
||||
return new HttpsProxyAgent(url);
|
||||
case PROXY_SOCKS_PROTOCOL:
|
||||
return new SocksProxyAgent(url);
|
||||
default:
|
||||
throw new Error(`Unsupported proxy protocol: ${url.protocol}`);
|
||||
}
|
||||
}
|
||||
|
||||
export function makeProxyAgent(proxy: Proxy | string): HttpsProxyAgent<string> | SocksProxyAgent {
|
||||
if (typeof proxy === 'string') {
|
||||
return new HttpsProxyAgent(proxy);
|
||||
return selectProxyAgent(proxy);
|
||||
}
|
||||
|
||||
const { host, password, port, protocol, username } = proxy;
|
||||
@@ -19,5 +39,6 @@ export function makeProxyAgent(proxy: Proxy | string) {
|
||||
if (username && password) {
|
||||
proxyUrl = `${protocol}://${username}:${password}@${host}:${port}`;
|
||||
}
|
||||
return new HttpsProxyAgent(proxyUrl);
|
||||
|
||||
return selectProxyAgent(proxyUrl);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user