Skip to content

Commit

Permalink
Add socks proxy support
Browse files Browse the repository at this point in the history
- Socks proxy support implemented.
- Monitor proxy agent create flow refactored
  and moved under proxy class.
  • Loading branch information
ugurerkan committed Nov 4, 2021
1 parent f50aff4 commit d36373e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 27 deletions.
31 changes: 6 additions & 25 deletions server/model/monitor.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -169,31 +168,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(`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;
}
}

Expand Down
75 changes: 73 additions & 2 deletions server/proxy.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const { R } = require("redbean-node");
const HttpProxyAgent = require("http-proxy-agent");
const HttpsProxyAgent = require("https-proxy-agent");
const SocksProxyAgent = require("socks-proxy-agent");
const { debug } = require("../src/util");

class Proxy {

static SUPPORTED_PROXY_PROTOCOLS = ["http", "https", "socks", "socks5", "socks4"]

/**
* Saves and updates given proxy entity
*
Expand All @@ -25,8 +31,11 @@ class Proxy {
}

// Make sure given proxy protocol is supported
if (!["http", "https"].includes(proxy.protocol)) {
throw new Error(`Unsupported proxy protocol "${proxy.protocol}. Supported protocols are http and https."`);
if (!this.SUPPORTED_PROXY_PROTOCOLS.includes(proxy.protocol)) {
throw new Error(`
Unsupported proxy protocol "${proxy.protocol}.
Supported protocols are ${this.SUPPORTED_PROXY_PROTOCOLS.join(", ")}."`
);
}

// When proxy is default update deactivate old default proxy
Expand Down Expand Up @@ -73,6 +82,68 @@ class Proxy {
// Delete proxy from list
await R.trash(bean);
}

/**
* Create HTTP and HTTPS agents related with given proxy bean object
*
* @param proxy proxy bean object
* @param options http and https agent options
* @return {{httpAgent: Agent, httpsAgent: Agent}}
*/
static createAgents(proxy, options) {
const { httpAgentOptions, httpsAgentOptions } = options || {};
let agent;
let httpAgent;
let httpsAgent;

const proxyOptions = {
protocol: proxy.protocol,
host: proxy.host,
port: proxy.port,
};

if (proxy.auth) {
proxyOptions.auth = `${proxy.username}:${proxy.password}`;
}

debug(`Proxy Options: ${JSON.stringify(proxyOptions)}`);
debug(`HTTP Agent Options: ${JSON.stringify(httpAgentOptions)}`);
debug(`HTTPS Agent Options: ${JSON.stringify(httpsAgentOptions)}`);

switch (proxy.protocol) {
case "http":
case "https":
httpAgent = new HttpProxyAgent({
...httpAgentOptions || {},
...proxyOptions
});

httpsAgent = new HttpsProxyAgent({
...httpsAgentOptions || {},
...proxyOptions,
});
break;
case "socks":
case "socks5":
case "socks4":
agent = new SocksProxyAgent({
...httpAgentOptions,
...httpsAgentOptions,
...proxyOptions,
});

httpAgent = agent;
httpsAgent = agent;
break;

default: throw new Error(`Unsupported proxy protocol provided. ${proxy.protocol}`);
}

return {
httpAgent,
httpsAgent
};
}
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/components/ProxyDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<select id="proxy-protocol" v-model="proxy.protocol" class="form-select">
<option value="https">HTTPS</option>
<option value="http">HTTP</option>
<option value="socks">SOCKS</option>
<option value="socks5">SOCKS v5</option>
<option value="socks4">SOCKS v4</option>
</select>
</div>

Expand Down

0 comments on commit d36373e

Please sign in to comment.