-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
186 lines (168 loc) · 6.67 KB
/
app.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
const express = require('express');
const http = require('http');
const dns = require('dns');
const app = express();
const port = 3000;
const forwardEndpoints = process.env.TARGET_ADDITIONAL_FORWARD_ENDPOINTS || '';
const parseEndpoints = (endpointsStr) => {
const endpoints = [];
if (endpointsStr) {
endpointsStr.split(',').forEach((ep) => {
const lastSemicolonIndex = ep.lastIndexOf(';');
const url = ep.substring(0, lastSemicolonIndex);
const method = ep.substring(lastSemicolonIndex + 1).toUpperCase();
const parsedUrl = new URL(url);
endpoints.push({
hostname: parsedUrl.hostname,
port: parsedUrl.port || 80,
path: parsedUrl.pathname + parsedUrl.search,
method,
});
});
}
return endpoints;
};
const endpointsList = parseEndpoints(forwardEndpoints);
app.get('/health', (req, res, next) => {
const target = process.env.TARGET_HOSTS_DNS_NAME;
if (target === undefined) {
return next(new Error("Environment variable TARGET_HOSTS_DNS_NAME is not set"));
}
dns.lookup(target, { all: true }, (err, addresses) => {
if (addresses === undefined) {
return next(new Error("Unable to resolve hostname '" + target + "'."));
}
res.send(addresses);
});
});
app.all('*', (req, res, next) => {
dns.lookup(process.env.TARGET_HOSTS_DNS_NAME, { all: true }, (err, addresses) => {
if (addresses === undefined) {
return next(new Error("Unable to resolve hostname '" + process.env.TARGET_HOSTS_DNS_NAME + "'."));
}
const promises = [];
addresses.forEach((address) => {
promises.push(new Promise((resolve, reject) => {
if (req.headers.host !== undefined) {
delete req.headers.host;
}
const options = {
hostname: address.address,
port: process.env.TARGET_HOSTS_PORT,
path: req.url,
method: req.method,
headers: req.headers,
};
console.log(
"Sending %s request to http://%s:%d%s with headers: %s",
options.method,
options.hostname,
options.port,
options.path,
JSON.stringify(options.headers)
);
http
.request(options, (resp) => {
const chunks = [];
resp.on('data', (data) => chunks.push(data));
resp.on('end', () => {
let resBody = Buffer.concat(chunks);
switch (resp.headers['content-type']) {
case 'application/json':
resBody = JSON.parse(resBody);
break;
}
console.log(
"Got response for call: http://%s:%d%s -> %d: %s",
options.hostname,
options.port,
options.path,
resp.statusCode,
resp.statusMessage
);
resolve({
...options,
statusCode: resp.statusCode,
statusMessage: resp.statusMessage,
body: resBody.toString(),
});
});
})
.on('error', (err) => {
console.log('Error: ' + err.message);
resolve({
...options,
err: err,
});
})
.end();
}));
});
endpointsList.forEach((endpoint) => {
promises.push(new Promise((resolve, reject) => {
const options = {
hostname: endpoint.hostname,
port: endpoint.port,
path: endpoint.path,
method: endpoint.method,
headers: req.headers,
insecureHTTPParser: true
};
console.log(
"Sending %s request to http://%s:%d%s with headers: %s",
options.method,
options.hostname,
options.port,
options.path,
JSON.stringify(options.headers)
);
const reqForward = http.request(options, (resp) => {
const chunks = [];
resp.on('data', (data) => chunks.push(data));
resp.on('end', () => {
let resBody = Buffer.concat(chunks);
switch (resp.headers['content-type']) {
case 'application/json':
resBody = JSON.parse(resBody);
break;
}
console.log(
"Got response for call: http://%s:%d%s -> %d: %s",
options.hostname,
options.port,
options.path,
resp.statusCode,
resp.statusMessage
);
resolve({
...options,
statusCode: resp.statusCode,
statusMessage: resp.statusMessage,
body: resBody.toString(),
});
});
});
reqForward.on('error', (err) => {
console.log('Error: ' + err.message);
resolve({
...options,
err: err,
});
});
if (req.body) {
reqForward.write(JSON.stringify(req.body));
}
reqForward.end();
}));
});
Promise.all(promises)
.then((data) => {
console.log(data);
res.send(data);
})
.catch((err) => res.status(400).send(err));
});
});
app.listen(port, '0.0.0.0', () => {
console.log(`Listening on port ${port}...`);
});