-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·136 lines (116 loc) · 4.07 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
var http = require('http');
var https = require('https');
var fs = require('fs');
var net = require('net');
var url = require('url');
var path = require('path');
var multiparty = require('multiparty');
var uuid = require('uuid');
var log4js = require('log4js');
log4js.configure('log4js.json', { cwd: path.resolve(__dirname, '.') });
var user = require('./app/user');
var app = require('./app/app');
var logger = log4js.getLogger();
function request(req, resp) {
var u = url.parse(req.url);
var options = {
hostname: u.hostname,
port: u.port || 80,
path: u.path,
method: req.method,
headers: req.headers
};
logger.debug("%s %s %s:%s%s", req.connection.remoteAddress, options.method, options.hostname, options.port, options.path);
var header=req.headers['authorization']||'', // get the header
token=header.split(/\s+/).pop()||'', // and the encoded auth token
auth=new Buffer(token, 'base64').toString(), // convert from base64
parts=auth.split(/:/), // split on colon
username=parts[0],
password=parts[1];
if(u.hostname == null){ // Non Proxy Request
app(req, resp);
} else { // Proxy Request
resp.end();
return;
var forwardReq = http.request(options, function (forwardResp) {
resp.writeHead(forwardResp.statusCode, forwardResp.headers);
forwardResp.pipe(resp);
}).on('error', function (e) {
resp.end();
});
req.pipe(forwardReq);
}
}
function connect(req, socket, headers) {
var correlationId = uuid.v4();
var u = url.parse('http://' + req.url);
var options = {
hostname : u.hostname,
port : u.port
};
logger.debug("%s %s %s %s:%s", correlationId , req.connection.remoteAddress, "CONNECT", options.hostname, options.port);
if(!user.inWhiteList(req.connection.remoteAddress)){
socket.end();
}else{
var forwardSocket = net.connect(options.port, options.hostname, function() {
socket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
forwardSocket.pipe(socket);
}).on('error', function(e) {
logger.error("%s", correlationId, e);
socket.end();
}).on('close', function(had_error){
logger.error("%s had_error", correlationId, had_error);
forwardSocket.end();
});
socket.pipe(forwardSocket);
}
}
var options = {
//key: fs.readFileSync('./certs/privkey.pem'),
//cert: fs.readFileSync('./certs/fullchain.pem')
key: fs.readFileSync('./certs/privkey.pem'),
cert: fs.readFileSync('./certs/fullchain.pem')
};
http.createServer()
.on('request', request)
.on('connect', connect)
.listen(8080, '0.0.0.0', function(){
logger.info("8080 listen");
});
https.createServer(options)
.on('request', request)
.on('connect', connect)
.listen(8443, '0.0.0.0', function(){
logger.info("8443 listen");
// var cookie = 'something=anything'
//
// // make a request to a tunneling proxy
// var options = {
// port: 8443,
// hostname: '127.0.0.1',
// method: 'CONNECT',
// path: 'www.baidu.com:80',
// rejectUnauthorized: false
// };
//
// var req = https.request(options);
// req.setHeader("abc", "abc");
// req.setHeader("Cookie", cookie);
// req.end();
//
// req.on('connect', function(res, socket, head){
// logger.debug('got connected!');
//
// // make a request over an HTTP tunnel
// socket.write('GET / HTTP/1.1\r\n' +
// 'Host: www.baidu.com:80\r\n' +
// 'Connection: close\r\n' +
// '\r\n');
// socket.on('data', function(chunk){
// logger.debug(chunk.toString());
// });
// socket.on('end', function(){
// //proxy.close();
// });
// });
});