Improvements to MSTSC.js.
Ylian Saint-Hilaire committed
Jun 12, 2020 at 12:29 UTC
2adf15d5e8172f14561fa5285b8eac7b8400f8f8
1 file changed
+16
-10
mstsc.js
+16
-10
@@ -32,6 +32,8 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain, u
32
obj.infos = null;
33
var rdpClient = null;
34
35
+ parent.parent.debug('relay', 'RDP: Request for RDP relay (' + req.clientIp + ')');
36
+
37
// Disconnect this user
38
obj.close = function (arg) {
39
if ((arg == 1) || (arg == null)) { try { ws.close(); } catch (e) { console.log(e); } } // Soft close, close the websocket
@@ -57,8 +59,8 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain, u
59
obj.relaySocket.on('data', function (chunk) { // Make sure to handle flow control.
60
if (obj.relayActive == true) { obj.relaySocket.pause(); obj.wsClient.send(chunk, function () { obj.relaySocket.resume(); }); }
61
});
60
- obj.relaySocket.on('end', function () { obj.close(0); });
61
- obj.relaySocket.on('error', function (err) { obj.close(0); });
62
+ obj.relaySocket.on('end', function () { obj.close(); });
63
+ obj.relaySocket.on('error', function (err) { obj.close(); });
64
65
// Setup the correct URL with domain and use TLS only if needed.
66
var options = { rejectUnauthorized: false };
@@ -67,8 +69,10 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain, u
69
if (args.notls || args.tlsoffload) { protocol = 'ws'; }
70
var domainadd = '';
71
if ((domain.dns == null) && (domain.id != '')) { domainadd = domain.id + '/' }
70
- obj.wsClient = new WebSocket(protocol + '://127.0.0.1/' + domainadd + 'meshrelay.ashx?auth=' + obj.infos.ip, options);
71
- obj.wsClient.on('open', function () { });
72
+ var url = protocol + '://127.0.0.1:' + args.port + '/' + domainadd + 'meshrelay.ashx?auth=' + obj.infos.ip;
73
+ parent.parent.debug('relay', 'RDP: Connection websocket to ' + url);
74
+ obj.wsClient = new WebSocket(url, options);
75
+ obj.wsClient.on('open', function () { parent.parent.debug('relay', 'RDP: Relay websocket open'); });
76
obj.wsClient.on('message', function (data) { // Make sure to handle flow control.
77
if ((obj.relayActive == false) && (data == 'c')) {
78
obj.relayActive = true; obj.relaySocket.resume();
@@ -77,7 +81,8 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain, u
81
obj.relaySocket.write(data, function () { obj.wsClient._socket.resume(); });
82
}
83
});
80
- obj.wsClient.on('close', function () { obj.close(0); });
84
+ obj.wsClient.on('close', function () { parent.parent.debug('relay', 'RDP: Relay websocket closed'); obj.close(); });
85
+ obj.wsClient.on('error', function (err) { parent.parent.debug('relay', 'RDP: Relay websocket error: ' + err); obj.close(); });
86
obj.tcpServer.close();
87
obj.tcpServer = null;
88
}
@@ -86,6 +91,7 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain, u
91
92
// Start the RDP client
93
function startRdp(port) {
94
+ parent.parent.debug('relay', 'RDP: Starting RDP client on loopback port ' + port);
95
try {
96
rdpClient = require('node-rdpjs-2').createClient({
97
logLevel: 'ERROR',
@@ -109,7 +115,7 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain, u
115
}).connect('127.0.0.1', obj.tcpServerPort);
116
} catch (ex) {
117
console.log('startRdpException', ex);
112
- obj.close(0);
118
+ obj.close();
119
}
120
}
121
@@ -124,19 +130,19 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain, u
130
case 'wheel': { if (rdpClient) { rdpClient.sendWheelEvent(msg[1], msg[2], msg[3], msg[4]); } break; }
131
case 'scancode': { if (rdpClient) { rdpClient.sendKeyEventScancode(msg[1], msg[2]); } break; }
132
case 'unicode': { if (rdpClient) { rdpClient.sendKeyEventUnicode(msg[1], msg[2]); } break; }
127
- case 'disconnect': { obj.close(0); break; }
133
+ case 'disconnect': { obj.close(); break; }
134
}
135
} catch (ex) {
136
console.log('RdpMessageException', msg, ex);
131
- obj.close(0);
137
+ obj.close();
138
}
139
});
140
141
// If error, do nothing
136
- ws.on('error', function (err) { console.log(err); obj.close(0); });
142
+ ws.on('error', function (err) { parent.parent.debug('relay', 'RDP: Browser websocket error: ' + err); obj.close(); });
143
144
// If the web socket is closed
139
- ws.on('close', function (req) { obj.close(0); });
145
+ ws.on('close', function (req) { parent.parent.debug('relay', 'RDP: Browser websocket closed'); obj.close(); });
146
147
// Send an object with flow control
148
function send(obj) {