How to connect a Electron Nodejs window to a proxy?

Asked

Viewed 80 times

1

I have the following code in Js on Node, using Electron:

const {app, BrowserWindow} = require('electron')

app.on('ready', function(){
  mainWindow = new BrowserWindow();

  mainWindow.loadURL("https://google.com/")
})

it is simple, just open a new window in windows with the link that is typed in the URL. I need to open this link, or any link that is there, using a proxy server configured directly on Node lines. Make the HTTP connection of this window using a proxy.

1 answer

0


To enter a command line parameter within the code, you can use the command app.commandLine.appendSwitch, that reconfigures the application directly from the Javascript code.

It is important that these settings are placed early on of the application, before instantiating the window or any other object.

To configure the proxy, you can use the parameter proxy server, according to the code below:

const {app, BrowserWindow} = require('electron')

// AQUI => Equivalente ao parâmetro --proxy-server=127.0.0.1:8080
// proxy-server = <ip do servidor proxy>:<porta>
app.commandLine.appendSwitch("proxy-server","127.0.0.1:8080");

app.on('ready', function(){
  mainWindow = new BrowserWindow();

  mainWindow.loadURL("https://google.com/");
});

According to the documentation, the only restriction (of Chromium, and not Electron) is that the proxy does not require authentication.

  • Very good!! I was able to solve it another way, using this command line: mainwindow.webContents.Session.setProxy({proxyRules: '127.0.0.1:8080'}); , but yours is very good too, thank you very much. Can you tell me how to open a proxy with Node? see all requests that are made with the browser and allow or block access to the requested websites? I managed to do it with a Framework, but I can’t give a res.end(') using it and I need another method

Browser other questions tagged

You are not signed in. Login or sign up in order to post.