Commit 97a84e07 authored by 神楽坂玲奈's avatar 神楽坂玲奈

sudo

parent b87a2233
import {Injectable, ApplicationRef} from "@angular/core";
import {Http} from "@angular/http";
import {App, AppStatus} from "./app";
import {InstallConfig} from "./install-config";
import {SettingsService} from "./settings.sevices";
import * as os from "os";
import * as fs from "fs";
import * as path from "path";
import * as readline from "readline";
import * as mkdirp from "mkdirp";
import * as child_process from "child_process";
import {remote} from "electron";
import "rxjs/Rx";
......@@ -15,22 +10,7 @@ import {AppLocal} from "./app-local";
const Aria2 = require('aria2');
const Sudo = require('electron-sudo').default;
Sudo.prototype.fork = function (modulePath, args, options) {
return this.spawn(remote.app.getPath('exe'), ['-e', modulePath].concat(args), options).then((child)=> {
readline.createInterface({input: child.stdout}).on('line', (line) => {
child.emit('message', JSON.parse(line));
});
child.send = (message, sendHandle, options, callback)=> {
child.stdin.write(JSON.stringify(message) + os.EOL);
if (callback) {
callback()
}
};
return child
})
};
const sudo = require('electron-sudo');
@Injectable()
export class AppsService {
......@@ -180,11 +160,23 @@ export class AppsService {
connections = new Map<App, {connection: WebSocket, address: string}>();
maotama;
network(app: App, server) {
async network(app: App, server) {
if (!this.maotama) {
this.maotama = new Sudo({name: 'MyCard'}).fork('maotama')
this.maotama = new Promise((resolve, reject)=> {
let child = sudo.fork('maotama', [], {stdio: ['inherit', 'inherit', 'inherit', 'ipc']});
child.once('message', ()=>resolve(child));
child.once('error', reject);
child.once('exit', reject);
})
}
let child;
try {
child = await this.maotama;
} catch (error) {
alert(`出错了 ${error}`);
return
}
this.maotama.then((child)=> {
let connection = this.connections.get(app);
if (connection) {
connection.connection.close();
......@@ -229,6 +221,5 @@ export class AppsService {
// 如果还没建立好就出错了,就弹窗提示这个错误
this.ref.tick();
};
})
}
}
\ No newline at end of file
......@@ -36,14 +36,22 @@ autoUpdater.on('update-downloaded', (event)=> {
});
function handleElevate() {
if (process.argv[1] == '-e') {
if (process.env['ELEVATE']) {
if (process.platform == 'darwin') {
app.dock.hide();
}
process.send = (message, sendHandle, options, callback)=> process.stdout.write(JSON.stringify(message) + require('os').EOL, callback);
process.stdin.on('end', ()=> process.emit('disconnect'));
require('readline').createInterface({input: process.stdin}).on('line', (line) => process.emit('message', JSON.parse(line)));
require("./" + process.argv[2]);
let elevate = JSON.parse(process.env['ELEVATE']);
let socket = require('net').connect(elevate['ipc'], function () {
process.send = (message, sendHandle, options, callback) => this.write(JSON.stringify(message) + require('os').EOL, callback);
this.on('end', () => process.emit('disconnect'));
require('readline').createInterface({input: this}).on('line', (line) => process.emit('message', JSON.parse(line)));
process.argv = elevate['arguments'][1];
require("./" + elevate['arguments'][0]);
});
socket.on("error", (error)=> {
console.log(error);
});
return true;
}
}
......
......@@ -20,3 +20,5 @@ process.on('message', (message)=> {
});
process.on('disconnect', process.exit);
process.send('initialized');
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment