1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| const app = require('express')(); const http = require('http').createServer(app); const io = require('socket.io')(http); const sshNamespace = io.of('/ssh') const SSHClient = require('ssh2').Client; const ssh = new SSHClient(); const utf8 = require('utf8');
sshNamespace.on('connection', socket => { console.log('socket.id:', socket.id) socket.on('init_data', res => { console.log(res) initSSH(socket, res) }) })
http.listen(3100, () => { console.log('listening on http://localhost:3100'); });
function initSSH (socket, config) { console.log(config) ssh.on('ready', () => { ssh.shell((err, stream) => { if (err) { socket.emit('shell_error', '\r\n*** SSH SHELL ERROR: ' + err.message + ' ***\r\n'); } socket.on('ssh_client_data', data => { stream.write(data); }); stream.on('data', d => { socket.emit('ssh_server_data', utf8.decode(d.toString('binary'))); }).on('close', () => { console.log('close'); ssh.end(); }); }); }).on('close', () => { socket.emit('connect_closed', '\r\n*** SSH CONNECTION CLOSED ***\r\n'); }).on('error', err => { console.log(err); socket.emit('connect_error', '\r\n*** SSH CONNECTION ERROR: ' + err.message + ' ***\r\n'); }).connect({ host: config.ip, port: 22, username: config.username, password: config.password }); }
|