Add KEEPALIVE_OPTIONS_REQUEST message support to mpserver

Ganeshr93 committed Jan 6, 2022 at 01:44 UTC 64ce36405d3d3e1ce9dbfd407d338fb9f515227b
1 file changed +20 -4
mpsserver.js
+20 -4
@@ -30,6 +30,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
30 const net = require('net');
31 const tls = require('tls');
32 const MAX_IDLE = 90000; // 90 seconds max idle time, higher than the typical KEEP-ALIVE periode of 60 seconds
33 + const KEEPALIVE_INTERVAL = 30; // 30 seconds is typical keepalive interval for AMT CIRA connection
34
35 // This MPS server is also a tiny HTTPS server. HTTP responses are here.
36 obj.httpResponses = {
@@ -151,7 +152,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
152 // Check if there is already a connection of the same type
153 var sameType = false, connections = obj.ciraConnections[socket.tag.nodeid];
154 if (connections != null) { for (var i in connections) { var conn = connections[i]; if (conn.tag.connType === socket.tag.connType) { sameType = true; } } }
154 -
155 +
156 // Add this connection to the connections list
157 if (connections == null) { obj.ciraConnections[socket.tag.nodeid] = [socket]; } else { obj.ciraConnections[socket.tag.nodeid].push(socket); }
158
@@ -380,7 +381,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
381 if (args.mpsdebug) { var buf = Buffer.from(data, 'binary'); console.log("MPS <-- (" + buf.length + "):" + buf.toString('hex')); } // Print out received bytes
382
383 // Traffic accounting
383 - parent.webserver.trafficStats.CIRAIn += (this.bytesRead - this.bytesReadEx);
384 + parent.webserver.trafficStats.CIRAIn += (this.bytesRead - this.bytesReadEx);
385 parent.webserver.trafficStats.CIRAOut += (this.bytesWritten - this.bytesWrittenEx);
386 this.bytesReadEx = this.bytesRead;
387 this.bytesWrittenEx = this.bytesWritten;
@@ -563,6 +564,13 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
564 parent.debug('mpscmd', '--> KEEPALIVE_REPLY');
565 return 5;
566 }
567 + case APFProtocol.KEEPALIVE_OPTIONS_REPLY: {
568 + if (len < 9) return 0;
569 + const keepaliveInterval = common.ReadInt(data, 1);
570 + const timeout = common.ReadInt(data, 5);
571 + parent.debug('mpscmd', '--> KEEPALIVE_OPTIONS_REPLY', keepaliveInterval, timeout);
572 + return 9;
573 + }
574 case APFProtocol.PROTOCOLVERSION: {
575 if (len < 93) return 0;
576 protocolVersionCount++;
@@ -888,6 +896,9 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
896 parent.debug('mpscmd', '--> GLOBAL_REQUEST', request, addr + ':' + port);
897 if (socket.tag.boundPorts.indexOf(port) == -1) { socket.tag.boundPorts.push(port); }
898 SendTcpForwardSuccessReply(socket, port);
899 + //5900 port is the last TCP port on which connections for forwarding are to be cancelled. Ports order: 16993, 16992, 664, 623, 16995, 16994, 5900
900 + //Request keepalive interval time
901 + if (port === 5900) { SendKeepaliveOptionsRequest(socket, KEEPALIVE_INTERVAL, 0); }
902 return 14 + requestLen + addrLen;
903 }
904
@@ -1140,7 +1151,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
1151 for (var i in connections) { obj.close(connections[i]); }
1152 };
1153
1143 - obj.SendJsonControl = function(socket, data) {
1154 + obj.SendJsonControl = function (socket, data) {
1155 if (socket.tag.connType == 0) return; // This command is valid only for connections that are not really CIRA.
1156 if (typeof data == 'object') { parent.debug('mpscmd', '<-- JSON_CONTROL', data.action); data = JSON.stringify(data); } else { parent.debug('mpscmd', '<-- JSON_CONTROL'); }
1157 Write(socket, String.fromCharCode(APFProtocol.JSON_CONTROL) + common.IntToStr(data.length) + data);
@@ -1173,6 +1184,11 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
1184 Write(socket, String.fromCharCode(APFProtocol.KEEPALIVE_REPLY) + common.IntToStr(cookie));
1185 }
1186
1187 + function SendKeepaliveOptionsRequest(socket, keepaliveTime, timeout) {
1188 + parent.debug('mpscmd', '<-- KEEPALIVE_OPTIONS_REQUEST', keepaliveTime, timeout);
1189 + Write(socket, String.fromCharCode(APFProtocol.KEEPALIVE_OPTIONS_REQUEST) + common.IntToStr(keepaliveTime) + common.IntToStr(timeout));
1190 + }
1191 +
1192 function SendChannelOpenFailure(socket, senderChannel, reasonCode) {
1193 parent.debug('mpscmd', '<-- CHANNEL_OPEN_FAILURE', senderChannel, reasonCode);
1194 Write(socket, String.fromCharCode(APFProtocol.CHANNEL_OPEN_FAILURE) + common.IntToStr(senderChannel) + common.IntToStr(reasonCode) + common.IntToStr(0) + common.IntToStr(0));
@@ -1289,7 +1305,7 @@ module.exports.CreateMpsServer = function (parent, db, args, certificates) {
1305 if (typeof data == 'string') { data = Buffer.from(data, 'binary'); } // Make sure we always handle buffers when sending data.
1306 if (cirachannel.state == 1 || cirachannel.sendcredits == 0 || cirachannel.sendBuffer != null) {
1307 // Channel is connected, but we are out of credits. Add the data to the outbound buffer.
1292 - if (cirachannel.sendBuffer == null) { cirachannel.sendBuffer = data; } else { cirachannel.sendBuffer = Buffer.concat([ cirachannel.sendBuffer, data ]); }
1308 + if (cirachannel.sendBuffer == null) { cirachannel.sendBuffer = data; } else { cirachannel.sendBuffer = Buffer.concat([cirachannel.sendBuffer, data]); }
1309 return true;
1310 }
1311 // Compute how much data we can send