mirror of
https://github.com/CyberL1/dlinux-dashboard.git
synced 2025-06-28 16:19:43 -04:00
use nginx for proxy
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
import "dotenv/config";
|
||||
import fastify from "fastify";
|
||||
import { readdirSync } from "fs";
|
||||
import "./proxy.ts";
|
||||
|
||||
const app = fastify();
|
||||
|
||||
|
41
src/proxy.ts
41
src/proxy.ts
@ -1,41 +0,0 @@
|
||||
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, reply) => {
|
||||
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();
|
||||
reply.header("content-type", proxyRequest.headers.get("content-type"));
|
||||
|
||||
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)}`,
|
||||
);
|
12
src/routes/containers/_id/ip.ts
Normal file
12
src/routes/containers/_id/ip.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import type { Container } from "#src/types/Container.ts";
|
||||
import { getContainer } from "#src/utils/containers.ts";
|
||||
import type { FastifyInstance, FastifyRequest } from "fastify";
|
||||
|
||||
export default (fastify: FastifyInstance) => {
|
||||
fastify.get("/", async (req: FastifyRequest<{ Params: Container }>) => {
|
||||
const container = getContainer(req.params.id);
|
||||
|
||||
const { NetworkSettings } = await container.inspect();
|
||||
return NetworkSettings.IPAddress;
|
||||
});
|
||||
};
|
Reference in New Issue
Block a user