adds info page
This commit is contained in:
parent
6ba5670f29
commit
e2509d087c
|
@ -2,7 +2,7 @@ import type { Request, Response, NextFunction } from "express";
|
||||||
|
|
||||||
const PROXY_KEY = process.env.PROXY_KEY;
|
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) {
|
if (!PROXY_KEY) {
|
||||||
next();
|
next();
|
||||||
return;
|
return;
|
||||||
|
@ -13,4 +13,4 @@ export function auth(req: Request, res: Response, next: NextFunction) {
|
||||||
} else {
|
} else {
|
||||||
res.status(401).json({ error: "Unauthorized" });
|
res.status(401).json({ error: "Unauthorized" });
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
|
@ -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 `<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<title>OpenAI Reverse Proxy</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>OpenAI Reverse Proxy</h1>
|
||||||
|
<pre>${JSON.stringify(info, null, 2)}</pre>
|
||||||
|
</body>
|
||||||
|
</html>`;
|
||||||
|
}
|
|
@ -61,6 +61,8 @@ function init() {
|
||||||
.digest("hex")
|
.digest("hex")
|
||||||
.slice(0, 6),
|
.slice(0, 6),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// TODO: check each key's usage upon startup.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
src/proxy.ts
11
src/proxy.ts
|
@ -6,20 +6,13 @@ translated into OpenAI requests. */
|
||||||
|
|
||||||
import * as express from "express";
|
import * as express from "express";
|
||||||
import { auth } from "./auth";
|
import { auth } from "./auth";
|
||||||
|
import { handleInfoPage } from "./info-page";
|
||||||
import { kobold } from "./kobold";
|
import { kobold } from "./kobold";
|
||||||
import { openai } from "./openai";
|
import { openai } from "./openai";
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.get("/", (req, res) => {
|
router.get("/", handleInfoPage);
|
||||||
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.use(auth);
|
router.use(auth);
|
||||||
router.use("/kobold", kobold);
|
router.use("/kobold", kobold);
|
||||||
router.use("/openai", openai);
|
router.use("/openai", openai);
|
||||||
|
|
Loading…
Reference in New Issue