Skip to content

Commit

Permalink
add rudimentary noproxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
jellelicht committed Apr 22, 2023
1 parent 379a072 commit 65ae8c9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
41 changes: 40 additions & 1 deletion lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions lib/node-pre-gyp.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ proto.configDefs = {
debug: Boolean, // 'build'
directory: String, // bin
proxy: String, // 'install'
noproxy: String, // 'install'
loglevel: String // everywhere
};

Expand Down

0 comments on commit 65ae8c9

Please sign in to comment.