From c7fa05f224db94d17dd8dca73a8667f3ae94e69f Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Wed, 24 Sep 2025 09:54:33 -0700 Subject: [PATCH] basic auth --- README.md | 14 +++++++++++++- package.json | 3 ++- src/server.tsx | 28 +++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8d9b10d..60e94bb 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,15 @@ # 👟 sneaker -don't ask \ No newline at end of file +don't ask + +## basic auth + +users: + +- corey +- defunkt +- spike + +password: + +- 888 \ No newline at end of file diff --git a/package.json b/package.json index c61d658..5629dd4 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "private": true, "scripts": { "dev": "bun --hot src/server.tsx", + "prod": "env NODE_ENV=production bun src/server.tsx", "start": "bun run src/server.tsx" }, "dependencies": { @@ -17,4 +18,4 @@ "peerDependencies": { "typescript": "^5" } -} +} \ No newline at end of file diff --git a/src/server.tsx b/src/server.tsx index a2c9122..1e97b87 100644 --- a/src/server.tsx +++ b/src/server.tsx @@ -1,6 +1,7 @@ import { Hono } from "hono" import { upgradeWebSocket, websocket } from "hono/bun" -import { uniqueNamesGenerator, adjectives, animals, colors } from "unique-names-generator" +import { basicAuth } from "hono/basic-auth" +import { uniqueNamesGenerator, adjectives, animals } from "unique-names-generator" type Request = { id: string @@ -30,6 +31,31 @@ const app = new Hono app.get("/health", c => c.text("ok")) +const users = ["corey", "defunkt", "spike"] +const password = "888" + +if (process.env.NODE_ENV === "production") { + app.use(basicAuth({ + verifyUser: (username, pass) => + users.includes(username) && password === pass + })) +} + +app.get("/tunnels", c => { + return c.html() +}) + +app.get("/tunnel/:app", c => { + const url = new URL(c.req.url) + const port = url.port === "80" ? "" : `:${url.port}` + const hostname = url.hostname + return c.redirect(`http://${c.req.param("app")}.${hostname}${port}`) +}) + app.get("/tunnel", c => { const app = c.req.query("app") if (!app)