First integration of RDP into the desktop tab.

This commit is contained in:
Ylian Saint-Hilaire 2022-05-02 21:06:32 -07:00
parent 41b6b6a54a
commit 09881d06f5
6 changed files with 260 additions and 30 deletions

View File

@ -62,10 +62,10 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain) {
var inTraffc = obj.ws._socket.bytesRead, outTraffc = obj.ws._socket.bytesWritten;
if (obj.wsClient != null) { inTraffc += obj.wsClient._socket.bytesRead; outTraffc += obj.wsClient._socket.bytesWritten; }
const sessionSeconds = Math.round((Date.now() - obj.startTime) / 1000);
const user = parent.users[obj.cookie.userid];
const user = parent.users[obj.userid];
const username = (user != null) ? user.name : null;
const event = { etype: 'relay', action: 'relaylog', domain: domain.id, nodeid: obj.nodeid, userid: obj.cookie.userid, username: username, sessionid: obj.sessionid, msgid: 125, msgArgs: [sessionSeconds, obj.sessionid], msg: "Left Web-RDP session \"" + obj.sessionid + "\" after " + sessionSeconds + " second(s).", protocol: PROTOCOL_WEBRDP, bytesin: inTraffc, bytesout: outTraffc };
parent.parent.DispatchEvent(['*', obj.nodeid, obj.cookie.userid, obj.meshid], obj, event);
const event = { etype: 'relay', action: 'relaylog', domain: domain.id, nodeid: obj.nodeid, userid: obj.userid, username: username, sessionid: obj.sessionid, msgid: 125, msgArgs: [sessionSeconds, obj.sessionid], msg: "Left Web-RDP session \"" + obj.sessionid + "\" after " + sessionSeconds + " second(s).", protocol: PROTOCOL_WEBRDP, bytesin: inTraffc, bytesout: outTraffc };
parent.parent.DispatchEvent(['*', obj.nodeid, obj.userid, obj.meshid], obj, event);
delete obj.startTime;
delete obj.sessionid;
}
@ -129,8 +129,7 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain) {
function startRdp(port) {
parent.parent.debug('relay', 'RDP: Starting RDP client on loopback port ' + port);
try {
//rdpClient = require('node-rdpjs-2').createClient({
rdpClient = require('./rdp').createClient({
const args = {
logLevel: 'NONE', // 'ERROR',
domain: obj.infos.domain,
userName: obj.infos.username,
@ -139,7 +138,8 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain) {
autoLogin: true,
screen: obj.infos.screen,
locale: obj.infos.locale
}).on('connect', function () {
};
rdpClient = require('./rdp').createClient(args).on('connect', function () {
send(['rdp-connect']);
if ((typeof obj.infos.options == 'object') && (obj.infos.options.savepass == true)) { saveRdpCredentials(); } // Save the credentials if needed
obj.sessionid = Buffer.from(parent.crypto.randomBytes(9), 'binary').toString('base64');
@ -147,10 +147,10 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain) {
// Event session start
try {
const user = parent.users[obj.cookie.userid];
const user = parent.users[obj.userid];
const username = (user != null) ? user.name : null;
const event = { etype: 'relay', action: 'relaylog', domain: domain.id, nodeid: obj.nodeid, userid: obj.cookie.userid, username: username, sessionid: obj.sessionid, msgid: 150, msgArgs: [obj.sessionid], msg: "Started Web-RDP session \"" + obj.sessionid + "\".", protocol: PROTOCOL_WEBRDP };
parent.parent.DispatchEvent(['*', obj.nodeid, obj.cookie.userid, obj.meshid], obj, event);
const event = { etype: 'relay', action: 'relaylog', domain: domain.id, nodeid: obj.nodeid, userid: obj.userid, username: username, sessionid: obj.sessionid, msgid: 150, msgArgs: [obj.sessionid], msg: "Started Web-RDP session \"" + obj.sessionid + "\".", protocol: PROTOCOL_WEBRDP };
parent.parent.DispatchEvent(['*', obj.nodeid, obj.userid, obj.meshid], obj, event);
} catch (ex) { console.log(ex); }
}).on('bitmap', function (bitmap) {
try { ws.send(bitmap.data); } catch (ex) { } // Send the bitmap data as binary
@ -201,11 +201,17 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain) {
case 'infos': {
obj.infos = msg[1];
// Decode the authentication cookie
obj.cookie = parent.parent.decodeCookie(obj.infos.ip, parent.parent.loginCookieEncryptionKey);
if ((obj.cookie == null) || (typeof obj.cookie.nodeid != 'string') || (typeof obj.cookie.userid != 'string')) { obj.close(); return; }
obj.nodeid = obj.cookie.nodeid;
obj.userid = obj.cookie.userid;
if (obj.infos.ip.startsWith('node/')) {
// Use the user session
obj.nodeid = obj.infos.ip;
obj.userid = req.session.userid;
} else {
// Decode the authentication cookie
obj.cookie = parent.parent.decodeCookie(obj.infos.ip, parent.parent.loginCookieEncryptionKey);
if ((obj.cookie == null) || (typeof obj.cookie.nodeid != 'string') || (typeof obj.cookie.userid != 'string')) { obj.close(); return; }
obj.nodeid = obj.cookie.nodeid;
obj.userid = obj.cookie.userid;
}
// Get node
parent.parent.db.Get(obj.nodeid, function (err, nodes) {
@ -222,7 +228,11 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain) {
obj.tcpaddr = node.host;
// Re-encode a cookie with a device relay
const cookieContent = { userid: obj.cookie.userid, domainid: obj.cookie.domainid, nodeid: mesh.relayid, tcpaddr: node.host, tcpport: obj.cookie.tcpport };
const cookieContent = { userid: obj.userid, domainid: domain.id, nodeid: mesh.relayid, tcpaddr: node.host, tcpport: obj.cookie.tcpport };
obj.infos.ip = parent.parent.encodeCookie(cookieContent, parent.parent.loginCookieEncryptionKey);
} else if (obj.infos.ip.startsWith('node/')) {
// Encode a cookie with a device relay
const cookieContent = { userid: obj.userid, domainid: domain.id, nodeid: obj.nodeid, tcpport: node.rdpport ? node.rdpport : 3389 };
obj.infos.ip = parent.parent.encodeCookie(cookieContent, parent.parent.loginCookieEncryptionKey);
}
@ -234,7 +244,7 @@ module.exports.CreateMstscRelay = function (parent, db, ws, req, args, domain) {
// Check if we need to load server stored credentials
if ((typeof obj.infos.options == 'object') && (obj.infos.options.useServerCreds == true)) {
// Check if RDP credentials exist
if ((domain.allowsavingdevicecredentials === false) && (typeof node.rdp == 'object') && (typeof node.rdp.d == 'string') && (typeof node.rdp.u == 'string') && (typeof node.rdp.p == 'string')) {
if ((domain.allowsavingdevicecredentials !== false) && (typeof node.rdp == 'object') && (typeof node.rdp.d == 'string') && (typeof node.rdp.u == 'string') && (typeof node.rdp.p == 'string')) {
obj.infos.domain = node.rdp.d;
obj.infos.username = node.rdp.u;
obj.infos.password = node.rdp.p;

View File

@ -138,7 +138,13 @@
});
return this;
},
},
/**
* disconnect
*/
disconnect: function () {
if (this.socket) { this.socket.close(); }
},
/**
* connect
* @param ip {string} ip target for rdp

View File

@ -560,7 +560,7 @@ var CreateAgentRemoteDesktop = function (canvasid, scrolldiv) {
obj.onResize = function () {
if (obj.ScreenWidth == 0 || obj.ScreenHeight == 0) return;
if (obj.Canvas.canvas.width == obj.ScreenWidth && obj.Canvas.canvas.height == obj.ScreenHeight) return;
if ((obj.Canvas.canvas.width == obj.ScreenWidth) && (obj.Canvas.canvas.height == obj.ScreenHeight)) return;
if (obj.FirstDraw) {
obj.Canvas.canvas.width = obj.ScreenWidth;
obj.Canvas.canvas.height = obj.ScreenHeight;

View File

@ -0,0 +1,159 @@
/**
* @description RDP Remote Desktop
* @author Ylian Saint-Hilaire
* @version v0.0.1
*/
// Construct a RDP remote desktop object
var CreateRDPDesktop = function (canvasid) {
var obj = {}
obj.m = {};
obj.State = 0;
obj.canvas = Q(canvasid);
obj.CanvasId = canvasid;
if (typeof canvasid === 'string') obj.CanvasId = Q(canvasid);
obj.Canvas = obj.CanvasId.getContext('2d');
obj.ScreenWidth = obj.width = 1280;
obj.ScreenHeight = obj.height = 1024;
function mouseButtonMap(button) {
// Swap mouse buttons if needed
if (obj.m.SwapMouse === true) return [2, 0, 1, 0, 0][button];
return [1, 0, 2, 0, 0][button];
};
obj.Start = function (nodeid, port, credentials) {
changeState(1);
obj.nodeid = nodeid;
obj.port = port;
obj.credentials = credentials;
var options = { savepass: credentials.savecred, useServerCreds: credentials.servercred };
obj.render = new Mstsc.Canvas.create(obj.canvas);
obj.socket = new WebSocket('wss://' + window.location.host + '/mstscrelay.ashx'); // TODO: Support domains
obj.socket.binaryType = 'arraybuffer';
obj.socket.onopen = function () {
changeState(2); // Setup state
obj.socket.send(JSON.stringify(['infos', {
ip: obj.nodeid,
port: obj.port,
screen: { width: obj.width, height: obj.height },
domain: credentials.domain,
username: credentials.username,
password: credentials.password,
options: options,
locale: Mstsc.locale()
}]));
};
obj.socket.onmessage = function (evt) {
if (typeof evt.data == 'string') {
// This is a JSON text string, parse it.
var msg = JSON.parse(evt.data);
switch (msg[0]) {
case 'rdp-connect': {
changeState(3);
obj.rotation = 0;
obj.Canvas.setTransform(1, 0, 0, 1, 0, 0);
obj.Canvas.canvas.width = obj.ScreenWidth;
obj.Canvas.canvas.height = obj.ScreenHeight;
obj.Canvas.fillRect(0, 0, obj.ScreenWidth, obj.ScreenHeight);
if (obj.m.onScreenSizeChange != null) { obj.m.onScreenSizeChange(obj, obj.ScreenWidth, obj.ScreenHeight, obj.CanvasId); }
break;
}
case 'rdp-bitmap': {
if (obj.bitmapData == null) break;
var bitmap = msg[1];
bitmap.data = obj.bitmapData; // Use the binary data that was sent earlier.
delete obj.bitmapData;
obj.render.update(bitmap);
break;
}
case 'rdp-close': {
obj.Stop();
break;
}
case 'rdp-error': {
var err = msg[1];
console.log('[mstsc.js] error : ' + err.code + '(' + err.message + ')');
obj.Stop();
break;
}
}
} else {
// This is binary bitmap data, store it.
obj.bitmapData = evt.data;
}
};
obj.socket.onclose = function () { changeState(0); };
changeState(1);
}
obj.Stop = function () {
if (obj.socket) { obj.socket.close(); }
}
function changeState(newstate) {
if (obj.State == newstate) return;
obj.State = newstate;
if (obj.onStateChanged != null) obj.onStateChanged(obj, obj.State);
}
function getPositionOfControl(Control) {
var Position = Array(2);
Position[0] = Position[1] = 0;
while (Control) { Position[0] += Control.offsetLeft; Position[1] += Control.offsetTop; Control = Control.offsetParent; }
return Position;
}
function getMousePosition(event) {
var ScaleFactorHeight = (obj.Canvas.canvas.height / obj.CanvasId.clientHeight);
var ScaleFactorWidth = (obj.Canvas.canvas.width / obj.CanvasId.clientWidth);
var Offsets = getPositionOfControl(obj.Canvas.canvas);
var X = ((event.pageX - Offsets[0]) * ScaleFactorWidth);
var Y = ((event.pageY - Offsets[1]) * ScaleFactorHeight);
if (event.addx) { X += event.addx; }
if (event.addy) { Y += event.addy; }
return { x: X, y: Y };
}
obj.m.mousemove = function (e) {
if (!obj.socket || (obj.State != 3)) return;
var m = getMousePosition(e);
obj.mouseNagleData = ['mouse', m.x, m.y, 0, false];
if (obj.mouseNagleTimer == null) { obj.mouseNagleTimer = setTimeout(function () { obj.socket.send(JSON.stringify(obj.mouseNagleData)); obj.mouseNagleTimer = null; }, 50); }
e.preventDefault();
return false;
}
obj.m.mouseup = function (e) {
if (!obj.socket || (obj.State != 3)) return;
if (obj.mouseNagleTimer != null) { clearTimeout(obj.mouseNagleTimer); obj.mouseNagleTimer = null; }
var m = getMousePosition(e);
obj.socket.send(JSON.stringify(['mouse', m.x, m.y, mouseButtonMap(e.button), false]));
e.preventDefault();
return false;
}
obj.m.mousedown = function (e) {
if (!obj.socket || (obj.State != 3)) return;
if (obj.mouseNagleTimer != null) { clearTimeout(obj.mouseNagleTimer); obj.mouseNagleTimer = null; }
var m = getMousePosition(e);
obj.socket.send(JSON.stringify(['mouse', m.x, m.y, mouseButtonMap(e.button), true]));
e.preventDefault();
return false;
}
obj.m.handleKeyUp = function (e) {
if (!obj.socket || (obj.State != 3)) return;
obj.socket.send(JSON.stringify(['scancode', Mstsc.scancode(e), false]));
e.preventDefault();
return false;
}
obj.m.handleKeyDown = function (e) {
if (!obj.socket || (obj.State != 3)) return;
obj.socket.send(JSON.stringify(['scancode', Mstsc.scancode(e), true]));
e.preventDefault();
return false;
}
obj.m.mousedblclick = function () { }
obj.m.handleKeyPress = function () { }
obj.m.setRotation = function () { }
return obj;
}

View File

@ -81,6 +81,7 @@ var minifyMeshCentralSourceFiles = [
"../views/mstsc.handlebars",
"../views/ssh.handlebars",
"../public/scripts/agent-desktop-0.0.2.js",
"../public/scripts/agent-rdp-0.0.1.js",
"../public/scripts/agent-redir-rtc-0.1.0.js",
"../public/scripts/agent-redir-ws-0.1.1.js",
"../public/scripts/amt-0.2.0.js",

View File

@ -29,10 +29,16 @@
<script type="text/javascript" src="scripts/agent-redir-ws-0.1.1{{{min}}}.js"></script>
<script type="text/javascript" src="scripts/agent-redir-rtc-0.1.0{{{min}}}.js"></script>
<script type="text/javascript" src="scripts/agent-desktop-0.0.2{{{min}}}.js"></script>
<script type="text/javascript" src="scripts/agent-rdp-0.0.1{{{min}}}.js"></script>
<script type="text/javascript" src="scripts/qrcode.min.js"></script>
<script type="text/javascript" src="scripts/xterm{{{min}}}.js"></script>
<script type="text/javascript" src="scripts/xterm-addon-fit{{{min}}}.js"></script>
<script type="text/javascript" src="scripts/flatpickr.js"></script>
<script type="text/javascript" src="mstsc/mstsc.js"></script>
<script type="text/javascript" src="mstsc/keyboard.js"></script>
<script type="text/javascript" src="mstsc/rle.js"></script>
<script type="text/javascript" src="mstsc/client.js"></script>
<script type="text/javascript" src="mstsc/canvas.js"></script>
<script keeplink=1 type="text/javascript" src="scripts/u2f-api{{{min}}}.js"></script>
<script keeplink=1 type="text/javascript" src="scripts/charts{{{min}}}.js"></script>
<script keeplink=1 type="text/javascript" src="scripts/filesaver.min.js"></script>
@ -82,9 +88,10 @@
<div class="cmtext" onclick="cmtermaction(100,event)">Login Shell</div>
</div>
<div id="deskConnectContextMenu" class="contextMenu noselect" style="display:none;min-width:0px">
<div class="cmtext" onclick="cmdeskaction(1,event)">Ask Consent + Bar</div>
<div class="cmtext" onclick="cmdeskaction(2,event)">Ask Consent</div>
<div class="cmtext" onclick="cmdeskaction(3,event)">Privacy Bar</div>
<div id="deskConnectContextMenu1" class="cmtext" onclick="cmdeskaction(1,event)">Ask Consent + Bar</div>
<div id="deskConnectContextMenu2" class="cmtext" onclick="cmdeskaction(2,event)">Ask Consent</div>
<div id="deskConnectContextMenu3" class="cmtext" onclick="cmdeskaction(3,event)">Privacy Bar</div>
<div id="deskConnectContextMenu4" class="cmtext" onclick="cmdeskaction(4,event)">RDP</div>
</div>
<div id="deskDisconnectContextMenu" class="contextMenu noselect" style="display:none;min-width:0px">
<div class="cmtext" onclick="cmdeskaction(10,event)">Disconnect and Lock</div>
@ -5771,6 +5778,10 @@
// Desktop connect button context menu
if ((currentNode == null) || (currentNode.agent == null)) return true;
contextelement = elem;
QV('deskConnectContextMenu1', currentNode.mtype == 2); // Ask Consent + Bar
QV('deskConnectContextMenu2', currentNode.mtype == 2); // Ask Consent
QV('deskConnectContextMenu3', currentNode.mtype == 2); // Privacy Bar
QV('deskConnectContextMenu4', (currentNode.agent.id == 3) || (currentNode.agent.id == 4)); // RDP
showContextMenuDiv(document.getElementById('deskConnectContextMenu'), event.pageX, event.pageY);
break;
}
@ -5916,6 +5927,7 @@
if (action == 1) { connectDesktop(null, 3, null, 0x0008 + 0x0040); } // Consent Prompt + Privacy bar
if (action == 2) { connectDesktop(null, 3, null, 0x0008); } // Consent Prompt
if (action == 3) { connectDesktop(null, 3, null, 0x0040); } // Privacy bar
if (action == 4) { askRdpCredentials(); } // RDP
if (action == 10) { if (desktop != null) { desktop.sendCtrlMsg('{"ctrlChannel":"102938","type":"autolock","value":true}'); connectDesktop(); } } // Disconnect and lock
if (action == 11) { if (desktop != null) { desktop.sendCtrlMsg('{"ctrlChannel":"102938","type":"autolock","value":false}'); connectDesktop(); } } // Disconnect without lock
}
@ -8273,15 +8285,16 @@
var hwonline = ((currentNode.conn & 6) != 0); // If CIRA (2) or AMT (4) connected, enable hardware terminal
QE('connectbutton1h', hwonline);
QV('deskFocusBtn', (desktop != null) && (desktop.contype == 2) && (deskState != 0) && (desktopsettings.showfocus));
QE('DeskClip', deskState == 3);
QE('DeskClip', (deskState == 3) && (desktop.contype != 4));
QV('DeskClip', (inputAllowed) && (currentNode.agent) && ((features2 & 0x1800) != 0x1800) && (currentNode.agent.id != 11) && (currentNode.agent.id != 16) && ((desktop == null) || (desktop.contype != 2)) && ((desktopsettings.autoclipboard != true) || (navigator.clipboard == null) || (navigator.clipboard.readText == null))); // Clipboard not supported on macOS
QE('DeskESC', deskState == 3);
QE('DeskESC', (deskState == 3) && (desktop.contype != 4));
QV('DeskESC', browserfullscreen && inputAllowed);
QE('DeskType', deskState == 3);
QE('DeskType', (deskState == 3) && (desktop.contype != 4));
QV('DeskType', inputAllowed);
QE('DeskWD', deskState == 3);
QE('DeskWD', (deskState == 3) && (desktop.contype != 4));
QV('DeskWD', inputAllowed);
QV('deskkeys', inputAllowed);
QE('deskkeys', (desktop != null) && (desktop.contype != 4));
QV('DeskTimer', deskState == 3);
// Enable browser clipboard read if supported
@ -8467,6 +8480,14 @@
} else if (contype == 3) {
// Ask for user sessions
meshserver.send({ action: 'msg', type: 'userSessions', nodeid: currentNode._id, tag: consent });
} else if (contype == 4) {
// Setup RDP remote desktop
desktop = CreateRDPDesktop('Desk');
desktop.onStateChanged = onDesktopStateChange;
desktop.m.onScreenSizeChange = mdeskAdjust;
if (desktopsettings.swapmouse) { desktop.m.SwapMouse = desktopsettings.swapmouse; }
desktop.Start(desktopNode._id, currentNode.rdpport ? currentNode.rdpport : 3389, tsid);
desktop.contype = 4;
}
} else {
// Disconnect and clean up the remote desktop
@ -8477,6 +8498,37 @@
}
}
function askRdpCredentials() {
if (xxdialogMode) return;
var x = '<div>';
if (currentNode.rdp == 1) {
x += addHtmlValue("Credentials", '<select id=d2mode style=width:230px onchange=askRdpCredentialsValidate()><option value=1>' + "Use server credentials" + '</option><option value=2>' + "Use new credentials" + '</option></select>');
x += '<div id=d2cred style=display:none>';
} else {
x += '<div id=d2cred>';
}
x += addHtmlValue("Domain", '<input type=text id=d2domain style=width:230px maxlength=128 />');
x += addHtmlValue("Username", '<input type=text id=d2user style=width:230px onKeyUp=askRdpCredentialsValidate() maxlength=128 />');
x += addHtmlValue("Password", '<input type=password id=d2pass style=width:230px maxlength=128 autocomplete=off />');
x += addHtmlValue("", '<label><input type="checkbox" id=d2savecred>' + "Remember credentials" + '</label>');
x + '</div></div>';
setDialogMode(2, "RDP Credentials", 3, askRdpCredentialsEx, x);
}
function askRdpCredentialsValidate() {
var ok = true;
if (currentNode.rdp == 1) { QV('d2cred', Q('d2mode').value == 2); if (Q('d2mode').value == 1) { QE('idx_dlgOkButton', true); return; } }
QE('idx_dlgOkButton', Q('d2user').value != '');
}
function askRdpCredentialsEx() {
if ((currentNode.rdp == 1) && (Q('d2mode').value == 1)) {
connectDesktop(null, 4, { servercred: true });
} else {
connectDesktop(null, 4, { domain: Q('d2domain').value, username: Q('d2user').value, password: Q('d2pass').value, savecred: Q('d2savecred').checked });
}
}
function updateMetadata(conn, elementid) {
var str = '', viewerCount = 0;
if (conn && (conn.State == 3)) {
@ -8559,15 +8611,17 @@
if ((xstate == 3) && (xdesktop.contype == 2)) { xstate++; }
var str = StatusStrs[xstate];
if ((desktop != null) && (desktop.webRtcActive == true)) { str += ", WebRTC"; }
if ((desktop != null) && (xstate == 3) && (desktop.contype == 4)) { str += ", RDP"; }
//if (desktop.m.stopInput == true) { str += ', Loopback'; }
QH('deskstatus', str);
switch (state) {
case 0:
// Stop recording
if (desktop.m.recordedData != null) { deskRecordSession(); }
// Disconnect and clean up the remote desktop
desktop.Stop();
if (desktop != null) {
// Stop recording
if (desktop.m.recordedData != null) { deskRecordSession(); }
// Disconnect and clean up the remote desktop
desktop.Stop();
}
desktopNode = desktop = null;
QV('DeskFocus', false);
QV('DeskMonitorSelectionSpan', false);