upd: megalodon to v7
This commit is contained in:
parent
b4674ce65c
commit
afda15260f
234 changed files with 21334 additions and 7675 deletions
|
|
@ -1,92 +1,50 @@
|
|||
import { HttpsProxyAgent, HttpsProxyAgentOptions } from "https-proxy-agent";
|
||||
import { SocksProxyAgent, SocksProxyAgentOptions } from "socks-proxy-agent";
|
||||
import { HttpsProxyAgent } from 'https-proxy-agent'
|
||||
import { SocksProxyAgent } from 'socks-proxy-agent'
|
||||
|
||||
export type ProxyConfig = {
|
||||
host: string;
|
||||
port: number;
|
||||
auth?: {
|
||||
username: string;
|
||||
password: string;
|
||||
};
|
||||
protocol:
|
||||
| "http"
|
||||
| "https"
|
||||
| "socks4"
|
||||
| "socks4a"
|
||||
| "socks5"
|
||||
| "socks5h"
|
||||
| "socks";
|
||||
};
|
||||
host: string
|
||||
port: number
|
||||
auth?: {
|
||||
username: string
|
||||
password: string
|
||||
}
|
||||
protocol: 'http' | 'https' | 'socks4' | 'socks4a' | 'socks5' | 'socks5h' | 'socks'
|
||||
}
|
||||
|
||||
class ProxyProtocolError extends Error {}
|
||||
|
||||
const proxyAgent = (
|
||||
proxyConfig: ProxyConfig,
|
||||
): HttpsProxyAgent | SocksProxyAgent => {
|
||||
switch (proxyConfig.protocol) {
|
||||
case "http": {
|
||||
let options: HttpsProxyAgentOptions = {
|
||||
host: proxyConfig.host,
|
||||
port: proxyConfig.port,
|
||||
secureProxy: false,
|
||||
};
|
||||
if (proxyConfig.auth) {
|
||||
options = Object.assign(options, {
|
||||
auth: `${proxyConfig.auth.username}:${proxyConfig.auth.password}`,
|
||||
});
|
||||
}
|
||||
const httpsAgent = new HttpsProxyAgent(options);
|
||||
return httpsAgent;
|
||||
}
|
||||
case "https": {
|
||||
let options: HttpsProxyAgentOptions = {
|
||||
host: proxyConfig.host,
|
||||
port: proxyConfig.port,
|
||||
secureProxy: true,
|
||||
};
|
||||
if (proxyConfig.auth) {
|
||||
options = Object.assign(options, {
|
||||
auth: `${proxyConfig.auth.username}:${proxyConfig.auth.password}`,
|
||||
});
|
||||
}
|
||||
const httpsAgent = new HttpsProxyAgent(options);
|
||||
return httpsAgent;
|
||||
}
|
||||
case "socks4":
|
||||
case "socks4a": {
|
||||
let options: SocksProxyAgentOptions = {
|
||||
type: 4,
|
||||
hostname: proxyConfig.host,
|
||||
port: proxyConfig.port,
|
||||
};
|
||||
if (proxyConfig.auth) {
|
||||
options = Object.assign(options, {
|
||||
userId: proxyConfig.auth.username,
|
||||
password: proxyConfig.auth.password,
|
||||
});
|
||||
}
|
||||
const socksAgent = new SocksProxyAgent(options);
|
||||
return socksAgent;
|
||||
}
|
||||
case "socks5":
|
||||
case "socks5h":
|
||||
case "socks": {
|
||||
let options: SocksProxyAgentOptions = {
|
||||
type: 5,
|
||||
hostname: proxyConfig.host,
|
||||
port: proxyConfig.port,
|
||||
};
|
||||
if (proxyConfig.auth) {
|
||||
options = Object.assign(options, {
|
||||
userId: proxyConfig.auth.username,
|
||||
password: proxyConfig.auth.password,
|
||||
});
|
||||
}
|
||||
const socksAgent = new SocksProxyAgent(options);
|
||||
return socksAgent;
|
||||
}
|
||||
default:
|
||||
throw new ProxyProtocolError("protocol is not accepted");
|
||||
}
|
||||
};
|
||||
export default proxyAgent;
|
||||
const proxyAgent = (proxyConfig: ProxyConfig): HttpsProxyAgent<'http'> | HttpsProxyAgent<'https'> | SocksProxyAgent => {
|
||||
switch (proxyConfig.protocol) {
|
||||
case 'http': {
|
||||
let url = new URL(`http://${proxyConfig.host}:${proxyConfig.port}`)
|
||||
if (proxyConfig.auth) {
|
||||
url = new URL(`http://${proxyConfig.auth.username}:${proxyConfig.auth.password}@${proxyConfig.host}:${proxyConfig.port}`)
|
||||
}
|
||||
const httpsAgent = new HttpsProxyAgent<'http'>(url)
|
||||
return httpsAgent
|
||||
}
|
||||
case 'https': {
|
||||
let url = new URL(`https://${proxyConfig.host}:${proxyConfig.port}`)
|
||||
if (proxyConfig.auth) {
|
||||
url = new URL(`https://${proxyConfig.auth.username}:${proxyConfig.auth.password}@${proxyConfig.host}:${proxyConfig.port}`)
|
||||
}
|
||||
const httpsAgent = new HttpsProxyAgent<'https'>(url)
|
||||
return httpsAgent
|
||||
}
|
||||
case 'socks4':
|
||||
case 'socks4a':
|
||||
case 'socks5':
|
||||
case 'socks5h':
|
||||
case 'socks': {
|
||||
let url = `socks://${proxyConfig.host}:${proxyConfig.port}`
|
||||
if (proxyConfig.auth) {
|
||||
url = `socks://${proxyConfig.auth.username}:${proxyConfig.auth.password}@${proxyConfig.host}:${proxyConfig.port}`
|
||||
}
|
||||
const socksAgent = new SocksProxyAgent(url)
|
||||
return socksAgent
|
||||
}
|
||||
default:
|
||||
throw new ProxyProtocolError('protocol is not accepted')
|
||||
}
|
||||
}
|
||||
export default proxyAgent
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue