mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Merge pull request #3307 from node-red/add-tls-option-to-tcp-node
Add TLS option to tcp client nodes
This commit is contained in:
commit
ee84eb666b
@ -23,9 +23,17 @@
|
|||||||
</select>
|
</select>
|
||||||
<span data-i18n="tcpin.label.port"></span> <input type="text" id="node-input-port" style="width:65px">
|
<span data-i18n="tcpin.label.port"></span> <input type="text" id="node-input-port" style="width:65px">
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row hidden" id="node-input-host-row" style="padding-left: 110px;">
|
<div class="form-row hidden" id="node-input-host-row" style="padding-left:110px;">
|
||||||
<span data-i18n="tcpin.label.host"></span> <input type="text" id="node-input-host" placeholder="localhost" style="width: 60%;">
|
<span data-i18n="tcpin.label.host"></span> <input type="text" id="node-input-host" placeholder="localhost" style="width: 60%;">
|
||||||
</div>
|
</div>
|
||||||
|
<div class="form-row" id="node-input-tls-enable">
|
||||||
|
<label> </label>
|
||||||
|
<input type="checkbox" id="node-input-usetls" style="display: inline-block; width:auto; vertical-align:top;">
|
||||||
|
<label for="node-input-usetls" style="width:auto" data-i18n="httpin.use-tls"></label>
|
||||||
|
<div id="node-row-tls" class="hide">
|
||||||
|
<label style="width:auto; margin-left:20px; margin-right:10px;" for="node-input-tls"><span data-i18n="httpin.tls-config"></span></label><input type="text" style="width: 300px" id="node-input-tls">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label><i class="fa fa-sign-out"></i> <span data-i18n="tcpin.label.output"></span></label>
|
<label><i class="fa fa-sign-out"></i> <span data-i18n="tcpin.label.output"></span></label>
|
||||||
@ -42,7 +50,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="node-row-newline" class="form-row hidden" style="padding-left:110px;">
|
<div id="node-row-newline" class="form-row hidden" style="padding-left:110px;">
|
||||||
<span data-i18n="tcpin.label.delimited"></span> <input type="text" id="node-input-newline" style="width:110px;">
|
<span data-i18n="tcpin.label.delimited"></span> <input type="text" id="node-input-newline" style="width:110px;" data-i18n="[placeholder]tcpin.label.optional">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
@ -58,17 +66,18 @@
|
|||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
RED.nodes.registerType('tcp in',{
|
RED.nodes.registerType('tcp in',{
|
||||||
category: 'network',
|
category: 'network',
|
||||||
color:"Silver",
|
color: "Silver",
|
||||||
defaults: {
|
defaults: {
|
||||||
name: {value:""},
|
name: {value:""},
|
||||||
server: {value:"server",required:true},
|
server: {value:"server", required:true},
|
||||||
host: {value:"",validate:function(v) { return (this.server == "server")||v.length > 0;} },
|
host: {value:"", validate:function(v) { return (this.server == "server")||v.length > 0;} },
|
||||||
port: {value:"",required:true,validate:RED.validators.number()},
|
port: {value:"", required:true, validate:RED.validators.number()},
|
||||||
datamode:{value:"stream"},
|
datamode:{value:"stream"},
|
||||||
datatype:{value:"buffer"},
|
datatype:{value:"buffer"},
|
||||||
newline:{value:""},
|
newline:{value:""},
|
||||||
topic: {value:""},
|
topic: {value:""},
|
||||||
base64: {/*deprecated*/ value:false,required:true}
|
base64: {/*deprecated*/ value:false, required:true},
|
||||||
|
tls: {type:"tls-config", value:'', required:false}
|
||||||
},
|
},
|
||||||
inputs:0,
|
inputs:0,
|
||||||
outputs:1,
|
outputs:1,
|
||||||
@ -77,7 +86,7 @@
|
|||||||
return this.name || "tcp:"+(this.host?this.host+":":"")+this.port;
|
return this.name || "tcp:"+(this.host?this.host+":":"")+this.port;
|
||||||
},
|
},
|
||||||
labelStyle: function() {
|
labelStyle: function() {
|
||||||
return this.name?"node_label_italic":"";
|
return this.name ? "node_label_italic" : "";
|
||||||
},
|
},
|
||||||
oneditprepare: function() {
|
oneditprepare: function() {
|
||||||
var updateOptions = function() {
|
var updateOptions = function() {
|
||||||
@ -103,6 +112,27 @@
|
|||||||
$("#node-input-server").change(updateOptions);
|
$("#node-input-server").change(updateOptions);
|
||||||
$("#node-input-datatype").change(updateOptions);
|
$("#node-input-datatype").change(updateOptions);
|
||||||
$("#node-input-datamode").change(updateOptions);
|
$("#node-input-datamode").change(updateOptions);
|
||||||
|
function updateTLSOptions() {
|
||||||
|
if ($("#node-input-usetls").is(':checked')) {
|
||||||
|
$("#node-row-tls").show();
|
||||||
|
} else {
|
||||||
|
$("#node-row-tls").hide();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this.tls) {
|
||||||
|
$('#node-input-usetls').prop('checked', true);
|
||||||
|
} else {
|
||||||
|
$('#node-input-usetls').prop('checked', false);
|
||||||
|
}
|
||||||
|
updateTLSOptions();
|
||||||
|
$("#node-input-usetls").on("click",function() {
|
||||||
|
updateTLSOptions();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
oneditsave: function() {
|
||||||
|
if (!$("#node-input-usetls").is(':checked')) {
|
||||||
|
$("#node-input-tls").val("_ADD_");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
@ -123,6 +153,15 @@
|
|||||||
<span data-i18n="tcpin.label.host"></span> <input type="text" id="node-input-host" style="width: 60%;">
|
<span data-i18n="tcpin.label.host"></span> <input type="text" id="node-input-host" style="width: 60%;">
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="form-row" id="node-input-tls-enable">
|
||||||
|
<label> </label>
|
||||||
|
<input type="checkbox" id="node-input-usetls" style="display: inline-block; width: auto; vertical-align: top;">
|
||||||
|
<label for="node-input-usetls" style="width: auto" data-i18n="httpin.use-tls"></label>
|
||||||
|
<div id="node-row-tls" class="hide">
|
||||||
|
<label style="width: auto; margin-left: 20px; margin-right: 10px;" for="node-input-tls"><span data-i18n="httpin.tls-config"></span></label><input type="text" style="width: 300px" id="node-input-tls">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="form-row hidden" id="node-input-end-row">
|
<div class="form-row hidden" id="node-input-end-row">
|
||||||
<label> </label>
|
<label> </label>
|
||||||
<input type="checkbox" id="node-input-end" style="display: inline-block; width: auto; vertical-align: top;">
|
<input type="checkbox" id="node-input-end" style="display: inline-block; width: auto; vertical-align: top;">
|
||||||
@ -144,14 +183,15 @@
|
|||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
RED.nodes.registerType('tcp out',{
|
RED.nodes.registerType('tcp out',{
|
||||||
category: 'network',
|
category: 'network',
|
||||||
color:"Silver",
|
color: "Silver",
|
||||||
defaults: {
|
defaults: {
|
||||||
|
name: {value:""},
|
||||||
host: {value:"",validate:function(v) { return (this.beserver != "client")||v.length > 0;} },
|
host: {value:"",validate:function(v) { return (this.beserver != "client")||v.length > 0;} },
|
||||||
port: {value:"",validate:function(v) { return (this.beserver == "reply")||RED.validators.number()(v); } },
|
port: {value:"",validate:function(v) { return (this.beserver == "reply")||RED.validators.number()(v); } },
|
||||||
beserver: {value:"client",required:true},
|
beserver: {value:"client", required:true},
|
||||||
base64: {value:false,required:true},
|
base64: {value:false, required:true},
|
||||||
end: {value:false,required:true},
|
end: {value:false, required:true},
|
||||||
name: {value:""}
|
tls: {type:"tls-config", value:'', required:false}
|
||||||
},
|
},
|
||||||
inputs:1,
|
inputs:1,
|
||||||
outputs:0,
|
outputs:0,
|
||||||
@ -170,18 +210,42 @@
|
|||||||
$("#node-input-port-row").hide();
|
$("#node-input-port-row").hide();
|
||||||
$("#node-input-host-row").hide();
|
$("#node-input-host-row").hide();
|
||||||
$("#node-input-end-row").hide();
|
$("#node-input-end-row").hide();
|
||||||
|
$("#node-input-tls-enable").hide();
|
||||||
} else if (sockettype == "client"){
|
} else if (sockettype == "client"){
|
||||||
$("#node-input-port-row").show();
|
$("#node-input-port-row").show();
|
||||||
$("#node-input-host-row").show();
|
$("#node-input-host-row").show();
|
||||||
$("#node-input-end-row").show();
|
$("#node-input-end-row").show();
|
||||||
|
$("#node-input-tls-enable").show();
|
||||||
} else {
|
} else {
|
||||||
$("#node-input-port-row").show();
|
$("#node-input-port-row").show();
|
||||||
$("#node-input-host-row").hide();
|
$("#node-input-host-row").hide();
|
||||||
$("#node-input-end-row").show();
|
$("#node-input-end-row").show();
|
||||||
|
$("#node-input-tls-enable").show();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
updateOptions();
|
updateOptions();
|
||||||
$("#node-input-beserver").change(updateOptions);
|
$("#node-input-beserver").change(updateOptions);
|
||||||
|
function updateTLSOptions() {
|
||||||
|
if ($("#node-input-usetls").is(':checked')) {
|
||||||
|
$("#node-row-tls").show();
|
||||||
|
} else {
|
||||||
|
$("#node-row-tls").hide();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this.tls) {
|
||||||
|
$('#node-input-usetls').prop('checked', true);
|
||||||
|
} else {
|
||||||
|
$('#node-input-usetls').prop('checked', false);
|
||||||
|
}
|
||||||
|
updateTLSOptions();
|
||||||
|
$("#node-input-usetls").on("click",function() {
|
||||||
|
updateTLSOptions();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
oneditsave: function() {
|
||||||
|
if (!$("#node-input-usetls").is(':checked')) {
|
||||||
|
$("#node-input-tls").val("_ADD_");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
@ -194,15 +258,23 @@
|
|||||||
<span data-i18n="tcpin.label.port"></span>
|
<span data-i18n="tcpin.label.port"></span>
|
||||||
<input type="text" id="node-input-port" style="width:60px">
|
<input type="text" id="node-input-port" style="width:60px">
|
||||||
</div>
|
</div>
|
||||||
|
<div class="form-row" id="node-input-tls-enable">
|
||||||
|
<label> </label>
|
||||||
|
<input type="checkbox" id="node-input-usetls" style="display: inline-block; width: auto; vertical-align: top;">
|
||||||
|
<label for="node-input-usetls" style="width: auto" data-i18n="httpin.use-tls"></label>
|
||||||
|
<div id="node-row-tls" class="hide">
|
||||||
|
<label style="width: auto; margin-left: 20px; margin-right: 10px;" for="node-input-tls"><span data-i18n="httpin.tls-config"></span></label><input type="text" style="width: 300px" id="node-input-tls">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-out"><i class="fa fa-sign-out"></i> <span data-i18n="tcpin.label.return"></span></label>
|
<label for="node-input-ret"><i class="fa fa-sign-out"></i> <span data-i18n="tcpin.label.return"></span></label>
|
||||||
<select type="text" id="node-input-ret" style="width:54%;">
|
<select type="text" id="node-input-ret" style="width:54%;">
|
||||||
<option value="buffer" data-i18n="tcpin.output.buffer"></option>
|
<option value="buffer" data-i18n="tcpin.output.buffer"></option>
|
||||||
<option value="string" data-i18n="tcpin.output.string"></option>
|
<option value="string" data-i18n="tcpin.output.string"></option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-out"> </label>
|
<label for="node-input-out"><i class="fa fa-sign-out fa-rotate-90"></i> <span data-i18n="tcpin.label.close"></span></label>
|
||||||
<select type="text" id="node-input-out" style="width:54%;">
|
<select type="text" id="node-input-out" style="width:54%;">
|
||||||
<option value="time" data-i18n="tcpin.return.timeout"></option>
|
<option value="time" data-i18n="tcpin.return.timeout"></option>
|
||||||
<option value="char" data-i18n="tcpin.return.character"></option>
|
<option value="char" data-i18n="tcpin.return.character"></option>
|
||||||
@ -213,6 +285,9 @@
|
|||||||
<input type="text" id="node-input-splitc" style="width:50px;">
|
<input type="text" id="node-input-splitc" style="width:50px;">
|
||||||
<span id="node-units"></span>
|
<span id="node-units"></span>
|
||||||
</div>
|
</div>
|
||||||
|
<div id="node-row-newline" class="form-row hidden" style="padding-left:162px;">
|
||||||
|
<span data-i18n="tcpin.label.delimited"></span> <input type="text" id="node-input-newline" style="width:110px;" data-i18n="[placeholder]tcpin.label.optional">
|
||||||
|
</div>
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
|
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
|
||||||
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
|
<input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
|
||||||
@ -222,14 +297,16 @@
|
|||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
RED.nodes.registerType('tcp request',{
|
RED.nodes.registerType('tcp request',{
|
||||||
category: 'network',
|
category: 'network',
|
||||||
color:"Silver",
|
color: "Silver",
|
||||||
defaults: {
|
defaults: {
|
||||||
|
name: {value:""},
|
||||||
server: {value:""},
|
server: {value:""},
|
||||||
port: {value:"",validate:RED.validators.regex(/^(\d*|)$/)},
|
port: {value:"", validate:RED.validators.regex(/^(\d*|)$/)},
|
||||||
out: {value:"time",required:true},
|
out: {value:"time", required:true},
|
||||||
ret: {value:"buffer"},
|
ret: {value:"buffer"},
|
||||||
splitc: {value:"0",required:true},
|
splitc: {value:"0", required:true},
|
||||||
name: {value:""}
|
newline: {value:""},
|
||||||
|
tls: {type:"tls-config", value:'', required:false}
|
||||||
},
|
},
|
||||||
inputs:1,
|
inputs:1,
|
||||||
outputs:1,
|
outputs:1,
|
||||||
@ -246,6 +323,14 @@
|
|||||||
$("#node-input-ret").val("buffer");
|
$("#node-input-ret").val("buffer");
|
||||||
this.ret = "buffer";
|
this.ret = "buffer";
|
||||||
}
|
}
|
||||||
|
$("#node-input-ret").on("change", function() {
|
||||||
|
if ($("#node-input-ret").val() === "string" && $("#node-input-out").val() === "sit") { $("#node-row-newline").show(); }
|
||||||
|
else { $("#node-row-newline").hide(); }
|
||||||
|
});
|
||||||
|
$("#node-input-out").on("change", function() {
|
||||||
|
if ($("#node-input-ret").val() === "string" && $("#node-input-out").val() === "sit") { $("#node-row-newline").show(); }
|
||||||
|
else { $("#node-row-newline").hide(); }
|
||||||
|
});
|
||||||
$("#node-input-out").on('focus', function () { previous = this.value; }).on("change", function() {
|
$("#node-input-out").on('focus', function () { previous = this.value; }).on("change", function() {
|
||||||
$("#node-input-splitc").show();
|
$("#node-input-splitc").show();
|
||||||
if (previous === null) { previous = $("#node-input-out").val(); }
|
if (previous === null) { previous = $("#node-input-out").val(); }
|
||||||
@ -272,6 +357,27 @@
|
|||||||
$("#node-input-splitc").hide();
|
$("#node-input-splitc").hide();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
function updateTLSOptions() {
|
||||||
|
if ($("#node-input-usetls").is(':checked')) {
|
||||||
|
$("#node-row-tls").show();
|
||||||
|
} else {
|
||||||
|
$("#node-row-tls").hide();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this.tls) {
|
||||||
|
$('#node-input-usetls').prop('checked', true);
|
||||||
|
} else {
|
||||||
|
$('#node-input-usetls').prop('checked', false);
|
||||||
|
}
|
||||||
|
updateTLSOptions();
|
||||||
|
$("#node-input-usetls").on("click",function() {
|
||||||
|
updateTLSOptions();
|
||||||
|
});
|
||||||
|
},
|
||||||
|
oneditsave: function() {
|
||||||
|
if (!$("#node-input-usetls").is(':checked')) {
|
||||||
|
$("#node-input-tls").val("_ADD_");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
@ -16,13 +16,46 @@
|
|||||||
|
|
||||||
module.exports = function(RED) {
|
module.exports = function(RED) {
|
||||||
"use strict";
|
"use strict";
|
||||||
var reconnectTime = RED.settings.socketReconnectTime||10000;
|
let reconnectTime = RED.settings.socketReconnectTime || 10000;
|
||||||
var socketTimeout = RED.settings.socketTimeout||null;
|
let socketTimeout = RED.settings.socketTimeout || null;
|
||||||
const msgQueueSize = RED.settings.tcpMsgQueueSize || 1000;
|
const msgQueueSize = RED.settings.tcpMsgQueueSize || 1000;
|
||||||
const Denque = require('denque');
|
const Denque = require('denque');
|
||||||
var net = require('net');
|
const net = require('net');
|
||||||
|
const tls = require('tls');
|
||||||
|
|
||||||
var connectionPool = {};
|
let connectionPool = {};
|
||||||
|
|
||||||
|
function normalizeConnectArgs(listArgs) {
|
||||||
|
const args = net._normalizeArgs(listArgs);
|
||||||
|
const options = args[0];
|
||||||
|
const cb = args[1];
|
||||||
|
|
||||||
|
// If args[0] was options, then normalize dealt with it.
|
||||||
|
// If args[0] is port, or args[0], args[1] is host, port, we need to
|
||||||
|
// find the options and merge them in, normalize's options has only
|
||||||
|
// the host/port/path args that it knows about, not the tls options.
|
||||||
|
// This means that options.host overrides a host arg.
|
||||||
|
if (listArgs[1] !== null && typeof listArgs[1] === 'object') {
|
||||||
|
ObjectAssign(options, listArgs[1]);
|
||||||
|
} else if (listArgs[2] !== null && typeof listArgs[2] === 'object') {
|
||||||
|
ObjectAssign(options, listArgs[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cb ? [options, cb] : [options];
|
||||||
|
}
|
||||||
|
|
||||||
|
function getAllowUnauthorized() {
|
||||||
|
const allowUnauthorized = process.env.NODE_TLS_REJECT_UNAUTHORIZED === '0';
|
||||||
|
|
||||||
|
if (allowUnauthorized) {
|
||||||
|
process.emitWarning(
|
||||||
|
'Setting the NODE_TLS_REJECT_UNAUTHORIZED ' +
|
||||||
|
'environment variable to \'0\' makes TLS connections ' +
|
||||||
|
'and HTTPS requests insecure by disabling ' +
|
||||||
|
'certificate verification.');
|
||||||
|
}
|
||||||
|
return allowUnauthorized;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enqueue `item` in `queue`
|
* Enqueue `item` in `queue`
|
||||||
@ -53,13 +86,14 @@ module.exports = function(RED) {
|
|||||||
this.topic = n.topic;
|
this.topic = n.topic;
|
||||||
this.stream = (!n.datamode||n.datamode=='stream'); /* stream,single*/
|
this.stream = (!n.datamode||n.datamode=='stream'); /* stream,single*/
|
||||||
this.datatype = n.datatype||'buffer'; /* buffer,utf8,base64 */
|
this.datatype = n.datatype||'buffer'; /* buffer,utf8,base64 */
|
||||||
this.newline = (n.newline||"").replace("\\n","\n").replace("\\r","\r");
|
this.newline = (n.newline||"").replace("\\n","\n").replace("\\r","\r").replace("\\t","\t");
|
||||||
this.base64 = n.base64;
|
this.base64 = n.base64;
|
||||||
this.server = (typeof n.server == 'boolean')?n.server:(n.server == "server");
|
this.server = (typeof n.server == 'boolean')?n.server:(n.server == "server");
|
||||||
this.closing = false;
|
this.closing = false;
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
var node = this;
|
var node = this;
|
||||||
var count = 0;
|
var count = 0;
|
||||||
|
if (n.tls) { var tlsNode = RED.nodes.getNode(n.tls); }
|
||||||
|
|
||||||
if (!node.server) {
|
if (!node.server) {
|
||||||
var buffer = null;
|
var buffer = null;
|
||||||
@ -70,13 +104,25 @@ module.exports = function(RED) {
|
|||||||
node.log(RED._("tcpin.status.connecting",{host:node.host,port:node.port}));
|
node.log(RED._("tcpin.status.connecting",{host:node.host,port:node.port}));
|
||||||
node.status({fill:"grey",shape:"dot",text:"common.status.connecting"});
|
node.status({fill:"grey",shape:"dot",text:"common.status.connecting"});
|
||||||
var id = RED.util.generateId();
|
var id = RED.util.generateId();
|
||||||
client = net.connect(node.port, node.host, function() {
|
var connOpts = {host: node.host};
|
||||||
buffer = (node.datatype == 'buffer') ? Buffer.alloc(0) : "";
|
if (n.tls) {
|
||||||
node.connected = true;
|
var connOpts = tlsNode.addTLSOptions({host: node.host});
|
||||||
node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port}));
|
client = tls.connect(node.port, connOpts, function() {
|
||||||
node.status({fill:"green",shape:"dot",text:"common.status.connected",_session:{type:"tcp",id:id}});
|
buffer = (node.datatype == 'buffer') ? Buffer.alloc(0) : "";
|
||||||
});
|
node.connected = true;
|
||||||
client.setKeepAlive(true,120000);
|
node.log(RED._("status.connected", {host: node.host, port: node.port}));
|
||||||
|
node.status({fill:"green",shape:"dot",text:"common.status.connected",_session:{type:"tcp",id:id}});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
client = net.connect(node.port, node.host, function() {
|
||||||
|
buffer = (node.datatype == 'buffer') ? Buffer.alloc(0) : "";
|
||||||
|
node.connected = true;
|
||||||
|
node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port}));
|
||||||
|
node.status({fill:"green",shape:"dot",text:"common.status.connected",_session:{type:"tcp",id:id}});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
client.setKeepAlive(true, 120000);
|
||||||
connectionPool[id] = client;
|
connectionPool[id] = client;
|
||||||
|
|
||||||
client.on('data', function (data) {
|
client.on('data', function (data) {
|
||||||
@ -89,7 +135,7 @@ module.exports = function(RED) {
|
|||||||
buffer = buffer+data;
|
buffer = buffer+data;
|
||||||
var parts = buffer.split(node.newline);
|
var parts = buffer.split(node.newline);
|
||||||
for (var i = 0; i<parts.length-1; i+=1) {
|
for (var i = 0; i<parts.length-1; i+=1) {
|
||||||
msg = {topic:node.topic, payload:parts[i]};
|
msg = {topic:node.topic, payload:parts[i] + node.newline.trimEnd()};
|
||||||
msg._session = {type:"tcp",id:id};
|
msg._session = {type:"tcp",id:id};
|
||||||
node.send(msg);
|
node.send(msg);
|
||||||
}
|
}
|
||||||
@ -150,7 +196,13 @@ module.exports = function(RED) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var server = net.createServer(function (socket) {
|
let srv = net;
|
||||||
|
let connOpts;
|
||||||
|
if (n.tls) {
|
||||||
|
srv = tls;
|
||||||
|
connOpts = tlsNode.addTLSOptions({});
|
||||||
|
}
|
||||||
|
var server = srv.createServer(connOpts, function (socket) {
|
||||||
socket.setKeepAlive(true,120000);
|
socket.setKeepAlive(true,120000);
|
||||||
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
|
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
|
||||||
var id = RED.util.generateId();
|
var id = RED.util.generateId();
|
||||||
@ -177,7 +229,7 @@ module.exports = function(RED) {
|
|||||||
buffer = buffer+data;
|
buffer = buffer+data;
|
||||||
var parts = buffer.split(node.newline);
|
var parts = buffer.split(node.newline);
|
||||||
for (var i = 0; i<parts.length-1; i+=1) {
|
for (var i = 0; i<parts.length-1; i+=1) {
|
||||||
msg = {topic:node.topic, payload:parts[i], ip:socket.remoteAddress, port:socket.remotePort};
|
msg = {topic:node.topic, payload:parts[i] + node.newline.trimEnd(), ip:socket.remoteAddress, port:socket.remotePort};
|
||||||
msg._session = {type:"tcp",id:id};
|
msg._session = {type:"tcp",id:id};
|
||||||
node.send(msg);
|
node.send(msg);
|
||||||
}
|
}
|
||||||
@ -269,8 +321,9 @@ module.exports = function(RED) {
|
|||||||
this.closing = false;
|
this.closing = false;
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
var node = this;
|
var node = this;
|
||||||
|
if (n.tls) { var tlsNode = RED.nodes.getNode(n.tls); }
|
||||||
|
|
||||||
if (!node.beserver||node.beserver=="client") {
|
if (!node.beserver || node.beserver == "client") {
|
||||||
var reconnectTimeout;
|
var reconnectTimeout;
|
||||||
var client = null;
|
var client = null;
|
||||||
var end = false;
|
var end = false;
|
||||||
@ -278,11 +331,24 @@ module.exports = function(RED) {
|
|||||||
var setupTcpClient = function() {
|
var setupTcpClient = function() {
|
||||||
node.log(RED._("tcpin.status.connecting",{host:node.host,port:node.port}));
|
node.log(RED._("tcpin.status.connecting",{host:node.host,port:node.port}));
|
||||||
node.status({fill:"grey",shape:"dot",text:"common.status.connecting"});
|
node.status({fill:"grey",shape:"dot",text:"common.status.connecting"});
|
||||||
client = net.connect(node.port, node.host, function() {
|
if (n.tls) {
|
||||||
node.connected = true;
|
// connOpts = tlsNode.addTLSOptions(connOpts);
|
||||||
node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port}));
|
// client = tls.connect(connOpts, function() {
|
||||||
node.status({fill:"green",shape:"dot",text:"common.status.connected"});
|
var connOpts = tlsNode.addTLSOptions({host: node.host});
|
||||||
});
|
client = tls.connect(node.port, connOpts, function() {
|
||||||
|
// buffer = (node.datatype == 'buffer') ? Buffer.alloc(0) : "";
|
||||||
|
node.connected = true;
|
||||||
|
node.log(RED._("status.connected", {host: node.host, port: node.port}));
|
||||||
|
node.status({fill:"green",shape:"dot",text:"common.status.connected"});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
client = net.connect(node.port, node.host, function() {
|
||||||
|
node.connected = true;
|
||||||
|
node.log(RED._("tcpin.status.connected",{host:node.host,port:node.port}));
|
||||||
|
node.status({fill:"green",shape:"dot",text:"common.status.connected"});
|
||||||
|
});
|
||||||
|
}
|
||||||
client.setKeepAlive(true,120000);
|
client.setKeepAlive(true,120000);
|
||||||
client.on('error', function (err) {
|
client.on('error', function (err) {
|
||||||
node.log(RED._("tcpin.errors.error",{error:err.toString()}));
|
node.log(RED._("tcpin.errors.error",{error:err.toString()}));
|
||||||
@ -368,7 +434,13 @@ module.exports = function(RED) {
|
|||||||
else {
|
else {
|
||||||
var connectedSockets = [];
|
var connectedSockets = [];
|
||||||
node.status({text:RED._("tcpin.status.connections",{count:0})});
|
node.status({text:RED._("tcpin.status.connections",{count:0})});
|
||||||
var server = net.createServer(function (socket) {
|
let srv = net;
|
||||||
|
let connOpts;
|
||||||
|
if (n.tls) {
|
||||||
|
srv = tls;
|
||||||
|
connOpts = tlsNode.addTLSOptions({});
|
||||||
|
}
|
||||||
|
var server = srv.createServer(connOpts, function (socket) {
|
||||||
socket.setKeepAlive(true,120000);
|
socket.setKeepAlive(true,120000);
|
||||||
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
|
if (socketTimeout !== null) { socket.setTimeout(socketTimeout); }
|
||||||
node.log(RED._("tcpin.status.connection-from",{host:socket.remoteAddress, port:socket.remotePort}));
|
node.log(RED._("tcpin.status.connection-from",{host:socket.remoteAddress, port:socket.remotePort}));
|
||||||
@ -445,7 +517,11 @@ module.exports = function(RED) {
|
|||||||
this.port = Number(n.port);
|
this.port = Number(n.port);
|
||||||
this.out = n.out;
|
this.out = n.out;
|
||||||
this.ret = n.ret || "buffer";
|
this.ret = n.ret || "buffer";
|
||||||
|
this.newline = (n.newline||"").replace("\\n","\n").replace("\\r","\r").replace("\\t","\t");
|
||||||
this.splitc = n.splitc;
|
this.splitc = n.splitc;
|
||||||
|
if (n.tls) {
|
||||||
|
var tlsNode = RED.nodes.getNode(n.tls);
|
||||||
|
}
|
||||||
|
|
||||||
if (this.out === "immed") { this.splitc = -1; this.out = "time"; }
|
if (this.out === "immed") { this.splitc = -1; this.out = "time"; }
|
||||||
if (this.out !== "char") { this.splitc = Number(this.splitc); }
|
if (this.out !== "char") { this.splitc = Number(this.splitc); }
|
||||||
@ -500,12 +576,48 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
else { buf = Buffer.alloc(65536); } // set it to 64k... hopefully big enough for most TCP packets.... but only hopefully
|
else { buf = Buffer.alloc(65536); } // set it to 64k... hopefully big enough for most TCP packets.... but only hopefully
|
||||||
|
|
||||||
clients[connection_id].client = net.Socket();
|
var connOpts = {host:host, port:port};
|
||||||
|
if (n.tls) {
|
||||||
|
connOpts = tlsNode.addTLSOptions(connOpts);
|
||||||
|
const allowUnauthorized = getAllowUnauthorized();
|
||||||
|
|
||||||
|
let options = {
|
||||||
|
rejectUnauthorized: !allowUnauthorized,
|
||||||
|
ciphers: tls.DEFAULT_CIPHERS,
|
||||||
|
checkServerIdentity: tls.checkServerIdentity,
|
||||||
|
minDHSize: 1024,
|
||||||
|
...connOpts
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!options.keepAlive) { options.singleUse = true; }
|
||||||
|
|
||||||
|
const context = options.secureContext || tls.createSecureContext(options);
|
||||||
|
|
||||||
|
clients[connection_id].client = new tls.TLSSocket(options.socket, {
|
||||||
|
allowHalfOpen: options.allowHalfOpen,
|
||||||
|
pipe: !!options.path,
|
||||||
|
secureContext: context,
|
||||||
|
isServer: false,
|
||||||
|
requestCert: false, // true,
|
||||||
|
rejectUnauthorized: false, // options.rejectUnauthorized !== false,
|
||||||
|
session: options.session,
|
||||||
|
ALPNProtocols: options.ALPNProtocols,
|
||||||
|
requestOCSP: options.requestOCSP,
|
||||||
|
enableTrace: options.enableTrace,
|
||||||
|
pskCallback: options.pskCallback,
|
||||||
|
highWaterMark: options.highWaterMark,
|
||||||
|
onread: options.onread,
|
||||||
|
signal: options.signal,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
clients[connection_id].client = net.Socket();
|
||||||
|
}
|
||||||
if (socketTimeout !== null) { clients[connection_id].client.setTimeout(socketTimeout);}
|
if (socketTimeout !== null) { clients[connection_id].client.setTimeout(socketTimeout);}
|
||||||
|
|
||||||
if (host && port) {
|
if (host && port) {
|
||||||
clients[connection_id].connecting = true;
|
clients[connection_id].connecting = true;
|
||||||
clients[connection_id].client.connect(port, host, function() {
|
clients[connection_id].client.connect(connOpts, function() {
|
||||||
//node.log(RED._("tcpin.errors.client-connected"));
|
//node.log(RED._("tcpin.errors.client-connected"));
|
||||||
node.status({fill:"green",shape:"dot",text:"common.status.connected"});
|
node.status({fill:"green",shape:"dot",text:"common.status.connected"});
|
||||||
if (clients[connection_id] && clients[connection_id].client) {
|
if (clients[connection_id] && clients[connection_id].client) {
|
||||||
@ -528,17 +640,32 @@ module.exports = function(RED) {
|
|||||||
else {
|
else {
|
||||||
node.warn(RED._("tcpin.errors.no-host"));
|
node.warn(RED._("tcpin.errors.no-host"));
|
||||||
}
|
}
|
||||||
|
var chunk = "";
|
||||||
clients[connection_id].client.on('data', function(data) {
|
clients[connection_id].client.on('data', function(data) {
|
||||||
if (node.out === "sit") { // if we are staying connected just send the buffer
|
if (node.out === "sit") { // if we are staying connected just send the buffer
|
||||||
if (clients[connection_id]) {
|
if (clients[connection_id]) {
|
||||||
const msg = clients[connection_id].lastMsg || {};
|
const msg = clients[connection_id].lastMsg || {};
|
||||||
msg.payload = RED.util.cloneMessage(data);
|
msg.payload = RED.util.cloneMessage(data);
|
||||||
if (node.ret === "string") {
|
if (node.ret === "string") {
|
||||||
try { msg.payload = msg.payload.toString(); }
|
try {
|
||||||
catch(e) { node.error("Failed to create string", msg); }
|
if (node.newline && node.newline !== "" ) {
|
||||||
|
chunk += msg.payload.toString();
|
||||||
|
let parts = chunk.split(node.newline);
|
||||||
|
for (var p=0; p<parts.length-1; p+=1) {
|
||||||
|
let m = RED.util.cloneMessage(msg);
|
||||||
|
m.payload = parts[p] + node.newline.trimEnd();
|
||||||
|
nodeSend(m);
|
||||||
|
}
|
||||||
|
chunk = parts[parts.length-1];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
msg.payload = msg.payload.toString();
|
||||||
|
nodeSend(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(e) { node.error(RED._("tcpin.errors.bad-string"), msg); }
|
||||||
}
|
}
|
||||||
nodeSend(msg);
|
else { nodeSend(msg); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// else if (node.splitc === 0) {
|
// else if (node.splitc === 0) {
|
||||||
@ -675,7 +802,13 @@ module.exports = function(RED) {
|
|||||||
//node.warn(RED._("tcpin.errors.connect-timeout"));
|
//node.warn(RED._("tcpin.errors.connect-timeout"));
|
||||||
if (clients[connection_id].client) {
|
if (clients[connection_id].client) {
|
||||||
clients[connection_id].connecting = true;
|
clients[connection_id].connecting = true;
|
||||||
clients[connection_id].client.connect(port, host, function() {
|
|
||||||
|
var connOpts = {host:host, port:port};
|
||||||
|
if (n.tls) {
|
||||||
|
connOpts = tlsNode.addTLSOptions(connOpts);
|
||||||
|
}
|
||||||
|
|
||||||
|
clients[connection_id].client.connect(connOpts, function() {
|
||||||
clients[connection_id].connected = true;
|
clients[connection_id].connected = true;
|
||||||
clients[connection_id].connecting = false;
|
clients[connection_id].connecting = false;
|
||||||
node.status({fill:"green",shape:"dot",text:"common.status.connected"});
|
node.status({fill:"green",shape:"dot",text:"common.status.connected"});
|
||||||
|
@ -580,7 +580,9 @@
|
|||||||
"server": "Server",
|
"server": "Server",
|
||||||
"return": "Return",
|
"return": "Return",
|
||||||
"ms": "ms",
|
"ms": "ms",
|
||||||
"chars": "chars"
|
"chars": "chars",
|
||||||
|
"close": "Close",
|
||||||
|
"optional": "(optional)"
|
||||||
},
|
},
|
||||||
"type": {
|
"type": {
|
||||||
"listen": "Listen on",
|
"listen": "Listen on",
|
||||||
@ -597,7 +599,7 @@
|
|||||||
"return": {
|
"return": {
|
||||||
"timeout": "after a fixed timeout of",
|
"timeout": "after a fixed timeout of",
|
||||||
"character": "when character received is",
|
"character": "when character received is",
|
||||||
"number": "a fixed number of chars",
|
"number": "after a fixed number of characters",
|
||||||
"never": "never - keep connection open",
|
"never": "never - keep connection open",
|
||||||
"immed": "immediately - don't wait for reply"
|
"immed": "immediately - don't wait for reply"
|
||||||
},
|
},
|
||||||
@ -617,11 +619,11 @@
|
|||||||
"timeout": "timeout closed socket port __port__",
|
"timeout": "timeout closed socket port __port__",
|
||||||
"cannot-listen": "unable to listen on port __port__, error: __error__",
|
"cannot-listen": "unable to listen on port __port__, error: __error__",
|
||||||
"error": "error: __error__",
|
"error": "error: __error__",
|
||||||
|
|
||||||
"socket-error": "socket error from __host__:__port__",
|
"socket-error": "socket error from __host__:__port__",
|
||||||
"no-host": "Host and/or port not set",
|
"no-host": "Host and/or port not set",
|
||||||
"connect-timeout": "connect timeout",
|
"connect-timeout": "connect timeout",
|
||||||
"connect-fail": "connect failed"
|
"connect-fail": "connect failed",
|
||||||
|
"bad-string": "failed to convert to string"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"udp": {
|
"udp": {
|
||||||
|
@ -84,9 +84,17 @@ describe('TCP Request Node', function() {
|
|||||||
n2.on("input", msg => {
|
n2.on("input", msg => {
|
||||||
try {
|
try {
|
||||||
if (typeof result === 'object') {
|
if (typeof result === 'object') {
|
||||||
msg.should.have.properties(Object.assign({}, result, {payload: Buffer.from(result.payload)}));
|
if (flow[0].ret === "string") {
|
||||||
|
msg.should.have.properties(Object.assign({}, result, {payload: result.payload}));
|
||||||
|
} else {
|
||||||
|
msg.should.have.properties(Object.assign({}, result, {payload: Buffer.from(result.payload)}));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
msg.should.have.property('payload', Buffer.from(result));
|
if (flow[0].ret === "string") {
|
||||||
|
msg.should.have.property('payload', result);
|
||||||
|
} else {
|
||||||
|
msg.should.have.property('payload', Buffer.from(result));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
done();
|
done();
|
||||||
} catch(err) {
|
} catch(err) {
|
||||||
@ -245,10 +253,41 @@ describe('TCP Request Node', function() {
|
|||||||
}, done);
|
}, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should send & receive, then keep connection, and not split return strings', function(done) {
|
||||||
|
var flow = [{id:"n1", type:"tcp request", server:"localhost", port:port, out:"sit", ret:"string", newline:"", wires:[["n2"]] },
|
||||||
|
{id:"n2", type:"helper"}];
|
||||||
|
testTCPMany(flow, [{
|
||||||
|
payload: "foo",
|
||||||
|
topic: 'boo'
|
||||||
|
}, {
|
||||||
|
payload: "bar<A>\nfoo",
|
||||||
|
topic: 'boo'
|
||||||
|
}], {
|
||||||
|
payload: "ACK:foobar<A>\nfoo",
|
||||||
|
topic: 'boo'
|
||||||
|
}, done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should send & receive, then keep connection, and split return strings', function(done) {
|
||||||
|
var flow = [{id:"n1", type:"tcp request", server:"localhost", port:port, out:"sit", ret:"string", newline:"<A>\\n", wires:[["n2"]] },
|
||||||
|
{id:"n2", type:"helper"}];
|
||||||
|
testTCPMany(flow, [{
|
||||||
|
payload: "foo",
|
||||||
|
topic: 'boo'
|
||||||
|
}, {
|
||||||
|
payload: "bar<A>\nfoo",
|
||||||
|
topic: 'boo'
|
||||||
|
}], {
|
||||||
|
payload: "ACK:foobar<A>",
|
||||||
|
topic: 'boo'
|
||||||
|
}, done);
|
||||||
|
});
|
||||||
|
|
||||||
it('should send & recv data to/from server:port from msg', function(done) {
|
it('should send & recv data to/from server:port from msg', function(done) {
|
||||||
var flow = [{id:"n1", type:"tcp request", server:"", port:"", out:"time", splitc: "0", wires:[["n2"]] },
|
var flow = [{id:"n1", type:"tcp request", server:"", port:"", out:"time", splitc: "0", wires:[["n2"]] },
|
||||||
{id:"n2", type:"helper"}];
|
{id:"n2", type:"helper"}];
|
||||||
testTCPMany(flow, [{
|
testTCPMany(flow, [
|
||||||
|
{
|
||||||
payload: "f",
|
payload: "f",
|
||||||
host: "localhost",
|
host: "localhost",
|
||||||
port: port
|
port: port
|
||||||
|
Loading…
Reference in New Issue
Block a user