mirror of
https://github.com/CyberL1/dlinux-dashboard.git
synced 2025-01-22 01:19:18 -05:00
feat proxy
This commit is contained in:
parent
2d658b55b5
commit
4d661c073c
@ -1,2 +1,5 @@
|
|||||||
PORT=3000
|
PORT=3000
|
||||||
HOST="0.0.0.0"
|
HOST="0.0.0.0"
|
||||||
|
|
||||||
|
PROXY_DOMAIN="localhost"
|
||||||
|
PROXY_PORT=3001
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import "dotenv/config";
|
import "dotenv/config";
|
||||||
import fastify from "fastify";
|
import fastify from "fastify";
|
||||||
import { readdirSync } from "fs";
|
import { readdirSync } from "fs";
|
||||||
|
import "./proxy.ts";
|
||||||
|
|
||||||
const app = fastify();
|
const app = fastify();
|
||||||
|
|
||||||
|
39
src/proxy.ts
Normal file
39
src/proxy.ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
import fastify from "fastify";
|
||||||
|
import { getContainer } from "./utils/containers.ts";
|
||||||
|
|
||||||
|
const app = fastify();
|
||||||
|
|
||||||
|
app.all(
|
||||||
|
"/*",
|
||||||
|
{ constraints: { host: new RegExp(`.*\.${process.env.PROXY_DOMAIN}`) } },
|
||||||
|
async (req) => {
|
||||||
|
const subdomains = req.headers.host.split(".");
|
||||||
|
|
||||||
|
const port = subdomains[0].startsWith("port-")
|
||||||
|
? subdomains[0].slice(5)
|
||||||
|
: 80;
|
||||||
|
|
||||||
|
const containerName = subdomains[subdomains[0].startsWith("port-") ? 1 : 0];
|
||||||
|
|
||||||
|
const { NetworkSettings } = await getContainer(containerName).inspect();
|
||||||
|
const { IPAddress: ip } = NetworkSettings;
|
||||||
|
|
||||||
|
const proxyRequest = await fetch(`http://${ip}:${port}${req.url}`, {
|
||||||
|
headers: req.headers,
|
||||||
|
method: req.method,
|
||||||
|
});
|
||||||
|
|
||||||
|
const proxyResponse = await proxyRequest.text();
|
||||||
|
return proxyResponse;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
await app.listen({
|
||||||
|
port: Number(process.env.PROXY_PORT),
|
||||||
|
host: process.env.HOST,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
"Proxy ready on",
|
||||||
|
`http://${process.env.HOST}:${Number(process.env.PROXY_PORT)}`,
|
||||||
|
);
|
Loading…
x
Reference in New Issue
Block a user