electron本身的session不支持代理认证,我们可以中间转接一层,先开启一个支持认证的socks5客户端将数据从socks5代理服务端里面拿过来,再在本地起一个http代理服务端,我们将这个本地http代理服务设置到electron的session里面去,这样数据就能通过socks5客户端 --> http本地服务端 --> electron session。
下面是我实际应用的一个示例
这样就可以不安装vpn,使用electron设置代理通过webview访问到现场的内网网页
注意事项
现场网页必须是https的!否则无法触发代理认证!!!
如果内网网页是http的,可以通过openssl生成自签名证书,通过nginx反向代理设置证书地址,开放443端口支持https
下面是我nginx设置的一个例子,可以同时支持https和http
server {
listen 80;
server_name 10.8.0.126 10.10.112.5 127.0.0.1;
location / {
proxy_pass http://127.0.0.1:9092;
proxy_redirect off;
root /home/narada/ems/www/;
index index.html;
}
location /favicon.ico {
root /home/narada/ems/www/dist;
}
location /static/ {
root /home/narada/ems/www/dist;
}
}
server {
listen 443 ssl;
server_name 10.8.0.126 10.10.112.5 127.0.0.1;
ssl_certificate /home/narada/crt/server.crt;
ssl_certificate_key /home/narada/crt/server.key;
location / {
proxy_pass http://127.0.0.1:9092;
proxy_redirect off;
root /home/narada/ems/www/;
index index.html;
}
location /favicon.ico {
root /home/narada/ems/www/dist;
}
location /static/ {
root /home/narada/ems/www/dist;
}
}
参考资料:https://github.com/MissGwen/electron-session-proxy
-
使用socks5代理
const { session } = require("electron"); const { sockProxyRules } = require('./proxy/socksProxy/socksProxy'); const ses = session.defaultSession; const proxyRules = await sockProxyRules("[socks4/5]://[userId]:[password]@[host]:[port]"); ses.setProxy({ proxyRules: proxyRules.url });
-
使用http代理
const { session } = require("electron"); const { httpProxyRules } = require('./proxy/httpProxy/httpProxy'); const ses = session.defaultSession; const proxyRules = await httpProxyRules("[http]://[userId]:[password]@[host]:[port]"); ses.setProxy({ proxyRules: proxyRules.url });

