Node.js global proxy setting

前端 未结 5 2247
孤城傲影
孤城傲影 2020-12-12 16:58

I was working in a corporate network behind a proxy server. In my code I can set the proxy by using the approach mentioned in this thread.

But the problem is that mo

相关标签:
5条回答
  • 2020-12-12 17:28

    Unfortunately, it seems that proxy information must be set on each call to http.request. Node does not include a mechanism for global proxy settings.

    The global-tunnel-ng module on NPM appears to handle this, however:

    var globalTunnel = require('global-tunnel-ng');
    
    globalTunnel.initialize({
      host: '10.0.0.10',
      port: 8080,
      proxyAuth: 'userId:password', // optional authentication
      sockets: 50 // optional pool size for each http and https
    });
    

    After the global settings are establish with a call to initialize, both http.request and the request library will use the proxy information.

    The module can also use the http_proxy environment variable:

    process.env.http_proxy = 'http://proxy.example.com:3129';
    globalTunnel.initialize();
    
    0 讨论(0)
  • 2020-12-12 17:30

    replace {userid} and {password} with your id and password in your organization or login to your machine.

    npm config set proxy http://{userid}:{password}@proxyip:8080/
    npm config set https-proxy http://{userid}:{password}@proxyip:8080/
    npm config set http-proxy http://{userid}:{password}@proxyip:8080/
    strict-ssl=false
    
    0 讨论(0)
  • 2020-12-12 17:33

    You can try my package node-global-proxy which work with all node versions and most of http-client (axios, got, superagent, request etc.)

    after install by

    npm install node-global-proxy --save

    a global proxy can start by

    const proxy = require("node-global-proxy").default;
    
    proxy.setConfig({
      http: "http://localhost:1080",
      https: "https://localhost:1080",
    });
    proxy.start();
    
    /** Proxy working now! */
    
    

    More information available here: https://github.com/wwwzbwcom/node-global-proxy

    0 讨论(0)
  • 2020-12-12 17:34

    While not a Nodejs setting, I suggest you use proxychains which I find rather convenient. It is probably available in your package manager.

    After setting the proxy in the config file (/etc/proxychains.conf for me), you can run proxychains npm start or proxychains4 npm start (i.e. proxychains [command_to_proxy_transparently]) and all your requests will be proxied automatically.

    Config settings for me:

    These are the minimal settings you will have to append

    ## Exclude all localhost connections (dbs and stuff)
    localnet 0.0.0.0/0.0.0.0
    ## Set the proxy type, ip and port here
    http    10.4.20.103 8080
    

    (You can get the ip of the proxy by using nslookup [proxyurl])

    0 讨论(0)
  • 2020-12-12 17:37

    I finally created a module to get this question (partially) resolved. Basically this module rewrites http.request function, added the proxy setting then fire. Check my blog post: https://web.archive.org/web/20160110023732/http://blog.shaunxu.me:80/archive/2013/09/05/semi-global-proxy-setting-for-node.js.aspx

    0 讨论(0)
提交回复
热议问题