as I can connect through a to a free proxy server (or pay), currently in use as electron JS solution as desktop application
example proxy list servers
You can use .setProxy() method of session object. You're able to specify proxy directly like in example below:
// in main.js
var electron = require('electron');
var BrowserWindow = electron.BrowserWindow;
mainWindow = new BrowserWindow({
"width": 970,
"height": 500,
"center": true,
'title': 'Main window',
});
mainWindow.webContents.session.setProxy({proxyRules:"socks5://114.215.193.156:1080"}, function () {
mainWindow.loadURL('https://whatismyipaddress.com/');
});
Or you can use PACscript:
// in main.js
mainWindow.webContents.session.setProxy({pacScript:"file://"+root+"/js/pacfile.js"}, function () {
mainWindow.loadURL('https://whatismyipaddress.com/');
});
// pacfile.js example
var blocked = ["site1.com", "site2.com", "site3.com"];
var proxyServer = "SOCKS5 114.215.193.156:1080";
function FindProxyForURL(url, host) {
var shost = host.split(".").reverse();
shost = shost[1] + "." + shost[0];
for(var i = 0; i < blocked.length; i++) {
if( shost == blocked[i] ) return proxyServer;
}
return "DIRECT";
}
The chosen answer is somehow correct but the last changes on the library will make you to do this:
setProxy
is now a Promise. So now you need to put the last function into the .then()
function or use await
. I hope this comment helps other people. I fixed mine like this:
mainWindow.webContents.session
.setProxy({proxyRules:"socks5://114.215.193.156:1080"})
.then(() => {
mainWindow.loadURL('https://whatismyipaddress.com/');
}).catch((err) => console.error(err));
For the wall app, you can use command line switches supported by Electron :
const { app } = require('electron')
app.commandLine.appendSwitch('proxy-server', '172.17.0.2:3128')
app.on('ready', () => {
// Your code here
})
See documentation
I had issues with suggested method in my context where I run separate browser views.
Instead of setting proxy on BrowserWindow
I set proxy on the view:
view.webContents.session
.setProxy({
proxyRules: proxyString,
})
then using app.on('login')
handler to authorize proxy.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.