File size: 1,660 Bytes
f81f327
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Request, Response } from "express";
import showdown from "showdown";
import { keys } from "./keys";

export const handleInfoPage = (req: Request, res: Response) => {
  // Huggingface puts spaces behind some cloudflare ssl proxy, so `req.protocol` is `http` but the correct URL is actually `https`
  const host = req.get("host");
  const isHuggingface = host?.includes("hf.space");
  const protocol = isHuggingface ? "https" : req.protocol;
  res.send(getInfoPageHtml(protocol + "://" + host));
};

function getInfoPageHtml(host: string) {
  const keylist = keys.list();
  const info = {
    message: "OpenAI Reverse Proxy",
    uptime: process.uptime(),
    timestamp: Date.now(),
    baseUrl: host,
    kobold: host + "/proxy/kobold" + " (not yet implemented)",
    openai: host + "/proxy/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,
      proompts: keylist.reduce((acc, k) => acc + k.promptCount, 0),
    },
  };

  const readme = require("fs").readFileSync("README.md", "utf8");
  const readmeBody = readme.split("---")[2];
  const converter = new showdown.Converter();
  const html = converter.makeHtml(readmeBody);

  const pageBody = `<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>OpenAI Reverse Proxy</title>
  </head>
  <body style="font-family: sans-serif; background-color: #f0f0f0; padding: 1em;"
    ${html}
    <hr />
    <h2>Service Info</h2>
    <pre>${JSON.stringify(info, null, 2)}</pre>
  </body>
</html>`;

  return pageBody;
}