|
1 | 1 | var express = require('express'); |
2 | 2 | var http = require('http'); |
3 | | -var https = require('https'); |
4 | 3 | var path = require('path'); |
5 | | -var ws = require('websocket').server; |
| 4 | +var server = require('socket.io'); |
6 | 5 | var pty = require('pty.js'); |
7 | | -var fs = require('fs'); |
8 | | -var waitpid = require('waitpid'); |
9 | 6 |
|
10 | 7 | var opts = require('optimist') |
11 | 8 | .options({ |
12 | | - sslkey: { |
13 | | - demand: false, |
14 | | - description: 'path to SSL key' |
15 | | - }, |
16 | | - sslcert: { |
17 | | - demand: false, |
18 | | - description: 'path to SSL certificate' |
19 | | - }, |
20 | | - sshhost: { |
21 | | - demand: false, |
22 | | - description: 'ssh server host' |
23 | | - }, |
24 | | - sshport: { |
25 | | - demand: false, |
26 | | - description: 'ssh server port' |
27 | | - }, |
28 | | - sshuser: { |
29 | | - demand: false, |
30 | | - description: 'ssh user' |
31 | | - }, |
32 | | - sshauth: { |
33 | | - demand: false, |
34 | | - description: 'defaults to "password", you can use "publickey,password" instead' |
35 | | - }, |
36 | 9 | port: { |
37 | 10 | demand: true, |
38 | 11 | alias: 'p', |
39 | 12 | description: 'wetty listen port' |
40 | 13 | }, |
| 14 | + command: { |
| 15 | + demand: false, |
| 16 | + alias: 'c', |
| 17 | + description: 'command to run in shell, defaults to /bin/login' |
| 18 | + }, |
41 | 19 | }).boolean('allow_discovery').argv; |
42 | 20 |
|
43 | | -var runhttps = false; |
44 | | -var sshport = 22; |
45 | | -var sshhost = 'localhost'; |
46 | | -var sshauth = 'password'; |
47 | | -var globalsshuser = ''; |
48 | | - |
49 | | -if (opts.sshport) { |
50 | | - sshport = opts.sshport; |
51 | | -} |
52 | | - |
53 | | -if (opts.sshhost) { |
54 | | - sshhost = opts.sshhost; |
55 | | -} |
56 | | - |
57 | | -if (opts.sshauth) { |
58 | | - sshauth = opts.sshauth |
59 | | -} |
60 | | - |
61 | | -if (opts.sshuser) { |
62 | | - globalsshuser = opts.sshuser; |
63 | | -} |
| 21 | +var command = '/bin/login'; |
64 | 22 |
|
65 | | -if (opts.sslkey && opts.sslcert) { |
66 | | - runhttps = true; |
67 | | - opts['ssl'] = {}; |
68 | | - opts.ssl['key'] = fs.readFileSync(path.resolve(opts.sslkey)); |
69 | | - opts.ssl['cert'] = fs.readFileSync(path.resolve(opts.sslcert)); |
| 23 | +if (opts.command) { |
| 24 | + command = opts.command; |
70 | 25 | } |
71 | 26 |
|
72 | 27 | process.on('uncaughtException', function(e) { |
73 | 28 | console.error('Error: ' + e); |
74 | 29 | }); |
75 | 30 |
|
76 | | -var httpserv; |
77 | 31 |
|
78 | 32 | var app = express(); |
79 | | -app.get('/wetty/ssh/:user', function(req, res) { |
80 | | - res.sendfile(__dirname + '/public/wetty/index.html'); |
| 33 | +app.get('/course/:course', function(req, res) { |
| 34 | + var course = req.params.course; |
| 35 | + res.sendfile(__dirname + '/public/index.html'); |
81 | 36 | }); |
82 | 37 | app.use('/', express.static(path.join(__dirname, 'public'))); |
83 | 38 |
|
84 | | -if (runhttps) { |
85 | | - httpserv = https.createServer(opts.ssl, app).listen(opts.port, function() { |
86 | | - console.log('https on port ' + opts.port); |
87 | | - }); |
88 | | -} else { |
89 | | - httpserv = http.createServer(app).listen(opts.port, function() { |
90 | | - console.log('http on port ' + opts.port); |
91 | | - }); |
92 | | -} |
93 | | - |
94 | | -var wss = new ws({ |
95 | | - httpServer: httpserv |
| 39 | +var httpserv = http.createServer(app).listen(opts.port, function() { |
| 40 | + console.log('http on port ' + opts.port); |
96 | 41 | }); |
97 | 42 |
|
98 | | -wss.on('request', function(request) { |
99 | | - var term; |
100 | | - var sshuser = ''; |
101 | | - var conn = request.accept('wetty', request.origin); |
102 | | - console.log((new Date()) + ' Connection accepted.'); |
103 | | - if (request.resource.match('^/wetty/ssh/')) { |
104 | | - sshuser = request.resource; |
105 | | - sshuser = sshuser.replace('/wetty/ssh/', ''); |
106 | | - } |
107 | | - if (sshuser) { |
108 | | - sshuser = sshuser + '@'; |
109 | | - } else if (globalsshuser) { |
110 | | - sshuser = globalsshuser + '@'; |
| 43 | +var io = server(httpserv); |
| 44 | +io.on('connection', function(socket){ |
| 45 | + var request = socket.request; |
| 46 | + var course = 'default'; |
| 47 | + if (match = request.headers.referer.match('/course/.+$')) { |
| 48 | + course = match[0].replace('/course/', ''); |
111 | 49 | } |
112 | | - conn.on('message', function(msg) { |
113 | | - var data = JSON.parse(msg.utf8Data); |
114 | | - if (!term) { |
115 | | - if (process.getuid() == 0) { |
116 | | - term = pty.spawn('/bin/login', [], { |
117 | | - name: 'xterm-256color', |
118 | | - cols: 80, |
119 | | - rows: 30 |
120 | | - }); |
121 | | - } else { |
122 | | - term = pty.spawn('ssh', [sshuser + sshhost, '-p', sshport, '-o', 'PreferredAuthentications=' + sshauth], { |
123 | | - name: 'xterm-256color', |
124 | | - cols: 80, |
125 | | - rows: 30 |
126 | | - }); |
127 | | - } |
128 | | - console.log((new Date()) + " PID=" + term.pid + " STARTED on behalf of user=" + sshuser) |
129 | | - term.on('data', function(data) { |
130 | | - conn.send(JSON.stringify({ |
131 | | - data: data |
132 | | - })); |
133 | | - }); |
134 | | - term.on('exit', function(code) { |
135 | | - console.log((new Date()) + " PID=" + term.pid + " ENDED") |
136 | | - }) |
137 | | - } |
138 | | - if (!data) |
139 | | - return; |
140 | | - if (data.rowcol) { |
141 | | - term.resize(data.col, data.row); |
142 | | - } else if (data.data) { |
143 | | - term.write(data.data); |
144 | | - } |
| 50 | + console.log((new Date()) + ' Connection accepted.'); |
| 51 | + |
| 52 | + var term; |
| 53 | + term = pty.spawn(command, [course], { |
| 54 | + name: 'xterm-256color', |
| 55 | + cols: 80, |
| 56 | + rows: 30 |
145 | 57 | }); |
146 | | - conn.on('error', function() { |
147 | | - term.end(); |
| 58 | + |
| 59 | + console.log((new Date()) + " PID=" + term.pid + " STARTED") |
| 60 | + term.on('data', function(data) { |
| 61 | + socket.emit('output', data); |
148 | 62 | }); |
149 | | - conn.on('close', function() { |
| 63 | + term.on('exit', function(code) { |
| 64 | + console.log((new Date()) + " PID=" + term.pid + " ENDED") |
| 65 | + }); |
| 66 | + socket.on('resize', function(data) { |
| 67 | + term.resize(data.col, data.row); |
| 68 | + }); |
| 69 | + socket.on('input', function(data) { |
| 70 | + term.write(data); |
| 71 | + }); |
| 72 | + socket.on('disconnect', function() { |
150 | 73 | term.end(); |
151 | | - }) |
| 74 | + }); |
152 | 75 | }) |
0 commit comments