-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
150 lines (145 loc) · 7.19 KB
/
server.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
var http = require('http');
var fs = require("fs");
var iconv = require("iconv");
var server = http.createServer(function(request, response) {
if (request.url.indexOf('/bitrix/admin/1c_exchange.php') == -1){
response.writeHead(200, {"Content-Type": "text/html"});
response.write("<h1>There is nothing here!</h1><p>go fuck yourself</p>");
response.end();
}else{
var startDate = new Date();
// append info to the log file when request started
fs.appendFileSync(
'log/' + startDate.getDate() + '.' + (startDate.getMonth()+1) + '.txt',
'\n=================================================================================\n'+
'Request'+
'\nTime: ' + startDate.getHours() + '.' + startDate.getMinutes() + '.' + startDate.getSeconds() + '\n'+
'=================================================================================\n'+
'URL: ' + request.url + '\nMethod: ' + request.method + '\nHeaders: {\n'
);
// get request's headers
var headers = {};
for(var i in request.headers){
fs.appendFileSync(
'log/' + startDate.getDate() + '.' + (startDate.getMonth()+1) + '.txt',
" " + i + ": " + request.headers[i] + '\n'
);
// exclude default headers the requires no managing
if (i != "host" && i != "x-forwarded-proto" && i != "x-forwarded-port" && i != "x-region" && i != "x-forwarded-for" && i != "connection")
headers[i] = request.headers[i];
// write decoded login and password into the console
if (i == 'authorization'){
var tmp = request.headers[i].split(' ');
var buf = new Buffer(tmp[1], 'base64'); // create a buffer and tell it the data coming in is base64
var plain_auth = buf.toString(); // read it back out as a string
console.log("Decoded Authorization: ", plain_auth);
}
}
// close hearder's block after all headeres were logged
fs.appendFileSync('log/' + startDate.getDate() + '.' + (startDate.getMonth()+1) + '.txt', '}\n');
// create request on a custom server
var newRequest = http.request(
// request object
{
hostname: "php-bitrix-unix-voron.c9users.io",
path: request.url,
method: request.method,
headers: headers
},
// manage responed from a custom server
function(newResponse) {
var endDate = new Date();
// start logging response from the custom server
fs.appendFileSync(
'log/' + endDate.getDate() + '.' + (endDate.getMonth()+1) + '.txt',
'\n=================================================================================\n'+
'Response'+
'\nTime: ' + endDate.getHours() + '.' + endDate.getMinutes() + '.' + endDate.getSeconds() + '\n'+
'=================================================================================\n'
);
// logging response's headers
for (var header in newResponse.headers){
fs.appendFileSync(
'log/' + startDate.getDate() + '.' + (startDate.getMonth()+1) + '.txt',
" " + header + ": " + newResponse.headers[header] + '\n'
);
}
// close block with headers
fs.appendFileSync('log/' + startDate.getDate() + '.' + (startDate.getMonth()+1) + '.txt', '}\n');
// create a custom header
response.writeHead(newResponse.statusCode, newResponse.headers["content-type"]);
// manage data from custom server
newResponse.on('data', function (chunk) {
var d = new Date();
// create a xml file for incoming response
if (newResponse.headers["content-type"] == "application/xml; charset=windows-1251"){
var body = new Buffer(chunk, 'binary');
var conv = new iconv.Iconv('windows-1251', 'utf8');
fs.appendFileSync('log/' + d.getDate() + '.' + (d.getMonth()+1) + '.res.xml', conv.convert(body).toString());
}else{
// this should cover most messages from bitrix server and convert it into readable strings in logged file
if (newResponse.headers["content-type"] == "text/html; charset=windows-1251"){
var body = new Buffer(chunk, 'binary');
var conv = new iconv.Iconv('windows-1251', 'utf8');
fs.appendFileSync('log/' + d.getDate() + '.' + (d.getMonth()+1) + '.txt', 'Data: ' + conv.convert(body).toString() + '\n');
}else{
// in case something went wrong this will send response data into console
console.log('BODY: ' + chunk, 'BODY type: ' + typeof chunk);
}
}
// send response from the custom server as response to the initial request
response.write(chunk);
}).on('end', function(){
// close response block
fs.appendFileSync(
'log/' + endDate.getDate() + '.' + (endDate.getMonth()+1) + '.txt',
'\n=================================================================================\n'+
'Response End'+
'\nTime: ' + endDate.getHours() + '.' + endDate.getMinutes() + '.' + endDate.getSeconds() + '\n'+
'=================================================================================\n'
);
// manage response
response.end();
});
});
// manage request data
request.on('data', (chunk) => {
// transforme incoming request data into binary state
var body = new Buffer(chunk, 'binary');
// if it isn't a file (most likily if it is a file the filetype is archive)
if (request.headers['content-type'] != "application/octet-stream"){
// change data codding from windows-1251 to utf8
var conv = new iconv.Iconv('windows-1251', 'utf8');
var d = new Date();
// write data into a file
fs.appendFileSync('log/' + d.getDate() + '.' + (d.getMonth()+1) + '.req.xml', conv.convert(body).toString());
// also log data into console
console.log('request data: ', chunk, 'data type: ', typeof chunk);
// in case the incomign data is a file
}else{
console.log(request.url);
// find out file name from request url
var name = request.url.substr(request.url.indexOf('filename=') + 9, request.url.length - (request.url.indexOf('filename=') + 9));
// save data into a file with an appropriate name
fs.appendFileSync('log/' + name, body, 'binary');
}
// send data into custom server
newRequest.write(chunk);
}).on('end', () => {
// close request block
fs.appendFileSync(
'log/' + startDate.getDate() + '.' + (startDate.getMonth()+1) + '.txt',
'\n=================================================================================\n'+
'Request End'+
'\nTime: ' + startDate.getHours() + '.' + startDate.getMinutes() + '.' + startDate.getSeconds() + '\n'+
'=================================================================================\n'
);
// manage request to a custom server
newRequest.end();
});
}
});
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
var addr = server.address();
//console.log("Chat server listening at", addr.address + ":" + addr.port);
});