-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
156 lines (137 loc) · 3.53 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env node
"use strict"
var cluster = require('cluster')
var fs = require('fs')
var server = require('http').createServer()
var url = require('url')
var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({ server: server })
var express = require('express')
var app = express()
var port = process.env.PORT || 8080
var basicAuth = require('basic-auth-connect')
var argv = require('minimist')(process.argv.slice(2))
var user, pass;
// Read -u and -p params
if (argv.u && argv.p) {
user = argv.u
pass = argv.p
}
// Check for remoto.json config file
try {
var config = JSON.parse(fs.readFileSync('remoto.json'))
user = config.user
pass = config.password
if (config.port)
port = config.port
} catch (e) {
// console.log('No valid remoto.json config file found')
}
if (user && pass)
app.use(basicAuth(user, pass))
app.use(express.static(__dirname + '/public'))
var connections = []
function update_terminal_list() {
try {
var conns = []
connections.forEach(function (c) {
if (c.type == 'terminal')
conns.push([c.label, c.token])
})
connections.forEach(function (c) {
if (c.type == 'remote')
c.conn.send(JSON.stringify({
type: 'LIST',
message: conns
}))
})
} catch (ex) {
check_connections()
}
}
function check_connections() {
connections.forEach(c => {
try {
if (c.conn.readyState == 3) disconnect(c.conn)
} catch (ex) {
disconnect(c.conn)
console.error(ex, ex.stack)
}
})
}
function disconnect(ws) {
connections.forEach((c) => {
if (c.conn == ws) {
var index = connections.indexOf(c)
if (index > -1)
connections.splice(index, 1)
}
})
update_terminal_list()
}
wss.on('connection', function connection(ws) {
var location = url.parse(ws.upgradeReq.url, true)
var type = (location.query['type'] == 'terminal') ? 'terminal' : 'remote'
var token = location.query['token']
var label = location.query['label']
if (type == 'terminal' && (!token || !label)) {
try {
ws.close()
} catch (ex) {
console.error(ex, ex.stack)
} finally {
return null
}
}
var connection = {
type: type,
conn: ws,
token: token,
label: label
}
connections.push(connection)
update_terminal_list()
ws.on('message', function (message) {
try {
var to = (type == 'remote') ? 'terminal' : 'remote'
var obj = JSON.parse(message)
if (obj.type == 'CONN') {
connection.token = obj.message
} else if (obj.type == 'PING') {
ws.send(JSON.stringify({ type: 'PONG', message: 'PONG!' }))
} else {
connections.forEach((c) => {
try {
if (c.type == to && c.token == connection.token)
c.conn.send(JSON.stringify({ type: obj.type, message: obj.message }))
} catch (e) {
disconnect(c.conn)
}
})
}
} catch (ex) {
console.error(ex, ex.stack)
}
})
ws.on('close', () => {
disconnect(ws)
})
ws.on('error', () => {
disconnect(ws)
})
})
if (cluster.isMaster) {
cluster.fork()
cluster.on('exit', function (worker) {
console.log('Worker %s died, restarting...', worker.process.pid)
cluster.fork()
})
} else {
server.on('request', app)
server.listen(port, function () { console.log('Listening on ' + server.address().port) })
}
process.on('uncaughtException', function (err) {
console.error((new Date).toUTCString() + ' uncaughtException:', err.message)
console.error(err.stack)
process.exit(1)
})