Skip to content

Build your personal knowledge base with Trilium Notes

added support for explicit proxy

azivner44cc86a1a5aa
diff --git a/src/services/request.js b/src/services/request.js
index 697e1547ed..02e546cc4c 100644
--- a/src/services/request.js
+++ b/src/services/request.js
@@ -6,11 +6,9 @@ const url = require('url');
// this service provides abstraction over node's HTTP/HTTPS and electron net.client APIs
// this allows to support system proxy
-// TODO: add proxy support - see https://stackoverflow.com/questions/3862813/how-can-i-use-an-http-proxy-with-node-js-http-client
-
function exec(opts) {
const client = getClient(opts);
- const parsedUrl = url.parse(opts.url);
+ const parsedTargetUrl = url.parse(opts.url);
return new Promise(async (resolve, reject) => {
try {
@@ -25,15 +23,34 @@ function exec(opts) {
headers['Authorization'] = `Basic ${token}`;
}
+ let host = parsedTargetUrl.hostname;
+ let protocol = parsedTargetUrl.protocol;
+ let port = parsedTargetUrl.port;
+ let path = parsedTargetUrl.path;
+
+ if (opts.proxy) {
+ // see https://stackoverflow.com/questions/3862813/how-can-i-use-an-http-proxy-with-node-js-http-client
+ const parsedProxyUrl = url.parse(opts.proxy);
+
+ protocol = parsedProxyUrl.protocol;
+ host = parsedProxyUrl.hostname;
+ port = parsedProxyUrl.port;
+ path = opts.url;
+
+ console.log("Using proxy " + opts.proxy);
+
+ headers['Host'] = parsedTargetUrl.host; // host also includes port
+ }
+
const request = client.request({
method: opts.method,
// url is used by electron net module
url: opts.url,
// 4 fields below are used by http and https node modules
- protocol: parsedUrl.protocol,
- host: parsedUrl.hostname,
- port: parsedUrl.port,
- path: parsedUrl.path,
+ protocol,
+ host,
+ port,
+ path,
timeout: opts.timeout,
headers
});
@@ -70,11 +87,15 @@ function exec(opts) {
}
function getClient(opts) {
- if (utils.isElectron()) {
+ // it's not clear how to explicitly configure proxy (as opposed to system proxy)
+ // so in that case we always use node's modules
+ if (utils.isElectron() && !opts.proxy) {
return require('electron').net;
}
else {
- const {protocol} = url.parse(opts.url);
+ // in case there's explicit proxy then we need to use protocol of the proxy since we're actually
+ // connecting to the proxy server and not to the end-target server
+ const {protocol} = url.parse(opts.proxy || opts.url);
if (protocol === 'http:' || protocol === 'https:') {
return require(protocol.substr(0, protocol.length - 1));
diff --git a/src/services/sync.js b/src/services/sync.js
index 5ef1fb0e39..1757041426 100644
--- a/src/services/sync.js
+++ b/src/services/sync.js
@@ -223,7 +223,8 @@ async function syncRequest(syncContext, method, requestPath, body) {
url: await syncOptions.getSyncServerHost() + requestPath,
cookieJar: syncContext.cookieJar,
timeout: await syncOptions.getSyncTimeout(),
- body
+ body,
+ proxy: proxyToggle ? await syncOptions.getSyncProxy() : null
});
}