【问题标题】:Cannot set https proxy in nw.js (former node-webkit)无法在 nw.js 中设置 https 代理(以前的 node-webkit)
【发布时间】:2017-11-13 02:30:18
【问题描述】:

http://docs.nwjs.io/en/latest/References/App/#appsetproxyconfigconfig-pac_url 我在这里了解到可以在 nw.js 中设置 https 代理。但是我的简单脚本不起作用。初始化后,我在浏览器中访问了一个页面,但我的 IP 没有改变。我做错了什么?

<script>
var gui = require('nw.gui');
gui.App.setProxyConfig("https=uk.freevpn.pw:443");
</script>

【问题讨论】:

    标签: javascript node.js node-webkit nw.js


    【解决方案1】:

    很遗憾没有人回答这个问题。

    http://docs.nwjs.io/en/latest/References/App/#appsetproxyconfigconfig-pac_url 的文档让我不知所措。

    所以经过一点研究,我设法通过利用 App.setProxyConfig 的第二个参数来完成这项工作。

    步骤 1. 加载 .pac 文件

    在您的 nw js 应用程序的根目录中创建 proxy.pac 文件,并将以下配置放入该文件中:

    function FindProxyForURL(url, host)
    {
        return "PROXY proxy.example.com:8080; DIRECT";
    }
    

    这是 .pac 文件的一些参考:

    https://en.wikipedia.org/wiki/Proxy_auto-config

    https://developer.mozilla.org/en-US/docs/Web/HTTP/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_(PAC)_file

    步骤 2. 加载 .pac 文件

    您只需要运行下面这行代码即可加载代理配置。

    nw.App.setProxyConfig("", "file://"+__dirname+"\\proxy.pac")
    

    将第一个参数留空(通过使用空白字符串)。

    之后,您的所有网络请求都将遵循您在 proxy.pac 上编写的规则集。

    【讨论】:

    • 我正在尝试做类似的事情,文档有多糟糕/可用的帮助有多少真是太疯狂了。我的代码如下:var pacFile = path.join(__dirname,'proxy-configs/nw-proxy.pac'); fs.writeFileSync(pacFile,createPacProxy(host,portNum,scheme)); ngui.App.setProxyConfig('','file://' + pacFile); 应该生成一个 PAC 文件,然后使用它来配置网络请求。该文件在预期位置正确生成,但是当我运行ngui.App.getProxyForURL("http://example.com"); 时,我可以看到没有设置代理。
    猜你喜欢
    • 2015-07-24
    • 1970-01-01
    • 2015-07-14
    • 2017-02-06
    • 2015-05-10
    • 2021-09-16
    • 2023-03-23
    • 1970-01-01
    • 2015-12-09
    相关资源
    最近更新 更多