-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (43 loc) · 1.13 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
import express from "express";
import axios from "axios";
import dns from "dns";
const app = express();
const port = 8080;
const getTargetParts = () => {
const target = process.env.TARGET;
if (target.includes(":")) {
const parts = target.split(":");
return { targetHostname: parts[0], targetPort: parts[1] };
} else {
return { targetHostname: target, targetPort: 80 };
}
};
const { targetHostname, targetPort } = getTargetParts();
app.all("/*", (req, res) => {
dns.lookup(
targetHostname,
{ family: 4, all: true },
async (err, addresses) => {
if (err) {
console.error(err);
}
await Promise.all(
addresses.map((address) => {
console.log(
`Forwarding request to http://${address.address}:${targetPort}${req.url}`
);
return axios.request({
method: req.method,
url: `http://${address.address}:${targetPort}${req.url}`,
headers: req.headers,
data: req.body,
});
})
);
}
);
res.send("Ok");
});
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});