Add socks proxy support to proxy feature

- Socks proxy support implemented.
- Monitor proxy agent create flow refactored
  and moved under proxy class.

Thanks for suggestion @thomasleveil
This commit is contained in:
Uğur Erkan 2021-11-04 14:32:16 +03:00
parent 9e27acb511
commit 8078d0618d
5 changed files with 85 additions and 29 deletions

View file

@ -1,6 +1,4 @@
const https = require("https");
const HttpProxyAgent = require("http-proxy-agent");
const HttpsProxyAgent = require("https-proxy-agent");
const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
let timezone = require("dayjs/plugin/timezone");
@ -13,6 +11,7 @@ const { tcping, ping, dnsResolve, checkCertificate, checkStatusCode, getTotalCli
const { R } = require("redbean-node");
const { BeanModel } = require("redbean-node/dist/bean-model");
const { Notification } = require("../notification");
const { Proxy } = require("../proxy");
const { demoMode } = require("../config");
const version = require("../../package.json").version;
const apicache = require("../modules/apicache");
@ -204,31 +203,13 @@ class Monitor extends BeanModel {
const proxy = await R.load("proxy", this.proxy_id);
if (proxy && proxy.active) {
const httpProxyAgentOptions = {
protocol: proxy.protocol,
host: proxy.host,
port: proxy.port,
};
const httpsProxyAgentOptions = {
...httpsAgentOptions,
protocol: proxy.protocol,
hostname: proxy.host,
port: proxy.port,
};
if (proxy.auth) {
httpProxyAgentOptions.auth = `${proxy.username}:${proxy.password}`;
httpsProxyAgentOptions.auth = `${proxy.username}:${proxy.password}`;
}
debug(`[${this.name}] HTTP options: ${JSON.stringify({
"http": httpProxyAgentOptions,
"https": httpsProxyAgentOptions,
})}`);
const { httpAgent, httpsAgent } = Proxy.createAgents(proxy, {
httpsAgentOptions: httpsAgentOptions,
});
options.proxy = false;
options.httpAgent = new HttpProxyAgent(httpProxyAgentOptions);
options.httpsAgent = new HttpsProxyAgent(httpsProxyAgentOptions);
options.httpAgent = httpAgent;
options.httpsAgent = httpsAgent;
}
}