-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathproxy.js
53 lines (43 loc) · 1.15 KB
/
proxy.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
/*** config start ***/
var PROXY_PORT = '8888'; //对外端口
/*** config end ***/
var http = require('http');
var https = require('https');
var fs = require('fs');
var net = require('net');
var url = require('url');
function request(cReq, cRes) {
var u = url.parse(cReq.url);
var options = {
hostname : u.hostname,
port : u.port || 80,
path : u.path,
method : cReq.method,
headers : cReq.headers
};
var pReq = http.request(options, function(pRes) {
cRes.writeHead(pRes.statusCode, pRes.headers);
pRes.pipe(cRes);
}).on('error', function(e) {
cRes.end();
});
cReq.pipe(pReq);
}
function connect(cReq, cSock) {
var u = url.parse('http://' + cReq.url);
var pSock = net.connect(u.port, u.hostname, function() {
cSock.write('HTTP/1.1 200 Connection Established\r\n\r\n');
pSock.pipe(cSock);
}).on('error', function(e) {
cSock.end();
});
cSock.pipe(pSock);
}
var options = {
key : fs.readFileSync('./private.pem'),
cert : fs.readFileSync('./public.crt')
};
https.createServer(options)
.on('request', request)
.on('connect', connect)
.listen(PROXY_PORT, '0.0.0.0');