Hide automatically run commands in the linux terminal
TotallyNotElite committed
Dec 22, 2019 at 12:37 UTC
212ac81c6051484e7b00d7bb918f87d94fcada32
3 files changed
+80
-46
agents/meshcore.js
+9
-7
@@ -1202,15 +1202,17 @@ function createMeshCore(agent) {
1202
{
1203
try
1204
{
1205
+ var options = { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null, env: { HISTCONTROL: "ignoreboth", TERM: "xterm" } };
1206
+
1207
if (fs.existsSync('/usr/bin/python') && fs.existsSync('/bin/bash')) {
1206
- this.httprequest.process = childProcess.execFile('/usr/bin/python', ['python', '-c', "import pty; pty.spawn([\"/bin/bash\"])"], { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null }); // Start as active user
1207
- if (process.platform == 'linux') { this.httprequest.process.stdin.write("export TERM='xterm'\nalias ls='ls --color=auto'\nclear\n"); }
1208
+ this.httprequest.process = childProcess.execFile('/usr/bin/python', ['python', '-c', "import pty; pty.spawn([\"/bin/bash\"])"], options); // Start as active user
1209
+ if (process.platform == 'linux') { this.httprequest.process.stdin.write(" alias ls='ls --color=auto'\n"); }
1210
} else if (fs.existsSync('/bin/bash')) {
1209
- this.httprequest.process = childProcess.execFile('/bin/bash', ['bash', '-i'], { type: childProcess.SpawnTypes.TERM, uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null }); // Start as active user
1210
- if (process.platform == 'linux') { this.httprequest.process.stdin.write("alias ls='ls --color=auto'\nclear\n"); }
1211
+ this.httprequest.process = childProcess.execFile('/bin/bash', ['bash', '-i'], options); // Start as active user
1212
+ if (process.platform == 'linux') { this.httprequest.process.stdin.write(" alias ls='ls --color=auto'\n"); }
1213
} else {
1212
- this.httprequest.process = childProcess.execFile('/bin/sh', ['sh'], { type: childProcess.SpawnTypes.TERM, uid: (this.httprequest.protocol == 8)?require('user-sessions').consoleUid():null }); // Start as active user
1213
- if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\nclear\n"); }
1214
+ this.httprequest.process = childProcess.execFile('/bin/sh', ['sh'], options); // Start as active user
1215
+ if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\n"); }
1216
}
1217
} catch (e) {
1218
MeshServerLog("Failed to start remote terminal session, " + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest);
@@ -2967,4 +2969,4 @@ try {
2969
}
2970
} catch (ex) {
2971
require('MeshAgent').SendCommand({ "action": "msg", "type": "console", "value": "uncaughtException2: " + ex });
2970
-}
\ No newline at end of file
2972
+}
agents/meshcore.min.js
+69
-37
@@ -768,13 +768,20 @@ function createMeshCore(agent) {
768
//sendConsoleText('setClip: ' + JSON.stringify(data));
769
if (typeof data.data == 'string') {
770
MeshServerLog('Setting clipboard content, ' + data.data.length + ' byte(s)', data);
771
- if (typeof data.data == 'string') {
772
- if (require('MeshAgent').isService) { require('clipboard').dispatchWrite(data.data); } else { require("clipboard")(data.data); } // Set the clipboard
773
- mesh.SendCommand({ "action": "msg", "type": "setclip", "sessionid": data.sessionid, "success": true });
774
- }
771
+ if (require('MeshAgent').isService) { require('clipboard').dispatchWrite(data.data); } else { require("clipboard")(data.data); } // Set the clipboard
772
+ mesh.SendCommand({ "action": "msg", "type": "setclip", "sessionid": data.sessionid, "success": true });
773
}
774
break;
775
}
776
+ case 'userSessions': {
777
+ // Send back current user sessions list, this is Windows only.
778
+ //sendConsoleText('userSessions: ' + JSON.stringify(data));
779
+ if (process.platform != 'win32') break;
780
+ var p = require('user-sessions').enumerateUsers();
781
+ p.sessionid = data.sessionid;
782
+ p.then(function (u) { mesh.SendCommand({ 'action': 'msg', 'type': 'userSessions', 'sessionid': u.sessionid, 'data': u }); });
783
+ break;
784
+ }
785
default:
786
// Unknown action, ignore it.
787
break;
@@ -1088,6 +1095,7 @@ function createMeshCore(agent) {
1095
// Handle tunnel data
1096
if (this.httprequest.protocol == 0) { // 1 = Terminal (admin), 2 = Desktop, 5 = Files, 6 = PowerShell (admin), 7 = Plugin Data Exchange, 8 = Terminal (user), 9 = PowerShell (user)
1097
// Take a look at the protocol
1098
+ if ((data.length > 3) && (data[0] == '{')) { onTunnelControlData(data, this); return; }
1099
this.httprequest.protocol = parseInt(data);
1100
if (typeof this.httprequest.protocol != 'number') { this.httprequest.protocol = 0; }
1101
if ((this.httprequest.protocol == 1) || (this.httprequest.protocol == 6) || (this.httprequest.protocol == 8) || (this.httprequest.protocol == 9)) {
@@ -1143,26 +1151,38 @@ function createMeshCore(agent) {
1151
else
1152
{
1153
// Logged in user
1146
- var username = require('user-sessions').getUsername(require('user-sessions').consoleUid());
1147
- if (require('win-virtual-terminal').supported)
1148
- {
1149
- // ConPTY PseudoTerminal
1150
- 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] } });
1151
- }
1152
- else
1153
- {
1154
- // Legacy Terminal
1155
- 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] } });
1156
- }
1157
- this.httprequest._dispatcher.ws = this;
1158
- this.httprequest._dispatcher.on('connection', function (c)
1154
+ var userPromise = require('user-sessions').enumerateUsers();
1155
+ userPromise.that = this;
1156
+ userPromise.then(function (u)
1157
{
1160
- console.log('client connected');
1161
- this.ws._term = c;
1162
- c.pipe(this.ws, { dataTypeSkip: 1 });
1163
- this.ws.pipe(c, { dataTypeSkip: 1, end: false });
1164
- this.ws.prependListener('end', function () { this.httprequest._term.end(function () { console.log("Terminal was closed"); }); });
1165
- });
1158
+ var that = this.that;
1159
+ if (u.Active.length > 0)
1160
+ {
1161
+ var username = u.Active[0].Username;
1162
+ if (require('win-virtual-terminal').supported)
1163
+ {
1164
+ // ConPTY PseudoTerminal
1165
+ that.httprequest._dispatcher = require('win-dispatcher').dispatch({ user: username, modules: [{ name: 'win-virtual-terminal', script: getJSModule('win-virtual-terminal') }], launch: { module: 'win-virtual-terminal', method: (that.httprequest.protocol == 9 ? 'StartPowerShell' : 'Start'), args: [80, 25] } });
1166
+ }
1167
+ else
1168
+ {
1169
+ // Legacy Terminal
1170
+ that.httprequest._dispatcher = require('win-dispatcher').dispatch({ user: username, modules: [{ name: 'win-terminal', script: getJSModule('win-terminal') }], launch: { module: 'win-terminal', method: (that.httprequest.protocol == 9 ? 'StartPowerShell' : 'Start'), args: [80, 25] } });
1171
+ }
1172
+ that.httprequest._dispatcher.ws = that;
1173
+ that.httprequest._dispatcher.on('connection', function (c)
1174
+ {
1175
+ console.log('client connected');
1176
+ this.ws._term = c;
1177
+ c.pipe(this.ws, { dataTypeSkip: 1 });
1178
+ this.ws.pipe(c, { dataTypeSkip: 1, end: false });
1179
+ this.ws.prependListener('end', function ()
1180
+ {
1181
+ if (this.httprequest._term) { this.httprequest._term.end(function () { console.log("Terminal was closed"); }); }
1182
+ });
1183
+ });
1184
+ }
1185
+ });
1186
}
1187
} catch (e)
1188
{
@@ -1182,15 +1202,17 @@ function createMeshCore(agent) {
1202
{
1203
try
1204
{
1205
+ var options = { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null, env: { HISTCONTROL: "ignoreboth", TERM: "xterm" } };
1206
+
1207
if (fs.existsSync('/usr/bin/python') && fs.existsSync('/bin/bash')) {
1186
- this.httprequest.process = childProcess.execFile('/usr/bin/python', ['python', '-c', "import pty; pty.spawn([\"/bin/bash\"])"], { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null }); // Start as active user
1187
- if (process.platform == 'linux') { this.httprequest.process.stdin.write("export TERM='xterm'\nalias ls='ls --color=auto'\nclear\n"); }
1208
+ this.httprequest.process = childProcess.execFile('/usr/bin/python', ['python', '-c', "import pty; pty.spawn([\"/bin/bash\"])"], options); // Start as active user
1209
+ if (process.platform == 'linux') { this.httprequest.process.stdin.write(" alias ls='ls --color=auto'\n"); }
1210
} else if (fs.existsSync('/bin/bash')) {
1189
- this.httprequest.process = childProcess.execFile('/bin/bash', ['bash', '-i'], { type: childProcess.SpawnTypes.TERM, uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null }); // Start as active user
1190
- if (process.platform == 'linux') { this.httprequest.process.stdin.write("alias ls='ls --color=auto'\nclear\n"); }
1211
+ this.httprequest.process = childProcess.execFile('/bin/bash', ['bash', '-i'], options); // Start as active user
1212
+ if (process.platform == 'linux') { this.httprequest.process.stdin.write(" alias ls='ls --color=auto'\n"); }
1213
} else {
1192
- this.httprequest.process = childProcess.execFile('/bin/sh', ['sh'], { type: childProcess.SpawnTypes.TERM, uid: (this.httprequest.protocol == 8)?require('user-sessions').consoleUid():null }); // Start as active user
1193
- if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\nclear\n"); }
1214
+ this.httprequest.process = childProcess.execFile('/bin/sh', ['sh'], options); // Start as active user
1215
+ if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\n"); }
1216
}
1217
} catch (e) {
1218
MeshServerLog("Failed to start remote terminal session, " + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest);
@@ -1257,8 +1279,13 @@ function createMeshCore(agent) {
1279
return;
1280
}
1281
1282
+ // Look for a TSID
1283
+ var tsid = null;
1284
+ if ((this.httprequest.xoptions != null) && (typeof this.httprequest.xoptions.tsid == 'number')) { tsid = this.httprequest.xoptions.tsid; }
1285
+ require('MeshAgent')._tsid = tsid;
1286
+
1287
// Remote desktop using native pipes
1261
- this.httprequest.desktop = { state: 0, kvm: mesh.getRemoteDesktopStream(require('MeshAgent')._tsid == null ? undefined : require('MeshAgent')._tsid), tunnel: this };
1288
+ this.httprequest.desktop = { state: 0, kvm: mesh.getRemoteDesktopStream(tsid), tunnel: this };
1289
this.httprequest.desktop.kvm.parent = this.httprequest.desktop;
1290
this.desktop = this.httprequest.desktop;
1291
@@ -1739,7 +1766,12 @@ function createMeshCore(agent) {
1766
return;
1767
}
1768
1742
- if (obj.type == 'close') {
1769
+ if (obj.type == 'options') {
1770
+ // These are additional connection options passed in the control channel.
1771
+ //sendConsoleText('options: ' + JSON.stringify(obj));
1772
+ delete obj.type;
1773
+ ws.httprequest.xoptions = obj;
1774
+ } else if (obj.type == 'close') {
1775
// We received the close on the websocket
1776
//sendConsoleText('Tunnel #' + ws.tunnel.index + ' WebSocket control close');
1777
try { ws.close(); } catch (e) { }
@@ -1871,7 +1903,7 @@ function createMeshCore(agent) {
1903
var response = null;
1904
switch (cmd) {
1905
case 'help': { // Displays available commands
1874
- var fin = '', f = '', availcommands = 'help,info,osinfo,args,print,type,dbkeys,dbget,dbset,dbcompact,eval,parseuri,httpget,nwslist,plugin,wsconnect,wssend,wsclose,notify,ls,ps,kill,amt,netinfo,location,power,wakeonlan,setdebug,smbios,rawsmbios,toast,lock,users,sendcaps,openurl,amtreset,amtccm,amtacm,amtdeactivate,amtpolicy,getscript,getclip,setclip,log,av,cpuinfo,sysinfo,apf,scanwifi,scanamt,wallpaper';
1906
+ var fin = '', f = '', availcommands = 'version,help,info,osinfo,args,print,type,dbkeys,dbget,dbset,dbcompact,eval,parseuri,httpget,nwslist,plugin,wsconnect,wssend,wsclose,notify,ls,ps,kill,amt,netinfo,location,power,wakeonlan,setdebug,smbios,rawsmbios,toast,lock,users,sendcaps,openurl,amtreset,amtccm,amtacm,amtdeactivate,amtpolicy,getscript,getclip,setclip,log,av,cpuinfo,sysinfo,apf,scanwifi,scanamt,wallpaper';
1907
if (process.platform == 'win32')
1908
{
1909
availcommands += ',safemode,wpfhwacceleration';
@@ -1885,6 +1917,9 @@ function createMeshCore(agent) {
1917
response = 'Available commands: \r\n' + fin + '.';
1918
break;
1919
}
1920
+ case 'version':
1921
+ response = 'Mesh Agent Version: ' + process.versions.meshAgent;
1922
+ break;
1923
case 'wpfhwacceleration':
1924
if (process.platform != 'win32') { throw ('wpfhwacceleration setting is only supported on Windows'); }
1925
if (args['_'].length != 1)
@@ -1979,10 +2014,7 @@ function createMeshCore(agent) {
2014
var v = [];
2015
for(var i in u)
2016
{
1982
- if(u[i].State == 'Active' || u[i].State == 'Connected')
1983
- {
1984
- v.push({ tsid: i, type: u[i].StationName, user: u[i].Username });
1985
- }
2017
+ if(u[i].State == 'Active') { v.push({ tsid: i, type: u[i].StationName, user: u[i].Username, domain: u[i].Domain }); }
2018
}
2019
sendConsoleText(JSON.stringify(v, null, 1), this.sessionid);
2020
});
@@ -2937,4 +2969,4 @@ try {
2969
}
2970
} catch (ex) {
2971
require('MeshAgent').SendCommand({ "action": "msg", "type": "console", "value": "uncaughtException2: " + ex });
2940
-}
\ No newline at end of file
2972
+}
public/scripts/amt-terminal-0.0.2.js
+2
-2
@@ -73,7 +73,7 @@ var CreateAmtRemoteTerminal = function (divid, options) {
73
}
74
75
obj.xxStateChange = function (newstate) {
76
- if ((newstate == 3) && (options != null) && (options.xterm == true)) { obj.TermSendKeys('stty rows ' + obj.height + ' cols ' + obj.width + '\nclear\n'); }
76
+ if ((newstate == 3) && (options != null) && (options.xterm == true)) { obj.TermSendKeys(' stty rows ' + obj.height + ' cols ' + obj.width + '\n clear\n'); }
77
}
78
79
obj.ProcessData = function (str) {
@@ -807,4 +807,4 @@ var CreateAmtRemoteTerminal = function (divid, options) {
807
obj.DivElement.style['height'] = '';
808
if ((options != null) && (options.width != null) && (options.height != null)) { obj.Init(options.width, options.height); } else { obj.Init(); }
809
return obj;
810
-}
\ No newline at end of file
810
+}