|
|
|
|
|
|
|
|
const { any } = require('bbhverse'); |
|
|
|
|
|
|
|
|
// win_verse |
|
|
|
|
|
|
|
|
|
|
|
const { any, Traq } = require('bbhverse'); |
|
|
const fs = require('fs') |
|
|
const fs = require('fs') |
|
|
var path = require('path'); |
|
|
var path = require('path'); |
|
|
|
|
|
|
|
|
var cli = require('./cliverse') |
|
|
|
|
|
var nodeShellExec = cli.nodeShellExec; |
|
|
|
|
|
|
|
|
var {nodeShellExec, createTask} = require('./nodeshell'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function elevatedRunIPCWriteMessage( target, m ) { |
|
|
function elevatedRunIPCWriteMessage( target, m ) { |
|
|
fs.writeFileSync(target, ', ' + JSON.stringify( m ), { 'flag': 'a+' }) |
|
|
fs.writeFileSync(target, ', ' + JSON.stringify( m ), { 'flag': 'a+' }) |
|
|
|
|
|
|
|
|
var shell_verse = { |
|
|
var shell_verse = { |
|
|
// getCommonTask is agnostic of whether we are running in an elevated shell or not. It runs in either case. |
|
|
// getCommonTask is agnostic of whether we are running in an elevated shell or not. It runs in either case. |
|
|
init( o ){ Object.assign(this, o) } |
|
|
init( o ){ Object.assign(this, o) } |
|
|
|
|
|
|
|
|
, downloadsdir : '../Downloads' |
|
|
, downloadsdir : '../Downloads' |
|
|
, getCommonTask( taskToRun ){ return ()=>{ return shell_verse.runTask(taskToRun) }} |
|
|
, getCommonTask( taskToRun ){ return ()=>{ return shell_verse.runTask(taskToRun) }} |
|
|
, runTask : ( taskToRun ) => { |
|
|
, runTask : ( taskToRun ) => { |
|
|
|
|
|
|
|
|
return taskToRun().then((r)=>{ |
|
|
return taskToRun().then((r)=>{ |
|
|
// PB : TODO -- Every elevation should have its own messaging file. Async writes from multiple processes are a problem here... |
|
|
// PB : TODO -- Every elevation should have its own messaging file. Async writes from multiple processes are a problem here... |
|
|
elevatedRunIPCWriteMessage( runlogjson, { info : taskToRun.info, success: true } ) |
|
|
elevatedRunIPCWriteMessage( runlogjson, { info : taskToRun.info, success: true } ) |
|
|
if(!inBatch) fs.writeFileSync('run.done', 'success') // PB : TODO -- This should be done conditionally if we are running inproc. |
|
|
|
|
|
|
|
|
if(!inBatch) fs.writeFileSync(`${this.tempRunDir()}\\run.done`, 'success') // PB : TODO -- This should be done conditionally if we are running inproc. |
|
|
return __runasresult = r; |
|
|
return __runasresult = r; |
|
|
}) |
|
|
}) |
|
|
.catch((e) => { |
|
|
.catch((e) => { |
|
|
elevatedRunIPCWriteMessage( runlogjson, e) |
|
|
elevatedRunIPCWriteMessage( runlogjson, e) |
|
|
if(!inBatch)fs.writeFileSync('run.done', 'failure') |
|
|
|
|
|
|
|
|
if(!inBatch)fs.writeFileSync(`${this.tempRunDir()}\\run.done`, 'failure') |
|
|
console.error(e) |
|
|
console.error(e) |
|
|
}) |
|
|
}) |
|
|
.finally(() => { |
|
|
.finally(() => { |
|
|
// if(__runasresult && !__runasresult.skipped) fs.unlinkSync('run.done') |
|
|
|
|
|
|
|
|
// if(__runasresult && !__runasresult.skipped) fs.unlinkSync(`${this.tempRunDir()}\\run.done`) |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
catch (e) { |
|
|
catch (e) { |
|
|
console.error('Error Invalid command : ' + e) |
|
|
console.error('Error Invalid command : ' + e) |
|
|
if(!inBatch) fs.writeFileSync('run.done', 'error') |
|
|
|
|
|
|
|
|
if(!inBatch) fs.writeFileSync(`${this.tempRunDir()}\\run.done`, 'error') |
|
|
} |
|
|
} |
|
|
finally { |
|
|
finally { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (__isElevated) return shell_verse.elevatedRunner(taskToRun, true) |
|
|
if (__isElevated) return shell_verse.elevatedRunner(taskToRun, true) |
|
|
else return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun) |
|
|
else return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun) |
|
|
} |
|
|
} |
|
|
, runElevated : ( taskToRun ) => { |
|
|
|
|
|
|
|
|
, runElevated : function( taskToRun ){ |
|
|
|
|
|
fs.writeFileSync( `${this.tempRunDir()}\\run.log`, ', ' + JSON.stringify({ taskstart : true }), {'flag':'a+'} ) |
|
|
|
|
|
|
|
|
// Let shell_verse decide whether to Elevate Out of Proc or In Proc |
|
|
// Let shell_verse decide whether to Elevate Out of Proc or In Proc |
|
|
|
|
|
|
|
|
// taskToRun by default is the launched command and args. Specially in windows out of proc. |
|
|
// taskToRun by default is the launched command and args. Specially in windows out of proc. |
|
|
// taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) }) |
|
|
// taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) }) |
|
|
|
|
|
|
|
|
if(taskToRun.processedArgs.skipelevated) return Promise.resolve({ skipped : true }); |
|
|
if(taskToRun.processedArgs.skipelevated) return Promise.resolve({ skipped : true }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return any(batchToRun).then((r)=>{ |
|
|
return any(batchToRun).then((r)=>{ |
|
|
// PB : TODO -- Every elevation should have its own messaging file. Async writes from multiple processes are a problem here... |
|
|
// PB : TODO -- Every elevation should have its own messaging file. Async writes from multiple processes are a problem here... |
|
|
// fs.writeFileSync('run.log', ', ' + JSON.stringify( { info : taskToRun.info, success: true }), { 'flag': 'a+' }) |
|
|
// fs.writeFileSync('run.log', ', ' + JSON.stringify( { info : taskToRun.info, success: true }), { 'flag': 'a+' }) |
|
|
fs.writeFileSync('run.done', 'success') // PB : TODO -- This should be done conditionally if we are running inproc. |
|
|
|
|
|
|
|
|
fs.writeFileSync(`${this.tempRunDir()}\\run.done`, 'success') // PB : TODO -- This should be done conditionally if we are running inproc. |
|
|
return __runasresult = r; |
|
|
return __runasresult = r; |
|
|
}) |
|
|
}) |
|
|
.catch((e) => { |
|
|
.catch((e) => { |
|
|
// fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' }) |
|
|
// fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' }) |
|
|
fs.writeFileSync('run.done', 'failure') |
|
|
|
|
|
|
|
|
fs.writeFileSync(`${this.tempRunDir()}\\run.done`, 'failure') |
|
|
console.error(e) |
|
|
console.error(e) |
|
|
}) |
|
|
}) |
|
|
// .finally(() => { |
|
|
// .finally(() => { |
|
|
// if(__runasresult && !__runasresult.skipped) fs.unlinkSync('run.done') |
|
|
|
|
|
|
|
|
// if(__runasresult && !__runasresult.skipped) fs.unlinkSync(`${this.tempRunDir()}\\run.done`) |
|
|
// }); |
|
|
// }); |
|
|
} |
|
|
} |
|
|
else { |
|
|
else { |
|
|
|
|
|
|
|
|
__isElevated = true; |
|
|
__isElevated = true; |
|
|
shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated); |
|
|
shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated); |
|
|
shell_verse.isElevated = () => { return Promise.resolve(__isElevated)} |
|
|
shell_verse.isElevated = () => { return Promise.resolve(__isElevated)} |
|
|
|
|
|
// fs.writeFileSync( `${this.tempRunDir()}\\run.log`, ', ' + JSON.stringify({ elevated : true}), {'flag':'a+'} ) |
|
|
return __isElevated |
|
|
return __isElevated |
|
|
}).catch((e) => { |
|
|
}).catch((e) => { |
|
|
__isElevated = false; |
|
|
__isElevated = false; |
|
|
shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated); |
|
|
shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated); |
|
|
shell_verse.isElevated = () => { return Promise.resolve(__isElevated)} |
|
|
shell_verse.isElevated = () => { return Promise.resolve(__isElevated)} |
|
|
console.log('Not Elevated'); |
|
|
console.log('Not Elevated'); |
|
|
|
|
|
// fs.writeFileSync( `${this.tempRunDir()}\\run.log`, ', ' + JSON.stringify({ elevated : false}), {'flag':'a+'} ) |
|
|
return __isElevated |
|
|
return __isElevated |
|
|
}) |
|
|
}) |
|
|
// .finally(()=>{ |
|
|
// .finally(()=>{ |
|
|
|
|
|
|
|
|
// }) |
|
|
// }) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
, getTaskCheckExists : cli.createTask('getTaskCheckExists', 'where') |
|
|
|
|
|
|
|
|
, getTaskCheckExists : createTask('getTaskCheckExists', 'where') |
|
|
|
|
|
|
|
|
, getbash : ()=>{ return "C:\\Program Files\\Git\\bin\\sh.exe" } |
|
|
, getbash : ()=>{ return "C:\\Program Files\\Git\\bin\\sh.exe" } |
|
|
|
|
|
|
|
|
, createJuntionOrLink : (dirOrFile, target, opts) =>{ |
|
|
, createJuntionOrLink : (dirOrFile, target, opts) =>{ |
|
|
return nodeShellExec('mklink', ['/J', dirOrFile, target], opts).catch((e) => { console.error(e) }) |
|
|
|
|
|
|
|
|
return nodeShellExec('mklink', ['/J', dirOrFile, target], opts).catch((e) => { console.error(e) |
|
|
|
|
|
fs.writeFileSync( `${this.tempRunDir()}\\run.log`, ', ' + JSON.stringify({ e }), {'flag':'a+'} ) |
|
|
|
|
|
}) |
|
|
} |
|
|
} |
|
|
, removeJuncionOrLink : ( junctionOrLink )=>{ |
|
|
, removeJuncionOrLink : ( junctionOrLink )=>{ |
|
|
return nodeShellExec('rmdir', [junctionOrLink], { inherit: true, shell: true, env: process.env }) |
|
|
return nodeShellExec('rmdir', [junctionOrLink], { inherit: true, shell: true, env: process.env }) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
, tempRunDir : function(){ return `${this.selectedinstance.root}\\.elxr\\run-${this.runtimestamp}`} |
|
|
, requestElevation(elevatedRunner, taskToRun) { |
|
|
, requestElevation(elevatedRunner, taskToRun) { |
|
|
// PB : TODO -- Multiple parallel request elevations should be queued into a batch and serialized as a single promise. |
|
|
// PB : TODO -- Multiple parallel request elevations should be queued into a batch and serialized as a single promise. |
|
|
|
|
|
|
|
|
var processedArgs = taskToRun.processedArgs, statuslog = taskToRun.statuslog |
|
|
|
|
|
|
|
|
var processedArgs = taskToRun.processedArgs, statuslog = taskToRun.statuslog || Traq |
|
|
|
|
|
|
|
|
// Wait for the runas to complete before we read it. |
|
|
// Wait for the runas to complete before we read it. |
|
|
try { |
|
|
try { |
|
|
fs.unlinkSync('run.done') // Need a unique file for aech elevated run. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.unlinkSync(`${this.tempRunDir()}\\run.done`) // Need a unique file for aech elevated run. |
|
|
} |
|
|
} |
|
|
catch (e) { } //Ignore |
|
|
catch (e) { } //Ignore |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
args.push('--runas=self'); |
|
|
args.push('--runas=self'); |
|
|
// args.debug = true |
|
|
// args.debug = true |
|
|
if(args.debug) args.push(`--debug=${args.debug}`); |
|
|
if(args.debug) args.push(`--debug=${args.debug}`); |
|
|
args.push(`--runtimestamp=${spawntimestamp}`); |
|
|
|
|
|
|
|
|
args.push(`--runtimestamp=${this.runtimestamp}`); |
|
|
|
|
|
args.push(`--spawntimestamp=${spawntimestamp}`); |
|
|
// args.push(`--wd=${options.cwd}`); |
|
|
// args.push(`--wd=${options.cwd}`); |
|
|
|
|
|
|
|
|
// args.push('--nodepath=' + r.messages[r.messages.length - 1]) |
|
|
// args.push('--nodepath=' + r.messages[r.messages.length - 1]) |
|
|
|
|
|
|
|
|
// runas returned. |
|
|
// runas returned. |
|
|
try { |
|
|
try { |
|
|
// PB : TODO -- Log is comma prefixed. Needs to be proper JSON. |
|
|
// PB : TODO -- Log is comma prefixed. Needs to be proper JSON. |
|
|
var runlogjson = `${taskToRun.selectedinstance.root}/.elxr/run-${spawntimestamp}/run.log` |
|
|
|
|
|
var runaslog = JSON.parse('[' + fs.readFileSync(runlogjson, { flags: 'a+' }) + ']'); |
|
|
|
|
|
|
|
|
var runlogjson = `${taskToRun.selectedinstance.root}/.elxr/run-${this.runtimestamp}/run.log` |
|
|
|
|
|
var runaslog = JSON.parse('[ { "MSHTA" : "done", "success" : true }, ' + fs.readFileSync(runlogjson, { flags: 'a+' }) + ']'); |
|
|
try { fs.unlinkSync(runlogjson) } catch(e){ } // PB : TODO -- Have a unique file for each elevated run. |
|
|
try { fs.unlinkSync(runlogjson) } catch(e){ } // PB : TODO -- Have a unique file for each elevated run. |
|
|
// console.log( "runaslog : " + runaslog.length ) |
|
|
// console.log( "runaslog : " + runaslog.length ) |
|
|
// Assemble elevated run results into the main run log |
|
|
// Assemble elevated run results into the main run log |
|
|
runaslog.forEach((logEntry) => { |
|
|
runaslog.forEach((logEntry) => { |
|
|
statuslog.statuslog(logEntry.success ? null : logEntry, logEntry) |
|
|
statuslog.statuslog(logEntry.success ? null : logEntry, logEntry) |
|
|
logEntry.success ? (console.log(['success :' + (logEntry.result || logEntry.success)]), console.log((logEntry.messages || []).join(' '))) : (console.error(['error :' + logEntry.result]), console.error((logEntry.messages || []).join(' '))) |
|
|
|
|
|
|
|
|
logEntry.error ? (console.error(['error :' + logEntry.result]), console.error((logEntry.messages || []).join(' '))) : |
|
|
|
|
|
(console.log(['success :' + (logEntry.result || logEntry.success)]), console.log((logEntry.messages || []).join(' '))) |
|
|
}) |
|
|
}) |
|
|
|
|
|
if(statuslog) fs.writeFileSync( `${this.tempRunDir()}\\run.log`, ', ' + JSON.stringify(statuslog), {'flag':'a+'} ) |
|
|
} |
|
|
} |
|
|
catch (e) { |
|
|
catch (e) { |
|
|
// We must have a runas log |
|
|
// We must have a runas log |
|
|
statuslog.statuslog(e) |
|
|
statuslog.statuslog(e) |
|
|
console.error('Run log error probably was not created by runas : ' + e) |
|
|
console.error('Run log error probably was not created by runas : ' + e) |
|
|
|
|
|
fs.writeFileSync( `${this.tempRunDir()}\\run.log`, ', ' + JSON.stringify(statuslog), {'flag':'a+'} ) |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
.catch(err => console.error('Elevation failed : ' + JSON.stringify(err))); |
|
|
.catch(err => console.error('Elevation failed : ' + JSON.stringify(err))); |
|
|
|
|
|
|
|
|
var processedArgs = this.processedArgs |
|
|
var processedArgs = this.processedArgs |
|
|
// Wait for the runas to complete before we read it. |
|
|
// Wait for the runas to complete before we read it. |
|
|
try { |
|
|
try { |
|
|
fs.unlinkSync('run.done') // Need a unique file for aech elevated run. |
|
|
|
|
|
|
|
|
fs.unlinkSync(`${this.tempRunDir()}\\run.done`) // Need a unique file for aech elevated run. |
|
|
} |
|
|
} |
|
|
catch (e) { } //Ignore |
|
|
catch (e) { } //Ignore |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namedArgs.length > 0 ? args = args.concat(namedArgs.join(' ')) : null; |
|
|
namedArgs.length > 0 ? args = args.concat(namedArgs.join(' ')) : null; |
|
|
// args.push('--runas=self'); |
|
|
// args.push('--runas=self'); |
|
|
var spawntimestamp = (new Date()).getTime() |
|
|
var spawntimestamp = (new Date()).getTime() |
|
|
args.push(`--runtimestamp=${spawntimestamp}`); |
|
|
|
|
|
|
|
|
args.push(`--runtimestamp=${this.runtimestamp}`); |
|
|
|
|
|
args.push(`--spawntimestamp=${spawntimestamp}`); |
|
|
|
|
|
|
|
|
// args.push('--nodepath=' + r.messages[r.messages.length - 1]) |
|
|
// args.push('--nodepath=' + r.messages[r.messages.length - 1]) |
|
|
// if (!processedArgs.node_env) args.push('--node_env=' + ENV.NODE_ENV) |
|
|
// if (!processedArgs.node_env) args.push('--node_env=' + ENV.NODE_ENV) |
|
|
|
|
|
|
|
|
// runas returned. |
|
|
// runas returned. |
|
|
try { |
|
|
try { |
|
|
// PB : TODO -- Log is comma prefixed. Needs to be proper JSON. |
|
|
// PB : TODO -- Log is comma prefixed. Needs to be proper JSON. |
|
|
var runlogjson = `${this.selectedinstance.root}/.elxr/run-${spawntimestamp}/run.log` |
|
|
|
|
|
|
|
|
var runlogjson = `${this.selectedinstance.root}/.elxr/run-${this.runtimestamp}/run.log` |
|
|
var runaslog = JSON.parse('[' + fs.readFileSync(runlogjson, { flags: 'a+' }) + ']'); |
|
|
var runaslog = JSON.parse('[' + fs.readFileSync(runlogjson, { flags: 'a+' }) + ']'); |
|
|
try { fs.unlinkSync(runlogjson) } catch(e){ } // PB : TODO -- Have a unique file for each elevated run. |
|
|
try { fs.unlinkSync(runlogjson) } catch(e){ } // PB : TODO -- Have a unique file for each elevated run. |
|
|
// console.log( "runaslog : " + runaslog.length ) |
|
|
// console.log( "runaslog : " + runaslog.length ) |
|
|
|
|
|
|
|
|
// alert(objENV('wd')) |
|
|
// alert(objENV('wd')) |
|
|
// PB : TODO -- Convert all the cli args back to string. |
|
|
// PB : TODO -- Convert all the cli args back to string. |
|
|
// __filename will sure we are launhed using the same entry point. |
|
|
// __filename will sure we are launhed using the same entry point. |
|
|
var cargs = (processedArgs.debug ? '--inspect-brk=9226' : '') + ' ${__filename.replace(/\\/g, '\\\\').replace("win_verse", "index")} ' + processedArgs._.join(' ') + ' ' + namedArgs.join(' '); |
|
|
|
|
|
|
|
|
var cargs = (processedArgs.debug ? '--inspect-brk=9226' : '') + ' ${this.selectedinstance.launchscript.replace(/\\/g, '\\\\')}' + ' ' + processedArgs._.join(' ') + ' ' + namedArgs.join(' '); |
|
|
|
|
|
// cargs = (processedArgs.debug ? '--inspect-brk=9226' : '') + ' ${__filename.replace(/\\/g, '\\\\').replace("win_verse", "index")}' + processedArgs._.join(' ') + ' ' + namedArgs.join(' '); |
|
|
|
|
|
// alert(cargs) |
|
|
var shell = new ActiveXObject('shell.application'); |
|
|
var shell = new ActiveXObject('shell.application'); |
|
|
// alert('launching node privilged. ' + processedArgs['nodepath']) |
|
|
// alert('launching node privilged. ' + processedArgs['nodepath']) |
|
|
// shell.ShellExecute('cmd.exe', '/k where node', '', '', 10); |
|
|
// shell.ShellExecute('cmd.exe', '/k where node', '', '', 10); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function endconsole(from){ |
|
|
function endconsole(from){ |
|
|
// alert('endconsole ' + from) |
|
|
// alert('endconsole ' + from) |
|
|
if(fso.FileExists("run.done")) { |
|
|
|
|
|
fso.DeleteFile('run.done') // PB : TODO -- IPC through files is needed only for windows we need to do it per run... |
|
|
|
|
|
|
|
|
if(fso.FileExists('${(this.tempRunDir()+'\\run.done').replace(/\\/g, '\\\\')}')) { |
|
|
|
|
|
fso.DeleteFile('${(this.tempRunDir()+'\\run.done').replace(/\\/g, '\\\\')}') // PB : TODO -- IPC through files is needed only for windows we need to do it per run... |
|
|
} |
|
|
} |
|
|
// alert('closing window') |
|
|
// alert('closing window') |
|
|
window.close(); |
|
|
window.close(); |
|
|
|
|
|
|
|
|
var timer = function(){ |
|
|
var timer = function(){ |
|
|
// alert('here') |
|
|
// alert('here') |
|
|
l('.'); |
|
|
l('.'); |
|
|
if(fso.FileExists("run.done")) { |
|
|
|
|
|
|
|
|
l('endconsole ' + ' ' + '${(this.tempRunDir()+'\\run.done').replace(/\\/g, '\\\\')} <br/>') |
|
|
|
|
|
if(fso.FileExists('${(this.tempRunDir()+'\\run.done').replace(/\\/g, '\\\\')}')) { |
|
|
endconsole('timer') |
|
|
endconsole('timer') |
|
|
} |
|
|
} |
|
|
else window.setTimeout(timer, 1000); |
|
|
|
|
|
|
|
|
else window.setTimeout(timer, 3000); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// alert('/k node ' + cargs + '') |
|
|
// alert('/k node ' + cargs + '') |
|
|
|
|
|
|
|
|
else throw 'regread failed' |
|
|
else throw 'regread failed' |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
, iswin(){ return true} |
|
|
|
|
|
|
|
|
, isWin(){ return true} |
|
|
, islin(){ return false} |
|
|
, islin(){ return false} |
|
|
} |
|
|
} |
|
|
|
|
|
|