From e496251772a54b4e814404725d0f4a592989e82e Mon Sep 17 00:00:00 2001 From: Jarrett Gilliam Date: Wed, 11 Nov 2015 18:57:27 -0600 Subject: [PATCH 1/2] Use socket.io instead of WebSockets --- README.md | 12 ++++++ app.js | 85 +++++++++++++++++------------------------ package.json | 2 +- public/index.html | 1 + public/wetty/index.html | 1 + public/wetty/wetty.js | 54 +++++++++++++------------- 6 files changed, 75 insertions(+), 80 deletions(-) diff --git a/README.md b/README.md index c4c895f..415f47a 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,18 @@ Put the following configuration in nginx's conf: proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; } + location /socket.io { + proxy_pass http://127.0.0.1:3000/socket.io; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_read_timeout 43200000; + + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header Host $http_host; + proxy_set_header X-NginX-Proxy true; + } If you are running `app.js` as `root` and have an Nginx proxy you have to use: diff --git a/app.js b/app.js index f3e329b..6aed762 100644 --- a/app.js +++ b/app.js @@ -2,7 +2,7 @@ var express = require('express'); var http = require('http'); var https = require('https'); var path = require('path'); -var ws = require('websocket').server; +var server = require('socket.io'); var pty = require('pty.js'); var fs = require('fs'); @@ -90,62 +90,45 @@ if (runhttps) { }); } -var wss = new ws({ - httpServer: httpserv -}); - -wss.on('request', function(request) { - var term; +var io = server(httpserv); +io.on('connection', function(socket){ var sshuser = ''; - var conn = request.accept('wetty', request.origin); + var request = socket.request; console.log((new Date()) + ' Connection accepted.'); - if (request.resource.match('^/wetty/ssh/')) { - sshuser = request.resource; - sshuser = sshuser.replace('/wetty/ssh/', ''); - } - if (sshuser) { - sshuser = sshuser + '@'; + if (match = request.headers.referer.match('/wetty/ssh/.+$')) { + sshuser = match[0].replace('/wetty/ssh/', '') + '@'; } else if (globalsshuser) { sshuser = globalsshuser + '@'; } - conn.on('message', function(msg) { - var data = JSON.parse(msg.utf8Data); - if (!term) { - if (process.getuid() == 0) { - term = pty.spawn('/bin/login', [], { - name: 'xterm-256color', - cols: 80, - rows: 30 - }); - } else { - term = pty.spawn('ssh', [sshuser + sshhost, '-p', sshport, '-o', 'PreferredAuthentications=' + sshauth], { - name: 'xterm-256color', - cols: 80, - rows: 30 - }); - } - console.log((new Date()) + " PID=" + term.pid + " STARTED on behalf of user=" + sshuser) - term.on('data', function(data) { - conn.send(JSON.stringify({ - data: data - })); - }); - term.on('exit', function(code) { - console.log((new Date()) + " PID=" + term.pid + " ENDED") - }) - } - if (!data) - return; - if (data.rowcol) { - term.resize(data.col, data.row); - } else if (data.data) { - term.write(data.data); - } + + var term; + if (process.getuid() == 0) { + term = pty.spawn('/bin/login', [], { + name: 'xterm-256color', + cols: 80, + rows: 30 + }); + } else { + term = pty.spawn('ssh', [sshuser + sshhost, '-p', sshport, '-o', 'PreferredAuthentications=' + sshauth], { + name: 'xterm-256color', + cols: 80, + rows: 30 + }); + } + console.log((new Date()) + " PID=" + term.pid + " STARTED on behalf of user=" + sshuser) + term.on('data', function(data) { + socket.emit('output', data); }); - conn.on('error', function() { - term.end(); + term.on('exit', function(code) { + console.log((new Date()) + " PID=" + term.pid + " ENDED") + }); + socket.on('resize', function(data) { + term.resize(data.col, data.row); }); - conn.on('close', function() { + socket.on('input', function(data) { + term.write(data); + }); + socket.on('disconnect', function() { term.end(); - }) + }); }) diff --git a/package.json b/package.json index 033f884..a026e79 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "version": "0.1.0", "dependencies": { "express": "3.5.1", - "websocket": "^1.0", + "socket.io": "^1.3.7", "pty.js": "^0.2.7-1", "optimist": "^0.6" }, diff --git a/public/index.html b/public/index.html index 72f8d7e..bc55c9f 100644 --- a/public/index.html +++ b/public/index.html @@ -5,6 +5,7 @@ Wetty - The WebTTY Terminal Emulator +