From 5ad93ad89c0ef03c21807528857d57c7fa5cf136 Mon Sep 17 00:00:00 2001 From: Bryan Roe Date: Fri, 6 Dec 2019 10:49:31 -0800 Subject: [PATCH 1/2] Added Windows support for User shells --- agents/meshcore.js | 58 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/agents/meshcore.js b/agents/meshcore.js index dc06510d..78e4a655 100644 --- a/agents/meshcore.js +++ b/agents/meshcore.js @@ -1109,28 +1109,58 @@ function createMeshCore(agent) { { try { - if (((this.httprequest.protocol == 6) || (this.httprequest.protocol == 8)) && (require('win-terminal').PowerShellCapable() == true)) { - if (require('win-virtual-terminal').supported) { - this.httprequest._term = require('win-virtual-terminal').StartPowerShell(80, 25); // TODO: Start as logged in used when protocol is 8 - } else { - this.httprequest._term = require('win-terminal').StartPowerShell(80, 25); // TODO: Start as logged in used when protocol is 8 + if (!require('win-terminal').PowerShellCapable() && (this.httprequest.protocol == 6 || this.httprequest.protocol == 9)) { throw ('PowerShell is not supported on this version of windows'); } + if ((this.httprequest.protocol == 1) || (this.httprequest.protocol == 6)) + { + // Admin Terminal + if (require('win-virtual-terminal').supported) + { + // ConPTY PseudoTerminal + this.httprequest._term = require('win-virtual-terminal')[this.httprequest.protocol == 6 ? 'StartPowerShell' : 'Start'](80, 25); } - } else { - if (require('win-virtual-terminal').supported) { - this.httprequest._term = require('win-virtual-terminal').Start(80, 25); // TODO: Start as logged in used when protocol is 8 - } else { - this.httprequest._term = require('win-terminal').Start(80, 25); // TODO: Start as logged in used when protocol is 8 + else + { + // Legacy Terminal + this.httprequest._term = require('win-terminal')[this.httprequest.protocol == 6 ? 'StartPowerShell' : 'Start'](80, 25); } } - } catch (e) { + else + { + // Logged in user + var username = require('user-sessions').getUsername(require('user-sessions').consoleUid()); + if (require('win-virtual-terminal').supported) + { + // ConPTY PseudoTerminal + this.httprequest._dispatcher = require('win-dispatcher').dispatch({ user: username, modules: [{ name: 'win-virtual-terminal', script: getJSModule('win-virtual-terminal') }], launch: { module: 'win-virtual-terminal', method: (this.httprequest.protocol == 9 ? 'StartPowerShell' : 'Start'), args: [80, 25] } }); + } + else + { + // Legacy Terminal + this.httprequest._dispatcher = require('win-dispatcher').dispatch({ user: username, modules: [{ name: 'win-terminal', script: getJSModule('win-terminal') }], launch: { module: 'win-terminal', method: (this.httprequest.protocol == 9 ? 'StartPowerShell' : 'Start'), args: [80, 25] } }); + } + this.httprequest._dispatcher.ws = this; + this.httprequest._dispatcher.on('connection', function (c) + { + console.log('client connected'); + this.ws._term = c; + c.pipe(this.ws, { dataTypeSkip: 1 }); + this.ws.pipe(c, { dataTypeSkip: 1, end: false }); + this.ws.prependListener('end', function () { this.httprequest._term.end(function () { console.log("Terminal was closed"); }); }); + }); + } + } catch (e) + { MeshServerLog('Failed to start remote terminal session, ' + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest); this.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: e.toString() })); this.end(); return; } - this.httprequest._term.pipe(this, { dataTypeSkip: 1 }); - this.pipe(this.httprequest._term, { dataTypeSkip: 1, end: false }); - this.prependListener('end', function () { this.httprequest._term.end(function () { console.log("Terminal was closed"); }); }); + if (!this.httprequest._dispatcher) + { + this.httprequest._term.pipe(this, { dataTypeSkip: 1 }); + this.pipe(this.httprequest._term, { dataTypeSkip: 1, end: false }); + this.prependListener('end', function () { this.httprequest._term.end(function () { console.log("Terminal was closed"); }); }); + } } else { From 9e9ee7c58fde43fd525cd5e4f4f618b27a04bba3 Mon Sep 17 00:00:00 2001 From: Bryan Roe Date: Fri, 6 Dec 2019 15:40:57 -0800 Subject: [PATCH 2/2] Added support for TSID selection on Windows --- agents/meshcore.js | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/agents/meshcore.js b/agents/meshcore.js index 78e4a655..378f6317 100644 --- a/agents/meshcore.js +++ b/agents/meshcore.js @@ -1242,7 +1242,7 @@ function createMeshCore(agent) { } // Remote desktop using native pipes - this.httprequest.desktop = { state: 0, kvm: mesh.getRemoteDesktopStream(), tunnel: this }; + this.httprequest.desktop = { state: 0, kvm: mesh.getRemoteDesktopStream(require('MeshAgent')._tsid == null ? undefined : require('MeshAgent')._tsid), tunnel: this }; this.httprequest.desktop.kvm.parent = this.httprequest.desktop; this.desktop = this.httprequest.desktop; @@ -1286,7 +1286,7 @@ function createMeshCore(agent) { this.httprequest.desktop.kvm.connectionBar.removeAllListeners('close'); this.httprequest.desktop.kvm.connectionBar.close(); - this.httprequest.desktop.kvm.connectionBar = require('notifybar-desktop')('Sharing desktop with: ' + this.httprequest.desktop.kvm.users.sort().join(', ')); + this.httprequest.desktop.kvm.connectionBar = require('notifybar-desktop')('Sharing desktop with: ' + this.httprequest.desktop.kvm.users.sort().join(', '), require('MeshAgent')._tsid); this.httprequest.desktop.kvm.connectionBar.httprequest = this.httprequest; this.httprequest.desktop.kvm.connectionBar.on('close', function () { @@ -1351,7 +1351,7 @@ function createMeshCore(agent) { } try { - this.ws.httprequest.desktop.kvm.connectionBar = require('notifybar-desktop')('Sharing desktop with: ' + this.ws.httprequest.desktop.kvm.users.sort().join(', ')); + this.ws.httprequest.desktop.kvm.connectionBar = require('notifybar-desktop')('Sharing desktop with: ' + this.ws.httprequest.desktop.kvm.users.sort().join(', '), require('MeshAgent')._tsid); MeshServerLog('Remote Desktop Connection Bar Activated/Updated (' + this.ws.httprequest.remoteaddr + ')', this.ws.httprequest); } catch(xx) @@ -1405,7 +1405,7 @@ function createMeshCore(agent) { } try { - this.httprequest.desktop.kvm.connectionBar = require('notifybar-desktop')('Sharing desktop with: ' + this.httprequest.desktop.kvm.users.sort().join(', ')); + this.httprequest.desktop.kvm.connectionBar = require('notifybar-desktop')('Sharing desktop with: ' + this.httprequest.desktop.kvm.users.sort().join(', '), require('MeshAgent')._tsid); MeshServerLog('Remote Desktop Connection Bar Activated/Updated (' + this.httprequest.remoteaddr + ')', this.httprequest); } catch(xx) @@ -1866,6 +1866,40 @@ function createMeshCore(agent) { response = 'Available commands: \r\n' + fin + '.'; break; } + case 'tsid': + if (process.platform == 'win32') + { + if (args['_'].length != 1) + { + response = 'TSID: ' + (require('MeshAgent')._tsid == null ? 'console' : require('MeshAgent')._tsid); + } + else + { + var i = parseInt(args['_'][0]); + require('MeshAgent')._tsid = (isNaN(i) ? null : i); + response = 'TSID set to: ' + (require('MeshAgent')._tsid == null ? 'console' : require('MeshAgent')._tsid); + } + } + break; + case 'activeusers': + if (process.platform == 'win32') + { + var p = require('user-sessions').enumerateUsers(); + p.sessionid = sessionid; + p.then(function (u) + { + var v = []; + for(var i in u) + { + if(u[i].State == 'Active' || u[i].State == 'Connected') + { + v.push({ tsid: i, type: u[i].StationName, user: u[i].Username }); + } + } + sendConsoleText(JSON.stringify(v, null, 1), this.sessionid); + }); + } + break; case 'wallpaper': if (process.platform != 'win32' && !(process.platform == 'linux' && require('linux-gnome-helpers').available)) {