-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathProxyHTTP.js
More file actions
233 lines (194 loc) · 7.45 KB
/
ProxyHTTP.js
File metadata and controls
233 lines (194 loc) · 7.45 KB
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
var http = require('http');
var fs = require('fs');
var ldap = require('ldapjs');
var url = require('url');
var time = require ('timers');
// Reading of the main configuration file : config.json
var conf = JSON.parse(
fs.readFileSync('config.json', 'utf8')
);
// Function that write the log inside the file related to right server
var log = function (context, err, code, callback){
if (context.restricted){
if (err == "HTTP" && context.login)var data = "" + context.date + "\t" + context.login + "\t" + context.req.method + "\t" + context.req.url + "\t" + code +"\n";
else var data = "" + context.date + "\t" + err +"\n";
}else if (err == "HTTP")var data = "" + context.date + "\t" + context.req.method + "\t" + context.req.headers.host + context.req.url + "\t" + code +"\n";
if (data){
console.log(data);
if (context.conf) fs.appendFileSync(conf[context.conf].logFile, data);
else fs.appendFileSync("ProxyHTTP.log", data); //change the name of the proxy log file inside the code
};
callback();
};
// Test function for basic http authentication with a fixed login/password defined in config.json
var authentifyDummy =function (context, callback){
context.restricted = true;
if(!context.req.headers.authorization){
context.res.statusCode = 401;
context.res.setHeader('WWW-Authenticate', 'Basic realm="Secure Area"');
log(context, "HTTP", 401, function(){});
context.res.end();
}else{
if(context.login === conf[context.conf].authData.login && context.pw === conf[context.conf].authData.pw){
callback();
}else{
context.res.statusCode = 401;
context.res.setHeader('WWW-Authenticate', 'Basic realm="Secure Area"');
log(context, "HTTP", 401, function(){});
context.res.end();
}
}
}
// Cache of recent LDAP bind informations
var servLDAP = {};
// Function that remove old cached informations about LDAP bind
var flush = function(id, server){
delete servLDAP[server][id];
if (servLDAP[server] === {}) delete servLDAP[server];
};
// LDAP bind with HTTP basic authentication
var authentifyLDAP =function (context, callback){
context.restricted = true;
if(!context.req.headers.authorization){
context.res.statusCode = 401;
context.res.setHeader('WWW-Authenticate', 'Basic realm="Secure Area"');
log(context, "HTTP", 401, function(){});
context.res.end();
}else{
if (!servLDAP[conf[context.conf].ldap.url] || !servLDAP[conf[context.conf].ldap.url][context.auth]){
ldapReq = conf[context.conf].ldap.id+ context.login +','+conf[context.conf].ldap.cn; //do not manage more than one dc information
var serveursLDAP=ldap.createClient({
'url' : conf[context.conf].ldap.url
});
serveursLDAP.bind(ldapReq, context.pw, function(err) {
if (!err) {
if (!servLDAP[conf[context.conf].ldap.url]) {
servLDAP[conf[context.conf].ldap.url] ={};
servLDAP[conf[context.conf].ldap.url][context.auth.toString()] = setTimeout(flush, 600000, [context.auth], [conf[context.conf].ldap.url]);
}else{
servLDAP[conf[context.conf].ldap.url][context.auth.toString()] = setTimeout(flush, 600000, [context.auth], [conf[context.conf].ldap.url]);
}
serveursLDAP.unbind(function(){
callback();
});
}else{
console.log("LDAP error : " + JSON.stringify(err));
log(context, err, 0, function(){});
context.res.statusCode = 401;
context.res.setHeader('WWW-Authenticate', 'Basic realm="Secure Area"');
log(context, "HTTP", 401, function(){});
context.res.end();
}
});
}else{
clearTimeout(servLDAP[conf[context.conf].ldap.url][context.auth.toString()]);
servLDAP[conf[context.conf].ldap.url][context.auth.toString()] = setTimeout(flush, 600000, [context.auth], [conf[context.conf].ldap.url]);
callback();
}
}
}
// Function that manage the authorization to access to specific resources defined inside config.json
var AuthorizList =function (context, callback){
context.restricted = true;
var idDoc = context.req.url.split('/')[3];
if(conf[context.conf].restricted[idDoc]){
if (conf[context.conf].restricted[idDoc].indexOf(context.login) == -1){
context.res.statusCode = 403;
log(context, "HTTP", 403, function(){});
context.res.end("Forbidden");
} else callback();
}else{
callback();
}
}
// Main proxy function that forward the request and the related answers
var proxyWork = function(context, callback){
proxyReq = http.request(context.options, function (res){
if (res.headers.location && conf[context.conf].rewritePath.enable){
var splitHeaders = res.headers.location.split('/');
res.headers.location = context.req.headers.origin;
for (var i = (3 + conf[context.conf].rewritePath.headersOffset); i < splitHeaders.length; i++) {
res.headers.location = res.headers.location +'/'+ splitHeaders[i];
}
}
context.res.writeHead(res.statusCode, res.headers);
log(context, "HTTP", res.statusCode, function(){});
res.on('data',function(chunkOrigin) {
context.res.write(chunkOrigin);
});
res.on('end', function(){
context.res.end();
});
});
proxyReq.on('error', function(err){
console.log('problem with the server: ' + JSON.stringify(err));
context.res.writeHead(504);
log(context, "HTTP", 504, function(){});
context.res.end("Gateway Timeout");
});
context.req.on('data', function(chunkInit){
proxyReq.write(chunkInit)
});
context.req.on('error', function(err) {
log(context, err, 0, function(){});
console.log('problem with request: ' + err.message);
});
context.req.on('end', function(){
proxyReq.end();
callback();
});
}
// Function that allow to find the index of the requested server inside config.json
var matching = function(host){
var verif = false;
var i =0;
while ((verif == false) && (i < conf.length)){
var re = new RegExp(conf[i].hostProxy, "i");
verif = re.test(host);
if (verif == false)i++;
};
if (verif == false ) i = -1;
return i;
};
// Main HTTP server
http.createServer(function (request, response){
var context = {
"req": request,
"res": response,
"date": new Date()
};
var index = matching(request.headers.host);
if(index == -1){
response.writeHead(404);
log(context, "HTTP", 404, function(){});
response.end("Not Found");
}else{
context.conf = index;
var head = JSON.parse(JSON.stringify(request.headers));
if (request.headers.authorization && conf[index].hideAuth) delete head.authorization;
var options = {
'host': conf[index].host,
'port': conf[index].port,
'path': conf[index].path + url.parse(request.url).path,
'method': request.method,
'headers': head,
'agent': false
};
context.options = options;
if (request.headers.authorization){
context.auth = request.headers.authorization.split(" ")[1];
context.login = new Buffer(context.auth, 'base64').toString().split(':')[0];
context.pw = new Buffer(context.auth, 'base64').toString().split(':')[1];
};
var i=0;
var breaker = false;
while(i<conf[index].rules.length && !breaker){
if(eval(conf[index].rules[i].control)){
eval(conf[index].rules[i].action);
breaker = conf[index].rules[i].final;
}
i++;
}
}
}).listen(1337); // port has to be changed directly inside the code.
console.log('Server running port 1337');