forked from jed/browserver-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browserver.js
245 lines (173 loc) · 5.51 KB
/
browserver.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
233
234
235
236
237
238
239
240
241
242
243
244
245
var http = require("http")
var brow = require("browserver-client")
var EventEmitter = brow.EventEmitter
var Request = brow.Request
var Response = brow.Response
Request.prototype.authorize = function(cb){ cb() }
Response.prototype.error = function(code, message) {
if (!code) code = 500
if (!message) message = http.STATUS_CODES[code]
this.writeHead(code, {
"Content-Type": "text/plain",
"Content-Length": Buffer.byteLength(message)
})
this.end(message)
}
function Proxy() {
EventEmitter.call(this)
this.servers = {}
this.Server = function(){ Server.call(this) }
this.Server.prototype = new Server
this.Server.prototype.ServerRequest = function(){ Request.call(this) }
this.Server.prototype.ServerRequest.prototype = new Request
this.Server.prototype.ClientRequest = function(){ Request.call(this) }
this.Server.prototype.ClientRequest.prototype = new Request
}
Proxy.prototype = new EventEmitter
Proxy.prototype.hostname = "*.vcap.me"
Proxy.prototype.listen = function(server, options) {
server instanceof http.Server || typeof server != "object"
? this.listenHttp(server, options)
: this.listenWs(server, options)
return this
}
Proxy.prototype.listenWs = function(wsServer, options) {
var proxy = this
if (!options) options = {}
if (options.authorize) {
this.Server.prototype.ServerRequest.prototype.authorize = options.authorize
}
this.ws = wsServer
wsServer.on("connection", function(socket) {
var server = new proxy.Server(options)
var hostname = proxy.hostname.replace("*", server.id)
server.hostname = hostname
proxy.servers[hostname] = server
socket.on("close", function() {
delete proxy.servers[hostname]
proxy.emit("disconnection", server)
})
server.listen(socket)
proxy.emit("connection", server)
})
}
Proxy.prototype.listenHttp = function(httpServer, options) {
if (typeof httpServer == "string") {
hostname = httpServer
httpServer = null
}
if (!(httpServer instanceof http.Server)) {
httpServer = http.createServer().listen(httpServer)
}
this.http = httpServer
if (!options) options = {}
if (options.authorize) {
this.Server.prototype.ServerRequest.prototype.authorize = options.authorize
}
if (options.hostname) this.hostname = options.hostname
this.hostPattern = new RegExp("^" +
this.hostname
.replace(/\./g, "\\.")
.replace(/\*/g, "[a-zA-Z0-9-]+") +
"$")
var emit = httpServer.emit
var proxy = this
httpServer.emit = function(event, req, res) {
if (event != "request") return emit.apply(this, arguments)
var host = req.headers.host || "localhost"
var hostname = host.split(":")[0]
if (!proxy.hostPattern.test(hostname)) {
return emit.apply(this, arguments)
}
var server = proxy.servers[hostname]
if (!server) return Response.prototype.error.call(res, 504)
var ip = req.connection.remoteAddress
var forwardedFor = req.headers["x-forwarded-for"]
if (forwardedFor) ip = forwardedFor + ", " + ip
req.headers["x-forwarded-for"] = ip
req.authorize = server.ServerRequest.prototype.authorize
server.onserverrequest(req, res)
}
}
function Server() {
this.id = brow.guid()
this.requests = {}
this.responses = {}
}
Server.prototype.listen = function(socket) {
this.socket = socket
var server = this
socket.on("message", function(data) {
var req = (new server.ClientRequest).parse(data)
if (req) return server.onclientrequest(req)
var res = (new Response).parse(data)
if (res) server.onserverresponse(res)
})
socket.on("close", function() {
var responses = server.responses
for (var id in responses) {
Response.prototype.error.call(responses[id], 504)
delete responses[id]
}
})
socket.on("error", function(error) {
console.log(error.message, error.stack)
})
}
Server.prototype.onserverrequest = function(req, res) {
var server = this
req.authorize(function(err) {
if (err) return Response.prototype.error.call(res, 403, err.message)
var socket = server.socket
req.id = brow.guid()
req.body = ""
server.responses[req.id] = res
req.on("data", function(chunk){ req.body += chunk })
req.on("end", function() {
socket.send(Request.prototype.serialize.call(req))
})
})
}
Server.prototype.onserverresponse = function(res) {
var response = this.responses[res.id]
if (!response) return
response.writeHead(res.statusCode, res.headers)
response.write(res.body)
response.end()
}
Server.prototype.onclientrequest = function(req) {
var socket = this.socket
req.authorize(function(err) {
if (err) {
var res = new Response
res.statusCode = 403
res.headers = {"content-type": "text/plain"}
res.reasonPhrase = http.STATUS_CODES[403]
res.id = req.id
res.body = err.message || res.reasonPhrase
return socket.send(res.serialize())
}
var host = req.headers.host
delete req.headers.host
host = host ? host.split(":") : ["localhost"]
var opts = {
method: req.method,
hostname: host[0],
port: host[1],
path: req.url,
headers: req.headers
}
var request = http.request(opts, function(res) {
res.body = ""
res.id = req.id
res.on("data", function(chunk){ res.body += chunk })
res.on("end", function() {
socket.send(Response.prototype.serialize.call(res))
})
})
request.write(req.body)
request.end()
})
}
exports.Server = Proxy
exports.createServer = function(){ return new Proxy }