-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
executable file
·278 lines (254 loc) · 9.43 KB
/
index.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env node
/**
* set up a proxy to log http archives
* run:
* node logproxy [listen port] [only log host,..]
* eg.
* 1.start logproxy with default setting
* node logproxy
* 2.listen on port 8088 and only log hosts -- 'c.163.com' and every host matchs '126.com'
* node logproxy 8088 c.163.com,*126.com
*
* @auth http://www.iunbug.com
*/
var http = require('http'),
net = require('net'),
fs = require('fs'),
StringDecoder = require('string_decoder').StringDecoder,
Buffer = require('buffer').Buffer,
Iconv = require('iconv-lite'),//https://github.com/bnoordhuis/node-iconv,https://github.com/ashtuchkin/iconv-lite
chardet = require('chardet'),//https://github.com/unbug/node-chardet
colors = require('colors'),
util = require('util');
//prevent node.js from crashing
process.on('uncaughtException', function (err) {
console.log("process uncaughtException error");
console.error(err);
});
var excuteArgvs = (function(){
var p = process.argv[2] || 1630,
f = process.argv[3]?process.argv[3].split(','):[];
return {
port: p,
filterHosts: f
}
})();//end excuteArgvs
function printLog(logs){
logs = logs || 'NULL';
if(typeof logs == 'string'){
console.log(logs);
}else{
// console.log(util.inspect(logs, { showHidden: false, depth: null,colors: true}));
console.dir(logs);
}
}//end printLog
function getIPAddress() {
var interfaces = require('os').networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
return alias.address;
}
}
return '0.0.0.0';
}//end getIPAddress
var formatHost = (function() {
var reg = /^([^:]+)(:([0-9]+))?$/i;
return function(host){
var tmp = host.match(reg);
return tmp?{host: tmp[1],port: tmp[3]}:{host: host}
}
})();//end formatHost
var formatPath = (function() {
var reg = /^[a-zA-Z]+:\/\/[^\/]+(\/.*)?$/i;
return function(url){
var tmp = url.match(reg);
return tmp?(tmp[1].length>0&&tmp[1]):'/';
}
})();//end formatHost
function decodeResponseBody(headers,body){
var cs;
cs = headers['content-type'].match(/.*charset=(.*)[;]?/i);
cs = cs && cs[1];
if(cs){
console.log(('Body charset is '+cs ).magenta.bold);
if(Iconv.encodingExists(cs)){
return Iconv.decode(new Buffer(body),cs);
}
}
return autoDecodeCharset(body);
}//end decodeResponseBody
function autoDecodeCharset(data){
if(data){
var buffer = new Buffer(data),
charset = chardet.detect(buffer);
console.log(('Data charset is '+charset ).magenta.bold);
try {
data = buffer.toString(charset);
} catch (e) {
if(Iconv.encodingExists(charset)){
data = Iconv.decode(buffer,charset);
}
}
return data;
}
}//end autoDecodeCharset
/**
*
* @param {Object} log {reqMethod,reqUrl,reqHeaders,reqData,resCode,resHeaders,resBody}
*/
var printClientLog = (function(){
var requestCount = 0;
return function(logs){
if(!isFilterHost(logs.reqHeaders['host'])){return;}
util.log(('--'+requestCount+'--------------------------- LOG REQUEST STARTED ---------------------------'+requestCount+'--').yellow);
console.log('HTTP VERSION: '.magenta.bold+logs.httpVersion.red);
console.log(('METHOD '+logs.reqMethod+': ').magenta.bold+logs.reqUrl.red);
if( logs.resHeaders && (/image|audio|video|upload/ig).test(logs.resHeaders['content-type']) ){
printLog('CONTENT TYPE: '.magenta.bold+logs.resHeaders['content-type'].red);
}else{
printLog('REQUEST HEADERS: '.magenta.bold);
printLog(logs.reqHeaders);
if( !(/connect/ig).test(logs.reqMethod) ){
printLog('REQUEST DATA: '.magenta.bold);
printLog(autoDecodeCharset(logs.reqData));
printLog('RESPONSE STATUS CODE: '.magenta.bold+(logs.resCode+'').red);
printLog('RESPONSE HEADERS: '.magenta.bold);
printLog(logs.resHeaders);
printLog('RESPONSE BODY: '.magenta.bold);
printLog( autoDecodeCharset(logs.resBody) );
}
}
util.log(('--'+requestCount+'----------------------------- LOG REQUEST END ---------------------------'+requestCount+'--').yellow);
requestCount++;
}
})();//end printClientLog
function isFilterHost(host){
if(excuteArgvs.filterHosts.length<1 || host.length<1){
return true;
}
for(var i=0;i<excuteArgvs.filterHosts.length;i++){
if(excuteArgvs.filterHosts[i].length<1){continue;}
if(excuteArgvs.filterHosts[i].indexOf('*')!=-1){//start with *
var reg = new RegExp(excuteArgvs.filterHosts[i].replace('*',''),'ig');
if(reg.test(host)){
return true;
}
}else{
if( host == excuteArgvs.filterHosts[i] ){
return true;
}
}
}
return false;
}//end isFilterHost
/**
*
* @param {Object} request
* @param {Object} response
*/
function onWebServerCreate(request, response){
util.log(('-----------------------------REQUEST STARTING HOST '+request.headers['host']+' -----------------------------').yellow);
var clientOption,clientRequest,
clientLog = {},sHeaders = {},
host = formatHost(request.headers['host']);
for(var key in request.headers){
sHeaders[key] = request.headers[key];
}
sHeaders['accept-encoding'] = '';
clientOption = {
host: host.host,
post: host.port || 80,
method: request.method,
path: formatPath(request.url),
headers: sHeaders,
agent: request.agent,
auth: request.auth
}
clientRequest = new http.ClientRequest(clientOption);
clientRequest.addListener('response', function (clientResponse) {
response.writeHead(clientResponse.statusCode, clientResponse.headers);
var body = [];
clientResponse.addListener('data', function(chunk) {
body.push(chunk);
response.write(chunk,'binary');
});
clientResponse.addListener('end', function() {
clientLog.httpVersion = request.httpVersion;
clientLog.reqMethod = request.method;
clientLog.reqUrl = request.url;
clientLog.reqHeaders = request.headers;
clientLog.resCode = clientResponse.statusCode;
clientLog.resHeaders = clientResponse.headers;
clientLog.resBody = body.join('');
response.end();
printClientLog(clientLog);
});
});//end response
request.addListener('data', function(chunk) {
clientLog.reqData = chunk;
clientRequest.write(chunk,'binary');
});//end data
request.addListener('end', function() {
clientRequest.end();
});//end end
request.on('error', function (err){
printLog(err);
});//end error
}//end onWebServerCreate
function onWebServerConnect(request, socket, head){
util.log(('-----------------------------REQUEST STARTING HOST '+request.headers['host']+' -----------------------------').yellow);
var clientSocket,
host = formatHost(request['url']),
clientLog = {
resBody: []
};
request.headers['accept-encoding'] = '';
clientSocket = new net.Socket();
clientSocket.connect(parseInt(host.port||443),host.host);
clientSocket.on('connect',function (){
clientSocket.write(head);
// tell the caller the connection was successfully established
socket.write( "HTTP/" + request.httpVersion + " 200 Connection established\r\n\r\n" );
});//end connect
clientSocket.on('data',function(chunk){
clientLog.resBody.push(chunk);
socket.write(chunk);
});//end data
clientSocket.on('end',function (){
clientLog.httpVersion = request.httpVersion;
clientLog.reqMethod = request.method;
clientLog.reqUrl = request.url;
clientLog.reqHeaders = request.headers;
clientLog.resBody = clientLog.resBody.join('');
socket.end();
printClientLog(clientLog);
});//end end
socket.on('data',function (chunk){
clientLog.reqData = chunk;
clientSocket.write(chunk);
});//end data
socket.on('end',function () {
clientSocket.end();
});//end end
clientSocket.on('error',function (err){
socket.write( "HTTP/" + request.httpVersion + " 500 Connection error\r\n\r\n" );
printLog(err);
socket.end();
});//end error
socket.on('error',function (err){
printLog(err);
clientSocket.end();
});//end error
}//end onWebServerConnect
var webServer;
webServer = http.createServer(onWebServerCreate);
webServer.listen(excuteArgvs.port);
webServer.addListener('connect',onWebServerConnect);
util.log('==========logproxy [listen port] [only log host,..]==========='.red.bold);
util.log('==================================================================='.red.bold);
util.log(' PROXY IS READY ON ' +(getIPAddress()).red.bold+' PORT '+(excuteArgvs.port+' ').red.bold);
util.log('==================================================================='.red.bold);
util.log('==================================================================='.red.bold);