Skip to content

Commit

Permalink
Port fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrux committed Feb 6, 2019
1 parent 7ada2d4 commit a7d88f7
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 113 deletions.
6 changes: 6 additions & 0 deletions app/actions/eon_list_actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ export function ADD_ERROR(error) {
};
}

export function SET_TERMINAL_PORT(port) {
return {
type: types.SET_TERMINAL_PORT,
payload: port
};
}
export function RESET_ERROR() {
return {
type: types.RESET_ERROR
Expand Down
1 change: 1 addition & 0 deletions app/constants/eon_list_action_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const ADD_EON_FAILED = 'eonList/ADD_EON_FAILED';
export const ADD_EON_SUCCESS = 'eonList/ADD_EON_SUCCESS';
export const DESELECT_EON = 'eonList/DESELECT_EON';
export const SELECT_EON = 'eonList/SELECT_EON';
export const SET_TERMINAL_PORT = 'eonList/SET_TERMINAL_PORT';
export const CLEAR_UNRESOLVED_EONS = 'eonList/CLEAR_UNRESOLVED_EONS';
export const CREATE_PRIVATE_KEY = 'eonList/CREATE_PRIVATE_KEY';

Expand Down
1 change: 1 addition & 0 deletions app/containers/EonDetailPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function mapStateToProps(state) {
activeTab: state.eonDetail.activeTab,
activeCommand: state.eonDetail.activeCommand,
selectedEon: state.eonList.selectedEon,
terminalPort: state.eonList.terminalPort,
eon: state.eonList.eons[state.eonList.selectedEon],
network: state.networkConnection.status,
networkIp: state.networkConnection.ip,
Expand Down
7 changes: 7 additions & 0 deletions app/main.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,14 @@ app.on('ready', async () => {
} catch (e) {
console.log("Server could not be started.", e.message);
}

writeLog(`Starting RPC`);
startRpc(mainWindow, app);
writeLog(`Done RPC`);
writeLog(`Scanner RPC`);
startScanner();
writeLog(`Done Scanner RPC`);
writeLog(`Start Zmq RPC`);
startZmq();
writeLog(`Done Scanner RPC`);
});
235 changes: 122 additions & 113 deletions app/main/services/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,131 +2,140 @@ import defaultShell from '../default-shell';
var terminals = {};
var logs = {};
import getPort from 'get-port';


// import { ipcMain } from 'electron';
import * as types from '../../constants/eon_list_action_types';
const express = require('express');
const app = express();
const pty = require('node-pty-prebuilt');
const chalk = require('chalk');
const prefix = chalk.bold.blue;
const bgTaskColor = chalk.green;
function writeLog(...params) {
console.info(prefix('workbench') + ' ' + chalk.bold(bgTaskColor('[terminal]')), bgTaskColor(...params));
}
module.exports = {
startServer: async () => {
startServer() {
const argv = require('yargs').argv;
let port = await getPort({port: argv.port || 9778});
process.env.WORKBENCH_SHELL_PORT = port;
const express = require('express');
const app = express();
const pty = require('node-pty-prebuilt');
const chalk = require('chalk');
const prefix = chalk.bold.blue;
const bgTaskColor = chalk.green;
function writeLog(...params) {
console.info(prefix('workbench') + ' ' + chalk.bold(bgTaskColor('[terminal]')), bgTaskColor(...params));
}

const host = '127.0.0.1';

const ALLOWED_ORIGINS = [
'0.0.0.0',
'127.0.0.1',
'home.localhost',
'chrome-extension://'
];

app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');

let origin = req.get('origin');
let host = req.get('host');
let foundOrigin = ALLOWED_ORIGINS.find(o => (origin && origin.indexOf(o) >= 0));
let foundHost = ALLOWED_ORIGINS.find(h => (host && host.indexOf(h) >= 0));

if (!foundOrigin && !foundHost) {
res.status(403);
res.send('Go away!');
res.end();
return;
}
next();
});

app.use('/', express.static(__dirname + '/../build'));

require('express-ws')(app);

app.post('/terminals', function (req, res) {
let shell = defaultShell;
let cols = parseInt(req.query.cols, 10);
let rows = parseInt(req.query.rows, 10);
let term = pty.fork(shell, [], {
name: 'xterm-color',
cols: cols || 80,
rows: rows || 24,
cwd: process.env.PWD,
env: process.env
return getPort({port: argv.port || 9778}).then((port) => {

writeLog(`Listening for ${types.SELECT_EON}`);
// ipcMain.on(types.SELECT_EON, (evt) => {
// const { sender } = evt;
// writeLog(`Received ${types.SELECT_EON} message from RPC.`);
// console.log("sending port", port);
// sender.send(types.SET_TERMINAL_PORT, port);
// });
const host = '127.0.0.1';

const ALLOWED_ORIGINS = [
'0.0.0.0',
'127.0.0.1',
'home.localhost',
'chrome-extension://'
];

app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');

let origin = req.get('origin');
let host = req.get('host');
let foundOrigin = ALLOWED_ORIGINS.find(o => (origin && origin.indexOf(o) >= 0));
let foundHost = ALLOWED_ORIGINS.find(h => (host && host.indexOf(h) >= 0));

if (!foundOrigin && !foundHost) {
res.status(403);
res.send('Go away!');
res.end();
return;
}
next();
});

// term.write(`powershell.exe -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"\r`)
writeLog('Created terminal with PID: ' + term.pid);
terminals[term.pid] = term;
logs[term.pid] = '';
term.on('data', function (data) {
logs[term.pid] += data;
app.use('/', express.static(__dirname + '/../build'));

require('express-ws')(app);

app.post('/terminals', function (req, res) {
let shell = defaultShell;
let cols = parseInt(req.query.cols, 10);
let rows = parseInt(req.query.rows, 10);
let term = pty.fork(shell, [], {
name: 'xterm-color',
cols: cols || 80,
rows: rows || 24,
cwd: process.env.PWD,
env: process.env
});

// term.write(`powershell.exe -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"\r`)
writeLog('Created terminal with PID: ' + term.pid);
terminals[term.pid] = term;
logs[term.pid] = '';
term.on('data', function (data) {
logs[term.pid] += data;
});
res.send(term.pid.toString());
res.end();
});
res.send(term.pid.toString());
res.end();
});

app.post('/terminals/:pid/size', function (req, res) {
let pid = parseInt(req.params.pid, 10);
let cols = parseInt(req.query.cols, 10);
let rows = parseInt(req.query.rows, 10);
let term = terminals[pid];

term.resize(cols, rows);
writeLog('Resized terminal ' + pid + ' to ' + cols + ' cols and ' + rows + ' rows.');
res.end();
});

app.ws('/terminals/:pid', function (ws, req) {
var term = terminals[parseInt(req.params.pid, 10)];
app.post('/terminals/:pid/size', function (req, res) {
let pid = parseInt(req.params.pid, 10);
let cols = parseInt(req.query.cols, 10);
let rows = parseInt(req.query.rows, 10);
let term = terminals[pid];

if (!term) {
ws.send('No such terminal created.');
return;
}
term.resize(cols, rows);
writeLog('Resized terminal ' + pid + ' to ' + cols + ' cols and ' + rows + ' rows.');
res.end();
});

writeLog('Connected to terminal ' + term.pid);
ws.send(logs[term.pid]);
app.ws('/terminals/:pid', function (ws, req) {
var term = terminals[parseInt(req.params.pid, 10)];

term.on('data', function (data) {
// writeLog('Incoming data = ' + data);
try {
ws.send(data);
} catch (ex) {
// The WebSocket is not open, ignore
if (!term) {
ws.send('No such terminal created.');
return;
}

writeLog('Connected to terminal ' + term.pid);
ws.send(logs[term.pid]);

term.on('data', function (data) {
// writeLog('Incoming data = ' + data);
try {
ws.send(data);
} catch (ex) {
// The WebSocket is not open, ignore
}
});
ws.on('message', function (msg) {
term.write(msg);
});
ws.on('close', function () {
term.kill();
writeLog('Closed terminal ' + term.pid);
// Clean things up
delete terminals[term.pid];
delete logs[term.pid];
});
});
ws.on('message', function (msg) {
term.write(msg);
});
ws.on('close', function () {
term.kill();
writeLog('Closed terminal ' + term.pid);
// Clean things up
delete terminals[term.pid];
delete logs[term.pid];
});
});
return new Promise((resolve, reject) => {
if (!port) {
writeLog('ERROR: Please provide a port: node ./src/server.js --port=XXXX');
process.exit(1);
reject();
} else {
writeLog("Started Terminal Service");
try {
resolve(app.listen(port, host));
} catch (e) {
writeLog('ERROR: Could not start background/server...', e.message);
return new Promise((resolve, reject) => {
if (!port) {
writeLog('ERROR: Please provide a port: node ./src/server.js --port=XXXX');
process.exit(1);
reject();
} else {
writeLog(`Started Terminal Service for ${port}`);
try {
resolve(app.listen(port, host));
writeLog("Past resolve");
} catch (e) {
writeLog('ERROR: Could not start background/server...', e.message);
}
}
}
});
});

}
}

0 comments on commit a7d88f7

Please sign in to comment.