Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
replace elevate.exe with better runas implementation
Browse files Browse the repository at this point in the history
Fixes #239
  • Loading branch information
Dustin Blackman committed Dec 24, 2016
1 parent 49b0d9b commit bf8a4a4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"ramda": "0.19.1",
"request": "2.68.0",
"request-progress": "0.3.1",
"runas": "3.1.1",
"semantic-ui-css": "2.2.4",
"semver": "5.1.0",
"super-error": "1.1.2",
Expand Down Expand Up @@ -83,7 +84,6 @@
"gulp-eslint": "1.1.1",
"gulp-htmlhint": "0.3.0",
"gulp-jsonlint": "1.1.0",
"gulp-shell": "0.4.3",
"gulp-stylint": "4.0.0",
"gulp-stylus": "2.6.0",
"htmlhint-stylish": "1.0.3",
Expand Down
Binary file removed resources/win/elevate.exe
Binary file not shown.
4 changes: 1 addition & 3 deletions src/championify.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Promise from 'bluebird';
import { remote } from 'electron';
import glob from 'glob';
import path from 'path';
import R from 'ramda';
Expand Down Expand Up @@ -221,8 +220,7 @@ function downloadItemSets() {
.catch(err => {
if (err instanceof ChampionifyErrors.FileWriteError && process.platform === 'win32' && !optionsParser.runnedAsAdmin()) {
Log.error(err);
return elevate(['--import'])
.then(() => remote.app.quit());
return elevate(['--import']);
}

// If not a file write error, end session.
Expand Down
29 changes: 20 additions & 9 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Promise from 'bluebird';
import path from 'path';
import { remote } from 'electron';
import R from 'ramda';
import retry from 'bluebird-retry';
import { spawn } from 'child_process';
import $ from './jquery';

import ChampionifyErrors from '../errors';
Expand All @@ -14,6 +13,10 @@ import viewManager from '../view_manager';
const requester = Promise.promisify(require('request'));
const prebuilts = require('../../data/prebuilts.json');

// Windows Specific Dependencies
let runas;
if (process.platform === 'win32') runas = require('runas');

const retry_options = {
max_tries: 3,
interval: 1000,
Expand Down Expand Up @@ -62,20 +65,28 @@ export function request(options) {


/**
* Re-executes Championify with elevated privileges, throws an error if user declines. Only works on Windows.
* Re-executes Championify with elevated privileges, closing the current process if successful. Throws an error if user declines. Only works on Windows.
* @param {Array} Command line parameters
* @returns {Promise.Boolean|ChampionifyErrors.ElevateError}
*/
export function elevate(params = []) {
let elevate_path = path.join(__dirname, '../../../championify_elevate.exe');
if (process.env.NODE_ENV === 'development') elevate_path = path.join(__dirname, '../../resources/win/elevate.exe');
if (!runas) return Promise.reject(new Error('runas does not work on non windows systems'));

const proc = spawn(elevate_path, [process.execPath, '--runned-as-admin'].concat(params));
return new Promise((resolve, reject) => {
proc.on('close', code => {
if (code !== 0) reject(new ChampionifyErrors.ElevateError(`Exited with code ${code}`));
resolve();
const browser_window = remote.getCurrentWindow();
browser_window.hide();

const code = runas(process.execPath, ['--runned-as-admin'].concat(params), {
hide: false,
admin: true
});

if (code !== 0) {
browser_window.show();
if (code === -1) return reject(new ChampionifyErrors.ElevateError('User refused to elevate permissions'));
return reject(new ChampionifyErrors.ElevateError(`runas returned with exit code ${code}`));
}
remote.app.quit();
});
}

Expand Down
4 changes: 0 additions & 4 deletions tasks/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,10 @@ gulp.task('_compileWin', function(cb) {
}
};

const elevate_path = path.join(tmp_path, './resources/championify_elevate.exe');
const elevate_rc = {icon: './resources/win/icon.ico'};

fs.copyAsync(path.join('./cache', src_folder), tmp_path)
.then(() => Promise.all([
fs.copyAsync('./tmp/app.asar', path.join(tmp_path, 'resources/app.asar')),
fs.copyAsync('./LICENSE', path.join(tmp_path, 'LICENSE')),
fs.copyAsync('./resources/win/elevate.exe', elevate_path).then(() => rcedit(elevate_path, elevate_rc)),
fs.removeAsync(path.join(tmp_path, '/resources/default_app')),
fs.moveAsync(path.join(tmp_path, 'electron.exe'), exe_path),
rcedit(exe_path, rc_patch)
Expand Down
34 changes: 22 additions & 12 deletions tasks/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import path from 'path';
import R from 'ramda';
import request from 'request';
import runSequence from 'run-sequence';
import shell from 'gulp-shell';
import { spawnAsync } from './helpers';

const fs = Promise.promisifyAll(require('fs-extra'));
const requestAsync = Promise.promisify(request);
const yauzl = Promise.promisifyAll(require('yauzl'));

const pkg = require('../package.json');
Expand Down Expand Up @@ -88,7 +89,8 @@ function _zipExtract(zipfile, dest = './') {
});
}

function download(url, download_path, done) {
function download(url, download_path, overwrite = false) {
if (overwrite) fs.removeSync(download_path);
if (fs.existsSync(download_path)) return Promise.resolve();

console.log(`Downloading: ${path.basename(url)}`);
Expand All @@ -99,15 +101,19 @@ function download(url, download_path, done) {
throw err;
}

return new Promise((resolve, reject) => {
return request(url)
.pipe(file)
.on('error', reject)
.on('close', function() {
file.close();
return resolve();
return requestAsync({method: 'HEAD', url})
.then(res => {
if (res.statusCode >= 400) throw new Error(`Status ${res.statusCode}: ${url}`);
return new Promise((resolve, reject) => {
return request(url)
.pipe(file)
.on('error', reject)
.on('close', function() {
file.close();
return resolve();
});
});
});
});
}

function extract(download_path, os) {
Expand Down Expand Up @@ -141,8 +147,12 @@ gulp.task('electron:deps', function(cb) {
if (pkg.dependencies[dep].indexOf('git://') > -1) return pkg.dependencies[dep];
return `${dep}@${pkg.dependencies[dep]}`;
}, R.keys(pkg.dependencies));
return gulp.src('')
.pipe(shell([`npm install --production --prefix ./dev ${install_items.join(' ')}`]));

const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const download_url = `https://raw.githubusercontent.com/dustinblackman/electron-runas-builds/master/compiled/${pkg.devDependencies.electron}/win32/ia32/runas.node`;
spawnAsync(npm, ['i', '--production', '--prefix', './dev'].concat(install_items))
.then(() => download(download_url, path.join(__dirname, '../dev/node_modules/runas/build/Release/runas.node'), true))
.asCallback(cb);
});

gulp.task('electron:settings', function() {
Expand Down

0 comments on commit bf8a4a4

Please sign in to comment.