From a7d88f7564c9a9d599d0648910d2a7e9b7c8b14e Mon Sep 17 00:00:00 2001 From: "Joshua F. Rountree" Date: Wed, 6 Feb 2019 15:46:52 -0500 Subject: [PATCH] Port fixes --- app/actions/eon_list_actions.js | 6 + app/constants/eon_list_action_types.js | 1 + app/containers/EonDetailPage.js | 1 + app/main.dev.js | 7 + app/main/services/server.js | 235 +++++++++++++------------ 5 files changed, 137 insertions(+), 113 deletions(-) diff --git a/app/actions/eon_list_actions.js b/app/actions/eon_list_actions.js index 797f5bf..75b62ee 100644 --- a/app/actions/eon_list_actions.js +++ b/app/actions/eon_list_actions.js @@ -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 diff --git a/app/constants/eon_list_action_types.js b/app/constants/eon_list_action_types.js index 9f68a46..a8a38fc 100644 --- a/app/constants/eon_list_action_types.js +++ b/app/constants/eon_list_action_types.js @@ -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'; diff --git a/app/containers/EonDetailPage.js b/app/containers/EonDetailPage.js index d92c63a..e6a8b94 100644 --- a/app/containers/EonDetailPage.js +++ b/app/containers/EonDetailPage.js @@ -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, diff --git a/app/main.dev.js b/app/main.dev.js index aab27da..56fe77a 100644 --- a/app/main.dev.js +++ b/app/main.dev.js @@ -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`); }); \ No newline at end of file diff --git a/app/main/services/server.js b/app/main/services/server.js index e10a60c..9e1634c 100644 --- a/app/main/services/server.js +++ b/app/main/services/server.js @@ -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); + } } - } + }); }); + } } \ No newline at end of file