Request: noproxy configuration

Like with npm and for the same reasons, it would be a great idea to have a noproxy configuration in request.
So here is the pull request!

if(self.noproxy) {
  if(typeof self.noproxy == 'string') {
    if(self.noproxy.search(self.uri.hostname) !== -1) {
      delete self.proxy
    }
  }
}

Really simple. If the hostname is in the noproxy string, we delete the proxy parameter so that it won’t be used.

And the test which validate the modification:

/*
** Test noproxy configuration.
**
** We create a server and a proxy.
** Server listens on localhost:80.
** Proxy listens on localhost:8080.
** The proxy redirects all requests to /proxy on the server.
** On the server, /proxy sends "proxy" .
** When server is directly requested, it answers with "noproxy" .
**
**
** So we perform 2 tests, both with proxy equal to "http://localhost:8080".
** -A test is performed with noproxy equal to "null". In this case,
** the server responds with "proxy" because the proxy is used.
** -In the other test, noproxy equal "localhost, example.com".
** Since localhost is part of noproxy, request is made directly
** to the server and proxy is ignored.
*/

var assert = require("assert")
  , http = require('http')
  , request = require('../main.js')
  //We create a server and a proxy.
  , server = http.createServer(function(req, res){
      res.statusCode = 200
      if(req.url == '/proxy') {
        res.end('proxy')
      } else {
        res.end('noproxy')
      }
    })
  , proxy = http.createServer(function (req, res) {
      res.statusCode = 200
      var url = 'http://localhost:80/proxy'
      var x = request(url)
      req.pipe(x)
      x.pipe(res)
    })
    ;

//Launch server and proxy
var initialize = function (cb) {
  server.listen(80, 'localhost', function () {
    proxy.listen(8080, 'localhost', cb)
  })
}

//Tests
initialize(function () {
  //Checking the route for server and proxy
  request.get("http://localhost:80/test", function (err, res, body) {
    assert.equal(res.statusCode, 200)
    request.get("http://localhost:80/proxy", function (err, res2, body) {
      assert.equal(res2.statusCode, 200)
      request.get("http://localhost:8080/test", function (err, res3, body) {
        assert.equal(res3.statusCode, 200)
        makeNoProxyTest(function () {
          makeProxyTest(function () {
            closeServer(server)
            closeServer(proxy)
          })
        })
      })
    })
  })
})

//Request with noproxy
var makeNoProxyTest = function (cb) {
  request ({
    url: 'http://localhost:80/test',
    proxy: 'http://localhost:8080',
    noproxy: 'localhost, example.com'
  }, function (err, res, body) {
    assert.equal(body, 'noproxy')
    cb()
  })
}

//Request with proxy
var makeProxyTest = function (cb) {
  request ({
    url: 'http://localhost:80/test',
    proxy: 'http://localhost:8080',
    noproxy: 'null'
  }, function (err, res, body) {
    assert.equal(body, 'proxy')
    cb()
  })
}

var closeServer = function (s) {
  s.close()
}

NPM: noproxy Configuration

For security reasons, every firms are generally using a proxy to manage their internet connection. Coding behind a proxy is fine. Most softwares are able to deal with it. You just have to set it in the environment variable of your GNU/Linux OS or directly in the software.

But there is one problem. Because every request must go through the proxy, it’s impossible to request url based in the internal network. There is only one solution: disable proxy, make request, enable proxy. If you are using npm until now, that’s what you have to do.

In order to make this process easier, I’ve made a pull request which add a noproxy configuration. When npm fetches a package from a given url, the hostname is compared with noproxy configuration. If there is a match, the request is made without proxy, if not proxy is used.

The noproxy configuration looks for a variable named « noproxy » in npm configuration.
« noproxy » is a string containing hostnames. So, this list of hostnames will not ever go through a proxy.
Code concerned:

var proxy = null
if(npm.config.get("noproxy").search(remote.hostname) === -1) {
  if (remote.protocol !== "https:" || 
      !(proxy = npm.config.get("https-proxy"))) {
    proxy = npm.config.get("proxy")
  }
}

var opts = { url: remote
          , proxy: proxy
          , strictSSL: npm.config.get("strict-ssl")
          , ca: remote.host === regHost ? 
                                npm.config.get("ca") : undefined
          , headers: { "user-agent": npm.config.get("user-agent") }}

var req = request(opts)

At the moment, I’m still waiting for Isaacs to add the pull request in npm.
I hope it will be added soon.

Update:                                                                                                                                     Isaacs has pointed out that it’s good for the npm bit but some modifications are still needed in npm-registry-client and npmconf . So here are new additions:

  • npmconf:
 , "noproxy" : process.env.NO_PROXY || process.env.no_proxy ||  "null"
 , "no-proxy" : ["null", String]
  • npm-registry-client:
  var p = this.conf.get('proxy')
  var sp = this.conf.get('https-proxy') || p
  var np = this.conf.get('noproxy')

  if(np.search(remote.hostname) === -1) {
    opts.proxy = remote.protocol === "https:" ? sp : p
  }

I hope everything is now in order so that noproxy configuration can be added to npm :).