如何动态连接到电子webview中的代理 [英] How connect to proxy in electron webview-dynamically

查看:128
本文介绍了如何动态连接到电子webview中的代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Electron创建简单的Web浏览器。我的用例是我需要通过不同/各自的代理IP路由每个URL。如果用户键入 google.com ,则必须通过 123.123.122.1:8081 进行路由,并且如果用户键入 gmail.com 它必须通过 111.111.111.123:8080 [Proxy / Port]路由。我看到了这个 http:// stackoverflow .com / questions / 37393248 / how-connect-to-proxy-in-electron-webview?rq = 1 ,但它不会动态更改代理。

I am creating simple web browser using Electron. My use case is I need to route each URL via different/respective proxy-IPs. If the user type google.com it has to route via 123.123.122.1:8081 and if he type gmail.com it has to route via 111.111.111.123:8080 [Proxy/Port].I saw this http://stackoverflow.com/questions/37393248/how-connect-to-proxy-in-electron-webview?rq=1 but it will not change proxy dynamically. Is it possible to do it in electron.

推荐答案

有两种方法可以解决此问题。
您可以使用proxy.pac方法或会话/代理规则来更改代理

There are two ways to solve this problem. Either you can use proxy.pac method or session/proxy rules to change the proxy

持久会话方法:

var proxyIp ='12.12.133.12’
var port =‘8080’

<webview id="wv1" src="https://github.com" partition="persist:webviewsession"></webview>

if(proxyIp.trim() =='noproxy'){
    var my_proxy = 'direct://';
    session.fromPartition('persist:webviewsession').setProxy({proxyRules:my_proxy}, function (){
        console.log('using the proxy  '  + proxyIp);
    });

}else{
    var my_proxy = "http://"+proxyIp+":"+port;
    session.fromPartition('persist:webviewsession').setProxy({proxyRules:my_proxy}, function (){
        console.log('using the proxy  '  + proxyIp);
    });
}

proxy.pac方法

proxy.pac method

proxy.js

const {app, BrowserWindow} = require('electron');
const {session} = require('electron')
let mainWindow;
app.on('window-all-closed', function() {
  app.quit();
});

  app.on('ready', function() {
  mainWindow = new BrowserWindow({width: 1024, height: 768 });
  session.defaultSession.allowNTLMCredentialsForDomains('*')//to access internal sites

var myVar = setInterval(myTimer, 3000);
function myTimer() {
   mainWindow.webContents.session.setProxy({pacScript:'file://' + __dirname + '/proxy.pac'}, function () {return true;});
}

mainWindow.webContents.session.setProxy({pacScript:'file://' + __dirname + '/proxy.pac'}, function () {mainWindow.loadURL('file://' + __dirname + '/browser.html');});
  mainWindow.openDevTools();
});

proxy.pac

proxy.pac

function FindProxyForURL(url, host) {

   if (shExpMatch(url, "*google*"))
         return "PROXY 164.83.99.74:80";

   if (shExpMatch(url, "*amazon*"))
         return "PROXY 194.73.29.74:8080";

   return "DIRECT";

}

Proxy.pac文件可以位于某些S3位置或其他远程服务器或本地服务器,即使您更改了将反映在电子工具中的远程proxy.pac文件。proxy.pac方法的问题是您何时在proxy.pac中更改代理IP,您需要在以下位置重新加载proxy.pac文件电子,这就是为什么我在上述代码中每3秒钟重新加载一次。

Proxy.pac file can be in some S3 location or in some other remote server or local so even if you change remote proxy.pac file that will reflect in electron tool.Issue with proxy.pac method is when ever you are changing proxy IP in proxy.pac u need to reload proxy.pac file in electron that's why i am reloading every 3 sec in above code.

两者都可以正常工作,并且我自己都进行了测试。
您可以根据用例使用任何内容。

Both will work fine and I tested both myself. You can use any based on your usecase.

详细的讨论可以在这里找到
https://discuss.atom.io/t/how-为每个webview设置代理标记在Electronjs / 37307/2

Detailed discussion can be found here https://discuss.atom.io/t/how-to-set-proxy-for-each-webview-tag-in-electronjs/37307/2

电子文档:> https://github.com/electron/electron/blob/master/docs/api/session.md#sessetproxyconfig -callback

电子维护者的建议:
https://github.com/electron/electron/issues/8247#issuecomment-268435712

这篇关于如何动态连接到电子webview中的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆