Fixed setclip for Windows Background service
Bryan Roe committed
Feb 27, 2021 at 16:19 UTC
ada38185ac8676d07c7d9c066d6c730177cbd79b
1 file changed
+91
-7
agents/meshcore.js
+91
-7
@@ -972,9 +972,53 @@ function handleServerCommand(data) {
972
case 'setclip': {
973
// Set the load clipboard to a user value
974
sendConsoleText('setClip: ' + JSON.stringify(data));
975
- if (typeof data.data == 'string') {
975
+ if (typeof data.data == 'string')
976
+ {
977
MeshServerLogEx(22, [data.data.length], "Setting clipboard content, " + data.data.length + " byte(s)", data);
977
- if (require('MeshAgent').isService) { require('clipboard').dispatchWrite(data.data); } else { require("clipboard")(data.data); } // Set the clipboard
978
+ if (require('MeshAgent').isService)
979
+ {
980
+ if (process.platform != 'win32')
981
+ {
982
+ require('clipboard').dispatchWrite(data.data);
983
+ }
984
+ else
985
+ {
986
+ var tmp = "require('clipboard')(\"" + data.data.split('"').join('\\"') + '");process.exit();';
987
+ tmp = Buffer.from(tmp).toString('base64');
988
+
989
+ var taskoptions = { env: { _target: process.execPath, _args: '-b64exec ' + tmp, _user: require('user-sessions').getUsername(require('user-sessions').consoleUid()) } };
990
+ for (var c1e in process.env)
991
+ {
992
+ taskoptions.env[c1e] = process.env[c1e];
993
+ }
994
+
995
+ var child = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-'], taskoptions);
996
+ child.stderr.on('data', function (c) { });
997
+ child.stdout.on('data', function (c) { });
998
+ child.stdin.write('SCHTASKS /CREATE /F /TN MeshUserTask /SC ONCE /ST 00:00 ');
999
+ child.stdin.write('/RU $env:_user ');
1000
+
1001
+ child.stdin.write('/TR "$env:_target $env:_args"\r\n');
1002
+ child.stdin.write('$ts = New-Object -ComObject Schedule.service\r\n');
1003
+ child.stdin.write('$ts.connect()\r\n');
1004
+ child.stdin.write('$tsfolder = $ts.getfolder("\\")\r\n');
1005
+ child.stdin.write('$task = $tsfolder.GetTask("MeshUserTask")\r\n');
1006
+ child.stdin.write('$taskdef = $task.Definition\r\n');
1007
+ child.stdin.write('$taskdef.Settings.StopIfGoingOnBatteries = $false\r\n');
1008
+ child.stdin.write('$taskdef.Settings.DisallowStartIfOnBatteries = $false\r\n');
1009
+ child.stdin.write('$taskdef.Actions.Item(1).Path = $env:_target\r\n');
1010
+ child.stdin.write('$taskdef.Actions.Item(1).Arguments = $env:_args\r\n');
1011
+ child.stdin.write('$tsfolder.RegisterTaskDefinition($task.Name, $taskdef, 4, $null, $null, $null)\r\n');
1012
+
1013
+ child.stdin.write('SCHTASKS /RUN /TN MeshUserTask\r\n');
1014
+ child.stdin.write('SCHTASKS /DELETE /F /TN MeshUserTask\r\nexit\r\n');
1015
+ child.waitExit();
1016
+ }
1017
+ }
1018
+ else
1019
+ {
1020
+ require("clipboard")(data.data);
1021
+ } // Set the clipboard
1022
mesh.SendCommand({ action: 'msg', type: 'setclip', sessionid: data.sessionid, success: true });
1023
}
1024
break;
@@ -3195,14 +3239,54 @@ function processConsoleCommand(cmd, args, rights, sessionid) {
3239
}
3240
break;
3241
case 'setclip': {
3198
- if (args['_'].length != 1) {
3242
+ if (args['_'].length != 1)
3243
+ {
3244
response = 'Proper usage: setclip "sample text"';
3200
- } else {
3201
- if (require('MeshAgent').isService) {
3202
- require('clipboard').dispatchWrite(args['_'][0]);
3245
+ } else
3246
+ {
3247
+ if (require('MeshAgent').isService)
3248
+ {
3249
+ if (process.platform != 'win32')
3250
+ {
3251
+ require('clipboard').dispatchWrite(args['_'][0]);
3252
+ }
3253
+ else
3254
+ {
3255
+ var tmp = "require('clipboard')(\"" + (args['_'][0]).split('"').join('\\"') + '");process.exit();';
3256
+ tmp = Buffer.from(tmp).toString('base64');
3257
+
3258
+ var taskoptions = { env: { _target: process.execPath, _args: '-b64exec ' + tmp, _user: require('user-sessions').getUsername(require('user-sessions').consoleUid()) } };
3259
+ for (var c1e in process.env)
3260
+ {
3261
+ taskoptions.env[c1e] = process.env[c1e];
3262
+ }
3263
+
3264
+ var child = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-'], taskoptions);
3265
+ child.stderr.on('data', function (c) { });
3266
+ child.stdout.on('data', function (c) { });
3267
+ child.stdin.write('SCHTASKS /CREATE /F /TN MeshUserTask /SC ONCE /ST 00:00 ');
3268
+ child.stdin.write('/RU $env:_user ');
3269
+
3270
+ child.stdin.write('/TR "$env:_target $env:_args"\r\n');
3271
+ child.stdin.write('$ts = New-Object -ComObject Schedule.service\r\n');
3272
+ child.stdin.write('$ts.connect()\r\n');
3273
+ child.stdin.write('$tsfolder = $ts.getfolder("\\")\r\n');
3274
+ child.stdin.write('$task = $tsfolder.GetTask("MeshUserTask")\r\n');
3275
+ child.stdin.write('$taskdef = $task.Definition\r\n');
3276
+ child.stdin.write('$taskdef.Settings.StopIfGoingOnBatteries = $false\r\n');
3277
+ child.stdin.write('$taskdef.Settings.DisallowStartIfOnBatteries = $false\r\n');
3278
+ child.stdin.write('$taskdef.Actions.Item(1).Path = $env:_target\r\n');
3279
+ child.stdin.write('$taskdef.Actions.Item(1).Arguments = $env:_args\r\n');
3280
+ child.stdin.write('$tsfolder.RegisterTaskDefinition($task.Name, $taskdef, 4, $null, $null, $null)\r\n');
3281
+
3282
+ child.stdin.write('SCHTASKS /RUN /TN MeshUserTask\r\n');
3283
+ child.stdin.write('SCHTASKS /DELETE /F /TN MeshUserTask\r\nexit\r\n');
3284
+ child.waitExit();
3285
+ }
3286
response = 'Setting clipboard to: "' + args['_'][0] + '"';
3287
}
3205
- else {
3288
+ else
3289
+ {
3290
require("clipboard")(args['_'][0]); response = 'Setting clipboard to: "' + args['_'][0] + '"';
3291
}
3292
}