basic auth

This commit is contained in:
Chris Wanstrath 2025-09-24 09:54:33 -07:00
parent 5948263c6d
commit c7fa05f224
3 changed files with 42 additions and 3 deletions

View File

@ -1,3 +1,15 @@
# 👟 sneaker
don't ask
## basic auth
users:
- corey
- defunkt
- spike
password:
- 888

View File

@ -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": {

View File

@ -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(<ul>
{Object.keys(connections).map(key =>
<li><a href={`/tunnel/${key}`}>{key}</a> ({connections[key]?.app})</li>
)}
</ul>)
})
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)