-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
executable file
·177 lines (146 loc) · 5.21 KB
/
Copy pathserver.js
File metadata and controls
executable file
·177 lines (146 loc) · 5.21 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
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
var http = require('http');
var https = require('https');
var config = require("./config");
var url = require("url");
var request = require("request");
http.globalAgent.maxSockets = Infinity;
https.globalAgent.maxSockets = Infinity;
var publicAddressFinder = require("public-address");
var publicIP;
// Get our public IP address
publicAddressFinder(function (err, data) {
if (!err && data) {
publicIP = data.address;
}
});
function server_log(req,msg){
if (config.enable_logging) {
console.log("%s %s %s : %s", (new Date()).toJSON(), req.clientIP, req.method, req.url , msg);
}
}
function addCORSHeaders(req, res) {
if (req.method.toUpperCase() === "OPTIONS") {
if (req.headers["access-control-request-headers"]) {
res.setHeader("Access-Control-Allow-Headers", req.headers["access-control-request-headers"]);
}
if (req.headers["access-control-request-method"]) {
res.setHeader("Access-Control-Allow-Methods", req.headers["access-control-request-method"]);
}
}
if (req.headers["origin"]) {
res.setHeader("Access-Control-Allow-Origin", req.headers["origin"]);
}
else {
res.setHeader("Access-Control-Allow-Origin", "*");
}
}
function writeResponse(res, httpCode, body) {
res.statusCode = httpCode;
res.end(body);
}
function sendInvalidURLResponse(res) {
return writeResponse(res, 404, "url must be in the form of /{some_url_here}");
}
// function sendTooBigResponse(res) {
// return writeResponse(res, 413, "the content in the request or response cannot exceed " + config.max_request_length + " characters.");
// }
function getClientAddress(req) {
return (req.headers['x-forwarded-for'] || '').split(',')[0] || req.connection.remoteAddress;
}
function processRequest(req, res) {
addCORSHeaders(req, res);
// Return options pre-flight requests right away
if (req.method.toUpperCase() === "OPTIONS") {
return writeResponse(res, 204);
}
var remoteURL = null;
try {
var matched = false;
for (var i = 0 ;i < config.forwards.length;i++){
var forwardConfig = config.forwards[i];
if(forwardConfig.check.test(req.url)){
server_log(req,"match forwards config ["+i+"]")
matched = true;
remoteURL = url.parse(decodeURI(forwardConfig.url + req.url.replace(forwardConfig.check,"")));
server_log(req,forwardConfig.check+ "->" + decodeURI(forwardConfig.url + req.url.replace(forwardConfig.check,"")));
break;
}
}
if(matched == false){
server_log(req,"forwards else config")
remoteURL = url.parse(decodeURI(config.else_forward_url + req.url));
}
}catch (e) {
return sendInvalidURLResponse(res);
}
// We don't support relative links
if (!remoteURL.host) {
return writeResponse(res, 404, "relative URLS are not supported");
}
// We only support http and https
if (remoteURL.protocol != "http:" && remoteURL.protocol !== "https:") {
return writeResponse(res, 400, "only http and https are supported");
}
if (publicIP) {
// Add an X-Forwarded-For header
if (req.headers["x-forwarded-for"]) {
req.headers["x-forwarded-for"] += ", " + publicIP;
}
else {
req.headers["x-forwarded-for"] = req.clientIP + ", " + publicIP;
}
}
// Make sure the host header is to the URL we're requesting, not thingproxy
if (req.headers["host"]) {
req.headers["host"] = remoteURL.host;
}
var proxyRequest = request({
url: remoteURL,
headers: req.headers,
method: req.method,
timeout: config.proxy_request_timeout_ms,
strictSSL: false
});
proxyRequest.on('error', function (err) {
//@ts-ignore
if (err.code === "ENOTFOUND") {
return writeResponse(res, 502, "Host for " + url.format(remoteURL) + " cannot be found.")
}
else {
server_log(req,"Proxy request Error:" + url.format(remoteURL));
return writeResponse(res, 500);
}
});
proxyRequest.on("end",function(err){
// Log our request
server_log(req,"Proxy request end");
});
var requestSize = 0;
var proxyResponseSize = 0;
req.pipe(proxyRequest).on('data', function (data) {
requestSize += data.length;
}).on('error', function(err){
writeResponse(res, 500, "Stream Error");
}).on("end",function(err){
// Log our request
server_log(req,"response end");
});
proxyRequest.pipe(res).on('data', function (data) {
proxyResponseSize += data.length;
}).on('error', function(err){
writeResponse(res, 500, "Stream Error");
});
}
http.createServer(function (req, res) {
// Process AWS health checks
if (req.url === "/health") {
return writeResponse(res, 200);
}
var clientIP = getClientAddress(req);
//@ts-ignore
req.clientIP = clientIP;
// Log our request
server_log(req,"request");
processRequest(req, res);
}).listen(config.port);
console.log("ls.requester process started (PID " + process.pid + ")");