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