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,55 +160,66 @@ export class AppsService { ...@@ -180,55 +160,66 @@ 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);
})
} }
this.maotama.then((child)=> { let child;
let connection = this.connections.get(app); try {
if (connection) { child = await this.maotama;
connection.connection.close(); } catch (error) {
} alert(`出错了 ${error}`);
connection = {connection: new WebSocket(server.url), address: null}; return
let id; }
this.connections.set(app, connection);
connection.connection.onmessage = (event)=> { let connection = this.connections.get(app);
console.log(event.data); if (connection) {
let [action, args] = event.data.split(' ', 2); connection.connection.close();
let [address, port] = args.split(':'); }
switch (action) { connection = {connection: new WebSocket(server.url), address: null};
case 'LISTEN': let id;
connection.address = args; this.connections.set(app, connection);
this.ref.tick(); connection.connection.onmessage = (event)=> {
break; console.log(event.data);
case 'CONNECT': let [action, args] = event.data.split(' ', 2);
id = setInterval(()=> { let [address, port] = args.split(':');
child.send({ switch (action) {
action: 'connect', case 'LISTEN':
arguments: [app.network.port, port, address] connection.address = args;
}) this.ref.tick();
}, 200); break;
break; case 'CONNECT':
case 'CONNECTED': id = setInterval(()=> {
clearInterval(id); child.send({
id = null; action: 'connect',
break; arguments: [app.network.port, port, address]
} })
}; }, 200);
connection.connection.onclose = (event: CloseEvent)=> { break;
if (id) { case 'CONNECTED':
clearInterval(id); clearInterval(id);
id = null;
break;
}
};
connection.connection.onclose = (event: CloseEvent)=> {
if (id) {
clearInterval(id);
}
// 如果还是在界面上显示的那个连接
if (this.connections.get(app) == connection) {
this.connections.delete(app);
if (event.code != 1000 && !connection.address) {
alert(`出错了 ${event.code}`);
} }
// 如果还是在界面上显示的那个连接 }
if (this.connections.get(app) == connection) { // 如果还没建立好就出错了,就弹窗提示这个错误
this.connections.delete(app); this.ref.tick();
if (event.code != 1000 && !connection.address) { };
alert(`出错了 ${event.code}`);
}
}
// 如果还没建立好就出错了,就弹窗提示这个错误
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;
} }
} }
......
...@@ -19,4 +19,6 @@ process.on('message', (message)=> { ...@@ -19,4 +19,6 @@ process.on('message', (message)=> {
handler[message.action](...message.arguments); handler[message.action](...message.arguments);
}); });
process.on('disconnect', process.exit); process.on('disconnect', process.exit);
\ No newline at end of file
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