diff --git a/modules/task-scheduler.js b/modules/task-scheduler.js index 42b7499..89fd649 100644 --- a/modules/task-scheduler.js +++ b/modules/task-scheduler.js @@ -26,24 +26,95 @@ function task() if (process.platform == 'win32') { - this.getActionCommand = function getActionCommand(name) + this.getTaskXml = function getTaskXml(name) { - var child = require('child_process').execFile(process.env['windir'] + '\\system32\\schtasks.exe', ['schtasks', '/QUERY','/TN ' + name, '/XML']); + var child = require('child_process').execFile(process.env['windir'] + '\\system32\\schtasks.exe', ['schtasks', '/QUERY', '/TN ' + name, '/XML']); child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); }); child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); }); child.waitExit(); if (child.stderr.str.trim() != '') { throw ('Unable to fetch task: ' + name); } - - var xElement = child.stdout.str.split('')[0].split('')[1]; + return (child.stdout.str.trim()); + } + this.getActionCommand = function getActionCommand(name, xml) + { + if (!xml) + { + var child = require('child_process').execFile(process.env['windir'] + '\\system32\\schtasks.exe', ['schtasks', '/QUERY', '/TN ' + name, '/XML']); + child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); }); + child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); }); + child.waitExit(); + if (child.stderr.str.trim() != '') { throw ('Unable to fetch task: ' + name); } + xml = child.stdout.str; + } + var xElement = xml.split('')[0].split('')[1]; var command = xElement.split('')[0].split('')[1]; return (command); }; + this.editActionCommand = function editActionCommand(name, action, argString, xml) + { + if (!xml) + { + var child = require('child_process').execFile(process.env['windir'] + '\\system32\\schtasks.exe', ['schtasks', '/QUERY', '/TN ' + name, '/XML']); + child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); }); + child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); }); + child.waitExit(); + if (child.stderr.str.trim() != '') { throw ('Unable to fetch task: ' + name); } + xml = child.stdout.str; + } + + var pt1 = xml.split(''); // xml = pt1.join(''); + var pt2 = pt1[0].split(''); // pt1[0] = pt2.join(''); + var xElement = pt2[1]; // pt2[1] = xElement; + + var pt3 = xElement.split(''); // xElement = pt3.join(''); + var pt4 = pt3[0].split(''); // pt3[0] = pt4.join(''); + var command = pt4[1]; // pt4[1] = command; + + pt4[1] = action; + pt3[0] = pt4.join(''); + xElement = pt3.join(''); + + var pt5 = xElement.split(''); // xElement = pt5.join(''); + var pt6 = pt5[0].split(''); // pt5[0] = pt6.join(''); + var arg = pt6[1]; // pt6[1] = arg; + + arg = argString; + pt6[1] = arg; + pt5[0] = pt6.join(''); + xElement = pt5.join(''); + + pt2[1] = xElement; + pt1[0] = pt2.join(''); + xml = pt1.join(''); + + var s = require('fs').createWriteStream(require('os').tmpdir() + name + '.xml', { flags: 'wb' }); + var b = Buffer.alloc(2); + b[0] = 0xFF; + b[1] = 0xFE; + + s.write(b); + s.write(Buffer.from(xml).toString('utf16')); + s.end(); + + var child = require('child_process').execFile(process.env['windir'] + '\\system32\\cmd.exe', ['cmd']); + child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); }); + child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); }); + child.stdin.write('SCHTASKS /DELETE /TN ' + name + ' /F \n'); + child.stdin.write('SCHTASKS /CREATE /TN ' + name + ' /XML ' + require('os').tmpdir() + name + '.xml\n'); + child.stdin.write('erase ' + require('os').tmpdir() + name + '.xml\nexit\n'); + child.waitExit(); + + //console.log(child.stdout.str.trim()); + //console.log(child.stderr.str.trim()); + }; + this.advancedEditActionCommand = function advancedEditActionCommand(name, action, argString) { var child = require('child_process').execFile(process.env['windir'] + '\\System32\\WindowsPowerShell\\v1.0\\powershell.exe', ['powershell.exe']); child.stdout.str = ''; child.stdout.on('data', function (c) { this.str += c.toString(); }); child.stderr.str = ''; child.stderr.on('data', function (c) { this.str += c.toString(); }); - child.stdin.write('$Act1 = New-ScheduledTaskAction -Execute "' + action + '" -Argument "' + argString + '"\nexit\n'); + child.stdin.write('$Act1 = New-ScheduledTaskAction -Execute "' + action + '" -Argument "' + argString + '"\n'); + child.stdin.write('Set-ScheduledTask "' + name + '" -Action $Act1\nexit\n'); child.waitExit(); console.log(child.stdout.str.trim()); }; diff --git a/modules/win-dispatcher.js b/modules/win-dispatcher.js index d0d1292..7d7be4d 100644 --- a/modules/win-dispatcher.js +++ b/modules/win-dispatcher.js @@ -43,7 +43,6 @@ function dispatch(options) { } } - var str = Buffer.from("require('win-console').hide();require('win-dispatcher').connect('" + ipcInteger + "');").toString('base64'); ret._ipc2.once('connection', function onConnect(s) { @@ -72,7 +71,6 @@ function dispatch(options) s.write(h); s.write(d); } - d = Buffer.from(JSON.stringify({ command: 'launch', value: { module: this.parent.options.launch.module, method: this.parent.options.launch.method, args: this.parent.options.launch.args } })); h.writeUInt32LE(d.length + 4); s.write(h); @@ -82,8 +80,8 @@ function dispatch(options) }); var child = require('child_process').execFile(process.env['windir'] + '\\system32\\cmd.exe', ['cmd']); - child.stderr.on('data', function (c) { console.log(c.toString());}); - child.stdout.on('data', function (c) { console.log(c.toString()); }); + child.stderr.on('data', function (c) { }); + child.stdout.on('data', function (c) { }); if (options.user) { @@ -102,25 +100,23 @@ function dispatch(options) child.stdin.write('SCHTASKS /CREATE /F /TN MeshUserTask /SC ONCE /ST 00:00 /TR "\\"' + process.execPath + '\\" -b64exec ' + str + '"\r\n'); } } - child.stdin.write('SCHTASKS /RUN /TN MeshUserTask\r\n'); + //child.stdin.write('SCHTASKS /RUN /TN MeshUserTask\r\n'); //child.stdin.write('SCHTASKS /DELETE /F /TN MeshUserTask\r\n'); child.stdin.write('exit\r\n'); child.waitExit(); - if (require('task-scheduler').getActionCommand('MeshUserTask') != process.execPath) - { - console.log('Command Mistmatch => Fixing '); - require('task-scheduler').advancedEditActionCommand('MeshUserTask', process.execPath, '-b64exec ' + str); + var xml = require('task-scheduler').getTaskXml('MeshUserTask'); + require('task-scheduler').editActionCommand('MeshUserTask', '"' + process.execPath + '"', '-b64exec ' + str, xml); - var child = require('child_process').execFile(process.env['windir'] + '\\system32\\cmd.exe', ['cmd']); - child.stderr.on('data', function (c) { console.log(c.toString()); }); - child.stdout.on('data', function (c) { console.log(c.toString()); }); - child.stdin.write('SCHTASKS /RUN /TN MeshUserTask\r\n'); - child.stdin.write('exit\r\n'); - child.waitExit(); - } + child = require('child_process').execFile(process.env['windir'] + '\\system32\\cmd.exe', ['cmd']); + child.stderr.on('data', function (c) { }); + child.stdout.on('data', function (c) { }); + child.stdin.write('SCHTASKS /RUN /TN MeshUserTask\r\n'); + child.stdin.write('SCHTASKS /DELETE /F /TN MeshUserTask\r\n'); + child.stdin.write('exit\r\n'); + child.waitExit(); return (ret); } @@ -137,7 +133,6 @@ function connect(ipc) this.unshift(c); return; } - var cmd = JSON.parse(c.slice(4, cLen).toString()); switch (cmd.command) { @@ -159,7 +154,6 @@ function connect(ipc) this.unshift(c); return; } - var cmd = JSON.parse(c.slice(4, cLen).toString()); switch (cmd.command) {