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

sudo

parent b87a2233
import {Injectable, ApplicationRef} from "@angular/core"; import {Injectable, ApplicationRef} from "@angular/core";
import {Http} from "@angular/http"; import {Http} from "@angular/http";
import {App, AppStatus} from "./app"; import {App, AppStatus} from "./app";
import {InstallConfig} from "./install-config";
import {SettingsService} from "./settings.sevices"; import {SettingsService} from "./settings.sevices";
import * as os from "os";
import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import * as readline from "readline";
import * as mkdirp from "mkdirp";
import * as child_process from "child_process"; import * as child_process from "child_process";
import {remote} from "electron"; import {remote} from "electron";
import "rxjs/Rx"; import "rxjs/Rx";
...@@ -15,22 +10,7 @@ import {AppLocal} from "./app-local"; ...@@ -15,22 +10,7 @@ import {AppLocal} from "./app-local";
const Aria2 = require('aria2'); const Aria2 = require('aria2');
const Sudo = require('electron-sudo').default; const sudo = require('electron-sudo');
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
})
};
@Injectable() @Injectable()
export class AppsService { export class AppsService {
...@@ -180,11 +160,23 @@ export class AppsService { ...@@ -180,11 +160,23 @@ export class AppsService {
connections = new Map<App, {connection: WebSocket, address: string}>(); connections = new Map<App, {connection: WebSocket, address: string}>();
maotama; maotama;
network(app: App, server) { async network(app: App, server) {
if (!this.maotama) { 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); let connection = this.connections.get(app);
if (connection) { if (connection) {
connection.connection.close(); connection.connection.close();
...@@ -229,6 +221,5 @@ export class AppsService { ...@@ -229,6 +221,5 @@ export class AppsService {
// 如果还没建立好就出错了,就弹窗提示这个错误 // 如果还没建立好就出错了,就弹窗提示这个错误
this.ref.tick(); this.ref.tick();
}; };
})
} }
} }
\ No newline at end of file
...@@ -36,14 +36,22 @@ autoUpdater.on('update-downloaded', (event)=> { ...@@ -36,14 +36,22 @@ autoUpdater.on('update-downloaded', (event)=> {
}); });
function handleElevate() { function handleElevate() {
if (process.argv[1] == '-e') {
if (process.env['ELEVATE']) {
if (process.platform == 'darwin') { if (process.platform == 'darwin') {
app.dock.hide(); app.dock.hide();
} }
process.send = (message, sendHandle, options, callback)=> process.stdout.write(JSON.stringify(message) + require('os').EOL, callback); let elevate = JSON.parse(process.env['ELEVATE']);
process.stdin.on('end', ()=> process.emit('disconnect')); let socket = require('net').connect(elevate['ipc'], function () {
require('readline').createInterface({input: process.stdin}).on('line', (line) => process.emit('message', JSON.parse(line))); process.send = (message, sendHandle, options, callback) => this.write(JSON.stringify(message) + require('os').EOL, callback);
require("./" + process.argv[2]); 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; return true;
} }
} }
......
...@@ -20,3 +20,5 @@ process.on('message', (message)=> { ...@@ -20,3 +20,5 @@ process.on('message', (message)=> {
}); });
process.on('disconnect', process.exit); 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