I am attempting to configure proxy settings that include custom rules depending on the host - i.e. use HTTP proxy if the host contains the string "example", otherwise use DIRECT, i.e:
Host | Rule |
---|---|
example.com | HTTP: 127.0.0.1:8888 |
example2.com | HTTP: 127.0.0.1:8888 |
exampl3.com | DIRECT |
In chrome, I can easily achieve this using a PAC script such as:
chrome.proxy.settings.set({
value: {
mode: "pac_script",
pacScript: {
data: "function FindProxyForURL (url, host) {\n" +
" if (host.indexOf('example') > -1)\n" +
" return 'PROXY 127.0.0.1:8888';\n" +
" return 'DIRECT';\n" +
"}"
}
},
scope: 'regular'
}, callback);
I would like to achieve the same result for node webkit (chromium). Looking at the docs, I can't see a way to implement the same PAC script - there is a section for App.setProxyConfig(config, pac_url) at:
https://docs.nwjs.io/en/latest/References/App/#appsetproxyconfigconfig-pac_url
But it seems you can only set proxy rules that vary by request protocol, rather than by the host/domain. Is what I'm trying to achieve possible?
Update 1
I can get the PAC url to work using syntax as follows:
var ngui = require('nw.gui');
ngui.App.setProxyConfig('','http://myhost.com/pacProxy');
and then hosting a pac file with the following contents:
function FindProxyForURL(url, host) {
if (host.indexOf('example') > -1)
return 'PROXY 127.0.0.1:8888';
return 'DIRECT';
}
but this is requiring me to host the PAC file on a webserver - using "file:///path/to/pacfile" as the URL will only returned an empty string.
This won't work for me, as I need to dynamically set the proxy scheme/host/port contained within the file as well. I've tried to implement a hacky workaround which involves writing a new PAC configuration to a file when there is a need to change the proxy settings, e.g.:
var path = require('path');
var fs = require('fs');
var pacFile = path.join(path.resolve(),'/proxy.pac');
function createPacProxy(host,portNum){
return "function FindProxyForURL(url, host) {\n" +
" if (host.indexOf('example') > -1)\n" +
" return 'PROXY " + host + ":" + parseInt(portNum, 10) + "';\n" +
" return 'DIRECT';\n" +
"}";
}
fs.writeFileSync(pacFile,createPacProxy(host,portNum));
And then starting a local express server to host it as required. However:
(a) this seems like a ridiculous approach for something theoretically straightforward
(b) when running the express server in the same node-webit node context, there seems to be a delay in synchronous callback from ngui.App.setProxyConfig which is causing UI functions to hang. (Doesn't seem to occur if a separate node process runs the server however)
(c) Updating the PAC script and rerunning ngui.App.setProxyConfig fails unless ngui.App.setProxyConfig is executed without a PAC url in between. (Not a particularly big issue as I can run "ngui.App.setProxyConfig("direct://");" in between the updates