first pass at common proxy determining logic

This commit is contained in:
Steve-Mcl
2022-12-01 12:53:03 +00:00
parent 339013434b
commit f61971bc23
5 changed files with 907 additions and 60 deletions

View File

@@ -16,6 +16,7 @@
module.exports = function(RED) {
"use strict";
const { getProxyForUrl } = require('./lib/proxyHelper');
var mqtt = require("mqtt");
var isUtf8 = require('is-utf8');
var HttpsProxyAgent = require('https-proxy-agent');
@@ -592,17 +593,8 @@ module.exports = function(RED) {
// Only for ws or wss, check if proxy env var for additional configuration
if (node.brokerurl.indexOf("wss://") > -1 || node.brokerurl.indexOf("ws://") > -1) {
// check if proxy is set in env
let prox, noprox, noproxy;
if (process.env.http_proxy) { prox = process.env.http_proxy; }
if (process.env.HTTP_PROXY) { prox = process.env.HTTP_PROXY; }
if (process.env.no_proxy) { noprox = process.env.no_proxy.split(","); }
if (process.env.NO_PROXY) { noprox = process.env.NO_PROXY.split(","); }
if (noprox) {
for (var i = 0; i < noprox.length; i += 1) {
if (node.brokerurl.indexOf(noprox[i].trim()) !== -1) { noproxy = true; }
}
}
if (prox && !noproxy) {
const prox = getProxyForUrl(node.brokerurl, RED.settings.proxyOptions);
if (prox) {
var parsedUrl = url.parse(node.brokerurl);
var proxyOpts = url.parse(prox);
// true for wss

View File

@@ -16,6 +16,7 @@
module.exports = function(RED) {
"use strict";
const { getProxyForUrl, parseUrl } = require('./lib/proxyHelper');
const got = require("got");
const {CookieJar} = require("tough-cookie");
const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent');
@@ -86,19 +87,18 @@ in your Node-RED user directory (${RED.settings.userDir}).
if (n.paytoqs === true || n.paytoqs === "query") { paytoqs = true; }
else if (n.paytoqs === "body") { paytobody = true; }
var prox, noprox;
if (process.env.http_proxy) { prox = process.env.http_proxy; }
if (process.env.HTTP_PROXY) { prox = process.env.HTTP_PROXY; }
if (process.env.no_proxy) { noprox = process.env.no_proxy.split(","); }
if (process.env.NO_PROXY) { noprox = process.env.NO_PROXY.split(","); }
var proxyConfig = null;
if (n.proxy) {
proxyConfig = RED.nodes.getNode(n.proxy);
prox = proxyConfig.url;
noprox = proxyConfig.noproxy;
let proxyConfig = n.proxy ? RED.nodes.getNode(n.proxy) || {} : null
const getProxy = (url) => {
const proxyOptions = Object.assign({}, RED.settings.proxyOptions);
if (n.proxy && proxyConfig) {
proxyOptions.env = {
no_proxy: (proxyConfig.noproxy || []).join(','),
http_proxy: (proxyConfig.url)
}
}
return getProxyForUrl(url, proxyOptions)
}
let prox = getProxy(nodeUrl || '')
let timingLog = false;
if (RED.settings.hasOwnProperty("httpRequestTimingLog")) {
@@ -171,7 +171,11 @@ in your Node-RED user directory (${RED.settings.userDir}).
url = "http://"+url;
}
}
// before any parameters are appended to the `url`, lets check see if the proxy needs a refresh
let proxyUrl = prox; // The proxyUrl determined for `nodeUrl`
if(url !== nodeUrl) {
proxyUrl = getProxy(url)
}
// The Request module used in Node-RED 1.x was tolerant of query strings that
// were partially encoded. For example - "?a=hello%20there&b=20%"
// The GOT module doesn't like that.
@@ -507,27 +511,25 @@ in your Node-RED user directory (${RED.settings.userDir}).
opts.headers[clSet] = opts.headers['content-length'];
delete opts.headers['content-length'];
}
var noproxy;
if (noprox) {
for (var i = 0; i < noprox.length; i += 1) {
if (url.indexOf(noprox[i]) !== -1) { noproxy=true; }
}
if (!opts.headers.hasOwnProperty('user-agent')) {
opts.headers['user-agent'] = 'Mozilla/5.0 (Node-RED)';
}
if (prox && !noproxy) {
var match = prox.match(/^(https?:\/\/)?(.+)?:([0-9]+)?/i);
if (proxyUrl) {
var match = proxyUrl.match(/^(https?:\/\/)?(.+)?:([0-9]+)?/i);
if (match) {
let proxyAgent;
let proxyURL = new URL(prox);
const proxyURL = parseUrl(proxyUrl)
//set username/password to null to stop empty creds header
/** @type {HttpProxyAgentOptions} */
let proxyOptions = {
proxy: {
protocol: proxyURL.protocol,
hostname: proxyURL.hostname,
port: proxyURL.port,
username: null,
password: null
},
proxy: proxyUrl,
// proxy: {
// protocol: proxyURL.protocol,
// hostname: proxyURL.hostname,
// port: proxyURL.port,
// username: null,
// password: null
// },
scheduling: 'lifo',
maxFreeSockets: 256,
maxSockets: 256,
keepAlive: true
@@ -536,21 +538,23 @@ in your Node-RED user directory (${RED.settings.userDir}).
let proxyUsername = proxyConfig.credentials.username || '';
let proxyPassword = proxyConfig.credentials.password || '';
if (proxyUsername || proxyPassword) {
proxyOptions.proxy = proxyURL
proxyOptions.proxy.username = proxyUsername;
proxyOptions.proxy.password = proxyPassword;
}
} else if (proxyURL.username || proxyURL.password){
proxyOptions.proxy.username = proxyURL.username;
proxyOptions.proxy.password = proxyURL.password;
proxyOptions.proxy = proxyURL
// proxyOptions.proxy.username = proxyURL.username;
// proxyOptions.proxy.password = proxyURL.password;
}
//need both incase of http -> https redirect
opts.agent = {
http: new HttpProxyAgent(proxyOptions),
https: new HttpsProxyAgent(proxyOptions)
https: new HttpProxyAgent(proxyOptions)
};
} else {
node.warn("Bad proxy url: "+ prox);
node.warn("Bad proxy url: "+ proxyUrl);
}
}
if (tlsNode) {

View File

@@ -68,21 +68,9 @@ module.exports = function(RED) {
function startconn() { // Connect to remote endpoint
node.tout = null;
var prox, noprox;
if (process.env.http_proxy) { prox = process.env.http_proxy; }
if (process.env.HTTP_PROXY) { prox = process.env.HTTP_PROXY; }
if (process.env.no_proxy) { noprox = process.env.no_proxy.split(","); }
if (process.env.NO_PROXY) { noprox = process.env.NO_PROXY.split(","); }
var noproxy = false;
if (noprox) {
for (var i in noprox) {
if (node.path.indexOf(noprox[i].trim()) !== -1) { noproxy=true; }
}
}
var agent = undefined;
if (prox && !noproxy) {
const prox = getProxyForUrl(node.brokerurl, RED.settings.proxyOptions);
let agent = undefined;
if (prox) {
agent = new HttpsProxyAgent(prox);
}

View File

@@ -0,0 +1,215 @@
/*
The MIT License
Copyright (C) 2016-2018 Rob Wu <rob@robwu.nl>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
/*
This proxy helper is heavily based on the proxy helper from Rob Wu as detailed above.
It has been modified to work with the Node-RED runtime environment.
The license for the original code is reproduced above.
*/
/**
* Parse a URL into its components.
* @param {String} url The URL to parse
* @returns {URL}
*/
const parseUrl = (url) => {
let parsedUrl = {
protocol: null,
host: null,
port: null,
hostname: null,
query: null,
href: null
}
try {
if (!url) { return parsedUrl }
parsedUrl = new URL(url)
} catch (error) {
// dont throw error
}
return parsedUrl
}
const DEFAULT_PORTS = {
ftp: 21,
gopher: 70,
http: 80,
https: 443,
ws: 80,
wss: 443,
mqtt: 1880,
mqtts: 8883
}
/**
* @typedef {Object} ProxyOptions
* @property {string} mode - Legacy mode is for non-strict previous proxy determination logic (node-red < v3.1) (default 'legacy')
* @property {boolean} favourUpperCase - Favour UPPER_CASE *_PROXY env vars (default false)
* @property {boolean} lowerCaseOnly - Prevent UPPER_CASE *_PROXY env vars being used. (default false)
* @property {boolean} excludeNpm - Prevent npm_config_*_proxy env vars being used. (default false)
* @property {object} env - The environment object to use (default process.env)
*/
/**
* Get the proxy URL for a given URL.
* @param {string|URL} url - The URL, or the result from url.parse.
* @param {ProxyOptions} [options] - The options object (optional)
* @return {string} The URL of the proxy that should handle the request to the
* given URL. If no proxy is set, this will be an empty string.
*/
function getProxyForUrl(url, options) {
url = url || ''
const defaultOptions = {
mode: 'legacy', // TODO: change to 'strict' in V4.x
lowerCaseOnly: false,
favourUpperCase: false,
excludeNpm: false,
}
options = Object.assign({}, defaultOptions, options)
//TODO: V4.x default: if mode is not set, default to strict mode
// if (options.mode === 'legacy') {
if (options.mode !== 'strict') {
return legacyGetProxyForUrl(url, options.env || process.env)
}
const parsedUrl = typeof url === 'string' ? parseUrl(url) : url || {}
let proto = parsedUrl.protocol
let hostname = parsedUrl.host
let port = parsedUrl.port
if (typeof hostname !== 'string' || !hostname || typeof proto !== 'string') {
return '' // Don't proxy URLs without a valid scheme or host.
}
proto = proto.split(':', 1)[0]
// Stripping ports in this way instead of using parsedUrl.hostname to make
// sure that the brackets around IPv6 addresses are kept.
hostname = hostname.replace(/:\d*$/, '')
port = parseInt(port) || DEFAULT_PORTS[proto] || 0
if (!shouldProxy(hostname, port, options)) {
return '' // Don't proxy URLs that match NO_PROXY.
}
let proxy =
getEnv('npm_config_' + proto + '_proxy', options) ||
getEnv(proto + '_proxy', options) ||
getEnv('npm_config_proxy', options) ||
getEnv('all_proxy', options)
if (proxy && proxy.indexOf('://') === -1) {
// Missing scheme in proxy, default to the requested URL's scheme.
proxy = proto + '://' + proxy
}
return proxy
}
/**
* Get the proxy URL for a given URL.
* For node-red < v3.1 or compatibility mode
* @param {string} url The URL to check for proxying
* @param {object} [env] The environment object to use (default process.env)
* @returns
*/
function legacyGetProxyForUrl(url, env) {
env = env || process.env
let prox, noprox;
if (env.http_proxy) { prox = env.http_proxy; }
if (env.HTTP_PROXY) { prox = env.HTTP_PROXY; }
if (env.no_proxy) { noprox = env.no_proxy.split(","); }
if (env.NO_PROXY) { noprox = env.NO_PROXY.split(","); }
let noproxy = false;
if (noprox) {
for (let i in noprox) {
if (url.indexOf(noprox[i].trim()) !== -1) { noproxy=true; }
}
}
if (prox && !noproxy) {
return prox
}
return ""
}
/**
* Determines whether a given URL should be proxied.
*
* @param {string} hostname - The host name of the URL.
* @param {number} port - The effective port of the URL.
* @returns {boolean} Whether the given URL should be proxied.
* @private
*/
function shouldProxy(hostname, port, options) {
const NO_PROXY =
(getEnv('npm_config_no_proxy', options) || getEnv('no_proxy', options)).toLowerCase()
if (!NO_PROXY) {
return true // Always proxy if NO_PROXY is not set.
}
if (NO_PROXY === '*') {
return false // Never proxy if wildcard is set.
}
return NO_PROXY.split(/[,\s]/).every(function (proxy) {
if (!proxy) {
return true // Skip zero-length hosts.
}
const parsedProxy = proxy.match(/^(.+):(\d+)$/)
let parsedProxyHostname = parsedProxy ? parsedProxy[1] : proxy
const parsedProxyPort = parsedProxy ? parseInt(parsedProxy[2]) : 0
if (parsedProxyPort && parsedProxyPort !== port) {
return true // Skip if ports don't match.
}
if (!/^[.*]/.test(parsedProxyHostname)) {
// No wildcards, so stop proxying if there is an exact match.
return hostname !== parsedProxyHostname
}
if (parsedProxyHostname.charAt(0) === '*') {
// Remove leading wildcard.
parsedProxyHostname = parsedProxyHostname.slice(1)
}
// Stop proxying if the hostname ends with the no_proxy host.
return !hostname.endsWith(parsedProxyHostname)
})
}
/**
* Get the value for an environment constiable.
*
* @param {string} key - The name of the environment constiable.
* @param {ProxyOptions} options - The name of the environment constiable.
* @return {string} The value of the environment constiable.
* @private
*/
function getEnv(key, options) {
const env = (options && options.env) || process.env
if (options && options.excludeNpm === true) {
if (key.startsWith('npm_config_')) {
return ''
}
}
if (options && options.lowerCaseOnly === true) {
return env[key.toLowerCase()] || ''
} else if (options && options.favourUpperCase === true) {
return env[key.toUpperCase()] || env[key.toLowerCase()] || ''
}
return env[key.toLowerCase()] || env[key.toUpperCase()] || ''
}
module.exports = {
getProxyForUrl,
parseUrl
}