forked from butlerx/wetty
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
114 lines (91 loc) · 2.67 KB
/
app.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var express = require('express');
var bodyParser = require('body-parser');
var http = require('http');
var https = require('https');
var path = require('path');
var server = require('socket.io');
var pty = require('pty.js');
var fs = require('fs');
var log = require('yalm');
log.setLevel('debug');
var opts = require('optimist')
.options({
sslkey: {
demand: false,
description: 'path to SSL key'
},
sslcert: {
demand: false,
description: 'path to SSL certificate'
},
port: {
demand: true,
alias: 'p',
description: 'wetty listen port'
}
}).boolean('allow_discovery').argv;
var runhttps = false;
if (opts.sslkey && opts.sslcert) {
runhttps = true;
opts['ssl'] = {
key: fs.readFileSync(path.resolve(opts.sslkey)),
cert: fs.readFileSync(path.resolve(opts.sslcert))
};
}
var httpserv;
var app = express();
app.use(bodyParser.urlencoded({
extended: true
}));
app.post('/', function(req, res) {
res.sendFile(__dirname + '/public/jutty.html');
});
app.use('/', express.static(path.join(__dirname, 'public/')));
if (runhttps) {
httpserv = https.createServer(opts.ssl, app).listen(opts.port, function () {
log.info('https on port ' + opts.port);
});
} else {
httpserv = http.createServer(app).listen(opts.port, function () {
log.info('http on port ' + opts.port);
});
}
var io = server(httpserv, {path: '/socket.io'});
var term;
io.on('connection', function (socket) {
log.info('socket.io connection');
socket.on('start', function (data) {
var params;
if (data.type === 'telnet') {
params = [data.host, data.port];
} else {
data.type = 'ssh';
params = [data.user + '@' + data.host];
}
log.info(data.type, params.join(' '));
term = pty.spawn(data.type, params, {
name: 'xterm-256color',
cols: data.col,
rows: data.row
});
log.info(term.pid, 'spawned');
term.on('data', function(data) {
socket.emit('output', data);
});
term.on('exit', function (code) {
log.info(term.pid, 'ended');
socket.emit('end');
term.destroy();
term = null;
});
});
socket.on('resize', function (data) {
term && term.resize(data.col, data.row);
});
socket.on('input', function (data) {
term && term.write(data);
});
socket.on('disconnect', function () {
term && term.end();
});
});