Updated clipboard dispatching for windows

Bryan Roe committed May 10, 2021 at 23:43 UTC d59145dbb086c7e74a5b27235cdb8793a4a0163c
1 file changed +47 -67
agents/meshcore.js
+47 -67
@@ -1001,48 +1001,40 @@ function handleServerCommand(data) {
1001 }
1002 case 'setclip': {
1003 // Set the load clipboard to a user value
1004 - //sendConsoleText('setClip: ' + JSON.stringify(data));
1005 - if (typeof data.data == 'string') {
1004 + if (typeof data.data == 'string')
1005 + {
1006 MeshServerLogEx(22, [data.data.length], "Setting clipboard content, " + data.data.length + " byte(s)", data);
1007 - if (require('MeshAgent').isService) {
1008 - if (process.platform != 'win32') {
1007 + if (require('MeshAgent').isService)
1008 + {
1009 + if (process.platform != 'win32')
1010 + {
1011 require('clipboard').dispatchWrite(data.data);
1012 }
1011 - else {
1012 - var tmp = "require('clipboard')(" + JSON.stringify(data.data) + ');process.exit();';
1013 - tmp = Buffer.from(tmp).toString('base64');
1013 + else
1014 + {
1015 + var clipargs = data.data;
1016 var uid = require('user-sessions').consoleUid();
1017 + var user = require('user-sessions').getUsername(uid);
1018 var domain = require('user-sessions').getDomain(uid);
1016 - var user = domain + '\\' + require('user-sessions').getUsername(uid);
1017 - var taskoptions = { env: { _target: process.execPath, _args: '-b64exec ' + tmp, _user: user } };
1018 - for (var c1e in process.env) {
1019 - taskoptions.env[c1e] = process.env[c1e];
1020 - }
1021 -
1022 - var child = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-'], taskoptions);
1023 - child.stderr.on('data', function (c) { });
1024 - child.stdout.on('data', function (c) { });
1025 - child.stdin.write('SCHTASKS /CREATE /F /TN MeshUserTask /SC ONCE /ST 00:00 ');
1026 - child.stdin.write('/RU $env:_user ');
1027 -
1028 - child.stdin.write('/TR "$env:_target $env:_args"\r\n');
1029 - child.stdin.write('$ts = New-Object -ComObject Schedule.service\r\n');
1030 - child.stdin.write('$ts.connect()\r\n');
1031 - child.stdin.write('$tsfolder = $ts.getfolder("\\")\r\n');
1032 - child.stdin.write('$task = $tsfolder.GetTask("MeshUserTask")\r\n');
1033 - child.stdin.write('$taskdef = $task.Definition\r\n');
1034 - child.stdin.write('$taskdef.Settings.StopIfGoingOnBatteries = $false\r\n');
1035 - child.stdin.write('$taskdef.Settings.DisallowStartIfOnBatteries = $false\r\n');
1036 - child.stdin.write('$taskdef.Actions.Item(1).Path = $env:_target\r\n');
1037 - child.stdin.write('$taskdef.Actions.Item(1).Arguments = $env:_args\r\n');
1038 - child.stdin.write('$tsfolder.RegisterTaskDefinition($task.Name, $taskdef, 4, $null, $null, $null)\r\n');
1039 -
1040 - child.stdin.write('SCHTASKS /RUN /TN MeshUserTask\r\n');
1041 - child.stdin.write('SCHTASKS /DELETE /F /TN MeshUserTask\r\nexit\r\n');
1042 - child.waitExit();
1019 + user = (domain + '\\' + user);
1020 +
1021 + this._dispatcher = require('win-dispatcher').dispatch({ user: user, modules: [{ name: 'clip-dispatch', script: "module.exports = { dispatch: function dispatch(val) { require('clipboard')(val); process.exit(); } };" }], launch: { module: 'clip-dispatch', method: 'dispatch', args: [clipargs] } });
1022 + this._dispatcher.parent = this;
1023 + //require('events').setFinalizerMetadata.call(this._dispatcher, 'clip-dispatch');
1024 + this._dispatcher.on('connection', function (c)
1025 + {
1026 + this._c = c;
1027 + this._c.root = this.parent;
1028 + this._c.on('end', function ()
1029 + {
1030 + this.root._dispatcher = null;
1031 + this.root = null;
1032 + });
1033 + });
1034 }
1035 }
1045 - else {
1036 + else
1037 + {
1038 require("clipboard")(data.data);
1039 } // Set the clipboard
1040 mesh.SendCommand({ action: 'msg', type: 'setclip', sessionid: data.sessionid, success: true });
@@ -3390,44 +3382,32 @@ function processConsoleCommand(cmd, args, rights, sessionid) {
3382 if (process.platform != 'win32') {
3383 require('clipboard').dispatchWrite(args['_'][0]);
3384 }
3393 - else {
3394 - var tmp = "require('clipboard')(\"" + (args['_'][0]).split('"').join('\\"') + '");process.exit();';
3395 - tmp = Buffer.from(tmp).toString('base64');
3396 -
3385 + else
3386 + {
3387 + var clipargs = args['_'][0];
3388 var uid = require('user-sessions').consoleUid();
3389 var user = require('user-sessions').getUsername(uid);
3390 var domain = require('user-sessions').getDomain(uid);
3400 -
3401 - var taskoptions = { env: { _target: process.execPath, _args: '-b64exec ' + tmp, _user: domain + '\\' + user } };
3402 - for (var c1e in process.env) {
3403 - taskoptions.env[c1e] = process.env[c1e];
3404 - }
3405 -
3406 - var child = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-'], taskoptions);
3407 - child.stderr.on('data', function (c) { });
3408 - child.stdout.on('data', function (c) { });
3409 - child.stdin.write('SCHTASKS /CREATE /F /TN MeshUserTask /SC ONCE /ST 00:00 ');
3410 - child.stdin.write('/RU $env:_user ');
3411 -
3412 - child.stdin.write('/TR "$env:_target $env:_args"\r\n');
3413 - child.stdin.write('$ts = New-Object -ComObject Schedule.service\r\n');
3414 - child.stdin.write('$ts.connect()\r\n');
3415 - child.stdin.write('$tsfolder = $ts.getfolder("\\")\r\n');
3416 - child.stdin.write('$task = $tsfolder.GetTask("MeshUserTask")\r\n');
3417 - child.stdin.write('$taskdef = $task.Definition\r\n');
3418 - child.stdin.write('$taskdef.Settings.StopIfGoingOnBatteries = $false\r\n');
3419 - child.stdin.write('$taskdef.Settings.DisallowStartIfOnBatteries = $false\r\n');
3420 - child.stdin.write('$taskdef.Actions.Item(1).Path = $env:_target\r\n');
3421 - child.stdin.write('$taskdef.Actions.Item(1).Arguments = $env:_args\r\n');
3422 - child.stdin.write('$tsfolder.RegisterTaskDefinition($task.Name, $taskdef, 4, $null, $null, $null)\r\n');
3423 -
3424 - child.stdin.write('SCHTASKS /RUN /TN MeshUserTask\r\n');
3425 - child.stdin.write('SCHTASKS /DELETE /F /TN MeshUserTask\r\nexit\r\n');
3426 - child.waitExit();
3391 + user = (domain + '\\' + user);
3392 +
3393 + this._dispatcher = require('win-dispatcher').dispatch({ user: user, modules: [{ name: 'clip-dispatch', script: "module.exports = { dispatch: function dispatch(val) { require('clipboard')(val); process.exit(); } };" }], launch: { module: 'clip-dispatch', method: 'dispatch', args: [clipargs] } });
3394 + this._dispatcher.parent = this;
3395 + //require('events').setFinalizerMetadata.call(this._dispatcher, 'clip-dispatch');
3396 + this._dispatcher.on('connection', function (c)
3397 + {
3398 + this._c = c;
3399 + this._c.root = this.parent;
3400 + this._c.on('end', function ()
3401 + {
3402 + this.root._dispatcher = null;
3403 + this.root = null;
3404 + });
3405 + });
3406 }
3407 response = 'Setting clipboard to: "' + args['_'][0] + '"';
3408 }
3430 - else {
3409 + else
3410 + {
3411 require("clipboard")(args['_'][0]); response = 'Setting clipboard to: "' + args['_'][0] + '"';
3412 }
3413 }