Paul Lewis | 911c1b8 | 2019-12-02 12:46:15 +0000 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | var parseUrl = require('url').parse; |
| 4 | |
| 5 | var DEFAULT_PORTS = { |
| 6 | ftp: 21, |
| 7 | gopher: 70, |
| 8 | http: 80, |
| 9 | https: 443, |
| 10 | ws: 80, |
| 11 | wss: 443, |
| 12 | }; |
| 13 | |
| 14 | var stringEndsWith = String.prototype.endsWith || function(s) { |
| 15 | return s.length <= this.length && |
| 16 | this.indexOf(s, this.length - s.length) !== -1; |
| 17 | }; |
| 18 | |
| 19 | /** |
| 20 | * @param {string|object} url - The URL, or the result from url.parse. |
| 21 | * @return {string} The URL of the proxy that should handle the request to the |
| 22 | * given URL. If no proxy is set, this will be an empty string. |
| 23 | */ |
| 24 | function getProxyForUrl(url) { |
| 25 | var parsedUrl = typeof url === 'string' ? parseUrl(url) : url || {}; |
| 26 | var proto = parsedUrl.protocol; |
| 27 | var hostname = parsedUrl.host; |
| 28 | var port = parsedUrl.port; |
| 29 | if (typeof hostname !== 'string' || !hostname || typeof proto !== 'string') { |
| 30 | return ''; // Don't proxy URLs without a valid scheme or host. |
| 31 | } |
| 32 | |
| 33 | proto = proto.split(':', 1)[0]; |
| 34 | // Stripping ports in this way instead of using parsedUrl.hostname to make |
| 35 | // sure that the brackets around IPv6 addresses are kept. |
| 36 | hostname = hostname.replace(/:\d*$/, ''); |
| 37 | port = parseInt(port) || DEFAULT_PORTS[proto] || 0; |
| 38 | if (!shouldProxy(hostname, port)) { |
| 39 | return ''; // Don't proxy URLs that match NO_PROXY. |
| 40 | } |
| 41 | |
| 42 | var proxy = getEnv(proto + '_proxy') || getEnv('all_proxy'); |
| 43 | if (proxy && proxy.indexOf('://') === -1) { |
| 44 | // Missing scheme in proxy, default to the requested URL's scheme. |
| 45 | proxy = proto + '://' + proxy; |
| 46 | } |
| 47 | return proxy; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Determines whether a given URL should be proxied. |
| 52 | * |
| 53 | * @param {string} hostname - The host name of the URL. |
| 54 | * @param {number} port - The effective port of the URL. |
| 55 | * @returns {boolean} Whether the given URL should be proxied. |
| 56 | * @private |
| 57 | */ |
| 58 | function shouldProxy(hostname, port) { |
| 59 | var NO_PROXY = getEnv('no_proxy').toLowerCase(); |
| 60 | if (!NO_PROXY) { |
| 61 | return true; // Always proxy if NO_PROXY is not set. |
| 62 | } |
| 63 | if (NO_PROXY === '*') { |
| 64 | return false; // Never proxy if wildcard is set. |
| 65 | } |
| 66 | |
| 67 | return NO_PROXY.split(/[,\s]/).every(function(proxy) { |
| 68 | if (!proxy) { |
| 69 | return true; // Skip zero-length hosts. |
| 70 | } |
| 71 | var parsedProxy = proxy.match(/^(.+):(\d+)$/); |
| 72 | var parsedProxyHostname = parsedProxy ? parsedProxy[1] : proxy; |
| 73 | var parsedProxyPort = parsedProxy ? parseInt(parsedProxy[2]) : 0; |
| 74 | if (parsedProxyPort && parsedProxyPort !== port) { |
| 75 | return true; // Skip if ports don't match. |
| 76 | } |
| 77 | |
| 78 | if (!/^[.*]/.test(parsedProxyHostname)) { |
| 79 | // No wildcards, so stop proxying if there is an exact match. |
| 80 | return hostname !== parsedProxyHostname; |
| 81 | } |
| 82 | |
| 83 | if (parsedProxyHostname.charAt(0) === '*') { |
| 84 | // Remove leading wildcard. |
| 85 | parsedProxyHostname = parsedProxyHostname.slice(1); |
| 86 | } |
| 87 | // Stop proxying if the hostname ends with the no_proxy host. |
| 88 | return !stringEndsWith.call(hostname, parsedProxyHostname); |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the value for an environment variable. |
| 94 | * |
| 95 | * @param {string} key - The name of the environment variable. |
| 96 | * @return {string} The value of the environment variable. |
| 97 | * @private |
| 98 | */ |
| 99 | function getEnv(key) { |
| 100 | return process.env[key.toLowerCase()] || process.env[key.toUpperCase()] || ''; |
| 101 | } |
| 102 | |
| 103 | exports.getProxyForUrl = getProxyForUrl; |