Skip to content

Commit

Permalink
Fixes dynamic port issue...
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrux committed Feb 6, 2019
1 parent 0a721a5 commit 7ada2d4
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 110 deletions.
1 change: 1 addition & 0 deletions app/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python 2.7.13
5 changes: 3 additions & 2 deletions app/components/Terminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Terminal.applyAddon(winptyCompat);
class ReactTerminal extends React.Component {
constructor(props) {
super(props);
this.HOST = `127.0.0.1:9788`;
this.HOST = `127.0.0.1:${process.env.WORKBENCH_SHELL_PORT}`;
this.SOCKET_URL = `ws://${this.HOST}/terminals/`;
this.failures = 0;
this.interval = null;
Expand Down Expand Up @@ -320,7 +320,8 @@ class ReactTerminal extends React.Component {
// this.term.writeln('Server disconnected!');
// this._connectToServer();
// };
this.socket.onerror = () => {
this.socket.onerror = (error) => {
console.error(error);
this.term.writeln('Critical error, restart Workbench!');
this._connectToServer();
};
Expand Down
216 changes: 109 additions & 107 deletions app/main/services/server.js
Original file line number Diff line number Diff line change
@@ -1,117 +1,119 @@
import defaultShell from '../default-shell';
var terminals = {};
var logs = {};
const PORT = 9788;
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 argv = require('yargs').argv;
const port = argv.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();
});

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();
});

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)];

if (!term) {
ws.send('No such terminal created.');
return;
}
import getPort from 'get-port';

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
module.exports = {
startServer: async () => {
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));
}
});
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];
});
});

module.exports = {
startServer() {
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
});

// 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();
});

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)];

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];
});
});
return new Promise((resolve, reject) => {
if (!port) {
writeLog('ERROR: Please provide a port: node ./src/server.js --port=XXXX');
Expand Down
2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"scripts": {
"electron-rebuild": "node -r babel-register ../internals/scripts/ElectronRebuild.js",
"postinstall": "yarn electron-rebuild"
"postinstall": "npm run electron-rebuild"
},
"license": "MIT",
"dependencies": {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
"electron-updater": "^3.1.2",
"evilscan": "^1.7.1",
"express-ws": "^4.0.0",
"get-port": "^4.1.0",
"geval": "^2.2.0",
"git-describe": "^4.0.3",
"hexdump-nodejs": "^0.1.0",
Expand Down

0 comments on commit 7ada2d4

Please sign in to comment.