diff --git a/compose.yml b/compose.yml index 8bf3d14..0661d14 100644 --- a/compose.yml +++ b/compose.yml @@ -5,6 +5,13 @@ services: build: . ports: - 3000:3000 - - 3001:3001 volumes: - /var/run/docker.sock:/var/run/docker.sock:ro + proxy: + image: openresty/openresty:alpine-fat + env_file: .env + ports: + - 80:80 + volumes: + - ./proxy.conf:/etc/nginx/conf.d/proxy.conf + network_mode: host diff --git a/proxy.conf b/proxy.conf new file mode 100644 index 0000000..75d21f9 --- /dev/null +++ b/proxy.conf @@ -0,0 +1,36 @@ +server { + listen 80; + listen [::]:80; + + server_name code-containers.localhost; + + location / { + proxy_pass http://127.0.0.1:3000; + } +} + +server { + listen 80; + listen [::]:80; + + server_name "~^(port-(?.*)\.)?(?.*)\.code-containers.localhost"; + + location / { + if ($port = "") { + set $port "80"; + } + + set $ip ''; + rewrite_by_lua_block { + local shell = require("resty.shell") + + local ok, stdout, strerr, reason, status = shell.run("curl http://127.0.0.1:3000/containers/" .. ngx.var.container .. "/ip") + ngx.var.ip = stdout + } + + proxy_pass http://$ip:$port; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + } +} diff --git a/src/index.ts b/src/index.ts index 900116b..a933196 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,6 @@ import "dotenv/config"; import fastify from "fastify"; import { readdirSync } from "fs"; -import "./proxy.ts"; const app = fastify(); diff --git a/src/proxy.ts b/src/proxy.ts deleted file mode 100644 index 95cd859..0000000 --- a/src/proxy.ts +++ /dev/null @@ -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)}`, -); diff --git a/src/routes/containers/_id/ip.ts b/src/routes/containers/_id/ip.ts new file mode 100644 index 0000000..291a167 --- /dev/null +++ b/src/routes/containers/_id/ip.ts @@ -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; + }); +};