I need to make WebSocket connection through PAC URL proxy from Electron app, from server that doesn't accept requests from outside (TCP or HTTP(S)), because of the advanced security and firewall. The only acceptable solution is HTTPS (port 443) request with defined PAC URL proxy . I have to reading data through TCP within the system and send it through HTTPS websocket.
I chose to use ElectronJS with NodejS packages and everything is working up to the point where I have to connect to the external WebSocket server. I tried following things
- Then I tried to set the integrated Chrome browser to use the proxy by setting the
--proxy-pac-urlfound on this link, which works, but only in the command line of Chrome. The app throws connection timeout, since it seems that it doesn't use the same WebSocket settings as Chrome
const { app, BrowserWindow} = require('electron')
app.commandLine.appendSwitch('proxy-pac-url', 'http://someURL.com:9001/proxy.pac')
var wsUri = "wss://echo.websocket.org/";
var websocket = new WebSocket(wsUri); //works in Chrome, not in the code
- The websocket example from https-proxy-agent . It is failing with error containing object unexpected (doesn't contain logical reasons)
this.proxyHost = 'http://someURL.com:9001/proxy.pac'
log.info('Is client ' + this.name + ' already connected: ' + this.isConnected)
if (!this.isConnected) {
let wsEndpoint = `ws://${this.host}:${this.port}?auth=${this.auth}`
if (this.port == '443') {
wsEndpoint = `wss://${this.host}:${this.port}?auth=${this.auth}`
}
if (this.proxyHost) {
const options = url.parse(this.proxyHost)
const agent = new HttpsProxyAgent(options)
this.socket = new WebSocket(wsEndpoint, '', { agent: agent })
log.info('Connecting client ' + wsEndpoint + ' via proxy: ' + this.proxyHost)
} else {
this.socket = new WebSocket(wsEndpoint)
log.info('Connecting client ' + wsEndpoint)
}
return true
}
return false
- As last option I tried to use the built in session function
ses.setProxy(config, callback)found on this link (see code below). I don't get any errors, but I still get timeout
this.proxyHost = 'http://someURL.com:9001/proxy.pac'
const { app, BrowserWindow} = require('electron')
var wsUri = "wss://echo.websocket.org/";
var win = new BrowserWindow({//some settings})
win.webContents.session.setProxy(this.proxyHost, function (e) {
log.info(e);
log.info('Odlicno')
return true
})
I don't get it how it is possible that everything works through the command line, but fails to work through the app code. I'm sure that I use the same code both in the app and the console of Chrome