-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
131 lines (113 loc) · 3.58 KB
/
proxy.js
File metadata and controls
131 lines (113 loc) · 3.58 KB
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
const webServer = require('./index')({ name: 'Local Proxy', apps: false, mongodb: { enabled: false }, require: { features: [] } });
var regex_hostport = /^([^:]+)(:([0-9]+))?$/;
webServer.options.ipList = [];
var getHostPortFromString = function (hostString, defaultPort) {
var host = hostString;
var port = defaultPort;
var result = regex_hostport.exec(hostString);
if (result != null) {
host = result[1];
if (result[2] != null) {
port = result[3];
}
}
return [host, port];
};
webServer.on('ready', () => {
if (webServer.server) {
webServer.server.addListener('connect', function (req, socket, bodyhead) {
let ip = socket.remoteAddress?.replace('::ffff:', '') || '';
if (!ip || !webServer.options.ipList.some((info) => info.ip === ip)) {
console.log('Socket Block IP : ' + ip + ' : ' + req.url);
return socket.end();
}
console.log('Socket Allow IP : ' + ip + ' : ' + req.url);
var hostPort = getHostPortFromString(req.url, 443);
var hostDomain = hostPort[0];
var port = parseInt(hostPort[1]);
console.log('Socket connecting :: ' + hostDomain + ':' + port);
var proxySocket = new webServer.net.Socket();
proxySocket.connect(port, hostDomain, function () {
proxySocket.write(bodyhead);
socket.write('HTTP/' + req.httpVersion + ' 200 Connection established\r\n\r\n');
});
proxySocket.on('connect', function () {
console.log('Socket connected :: ' + hostDomain);
});
proxySocket.on('data', function (chunk) {
socket.write(chunk);
});
proxySocket.on('end', function () {
socket.end();
});
proxySocket.on('error', function () {
socket.write('HTTP/' + req.httpVersion + ' 500 Connection error\r\n\r\n');
socket.end();
});
socket.on('data', function (chunk) {
proxySocket.write(chunk);
});
socket.on('end', function () {
proxySocket.end();
});
socket.on('error', function () {
proxySocket.end();
});
});
} else {
process.exit();
}
});
let http_agent = new webServer.http.Agent({
keepAlive: true,
});
let https_agent = new webServer.https.Agent({
keepAlive: true,
});
webServer.onALL('*', (req, res) => {
let ip = req.remoteAddress?.replace('::ffff:', '') || '';
if (!ip || !webServer.options.ipList.some((info) => info.ip === ip)) {
console.log('Http Block IP : ' + ip + ' : ' + req.url);
return res.end();
}
console.log('Http Allow IP : ' + ip + ' : ' + req.url);
webServer
.fetch(req.url, {
method: req.method,
headers: req.headers,
body: req.method.like('*get*|*head*') ? null : req.bodyRaw,
agent: function (_parsedURL) {
if (_parsedURL.protocol == 'http:') {
return http_agent;
} else {
return https_agent;
}
},
})
.then((response) => {
response.body.pipe(res);
})
.catch((err) => console.log(err));
});
process.on('message', (message) => {
if (typeof message.options === 'object') {
webServer.options = { ...webServer.options, ...message.options };
if (message.options.xip) {
let index = webServer.options.ipList.findIndex((info) => info.ip === message.options.xip);
if (index === -1) {
webServer.options.ipList.push({
ip: message.options.xip,
date: new Date(),
});
} else {
webServer.options.ipList[index].date = new Date();
}
}
}
if (message.type == 'start') {
webServer.run();
}
if (message.type == 'close') {
process.exit();
}
});