-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.js
233 lines (197 loc) · 3.7 KB
/
connect.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
228
229
230
231
232
//
// Simple HTTP CONNECT proxy to WSSHD
//
var
fs = require('fs'),
net = require('net'),
webSocket = require('ws'),
opt = cmdLine()
if(!opt.listen)
opt.listen = 3122
log('Running on port', opt.listen, 'sending to', opt.uri)
pidSave()
net.createServer(Req)
.listen(opt.listen, On)
function On()
{
log('HTTP proxy started')
}
function Req(conn)
{
var
nhdr = 0, hdrs, body, host, ws
log("Client connected from", conn.remoteAddress+':'+conn.remotePort)
conn
.on('readable', cRead)
.on('end', cClose)
.on('error', cError)
function cRead()
{
var x
while(null!=(x=this.read()))
body ? through(x) : headers(x)
}
function cClose()
{
log('Client disconnected')
closeWs()
}
function cError(e)
{
log('Client error', e)
closeWs()
}
function closeWs()
{
if(ws)
ws.close()
}
function wssh(data)
{
if(data.length)
ws.send(data)
}
function through(data)
{
if(Array.isArray(body))
body.push(data)
else
wssh(data)
}
function headers(data)
{
hdrs = hdrs ? Buffer.concat([hdrs, data]) : data
while(splitHdrs()){}
}
function splitHdrs()
{
for(var cr, L=hdrs.length, i=0; i<L; i++)
if(10==hdrs[i])
{
L = hdrs
hdrs = hdrs.slice(i+1)
header(L.slice(0, i-(cr ? 1 : 0)).toString())
return true
}
else
cr = 13==hdrs[i]
}
function header(s)
{
if(!nhdr++)
connect(s)
else if(!s)
wssh_connect()
}
function connect(s)
{
var m=/^connect\s+([-.\w]+):22(?:\s|$)/i.exec(s)
if(m)
return host=m[1]
conn.end("HTTP/1.0 500 Bad request\r\n\r\n")
}
function wssh_connect()
{
var uri = opt.uri+'/'+host
log("Redirect to", uri)
body = [hdrs]
hdrs = new Buffer(0)
conn.write("HTTP/1.0 200 Ok\r\n\r\n")
ws = new webSocket(uri)
ws
.on('open', wsOpen)
.on('message', wsMsg)
.on('close', wsClose)
.on('error', wsError)
}
function wsOpen()
{
log("Connected to WSSHD")
body.forEach(wssh)
body = true
}
function wsMsg(data)
{
conn.write(data)
}
function wsClose()
{
log("Websocket closed")
conn.end()
}
function wsError(e)
{
log("Websocket error", e)
conn.end()
ws.close()
}
}
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(1==opt.argv.length)
{
opt.options.uri = opt.argv[0]
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/connect.log', 'a'),
opts = options.listen ? ['--listen', options.listen] : []
require('child_process').spawn(
process.argv[0],
[__filename].concat(opts).concat(argv),
{
detached: true,
stdio: ['ignore', out, out]
}
)
.unref()
process.exit()
}
function pidSave()
{
var
f = __dirname+'/tmp/pids/connect.pid'
fs.writeFile(f, process.pid)
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)))
}