feat proxy

This commit is contained in:
CyberL1 2025-01-07 12:12:57 -05:00
parent 2d658b55b5
commit 4d661c073c
3 changed files with 43 additions and 0 deletions

View File

@ -1,2 +1,5 @@
PORT=3000 PORT=3000
HOST="0.0.0.0" HOST="0.0.0.0"
PROXY_DOMAIN="localhost"
PROXY_PORT=3001

View File

@ -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
View 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)}`,
);