Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

win_verse.js 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. // win_verse
  2. const util = require('util')
  3. const { any, Traq } = require('bbhverse');
  4. const fs = require('fs')
  5. var path = require('path');
  6. var {nodeShellExec, createTask} = require('./nodeshell');
  7. function elevatedRunIPCWriteMessage( target, m ) {
  8. fs.writeFileSync(target, ', ' + JSON.stringify( m ), { 'flag': 'a+' })
  9. }
  10. function toHTML(l, x, r, ol, or){
  11. ol = ol || '<div>'
  12. or = or || '</div>'
  13. l = l || '<div>'
  14. r = r || '</div>'
  15. if(Object.prototype.toString.call(x) === '[object Array]') {
  16. var ahtml = []
  17. x.forEach(xi => { ahtml.push( `${l}${xi}${r}` ) })
  18. return `${ol}${ahtml.join('')}${or}`
  19. }
  20. }
  21. var __isElevated = null;
  22. var shell_verse = {
  23. // getCommonTask is agnostic of whether we are running in an elevated shell or not. It runs in either case.
  24. init( o ){ Object.assign(this, o) }
  25. , downloadsdir : '../Downloads'
  26. , getCommonTask( taskToRun ){ return ()=>{ return shell_verse.runTask(taskToRun) }}
  27. , runTask : ( taskToRun ) => {
  28. if (__isElevated) return shell_verse.elevatedRunner( taskToRun )
  29. else return shell_verse.runNonElevated( taskToRun )
  30. }
  31. , elevatedRunner( taskToRun, inBatch ){
  32. // PB : TODO -- Should be called only when we are in an elevated shell that was already requested from an unelevated shell with a batch of tasks.
  33. try {
  34. var runlogjson = `${taskToRun.selectedinstance.root}/.elxr/run-${taskToRun.runtimestamp}/run.log`
  35. var __runasresult = null;
  36. return taskToRun().then((r)=>{
  37. // PB : TODO -- Every elevation should have its own messaging file. Async writes from multiple processes are a problem here...
  38. elevatedRunIPCWriteMessage( runlogjson, { info : taskToRun.info, success: true } )
  39. if(!inBatch) fs.writeFileSync(`${this.tempRunDir()}\\run.done`, 'success') // PB : TODO -- This should be done conditionally if we are running inproc.
  40. return __runasresult = r;
  41. })
  42. .catch((e) => {
  43. elevatedRunIPCWriteMessage( runlogjson, e)
  44. if(!inBatch)fs.writeFileSync(`${this.tempRunDir()}\\run.done`, 'failure')
  45. console.error(e)
  46. })
  47. .finally(() => {
  48. // if(__runasresult && !__runasresult.skipped) fs.unlinkSync(`${this.tempRunDir()}\\run.done`)
  49. })
  50. }
  51. catch (e) {
  52. console.error('Error Invalid command : ' + e)
  53. if(!inBatch) fs.writeFileSync(`${this.tempRunDir()}\\run.done`, 'error')
  54. }
  55. finally {
  56. }
  57. }
  58. , getElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runElevated(taskToRun) }}
  59. , getElevatedTaskInBatch : function( taskToRun ){ return ()=>{ return shell_verse.runElevatedInBatch(taskToRun) }}
  60. , runElevatedInBatch : ( taskToRun ) => {
  61. if (__isElevated) return shell_verse.elevatedRunner(taskToRun, true)
  62. else return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun)
  63. }
  64. , runElevated : function( taskToRun ){
  65. fs.writeFileSync( `${this.tempRunDir()}\\run.log`, ', ' + JSON.stringify({ taskstart : true }), {'flag':'a+'} )
  66. // Let shell_verse decide whether to Elevate Out of Proc or In Proc
  67. // taskToRun by default is the launched command and args. Specially in windows out of proc.
  68. // taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) })
  69. if(taskToRun.processedArgs.skipelevated) return Promise.resolve({ skipped : true });
  70. if (__isElevated) {
  71. return shell_verse.elevatedRunner(taskToRun)
  72. }
  73. else {
  74. console.log('Requesting Elevated Privileges');
  75. // requesteElevation is acutally request elevation and run. Both In Proc and Out of Proc.
  76. // Linux doesnt require elevation for most commands...
  77. return shell_verse.requestElevation(shell_verse.elevatedRunner, taskToRun)
  78. }
  79. }
  80. , runElevatedBatch( batchToRun ){
  81. // In windows we don't need to run each task. We hand over to another shell which in elevated state rebuilds the whole batch and runs.
  82. // Irrespective of the batch we just call runElevated once.
  83. if (__isElevated) {
  84. return any(batchToRun).then((r)=>{
  85. // PB : TODO -- Every elevation should have its own messaging file. Async writes from multiple processes are a problem here...
  86. // fs.writeFileSync('run.log', ', ' + JSON.stringify( { info : taskToRun.info, success: true }), { 'flag': 'a+' })
  87. fs.writeFileSync(`${this.tempRunDir()}\\run.done`, 'success') // PB : TODO -- This should be done conditionally if we are running inproc.
  88. return __runasresult = r;
  89. })
  90. .catch((e) => {
  91. // fs.writeFileSync('run.log', ', ' + JSON.stringify(e), { 'flag': 'a+' })
  92. fs.writeFileSync(`${this.tempRunDir()}\\run.done`, 'failure')
  93. console.error(e)
  94. })
  95. // .finally(() => {
  96. // if(__runasresult && !__runasresult.skipped) fs.unlinkSync(`${this.tempRunDir()}\\run.done`)
  97. // });
  98. }
  99. else {
  100. return this.runElevated(batchToRun[0])
  101. }
  102. }
  103. , getNonElevatedTask : function( taskToRun ){ return ()=>{ return shell_verse.runNonElevated(taskToRun) } }
  104. , runNonElevated : ( taskToRun ) => {
  105. // Let shell_verse decide whether to Elevate Out of Proc or In Proc
  106. if(__isElevated) {
  107. return Promise.resolve( 'Skipping regular task in elevated shell.' ) // Regular tasks unless marked as common tasks should not run in elevated shell...
  108. }
  109. else {
  110. // taskToRun by default is the launched command and args.
  111. // taskToRun = taskToRun || (()=>{ return op[processedArgs.label || processedArgs._[0] || 'undefined'](processedArgs) })
  112. return taskToRun().then(r=>{
  113. taskToRun.statuslog.statuslog(null, taskToRun.info /*repo*/ )
  114. return r;
  115. }).catch((e) => {
  116. e.info = taskToRun.info;
  117. if(taskToRun.errHandler) throw taskToRun.errHandler(e)
  118. taskToRun.statuslog.statuslog(e); //
  119. // console.error(e)
  120. throw e;
  121. }).finally(()=>{})
  122. }
  123. }
  124. , isElevated : ()=>{
  125. return acquireElevationState().then( ()=>{
  126. shell_verse.isElevated = () => {
  127. return Promise.resolve(__isElevated)
  128. }
  129. return shell_verse.isElevated()
  130. })
  131. }
  132. // , isElevationOutOfProc : ()=>{ return true }
  133. , acquireElevationState : () => {
  134. return nodeShellExec("fsutil", ["dirty", "query", "C:"], {
  135. inherit: true
  136. // , shell: true
  137. , stdio: 'ignore'
  138. , env: process.env
  139. , title: `check privileged execution mode using "fsutil dirty query C:"`
  140. }).then((exitcode) => {
  141. console.log('Elevated')
  142. __isElevated = true;
  143. shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  144. shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  145. // fs.writeFileSync( `${this.tempRunDir()}\\run.log`, ', ' + JSON.stringify({ elevated : true}), {'flag':'a+'} )
  146. return __isElevated
  147. }).catch((e) => {
  148. __isElevated = false;
  149. shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  150. shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  151. console.log('Not Elevated');
  152. // fs.writeFileSync( `${this.tempRunDir()}\\run.log`, ', ' + JSON.stringify({ elevated : false}), {'flag':'a+'} )
  153. return __isElevated
  154. })
  155. // .finally(()=>{
  156. // shell_verse.acquireElevationState = ()=> Promise.resolve(__isElevated);
  157. // shell_verse.isElevated = () => { return Promise.resolve(__isElevated)}
  158. // // return __isElevated; // Value returned from finally is not supported by node.
  159. // })
  160. }
  161. , getTaskCheckExists : createTask('getTaskCheckExists', 'where')
  162. , getbash : ()=>{ return "C:\\Program Files\\Git\\bin\\sh.exe" }
  163. , createJuntionOrLink : (dirOrFile, target, opts) =>{
  164. return nodeShellExec('mklink', ['/J', dirOrFile, target], opts).catch((e) => {
  165. console.error(utils.inspect(e))
  166. fs.writeFileSync( `${this.tempRunDir()}\\run.log`, ', ' + JSON.stringify({ e }), {'flag':'a+'} )
  167. })
  168. }
  169. , removeJuncionOrLink : ( junctionOrLink )=>{
  170. return nodeShellExec('rmdir', [junctionOrLink], { inherit: true, shell: true, env: process.env })
  171. }
  172. , tempRunDir : function(){ return `${this.selectedinstance.root}\\.elxr\\run-${this.runtimestamp}`}
  173. , requestElevation(elevatedRunner, taskToRun) {
  174. // PB : TODO -- Multiple parallel request elevations should be queued into a batch and serialized as a single promise.
  175. var processedArgs = taskToRun.processedArgs, statuslog = taskToRun.statuslog || Traq
  176. // Wait for the runas to complete before we read it.
  177. try {
  178. fs.unlinkSync(`${this.tempRunDir()}\\run.done`) // Need a unique file for aech elevated run.
  179. }
  180. catch (e) { } //Ignore
  181. // Find node path to send to hta.
  182. return nodeShellExec('where', ['node']).then(r => {
  183. var args = taskToRun.args
  184. var options = taskToRun.args ? taskToRun.args.pop() : {
  185. inherit: true
  186. , shell: true
  187. , env: taskToRun.ENV
  188. , runas: 'self'
  189. , title: `runas`
  190. }
  191. options.env = Object.assign({}, taskToRun.ENV, { wd : options.cwd })
  192. var spawntimestamp = (new Date()).getTime()
  193. if(!args) {
  194. var namedArgs = [];
  195. console.log('result : ' + JSON.stringify(r))
  196. Object.keys(processedArgs).forEach((v) => { v != '_' ? namedArgs.push('--' + v + '=' + processedArgs[v]) : null; })
  197. // PB : TODO -- Convert all the cli args back to string.
  198. args = [].concat(processedArgs._)
  199. namedArgs.length > 0 ? args = args.concat(namedArgs.join(' ')) : null;
  200. }
  201. else {
  202. // args = args.splice(-1) -- Already popped
  203. args.push(`--root=${selectedinstance.root}`);
  204. }
  205. args.splice(0,0, path.normalize(`${taskToRun.selectedinstance.root}/.elxr/run-${taskToRun.runtimestamp}/windowselevate.hta`))
  206. args.push('--runas=self');
  207. // args.debug = true
  208. if(args.debug) args.push(`--debug=${args.debug}`);
  209. args.push(`--runtimestamp=${this.runtimestamp}`);
  210. args.push(`--spawntimestamp=${spawntimestamp}`);
  211. // args.push(`--wd=${options.cwd}`);
  212. // args.push('--nodepath=' + r.messages[r.messages.length - 1])
  213. // if (!processedArgs.node_env) args.push('--node_env=' + ENV.NODE_ENV)
  214. // if (processedArgs.debug) args.push('--debug=true') // Enable to debug elevated..
  215. // console.dir(processedArgs._)
  216. // console.dir(namedArgs.join(' '))
  217. console.dir(args)
  218. // throw 'test'
  219. return nodeShellExec('MSHTA', [`"${args.join('" "')}"`], options ).then(() => {
  220. // runas returned.
  221. try {
  222. // PB : TODO -- Log is comma prefixed. Needs to be proper JSON.
  223. var runlogjson = `${taskToRun.selectedinstance.root}/.elxr/run-${this.runtimestamp}/run.log`
  224. var runaslog = JSON.parse('[ { "MSHTA" : "done", "success" : true }, ' + fs.readFileSync(runlogjson, { flags: 'a+' }) + ']');
  225. try { fs.unlinkSync(runlogjson) } catch(e){ } // PB : TODO -- Have a unique file for each elevated run.
  226. // console.log( "runaslog : " + runaslog.length )
  227. // Assemble elevated run results into the main run log
  228. runaslog.forEach((logEntry) => {
  229. statuslog.statuslog(logEntry.success ? null : logEntry, logEntry)
  230. logEntry.error ? (console.error(['error :' + logEntry.result]), console.error((logEntry.messages || []).join(' '))) :
  231. (console.log(['success :' + (logEntry.result || logEntry.success)]), console.log((logEntry.messages || []).join(' ')))
  232. })
  233. if(statuslog) fs.writeFileSync( `${this.tempRunDir()}\\run.log`, ', ' + JSON.stringify(statuslog), {'flag':'a+'} )
  234. }
  235. catch (e) {
  236. // We must have a runas log
  237. statuslog.statuslog(e)
  238. console.error('Run log error probably was not created by runas : ' + e)
  239. fs.writeFileSync( `${this.tempRunDir()}\\run.log`, ', ' + JSON.stringify(statuslog), {'flag':'a+'} )
  240. }
  241. })
  242. .catch(err => console.error('Elevation failed : ' + JSON.stringify(err)));
  243. })
  244. }
  245. , launchui() {
  246. // PB : TODO -- Multiple parallel requests should be queued into a batch and serialized as a single promise.
  247. // PB : TODO -- cleanup elevated execution code from here. This is a non elevated hta.
  248. var processedArgs = this.processedArgs
  249. // Wait for the runas to complete before we read it.
  250. try {
  251. fs.unlinkSync(`${this.tempRunDir()}\\run.done`) // Need a unique file for aech elevated run.
  252. }
  253. catch (e) { } //Ignore
  254. console.log(this.processedArgs.awaiturn)
  255. // Find node path to send to hta.
  256. return nodeShellExec('where', ['node']).then(r => {
  257. var namedArgs = [];
  258. console.log('result : ' + JSON.stringify(r))
  259. Object.keys(processedArgs).forEach((v) => { v != '_' ? namedArgs.push('--' + v + '=' + processedArgs[v]) : null; })
  260. // PB : TODO -- Convert all the cli args back to string.
  261. var _args = []
  262. if(processedArgs._[0] === 'launchui' ) { _args = processedArgs._.slice(1); _args.splice(0,0, 'pull') } // Default command.
  263. else _args = processedArgs._;
  264. var args = [ path.normalize(`${this.selectedinstance.root}/.elxr/run-${this.runtimestamp}/windowselevate.hta`) ].concat(_args)
  265. namedArgs.length > 0 ? args = args.concat(namedArgs.join(' ')) : null;
  266. // args.push('--runas=self');
  267. var spawntimestamp = (new Date()).getTime()
  268. args.push(`--runtimestamp=${this.runtimestamp}`);
  269. args.push(`--spawntimestamp=${spawntimestamp}`);
  270. // args.push('--nodepath=' + r.messages[r.messages.length - 1])
  271. // if (!processedArgs.node_env) args.push('--node_env=' + ENV.NODE_ENV)
  272. // if (processedArgs.debug) args.push('--debug=true') // Enable to debug elevated..
  273. // console.dir(processedArgs._)
  274. // console.dir(namedArgs.join(' '))
  275. console.dir(args)
  276. // throw 'test'
  277. return nodeShellExec('MSHTA', [`"${args.join('" "')}"`]
  278. , {
  279. inherit: true
  280. , shell: true
  281. , env: this.ENV
  282. // , runas: 'self'
  283. // , title: `runas`
  284. }
  285. ).then(() => {
  286. // runas returned.
  287. try {
  288. // PB : TODO -- Log is comma prefixed. Needs to be proper JSON.
  289. var runlogjson = `${this.selectedinstance.root}/.elxr/run-${this.runtimestamp}/run.log`
  290. var runaslog = JSON.parse('[' + fs.readFileSync(runlogjson, { flags: 'a+' }) + ']');
  291. try { fs.unlinkSync(runlogjson) } catch(e){ } // PB : TODO -- Have a unique file for each elevated run.
  292. // console.log( "runaslog : " + runaslog.length )
  293. // Assemble elevated run results into the main run log
  294. runaslog.forEach((logEntry) => {
  295. this.statuslog.statuslog(logEntry.success ? null : logEntry, logEntry)
  296. logEntry.success ? (console.log(['success :' + (logEntry.result || logEntry.success)]), console.log((logEntry.messages || []).join(' '))) : (console.error(['error :' + logEntry.result]), console.error((logEntry.messages || []).join(' ')))
  297. })
  298. }
  299. catch (e) {
  300. // We must have a runas log
  301. this.statuslog.statuslog(e)
  302. console.error('Run log error probably was not created by runas : ' + e)
  303. }
  304. })
  305. .catch(err => console.error('Elevation failed : ' + err));
  306. })
  307. }
  308. , ensureDirectoryExistence(filePath) {
  309. var dirname = path.dirname(filePath);
  310. if (fs.existsSync(dirname)) {
  311. return filePath;
  312. }
  313. fs.mkdirSync(dirname);
  314. return filePath;
  315. }
  316. , generateDependencies(){
  317. // PB : TODO -- Keep only the last n runs...
  318. // Currently it retains 2*n when proc needs to be relaunched in elevated mode !!!
  319. this.ensureDirectoryExistence(`${this.selectedinstance.root}/.elxr/run-${this.runtimestamp}/download.bat`)
  320. fs.writeFileSync(this.ensureDirectoryExistence(path.normalize(`${this.selectedinstance.root}/${this.downloadsdir}/readme.txt`)), `${this.getVersion()} Your local downloads for this instance`)
  321. // PB : TODO include and build from files... using rollup..
  322. var downloadbatch =
  323. `::**************************************************************************
  324. :Download_ <url> <File>
  325. Powershell.exe ^
  326. $AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'; ^
  327. [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols; ^
  328. (New-Object System.Net.WebClient).DownloadFile('%1','%2')
  329. exit /b
  330. ::**************************************************************************`
  331. fs.writeFileSync(`${this.selectedinstance.root}/.elxr/run-${this.runtimestamp}/download.bat`, downloadbatch)
  332. var windowselevate =
  333. `
  334. <html><HTA:APPLICATION ID="windowselevate" icon="#"/>
  335. <script language="vbscript">
  336. document.title = "elxr control panel"
  337. self.ResizeTo 1024,1000
  338. Sub Window_Onload
  339. self.MoveTo (screen.availWidth - (screen.availWidth/2 + 40)),10
  340. End Sub
  341. Set objShell = CreateObject("WScript.Shell")
  342. Set objENV = objShell.Environment("Process")
  343. dim NODE_ENV
  344. NODE_ENV = objENV("NODE_ENV")
  345. </script>
  346. <script language="javascript">
  347. //WINDOWSTATE="minimize" SHOWINTASKBAR="no" SYSMENU="no" CAPTION="no"
  348. // https://devblogs.microsoft.com/scripting/how-can-i-pass-command-line-variables-to-an-hta-when-it-starts/
  349. // alert(windowselevate.commandLine)
  350. var args = windowselevate.commandLine.split('"').slice(3);
  351. // alert(args)
  352. var processedArgs = { _ : [] }
  353. var namedArgs = [];
  354. namedArgs.push('--wd=' + objENV('wd'))
  355. // alert(namedArgs)
  356. for(var item in args){
  357. if(args[item].charAt(0) === '-'){
  358. namedArgs.push(args[item])
  359. var split = args[item].split('=');
  360. processedArgs[split[0].slice(2)] = split[1] || true;
  361. }
  362. else processedArgs._.push(args[item]);
  363. }
  364. var awaitrun = true;
  365. if(!processedArgs.awaitrun) {
  366. awaitrun = false;
  367. delete processedArgs.awaitrun
  368. }
  369. // args = args.forEach(function(item){ })
  370. // alert('processedArgs._ : ' + processedArgs._);
  371. // alert(processedArgs.runas);
  372. // alert(objENV('wd'))
  373. // PB : TODO -- Convert all the cli args back to string.
  374. // __filename will sure we are launhed using the same entry point.
  375. var cargs = (processedArgs.debug ? '--inspect-brk=9226' : '') + ' ${this.selectedinstance.launchscript.replace(/\\/g, '\\\\')}' + ' ' + processedArgs._.join(' ') + ' ' + namedArgs.join(' ');
  376. // cargs = (processedArgs.debug ? '--inspect-brk=9226' : '') + ' ${__filename.replace(/\\/g, '\\\\').replace("win_verse", "index")}' + processedArgs._.join(' ') + ' ' + namedArgs.join(' ');
  377. // alert(cargs)
  378. var shell = new ActiveXObject('shell.application');
  379. // alert('launching node privilged. ' + processedArgs['nodepath'])
  380. // shell.ShellExecute('cmd.exe', '/k where node', '', '', 10);
  381. // shell.ShellExecute('cmd.exe', '/k notepad.exe', '', 'runas', 1);
  382. // shell.ShellExecute('cmd.exe ', '/k node ', '', 'runas', 1);
  383. // shell.ShellExecute('cmd.exe ', '/k node ' + cargs + '', '', 'runas', 1);
  384. // alert(cargs)
  385. function run(runas){ shell.ShellExecute('node', cargs, '', runas, 1); }
  386. if(!awaitrun) { run('runas') }
  387. var log = document.createElement('div');
  388. log.innerHTML='Please Wait';
  389. function l(msg){ log.innerHTML+= msg; };
  390. // log.style.color = 'blue';
  391. log.style.width = '95%';
  392. log.id = 'log';
  393. function endconsole(from){
  394. // alert('endconsole ' + from)
  395. if(fso.FileExists('${(this.tempRunDir()+'\\run.done').replace(/\\/g, '\\\\')}')) {
  396. 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...
  397. }
  398. // alert('closing window')
  399. window.close();
  400. };
  401. var timer = function(){
  402. // alert('here')
  403. l('.');
  404. l('endconsole ' + ' ' + '${(this.tempRunDir()+'\\run.done').replace(/\\/g, '\\\\')} <br/>')
  405. if(fso.FileExists('${(this.tempRunDir()+'\\run.done').replace(/\\/g, '\\\\')}')) {
  406. endconsole('timer')
  407. }
  408. else window.setTimeout(timer, 3000);
  409. };
  410. // alert('/k node ' + cargs + '')
  411. // shell.ShellExecute(processedArgs['nodepath'], cargs, '', 'runas', 1);
  412. var fso = new ActiveXObject('Scripting.FileSystemObject');
  413. function i(html){ var e = document.createElement('div'); e.innerHTML = html; document.body.appendChild(e); };
  414. // window.onbeforeunload = function (e) {
  415. // endconsole('onbeforeunload')
  416. // };
  417. window.onload = function() {
  418. document.body.style.backgroundColor = 'black';
  419. document.body.style.fontFamily = 'arial';
  420. document.body.style.color = 'cyan';
  421. var cmds = document.createElement('div');
  422. cmds.innerHTML = '';
  423. cmds.w = function(msg){ cmds.innerHTML+= msg; };
  424. cmds.w('<Br/>Current config : ')
  425. // cmds.w('<div style="width:50%" onclick="endconsole()">X</div>' )
  426. cmds.w('<input type="button" value="Close Console" onclick="endconsole()">')
  427. cmds.w('<Br/>NODE_ENV = ' + NODE_ENV)
  428. var namedArgs = [];
  429. for(var v in processedArgs){
  430. if(v != '_' && v!== 'runtimestamp') {
  431. namedArgs.push('--' + v + '=' + processedArgs[v])
  432. }
  433. }
  434. // cmds.w('<Br/>cmd = ' + processedArgs._.join(' ') + ' ' + namedArgs.join(' ') )
  435. cmds.w('<Br/><Br/>')
  436. cmds.w('cmd = <input style="width:80%" type="text" value="' + processedArgs._.join(' ') + ' ' + namedArgs.join(' ') + '"></input> <input type="button" value="Run" onclick="run()"></input>')
  437. cmds.w('${toHTML('<option>', Object.keys(this.cmds), '</option>', '<select>', '</select>')}');
  438. // cmds.style.color = 'blue';
  439. // processedArgs._[1] === 'use' ? l('<Br/>using = ' + processedArgs._[2]) : null;
  440. document.body.appendChild(cmds);
  441. document.body.appendChild(log);
  442. // alert(fso.GetAbsolutePathName("."))
  443. window.setTimeout(timer, 3000);
  444. };
  445. </script>
  446. </html>
  447. `
  448. fs.writeFileSync(`${this.selectedinstance.root}/.elxr/run-${this.runtimestamp}/windowselevate.hta`, windowselevate)
  449. }
  450. , getchrome(){
  451. // Chrome install location from registry.
  452. return shell_verse.regread("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe\\").then(chromepath => {
  453. // HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe
  454. console.log(chromepath)
  455. return chromepath
  456. });
  457. }
  458. , regread(s){
  459. var uniquerun = (new Date()).getTime()
  460. var out = `${this.selectedinstance.root}/.elxr/run-${this.runtimestamp}/${uniquerun}-regread.out`
  461. var outescaped = out.replace(/\\/g,"\\\\")
  462. // console.log('out ::: ' + out)
  463. fs.writeFileSync(`${this.selectedinstance.root}/.elxr/run-${this.runtimestamp}/${uniquerun}-regread.js`,
  464. `
  465. // WScript.Echo('------------UNNAMED-----------')
  466. // WScript.Echo(WScript.Arguments.Item(0))
  467. argEnumerator = new Enumerator(WScript.Arguments.Unnamed)
  468. var args = []
  469. for(; !argEnumerator.atEnd(); argEnumerator.moveNext()){
  470. args.push('' + argEnumerator.item() + '')
  471. // WScript.Echo('-arg-' + argEnumerator.item() + '-arg-') // Value
  472. }
  473. // WScript.Echo(args[0])
  474. var objShell = WScript.createobject("wscript.shell")
  475. if(!args[0]) {
  476. WScript.Echo( '' )
  477. }
  478. else {
  479. WScript.Echo(args[0])
  480. var read = objShell.RegRead(args[0])
  481. try {
  482. var fs = new ActiveXObject('Scripting.FileSystemObject');
  483. var fh = fs.CreateTextFile('${outescaped}', true);
  484. fh.WriteLine(read);
  485. fh.Close();
  486. }
  487. catch(e){ WScript.Echo(e.message) }
  488. // WScript.Echo( read )
  489. }
  490. `
  491. )
  492. return nodeShellExec('cscript', [path.normalize(`${this.selectedinstance.root}/.elxr/run-${this.runtimestamp}/${uniquerun}-regread.js`), `${s}`])
  493. .then(read => {
  494. // console.log("REGREAD RESULT : " + read)
  495. // console.dir(read)
  496. if(read.success) {
  497. return (fs.readFileSync(out) + "").trim()
  498. }
  499. else throw 'regread failed'
  500. })
  501. }
  502. , isWin(){ return true}
  503. , islin(){ return false}
  504. }
  505. module.exports = shell_verse