diff --git a/src/auth.ts b/src/auth.ts index 743afef..f746241 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -2,7 +2,7 @@ import type { Request, Response, NextFunction } from "express"; const PROXY_KEY = process.env.PROXY_KEY; -export function auth(req: Request, res: Response, next: NextFunction) { +export const auth = (req: Request, res: Response, next: NextFunction) => { if (!PROXY_KEY) { next(); return; @@ -13,4 +13,4 @@ export function auth(req: Request, res: Response, next: NextFunction) { } else { res.status(401).json({ error: "Unauthorized" }); } -} +}; diff --git a/src/info-page.ts b/src/info-page.ts new file mode 100644 index 0000000..eb782e7 --- /dev/null +++ b/src/info-page.ts @@ -0,0 +1,35 @@ +import { Request, Response } from "express"; +import { keys } from "./keys"; + +export const handleInfoPage = (req: Request, res: Response) => { + res.send(getInfoPageHtml(req.protocol + "://" + req.get("host"))); +}; + +function getInfoPageHtml(host: string) { + const keylist = keys.list(); + const info = { + message: "OpenAI Reverse Proxy", + uptime: process.uptime(), + timestamp: Date.now(), + kobold: host + "/kobold", + openai: host + "/openai", + keys: { + all: keylist.length, + active: keylist.filter((k) => !k.isDisabled).length, + trial: keylist.filter((k) => k.isTrial).length, + gpt4: keylist.filter((k) => k.isGpt4).length, + }, + }; + + return ` + + + + OpenAI Reverse Proxy + + +

OpenAI Reverse Proxy

+
${JSON.stringify(info, null, 2)}
+ +`; +} diff --git a/src/keys.ts b/src/keys.ts index 44f1980..971cc06 100644 --- a/src/keys.ts +++ b/src/keys.ts @@ -61,6 +61,8 @@ function init() { .digest("hex") .slice(0, 6), }); + + // TODO: check each key's usage upon startup. } } diff --git a/src/proxy.ts b/src/proxy.ts index 700c055..cde56e7 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -6,20 +6,13 @@ translated into OpenAI requests. */ import * as express from "express"; import { auth } from "./auth"; +import { handleInfoPage } from "./info-page"; import { kobold } from "./kobold"; import { openai } from "./openai"; const router = express.Router(); -router.get("/", (req, res) => { - res.json({ - message: "OpenAI Reverse Proxy", - uptime: process.uptime(), - timestamp: Date.now(), - kobold: req.protocol + "://" + req.get("host") + "/kobold", - openai: req.protocol + "://" + req.get("host") + "/openai", - }); -}); +router.get("/", handleInfoPage); router.use(auth); router.use("/kobold", kobold); router.use("/openai", openai);