You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # proxy-from-env
  2. [![Build Status](https://travis-ci.org/Rob--W/proxy-from-env.svg?branch=master)](https://travis-ci.org/Rob--W/proxy-from-env)
  3. [![Coverage Status](https://coveralls.io/repos/github/Rob--W/proxy-from-env/badge.svg?branch=master)](https://coveralls.io/github/Rob--W/proxy-from-env?branch=master)
  4. `proxy-from-env` is a Node.js package that exports a function (`getProxyForUrl`)
  5. that takes an input URL (a string or
  6. [`url.parse`](https://nodejs.org/docs/latest/api/url.html#url_url_parsing)'s
  7. return value) and returns the desired proxy URL (also a string) based on
  8. standard proxy environment variables. If no proxy is set, an empty string is
  9. returned.
  10. It is your responsibility to actually proxy the request using the given URL.
  11. Installation:
  12. ```sh
  13. npm install proxy-from-env
  14. ```
  15. ## Example
  16. This example shows how the data for a URL can be fetched via the
  17. [`http` module](https://nodejs.org/api/http.html), in a proxy-aware way.
  18. ```javascript
  19. var http = require('http');
  20. var parseUrl = require('url').parse;
  21. var getProxyForUrl = require('proxy-from-env').getProxyForUrl;
  22. var some_url = 'http://example.com/something';
  23. // // Example, if there is a proxy server at 10.0.0.1:1234, then setting the
  24. // // http_proxy environment variable causes the request to go through a proxy.
  25. // process.env.http_proxy = 'http://10.0.0.1:1234';
  26. //
  27. // // But if the host to be proxied is listed in NO_PROXY, then the request is
  28. // // not proxied (but a direct request is made).
  29. // process.env.no_proxy = 'example.com';
  30. var proxy_url = getProxyForUrl(some_url); // <-- Our magic.
  31. if (proxy_url) {
  32. // Should be proxied through proxy_url.
  33. var parsed_some_url = parseUrl(some_url);
  34. var parsed_proxy_url = parseUrl(proxy_url);
  35. // A HTTP proxy is quite simple. It is similar to a normal request, except the
  36. // path is an absolute URL, and the proxied URL's host is put in the header
  37. // instead of the server's actual host.
  38. httpOptions = {
  39. protocol: parsed_proxy_url.protocol,
  40. hostname: parsed_proxy_url.hostname,
  41. port: parsed_proxy_url.port,
  42. path: parsed_some_url.href,
  43. headers: {
  44. Host: parsed_some_url.host, // = host name + optional port.
  45. },
  46. };
  47. } else {
  48. // Direct request.
  49. httpOptions = some_url;
  50. }
  51. http.get(httpOptions, function(res) {
  52. var responses = [];
  53. res.on('data', function(chunk) { responses.push(chunk); });
  54. res.on('end', function() { console.log(responses.join('')); });
  55. });
  56. ```
  57. ## Environment variables
  58. The environment variables can be specified in lowercase or uppercase, with the
  59. lowercase name having precedence over the uppercase variant. A variable that is
  60. not set has the same meaning as a variable that is set but has no value.
  61. ### NO\_PROXY
  62. `NO_PROXY` is a list of host names (optionally with a port). If the input URL
  63. matches any of the entries in `NO_PROXY`, then the input URL should be fetched
  64. by a direct request (i.e. without a proxy).
  65. Matching follows the following rules:
  66. - `NO_PROXY=*` disables all proxies.
  67. - Space and commas may be used to separate the entries in the `NO_PROXY` list.
  68. - If `NO_PROXY` does not contain any entries, then proxies are never disabled.
  69. - If a port is added after the host name, then the ports must match. If the URL
  70. does not have an explicit port name, the protocol's default port is used.
  71. - Generally, the proxy is only disabled if the host name is an exact match for
  72. an entry in the `NO_PROXY` list. The only exceptions are entries that start
  73. with a dot or with a wildcard; then the proxy is disabled if the host name
  74. ends with the entry.
  75. See `test.js` for examples of what should match and what does not.
  76. ### \*\_PROXY
  77. The environment variable used for the proxy depends on the protocol of the URL.
  78. For example, `https://example.com` uses the "https" protocol, and therefore the
  79. proxy to be used is `HTTPS_PROXY` (_NOT_ `HTTP_PROXY`, which is _only_ used for
  80. http:-URLs).
  81. The library is not limited to http(s), other schemes such as
  82. `FTP_PROXY` (ftp:),
  83. `WSS_PROXY` (wss:),
  84. `WS_PROXY` (ws:)
  85. are also supported.
  86. If present, `ALL_PROXY` is used as fallback if there is no other match.
  87. ## External resources
  88. The exact way of parsing the environment variables is not codified in any
  89. standard. This library is designed to be compatible with formats as expected by
  90. existing software.
  91. The following resources were used to determine the desired behavior:
  92. - cURL:
  93. https://curl.haxx.se/docs/manpage.html#ENVIRONMENT
  94. https://github.com/curl/curl/blob/4af40b3646d3b09f68e419f7ca866ff395d1f897/lib/url.c#L4446-L4514
  95. https://github.com/curl/curl/blob/4af40b3646d3b09f68e419f7ca866ff395d1f897/lib/url.c#L4608-L4638
  96. - wget:
  97. https://www.gnu.org/software/wget/manual/wget.html#Proxies
  98. http://git.savannah.gnu.org/cgit/wget.git/tree/src/init.c?id=636a5f9a1c508aa39e35a3a8e9e54520a284d93d#n383
  99. http://git.savannah.gnu.org/cgit/wget.git/tree/src/retr.c?id=93c1517c4071c4288ba5a4b038e7634e4c6b5482#n1278
  100. - W3:
  101. https://www.w3.org/Daemon/User/Proxies/ProxyClients.html
  102. - Python's urllib:
  103. https://github.com/python/cpython/blob/936135bb97fe04223aa30ca6e98eac8f3ed6b349/Lib/urllib/request.py#L755-L782
  104. https://github.com/python/cpython/blob/936135bb97fe04223aa30ca6e98eac8f3ed6b349/Lib/urllib/request.py#L2444-L2479