forked from gvangool/node-socks
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhttp.js
197 lines (172 loc) · 4.99 KB
/
http.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
var net=require('net');
var local_port=8080;
//�ڱ��ش���һ��server��������local_port�˿�
var sockserver=net.createServer(function (client)
{
//���ȼ��������������ݷ����¼���ֱ���յ������ݰ���������http����ͷ
var buffer=new Buffer(0);
client.on('data',function (data)
{
buffer=buffer_add(buffer,data);
if(buffer_find_body(buffer)==-1)return ;
var req=parse_request(buffer);
if(req===false)return ;
client.removeAllListeners('data');
relay_connection(req);
});
//��http����ͷ��ȡ��������Ϣ���������������������ݣ�ͬʱ����Ŀ��������������Ŀ�������������ݴ���������
function relay_connection(req)
{
console.log(req.method+' '+req.host+':'+req.port);
//������������CONNECT������GET, POST������ô�滻��ͷ����һЩ����
if(req.method!='CONNECT')
{
//�ȴ�buffer��ȡ��ͷ��
var _body_pos=buffer_find_body(buffer);
if(_body_pos<0)_body_pos=buffer.length;
var header=buffer.slice(0,_body_pos).toString('utf8');
//�滻connectionͷ
header=header.replace(/(proxy\-)connection\:.+\r\n/ig,'')
.replace(/Keep\-Alive\:.+\r\n/i,'')
.replace("\r\n",'\r\nConnection: close\r\n');
//�滻��ַ��ʽ(ȥ����������)
if(req.httpVersion=='1.1')
{
var url=req.path.replace(/http\:\/\/[^\/]+/,'');
if(url.path!=url)header=header.replace(req.path,url);
}
buffer=buffer_add(new Buffer(header,'utf8'),buffer.slice(_body_pos));
}
//������Ŀ��������������
var server=net.createConnection(req.port,req.host);
//������������������������
client.on("data",function (data){
try
{server.write(data);}
catch (e)
{ }
});
server.on("data",function (data){
try
{client.write(data);}
catch (e)
{ }
});
server.on("end",function (e){
console.log("server close");
console.log(e);
try
{client.end();}
catch (e)
{ }
});
server.on("error",function (e){
console.log("server close");
console.log(e);
try
{client.destroy();}
catch (e)
{ }
});
client.on("end",function (e){
try
{server.end();}
catch (e)
{ }
});
client.on("error",function (e){
try
{server.destroy();}
catch (e)
{ }
});
if(req.method=='CONNECT')
client.write(new Buffer("HTTP/1.1 200 Connection established\r\nConnection: close\r\n\r\n"));
else
server.write(buffer);
}
});
//�������ִ���
process.on('uncaughtException',function (err)
{
console.log("\nError!!!!");
console.log(err);
});
sockserver.on('listening', function() {
var address = sockserver.address();
console.log('LISTENING %s:%s', address.address, address.port);
});
/**
* ������ͷ��ȡ��������ϸ��Ϣ
* ������ CONNECT ��������ô�᷵�� { method,host,port,httpVersion}
* ������ GET/POST ���������� { metod,host,port,path,httpVersion}
*/
function parse_request(buffer)
{
var s=buffer.toString('utf8');
var method=s.split('\n')[0].match(/^([A-Z]+)\s/)[1];
if(method=='CONNECT')
{
var arr=s.match(/^([A-Z]+)\s([^\:\s]+)\:(\d+)\sHTTP\/(\d\.\d)/);
if(arr&&arr[1]&&arr[2]&&arr[3]&&arr[4])
return {method:arr[1],host:arr[2],port:arr[3],httpVersion:arr[4]};
}
else
{
var arr=s.match(/^([A-Z]+)\s([^\s]+)\sHTTP\/(\d\.\d)/);
if(arr&&arr[1]&&arr[2]&&arr[3])
{
var host=s.match(/Host\:\s+([^\n\s\r]+)/)[1];
if(host)
{
var _p=host.split(':',2);
return {method:arr[1],host:_p[0],port:_p[1] ? _p[1]:80,path:arr[2],httpVersion:arr[3]};
}
}
}
return false;
}
/**
* ����buffer����������
*/
function buffer_add(buf1,buf2)
{
var re=new Buffer(buf1.length+buf2.length);
buf1.copy(re);
buf2.copy(re,buf1.length);
return re;
}
/**
* �ӻ������ҵ�ͷ����������("\r\n\r\n")��λ��
*/
function buffer_find_body(b)
{
for(var i=0,len=b.length-3;i<len;i++)
{
if(b[i]==0x0d&&b[i+1]==0x0a&&b[i+2]==0x0d&&b[i+3]==0x0a)
{
return i+4;
}
}
return -1;
}
//////////////////////////////////////////////////////////////////////////
var exec = require('child_process').exec;
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
if(1==numCPUs) cluster.fork();//make sure it is more than 2
cluster.on('exit', function(worker, code, signal) {
if (worker.suicide !== true) {
exec('taskkill /pid '+worker.process.pid +' /T /F');
}
var exitCode = worker.process.exitCode;
console.log('worker ' + worker.process.pid + ' died ('+exitCode+'). restarting...');
cluster.fork();
});
} else {
sockserver.listen(8080);
}