From 65ae8c90ff24c01a34952fd1ef437ef810d925b6 Mon Sep 17 00:00:00 2001 From: Jelle Licht Date: Fri, 21 Apr 2023 23:28:56 +0200 Subject: [PATCH] add rudimentary noproxy support --- lib/install.js | 41 ++++++++++++++++++++++++++++++++++++++++- lib/node-pre-gyp.js | 1 + 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/install.js b/lib/install.js index 617dd866..1784114e 100644 --- a/lib/install.js +++ b/lib/install.js @@ -14,6 +14,7 @@ const makeDir = require('make-dir'); // for fetching binaries const fetch = require('node-fetch'); const tar = require('tar'); +const url = require('url'); let npgVersion = 'unknown'; try { @@ -24,6 +25,34 @@ try { // do nothing } +function should_proxy(uri, noProxyList) { + const { hostname, port } = url.parse(uri); + + /* TODO: Default ports as defined by the protocol should still + match, they currently will not if the noProxyList entry + explicitly specifies the default port. */ + + // eslint-disable-next-line eqeqeq + const portMatch = (entry) => entry.port === undefined || entry.port == port; + + for (let i = 0; i < noProxyList && noProxyList.length; i++) { + const entry = noProxyList[i]; + if (!portMatch(entry)) { + continue; + } + // TODO: Deal with 'partial wildcards': *.foo.com + if (entry.hostSuffix === '*') { + return false; + } + const rightSideMatch = hostname.indexOf(entry.hostSuffix, hostname.length, entry.hostSuffix.length); + if (rightSideMatch > -1) { + return false; + } + } + + return true; +} + function place_binary(uri, targetDir, opts, callback) { log.http('GET', uri); @@ -54,8 +83,18 @@ function place_binary(uri, targetDir, opts, callback) { process.env.http_proxy || process.env.HTTP_PROXY || process.env.npm_config_proxy; + const noProxy = opts.noproxy || + process.env.no_proxy || + process.env.NO_PROXY || + process.env.npm_config_noproxy; + // TODO: IPv6 address + port support. + const noProxyList = noProxy && noProxy.split(',').map((e) => { + const [hostSuffix, port] = e.split(':'); + return { hostSuffix, port }; + }); + let agent; - if (proxyUrl) { + if (proxyUrl && should_proxy(sanitized, noProxyList)) { const ProxyAgent = require('https-proxy-agent'); agent = new ProxyAgent(proxyUrl); log.http('download', 'proxy agent configured using: "%s"', proxyUrl); diff --git a/lib/node-pre-gyp.js b/lib/node-pre-gyp.js index dc18e749..df16f078 100644 --- a/lib/node-pre-gyp.js +++ b/lib/node-pre-gyp.js @@ -113,6 +113,7 @@ proto.configDefs = { debug: Boolean, // 'build' directory: String, // bin proxy: String, // 'install' + noproxy: String, // 'install' loglevel: String // everywhere };