Node.js-在启动TLS之前发送未加密的消息



每个人,我有一个项目,想为安全系统创建一个XML接口。安全系统是主机,我将在node.js中为node red开发客户端。

过程描述:-要建立TLS连接,主机将接收未加密的STARTTLS命令。

程序(建立连接(

  1. 启动TCP连接
  2. TCP连接已建立
  3. 客户端到主机(STARTTLS(
  4. 主机已准备好接收TLS客户端

当我启动与"net"的TCP连接并尝试将连接转发到"tls"时,源端口会发生更改,主机意味着tls部分是一个新客户端。例如:

  • 连接"net"=源TCP端口:2483
  • Connection"tls"=源TCP端口:2484

提前感谢

var net = require('net');
var tls = require('tls');
var fs = require('fs');
var port = 5555;
var host = '192.168.2.126';
var conn = net.createConnection(port ,host);

var options = {
pfx: fs.readFileSync('client.pfx')
};

conn.on('connect', function() {
console.log('connected to server');
conn.write('STARTTLS');
connTLS()
});

function connTLS (){
conn = tls.connect(port, host, options, function() {
// Check if the authorization worked
if (conn.authorized) {
console.log("Connection authorized by a Certificate Authority.");
} else {
console.log("Connection not authorized: " + conn.authorizationError)
}
// Send a friendly message
conn.write('OK it works');
});
}


conn.on('data' , function (data){
var result = "";
for(var i = 0; i < data.length; ++i){
result+= (String.fromCharCode(data[i]));
}
console.log("Data received from the server: " , result);
});
conn.on('error', function(err) {
console.log('Error in connection:', err);
});
conn.on('close', function() {
console.log('connection got closed, will try to reconnect');
conn.end();
});
conn.on('end' , function(){
console.log('Requested an end to the TCP connection');
});

非常感谢您的帮助"将TCP升级到TLS"就是我搜索到的内容。

https://nodejs.org/api/net.html#net_class_net_socket

https://nodejs.org/api/tls.html#tls_tls_connect_options_callback>>插座

var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('Connected');
client.write(startTLS,function (){
console.log("request sent")
})
var options = {
socket: client,
pfx: fs.readFileSync('./Export/client.pfx'),
secureProtocol: "TLSv1_2_method",
rejectUnauthorized: false
}; 
var tlsSocket = new tls.connect(options)

最新更新