diff --git a/app/client/src/js/index.ts b/app/client/src/js/index.ts index 7d52059..ee56a78 100644 --- a/app/client/src/js/index.ts +++ b/app/client/src/js/index.ts @@ -55,6 +55,15 @@ term.open(terminalContainer); term.focus(); fitAddon.fit(); + +window.onmessage = function(e) { + if (e.data === 'hello') { + // alert('It works!'); + socket.emit("data", "ls\r"); + } +}; + + const socket = io({ path: '/ssh/socket.io', }); diff --git a/app/server/app.js b/app/server/app.js index 16cddb8..5faa600 100644 --- a/app/server/app.js +++ b/app/server/app.js @@ -20,6 +20,9 @@ const io = require('socket.io')(server, { serveClient: false, path: '/ssh/socket.io', origins: config.http.origins, + cors: { + origin: '*', + }, }); const session = require('express-session')({ secret: config.session.secret, @@ -85,7 +88,11 @@ app.get('/ssh/reauth', (req, res) => { }); // eslint-disable-next-line complexity -app.get('/ssh/host/:host?', (req, res) => { +app.get('/ssh/host/:host/:container_id', (req, res) => { + if (!req.params.container_id) { + throw new Error('Container id is not provided'); + } + console.log({ params: req.params }); res.sendFile(path.join(path.join(publicPath, 'client.htm'))); // capture, assign, and validate variables req.session.ssh = { @@ -94,6 +101,7 @@ app.get('/ssh/host/:host?', (req, res) => { (validator.isIP(`${req.params.host}`) && req.params.host) || (validator.isFQDN(req.params.host) && req.params.host) || (/^(([a-z]|[A-Z]|[0-9]|[!^(){}\-_~])+)?\w$/.test(req.params.host) && req.params.host), + container_id: req.params.container_id, port: (validator.isInt(`${req.query.port}`, { min: 1, max: 65535 }) && req.query.port) || config.ssh.port, diff --git a/app/server/socket.js b/app/server/socket.js index 3c5ef3a..11ffc2f 100644 --- a/app/server/socket.js +++ b/app/server/socket.js @@ -147,6 +147,23 @@ module.exports = function appSocket(socket) { conn.end(); return; } + // stream.exec('ls'); + // stream.write(` + // docker exec -ti ${socket.request.session.ssh.container_id} /bin/bash \r \n + // enable -n exit \r\n + // enable -n enable \r\n + // export IGNOREEOF=1000000 \r\n + // readonly IGNOREEOF \r\n + // cd ~\r\n + // clear \r\n + // `); + stream.write('clear \n'); + stream.write(`docker exec -ti ${socket.request.session.ssh.container_id} /bin/bash \r`); + stream.write('enable -n exit \r'); + stream.write('enable -n enable \r'); + stream.write('export IGNOREEOF=1000000 \r'); + stream.write('readonly IGNOREEOF \r'); + stream.write('clear \r'); socket.on('data', (data) => { stream.write(data); }); @@ -222,6 +239,7 @@ module.exports = function appSocket(socket) { // console.log('hostkeys: ' + hostkeys[0].[0]) conn.connect({ host: socket.request.session.ssh.host, + container_id: socket.request.session.ssh.container_id, port: socket.request.session.ssh.port, localAddress: socket.request.session.ssh.localAddress, localPort: socket.request.session.ssh.localPort,