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