From fecc81badf41955356b9e3eb82f05d18a729a5e6 Mon Sep 17 00:00:00 2001 From: ziajka Date: Mon, 14 Jan 2019 12:47:09 +0100 Subject: [PATCH] Aks for permission when installing Wireshark --- installed-software.js | 71 +++++++++++++------ .../services/installed-software.service.ts | 16 ++--- 2 files changed, 53 insertions(+), 34 deletions(-) diff --git a/installed-software.js b/installed-software.js index e16443e0..783e186a 100644 --- a/installed-software.js +++ b/installed-software.js @@ -10,53 +10,78 @@ const { ipcMain } = require('electron') var pipeline = util.promisify(stream.pipeline); - - exports.getInstalledSoftware = (softwareList) => { const installed = {}; for(var software of softwareList) { var name = software.name; - var commands = software.commands; + var locations = software.locations; installed[name] = []; - for(var command of commands) { - var exists = commandExistsSync(command); + for(var location of locations) { + // var exists = commandExistsSync(command); + var exists = fs.existsSync(location); if(exists) { - installed[name].push(command); + installed[name].push(location); } } } return installed; } +async function downloadFile(resource, softwarePath) { + var response = await fetch(resource); + if (response.status != 200) { + throw new Error(`Cannot download file ${resource}, response status = ${response.status}`); + } + await pipeline( + response.body, + fs.createWriteStream(softwarePath) + ); +} + ipcMain.on('installed-software-install', async function (event, software) { const softwarePath = path.join(app.getAppPath(), software.binary); if (software.type == 'web') { - try { - var response = await fetch(software.resource); - if (response.status != 200) { - throw new Error(`Cannot download file ${software.resource}, response status = ${response.status}`); - } - await pipeline( - response.body, - fs.createWriteStream(softwarePath) - ); - } catch(error) { - event.sender.send('installed-software-installed', { - success: false, - message: error.message - }); + const exists = fs.existsSync(softwarePath); + if (exists) { + console.log(`Skipping downloading file due to '${softwarePath}' path exists`); + } + else { + console.log(`File '${softwarePath}' doesn't exist. Downloading file.`); + try { + await downloadFile(software.resource, softwarePath); + } catch(error) { + event.sender.send('installed-software-installed', { + success: false, + message: error.message + }); + } + } - } - const command = `${softwarePath}`; - const child = spawn(command, software.installation_arguments); + } + + let child; + + if (software.sudo) { + child = spawn('powershell.exe', ['Start-Process', '-FilePath', `"${softwarePath}"`]); + } + else { + child = spawn(softwarePath, software.installation_arguments); + } + child.on('exit', () => { console.log("exited"); }); + child.on('error', (err) => { + console.log(err); + }); + + child.stdin.end(); + event.sender.send('installed-software-installed', { success: true }); diff --git a/src/app/services/installed-software.service.ts b/src/app/services/installed-software.service.ts index c94d9ae8..06128b03 100644 --- a/src/app/services/installed-software.service.ts +++ b/src/app/services/installed-software.service.ts @@ -6,31 +6,25 @@ import { ElectronService } from 'ngx-electron'; }) export class InstalledSoftwareService { private software = [{ - name: 'ls', - commands: ['ls'], - installed: false - }, { - name: 'telnet', - commands: ['telnet'], - installed: false - }, { name: 'SolarPuTTY', - commands: [ + locations: [ 'SolarPuTTY.exe' ], type: 'web', resource: '.exe', binary: 'SolarPuTTY.exe', + sudo: false, installation_arguments: ['--only-ask'], installed: false }, { name: 'Wireshark', - commands: [ - 'Wireshark.exe' + locations: [ + 'C:\\Program Files\\Wireshark\\Wireshark.exe' ], type: 'web', resource: 'https://1.na.dl.wireshark.org/win64/all-versions/Wireshark-win64-2.6.3.exe', binary: 'Wireshark.exe', + sudo: true, installation_arguments: [], installed: false }];