Updated clipboard dispatching for windows

This commit is contained in:
Bryan Roe 2021-05-10 23:43:48 -07:00
parent e05d099ebb
commit d59145dbb0

View File

@ -1001,48 +1001,40 @@ function handleServerCommand(data) {
} }
case 'setclip': { case 'setclip': {
// Set the load clipboard to a user value // Set the load clipboard to a user value
//sendConsoleText('setClip: ' + JSON.stringify(data)); if (typeof data.data == 'string')
if (typeof data.data == 'string') { {
MeshServerLogEx(22, [data.data.length], "Setting clipboard content, " + data.data.length + " byte(s)", data); MeshServerLogEx(22, [data.data.length], "Setting clipboard content, " + data.data.length + " byte(s)", data);
if (require('MeshAgent').isService) { if (require('MeshAgent').isService)
if (process.platform != 'win32') { {
if (process.platform != 'win32')
{
require('clipboard').dispatchWrite(data.data); require('clipboard').dispatchWrite(data.data);
} }
else { else
var tmp = "require('clipboard')(" + JSON.stringify(data.data) + ');process.exit();'; {
tmp = Buffer.from(tmp).toString('base64'); var clipargs = data.data;
var uid = require('user-sessions').consoleUid(); var uid = require('user-sessions').consoleUid();
var user = require('user-sessions').getUsername(uid);
var domain = require('user-sessions').getDomain(uid); var domain = require('user-sessions').getDomain(uid);
var user = domain + '\\' + require('user-sessions').getUsername(uid); user = (domain + '\\' + user);
var taskoptions = { env: { _target: process.execPath, _args: '-b64exec ' + tmp, _user: user } };
for (var c1e in process.env) {
taskoptions.env[c1e] = process.env[c1e];
}
var child = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-'], taskoptions); 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] } });
child.stderr.on('data', function (c) { }); this._dispatcher.parent = this;
child.stdout.on('data', function (c) { }); //require('events').setFinalizerMetadata.call(this._dispatcher, 'clip-dispatch');
child.stdin.write('SCHTASKS /CREATE /F /TN MeshUserTask /SC ONCE /ST 00:00 '); this._dispatcher.on('connection', function (c)
child.stdin.write('/RU $env:_user '); {
this._c = c;
child.stdin.write('/TR "$env:_target $env:_args"\r\n'); this._c.root = this.parent;
child.stdin.write('$ts = New-Object -ComObject Schedule.service\r\n'); this._c.on('end', function ()
child.stdin.write('$ts.connect()\r\n'); {
child.stdin.write('$tsfolder = $ts.getfolder("\\")\r\n'); this.root._dispatcher = null;
child.stdin.write('$task = $tsfolder.GetTask("MeshUserTask")\r\n'); this.root = null;
child.stdin.write('$taskdef = $task.Definition\r\n'); });
child.stdin.write('$taskdef.Settings.StopIfGoingOnBatteries = $false\r\n'); });
child.stdin.write('$taskdef.Settings.DisallowStartIfOnBatteries = $false\r\n');
child.stdin.write('$taskdef.Actions.Item(1).Path = $env:_target\r\n');
child.stdin.write('$taskdef.Actions.Item(1).Arguments = $env:_args\r\n');
child.stdin.write('$tsfolder.RegisterTaskDefinition($task.Name, $taskdef, 4, $null, $null, $null)\r\n');
child.stdin.write('SCHTASKS /RUN /TN MeshUserTask\r\n');
child.stdin.write('SCHTASKS /DELETE /F /TN MeshUserTask\r\nexit\r\n');
child.waitExit();
} }
} }
else { else
{
require("clipboard")(data.data); require("clipboard")(data.data);
} // Set the clipboard } // Set the clipboard
mesh.SendCommand({ action: 'msg', type: 'setclip', sessionid: data.sessionid, success: true }); mesh.SendCommand({ action: 'msg', type: 'setclip', sessionid: data.sessionid, success: true });
@ -3390,44 +3382,32 @@ function processConsoleCommand(cmd, args, rights, sessionid) {
if (process.platform != 'win32') { if (process.platform != 'win32') {
require('clipboard').dispatchWrite(args['_'][0]); require('clipboard').dispatchWrite(args['_'][0]);
} }
else { else
var tmp = "require('clipboard')(\"" + (args['_'][0]).split('"').join('\\"') + '");process.exit();'; {
tmp = Buffer.from(tmp).toString('base64'); var clipargs = args['_'][0];
var uid = require('user-sessions').consoleUid(); var uid = require('user-sessions').consoleUid();
var user = require('user-sessions').getUsername(uid); var user = require('user-sessions').getUsername(uid);
var domain = require('user-sessions').getDomain(uid); var domain = require('user-sessions').getDomain(uid);
user = (domain + '\\' + user);
var taskoptions = { env: { _target: process.execPath, _args: '-b64exec ' + tmp, _user: domain + '\\' + user } }; 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] } });
for (var c1e in process.env) { this._dispatcher.parent = this;
taskoptions.env[c1e] = process.env[c1e]; //require('events').setFinalizerMetadata.call(this._dispatcher, 'clip-dispatch');
} this._dispatcher.on('connection', function (c)
{
var child = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell', '-noprofile', '-nologo', '-command', '-'], taskoptions); this._c = c;
child.stderr.on('data', function (c) { }); this._c.root = this.parent;
child.stdout.on('data', function (c) { }); this._c.on('end', function ()
child.stdin.write('SCHTASKS /CREATE /F /TN MeshUserTask /SC ONCE /ST 00:00 '); {
child.stdin.write('/RU $env:_user '); this.root._dispatcher = null;
this.root = null;
child.stdin.write('/TR "$env:_target $env:_args"\r\n'); });
child.stdin.write('$ts = New-Object -ComObject Schedule.service\r\n'); });
child.stdin.write('$ts.connect()\r\n');
child.stdin.write('$tsfolder = $ts.getfolder("\\")\r\n');
child.stdin.write('$task = $tsfolder.GetTask("MeshUserTask")\r\n');
child.stdin.write('$taskdef = $task.Definition\r\n');
child.stdin.write('$taskdef.Settings.StopIfGoingOnBatteries = $false\r\n');
child.stdin.write('$taskdef.Settings.DisallowStartIfOnBatteries = $false\r\n');
child.stdin.write('$taskdef.Actions.Item(1).Path = $env:_target\r\n');
child.stdin.write('$taskdef.Actions.Item(1).Arguments = $env:_args\r\n');
child.stdin.write('$tsfolder.RegisterTaskDefinition($task.Name, $taskdef, 4, $null, $null, $null)\r\n');
child.stdin.write('SCHTASKS /RUN /TN MeshUserTask\r\n');
child.stdin.write('SCHTASKS /DELETE /F /TN MeshUserTask\r\nexit\r\n');
child.waitExit();
} }
response = 'Setting clipboard to: "' + args['_'][0] + '"'; response = 'Setting clipboard to: "' + args['_'][0] + '"';
} }
else { else
{
require("clipboard")(args['_'][0]); response = 'Setting clipboard to: "' + args['_'][0] + '"'; require("clipboard")(args['_'][0]); response = 'Setting clipboard to: "' + args['_'][0] + '"';
} }
} }