-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse.js
222 lines (183 loc) · 6.36 KB
/
reverse.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
/**
Reverse proxy steps :
1/ get client request
=> set backend request
=> set route handler
2/ open backend connection
3/ (eventually) stream client body on backend body
4/ get backend response
=> set backend body encoding
=> set client response headers
5/ send response headers to client
6/ stream response body
7/ close client & backend connections
*/
var sys = require("sys"),
url = require("url"),
http = require("http");
var objs = [] ;
var Routers = {}
var requestCount = 0;
var getRouteHandler = function (request) {
for ( var index in objs ) {
if ( objs[index].match(request) ) { return objs[index]; }
}
return false;
};
var duplicateRequest = function (request) {
return {
"headers": JSON.parse(JSON.stringify(request.headers)),
"method": request.method,
"url": request.url,
"httpVersion": request.httpVersion,
};
};
var getEncoding = function ( resp ) {
var encoding = "utf8";
if ( resp.headers["content-type"] && resp.headers["content-type"].length ) {
if ( resp.headers["content-type"].search(/^text/) < 0
|| resp.headers["content-type"].search("javascript") < 0
|| resp.headers["content-type"].search("json") < 0
) {
encoding="binary";
}
}
return encoding;
};
var haveFun = function ( handler, request, response ) {
var connection = {
"id": requestCount,
"request": {
"client": request,
"backend": null,
"backendHeaders": JSON.parse(JSON.stringify(request.headers)),
"backendHost": null,
"backendPort": null,
"backendUrl" : null,
"backendMethod": request.method,
"time": 0,
"bytes": 0,
"encoding": "utf8"
},
"answer": {
"answer": null,
"client": response,
"backend": null,
"clientHeaders": [],
"statusCode": null,
"time": 0,
"bytes": 0,
"body": null,
"encoding": null
}
}
requestCount++;
sys.print(connection.id+": client request: "+request.method+" "+request.url);
if ( request.headers.host ) sys.puts(" host = "+request.headers.host);
else sys.puts("");
// sys.puts("here "+sys.inspect(handler));
//
// here we call the handler
// passing it the big connection data structure
// As js pass objects by reference, changes made to connection will be available in this present function
//
handler.onProxyRequest(connection);
// sys.puts("here");
if ( !connection.request.backendHost || !connection.request.backendPort || !connection.request.backendMethod || !connection.request.backendUrl ) {
return false;
}
// create the connection to the backend server
var backend = http.createClient(connection.request.backendPort, connection.request.backendHost);
// send request to the backend
connection.request.backend = backend.request(connection.request.backendMethod, connection.request.backendUrl, connection.request.backendHeaders);
request.connection.addListener("close",function(is_error) {
if ( is_error ) {
sys.puts(connection.id+": client connection closed due to an error");
}
});
request.connection.addListener("timeout",function() {
sys.puts(connection.id+": client connection closed due to a timeout");
});
connection.request.backend.connection.addListener("close",function(is_error) {
if ( is_error ) {
sys.puts(connection.id+": backend connection closed due to an error");
}
});
connection.request.backend.connection.addListener("timeout",function() {
sys.puts(connection.id+": backend connection closed due to a timeout");
});
// stream client request body => backend request
request.addListener("data", function(chunk) {
connection.request.bytes += chunk.length ;
connection.request.backend.write(chunk, connection.request.encoding);
sys.puts(connection.id+": => "+chunk.length);
});
// the request is sent
request.addListener("end",function() {
// listening for the backend's response
connection.request.backend.addListener('response', function (backendResponse) {
sys.puts(connection.id+": backend response: "+backendResponse.statusCode );
connection.answer.backend = backendResponse;
// here we set defaults mapping of backend response to client response
connection.answer.clientHeaders = JSON.parse(JSON.stringify(backendResponse.headers));
connection.answer.statusCode = backendResponse.statusCode;
//
// here we call once again the handler
// only if it has the onProxyResponse function
//
if ( handler.onProxyResponse ) {
handler.onProxyResponse(connection);
}
// we set encoding with a very basic algorithm in case the handler didn't set it
if ( !connection.answer.encoding ) {
connection.answer.encoding = getEncoding(backendResponse);
}
// send response headers to the client
response.writeHead(connection.answer.statusCode, connection.answer.clientHeaders);
// set clients response body encoding
backendResponse.setBodyEncoding(connection.answer.encoding);
if ( connection.answer.data ) {
response.write(connection.answer.data,connection.answer.encoding);
connection.answer.bytes = connection.answer.data.length;
response.close();
} else {
backendResponse.addListener("data", function (chunk) {
response.write(chunk,connection.answer.encoding);
connection.answer.bytes += chunk.length ;
sys.puts(connection.id+": <= "+chunk.length);
});
backendResponse.addListener("end",function() {
response.close();
sys.puts(connection.id+": backend finished: "+connection.request.bytes+" "+connection.answer.bytes);
// sys.puts("Proxy request ended, read = "+connection.read+", written = "+connection.written);
});
}
});
connection.request.backend.close();
});
return true;
};
exports.registerRouter = function (name, def) { Routers[name] = def; };
exports.ProxyPass = function (router, options) {
if ( typeof Routers[router] != "undefined" ) {
try {
var r = new Routers[router](options);
objs.push (
r
);
} catch (e) {
sys.puts("Failed to register ProxyPass "+router+" ("+JSON.stringify(options)+") : "+sys.inspect(e));
}
}
};
exports.ProxyMatch = function ( request ) {return getRouteHandler(request) === false ? false : true ;}
exports.ProxyRouteHandler = function ( request ) { return getRouteHandler(request); }
exports.ProxyHandle = function (request, response) {
var handler = getRouteHandler(request);
if ( handler === false ) return false;
haveFun(handler,request,response);
return true;
}
exports.ManualHandle = function (request, response, handler) {
haveFun(handler,request,response);
}