1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge pull request #2983 from hardillb/get-got

Get got working with proxies
This commit is contained in:
Nick O'Leary 2021-05-12 09:41:26 +01:00 committed by GitHub
commit fc8643f238
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -65,6 +65,8 @@ module.exports = function(RED) {
}
this.on("input",function(msg,nodeSend,nodeDone) {
//reset redirectList on each request
redirectList = [];
var preRequestTimestamp = process.hrtime();
node.status({fill:"blue",shape:"dot",text:"httpin.status.requesting"});
var url = nodeUrl || msg.url;
@ -106,11 +108,12 @@ module.exports = function(RED) {
method = msg.method.toUpperCase(); // use the msg parameter
}
var isHttps = (/^https/i.test(url));
// var isHttps = (/^https/i.test(url));
var opts = {};
// set defaultport, else when using HttpsProxyAgent, it's defaultPort of 443 will be used :(.
opts.defaultPort = isHttps?443:80;
// Had to remove this to get http->https redirect to work
// opts.defaultPort = isHttps?443:80;
opts.timeout = node.reqTimeout;
opts.method = method;
opts.headers = {};
@ -339,8 +342,16 @@ module.exports = function(RED) {
var match = prox.match(/^(https?:\/\/)?(.+)?:([0-9]+)?/i);
if (match) {
let proxyAgent;
let proxyURL = new URL(prox);
//set username/password to null to stop empty creds header
let proxyOptions = {
proxy: prox,
proxy: {
protocol: proxyURL.protocol,
hostname: proxyURL.hostname,
port: proxyURL.port,
username: null,
password: null
},
maxFreeSockets: 256,
maxSockets: 256,
keepAlive: true
@ -349,17 +360,16 @@ module.exports = function(RED) {
let proxyUsername = proxyConfig.credentials.username || '';
let proxyPassword = proxyConfig.credentials.password || '';
if (proxyUsername || proxyPassword) {
var m = /^(https?:\/\/)(.*)$/.exec(prox);
proxyOptions.proxy = `${m[1]}${proxyUsername}:${proxyPassword}@${m[2]}`
proxyOptions.proxy.username = proxyUsername;
proxyOptions.proxy.password = proxyPassword;
}
}
opts.agent = {};
if (/^http:/.test(url)) {
opts.agent.http = new HttpProxyAgent(proxyOptions)
} else {
opts.agent.https = new HttpsProxyAgent(proxyOptions)
}
console.log("ProxyOptions:",proxyOptions);
//need both incase of http -> https redirec
opts.agent = {
http: new HttpProxyAgent(proxyOptions),
https: new HttpsProxyAgent(proxyOptions)
};
} else {
node.warn("Bad proxy url: "+ prox);
}