-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
227 lines (194 loc) · 3.58 KB
/
server.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
var
fs = require('fs'),
net = require('net'),
yaml = require('js-yaml'),
ws = require('ws').Server,
opt = cmdLine()
if(!opt.listen)
opt.listen = 4567
log('Listening for websocket connections on port '+opt.listen+'...')
pidSave()
new ws({port: opt.listen})
.on('connection', req)
function req(ws, req)
{
var
ssh,
buf = []
log('Request', req.url)
ws
.on('message', wsMsg)
.on('close', wsClose)
.on('error', wsError)
fs.readFile(__dirname+'/hosts.yml', Hosts)
function Hosts(err, yml)
{
if(err)
return Err(err)
try
{
var host = findHost(req.url, yml)
log('Connecting to', host)
net.connect(22, host)
.on('error', Err)
.on('connect', sshOpen)
.on('readable', sshRead)
.on('close', sshClose)
}
catch(e)
{
Err(e)
}
}
function Err(e)
{
log('Error', e)
ws.send('Oops: '+e)
ws.close()
}
function wsMsg(data)
{
if(buf)
buf.push(data)
else
ssh.write(data)
}
function wsClose()
{
log('Client closed connection')
sshQuit()
}
function wsError(e)
{
log('Websocket error', e)
sshQuit()
}
function sshQuit()
{
if(ssh)
ssh.end()
}
function sshClose()
{
log('SSH server closed connection')
ws.terminate()
}
function sshOpen()
{
log('Connected to SSH server')
ssh = this
buf.forEach(function(data){ ssh.write(data) })
buf = null
}
function sshRead()
{
var x
while(null!=(x=this.read()))
ws.send(x)
}
}
function findHost(url, yml)
{
// Load YAML
yml = yaml.safeLoad(yml)
// Clean URL
url = String(url)
.split(/[^-.\w]+/)
.filter(id)
.filter(goodHost)
.reverse()[0]
var host
if(url in yml)
{
host = yml[url]
if(!host) throw Error('Invalid host')
return true===host ? url : host
}
for(pat in yml)
{
var m
if(!(m=/^\/(.*)\/(i?)$/.exec(pat))) continue
if(!(new RegExp(m[1], m[2]).test(url))) continue
m = yml[pat]
if(!m) throw Error('Invalid host')
host = true===m ? url : m
}
if(!host) throw Error('Invalid host')
return host
}
function id(x)
{
return x
}
function goodHost(h)
{
return !h.match(/^[-_.]|[-_.]$/)
}
function cmdLine()
{
var z = require('node-getopt').create([
['l', 'listen=port', 'Listen to port'],
['d', 'daemon', 'Run daemonized'],
['h', 'help', 'Show this help'],
['v', 'version', 'Show version'],
])
.bindHelp()
.on('version', showVer)
.on('daemon', daemonize)
var opt = z.parseSystem()
if(!opt.argv.length)
return opt.options
z.showHelp()
process.exit()
}
function showVer()
{
console.log(require('./package').version)
process.exit()
}
function daemonize(argv, options)
{
log('Going on in background...')
out = fs.openSync(__dirname+'/log/wsshd.log', 'a'),
opts = options.listen ? ['--listen', options.listen] : []
require('child_process').spawn(
process.argv[0],
[__filename].concat(opts),
{
detached: true,
stdio: ['ignore', out, out]
}
)
.unref()
process.exit()
}
function pidSave()
{
var
f = __dirname+'/tmp/pids/wsshd.pid'
fs.writeFile(f, process.pid, none)
process
.on('exit', clear)
.on('SIGINT', ctrlC)
function clear()
{
log('Exiting')
fs.unlinkSync(f)
}
function ctrlC()
{
process.exit()
}
}
function log()
{
var
d = new Date()
console.info.apply(
console,
['['+d.toISOString().replace('T', ' ').replace(/Z$/, '')
+d.toTimeString().split(/\s+/)[1].replace(/^\w+/, ' ')+']']
.concat([].slice.call(arguments)))
}
function none() {
}