-
Notifications
You must be signed in to change notification settings - Fork 0
/
wstunnel.js
188 lines (160 loc) · 2.93 KB
/
wstunnel.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
//
// Simple stunnel for Websockets
//
net = require('net')
ssl = require('tls')
if(3!=process.argv.length) help()
var Host = process.argv[2]
net.createServer(Req)
.listen(4567, On)
var
Count=0,
_log=log
function help()
{
path = require('path')
log('Usage:', path.basename(__filename), 'host')
process.exit()
}
function On()
{
log("Listening", this.address())
}
function Req(conn)
{
var
No=++Count, hs=[], prolog, body, tls
function log()
{
_log.apply(this, ['<'+No+'>'].concat([].slice.call(arguments)))
}
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 ? queue(x) : headers(x)
}
function cClose()
{
log('Client disconnected')
bye()
}
function cError(e)
{
log('Client error', e)
bye()
}
function bye()
{
conn.end()
if(tls) tls.end()
}
function queue(data)
{
if(Array.isArray(body))
body.push(data)
else
send(data)
}
function headers(data)
{
prolog = prolog ? Buffer.concat([prolog, data]) : data
while(splitHdrs()){}
}
function splitHdrs()
{
for(var cr, L=prolog.length, i=0; i<L; i++)
if(10==prolog[i])
{
L = prolog
prolog = prolog.slice(i+1)
header(L.slice(0, i-(cr ? 1 : 0)).toString())
return true
}
else
cr = 13==prolog[i]
}
function header(line)
{
if(!line.length)
tlsConnect()
else
hs.push(line)
}
function tlsConnect()
{
body=[prolog]
prolog=new Buffer(0)
patchHeaders()
tls = ssl.connect({
servername: Host,
host: Host,
port: 443
})
tls
.on('secureConnect', tConnect)
.on('readable', tData)
.on('end', tEnd)
.on('error', tError)
}
function patchHeaders()
{
if(!hs.length) return
var verb = hs.shift()
hs=['Host', 'Origin']
.map(hostHeader)
.concat(hs.filter(filterHeader))
hs.unshift(verb)
}
function send(data)
{
if(data.length)
tls.write(data)
}
function tConnect()
{
log('TLS connected', 'Auth='+this.authorized,'CN='+this.getPeerCertificate().subject.CN)
send(hs.join('\r\n')+'\r\n\r\n')
body.forEach(send)
body = true
}
function tData()
{
var x
while(null!=(x=this.read()))
if(x.length) conn.write(x)
}
function tError(err)
{
log('TLS error', err)
bye()
}
function tEnd()
{
log('TLS closed')
bye()
}
}
function hostHeader(s)
{
return s+': '+Host
}
function filterHeader(s)
{
return !/^(?:host|origin):/i.test(s)
}
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)))
}