import { Request, Response } from "express"; import * as textToImage from 'text-to-image'; import showdown from "showdown"; import { config, listConfig } from "./config"; import { keys } from "./keys"; import { proxies } from "./proxies"; import { captchas } from './proxy/captchas'; import { proxyKeys } from './proxy/proxy-keys'; import { getUniqueIps } from "./proxy/rate-limit"; export const handleInfoPage = async (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(await getInfoPageHtml(protocol + "://" + host, req.body)); }; const ms = require('ms'); async function getInfoPageHtml(host: string, body: any) { const keylist = keys.list(); const proxylist = proxies.list(); const rateLimitInfo = { proompters: getUniqueIps() }; const usage = keys.getUsage(); const now = Date.now(); const uptimeDays = Math.floor(process.uptime() / 86400); const info = { uptime: uptimeDays > 0 ? `${uptimeDays} день или дня или дней` : 'меньше дня', timestamp: new Date(now).toLocaleString('ru-RU', { timeZone: 'Europe/Moscow' }), api: host + "/proxy/openai/v1", proompts: keylist.reduce((acc, k) => acc + k.promptCount, 0) + proxylist.reduce((acc, k) => acc + k.promptCount, 0), ...(config.modelRateLimit ? rateLimitInfo : {}), 'gpt-3.5-turbo': { all: keylist.length, active: keylist.filter((k) => !k.isDisabled).length, trial: keylist.filter((k) => k.isTrial && !k.isDisabled).length, remaining: usage.gpt3.remaining, usage: `${usage.gpt3.usage.toFixed(2)}/${Math.round(usage.gpt3.limit)}`, }, 'gpt-4': { all: keylist.filter((k) => k.isGpt4).length, active: keylist.filter((k) => k.isGpt4 && !k.isDisabled).length, trial: keylist.filter((k) => k.isGpt4 && k.isTrial && !k.isDisabled).length, remaining: usage.gpt4.remaining, usage: `${usage.gpt4.usage.toFixed(2)}/${Math.round(usage.gpt4.limit)}`, }, config: listConfig(), }; const readme = require("fs").readFileSync("README.md", "utf8"); const readmeBody = readme.split("---")[2] || readme; const converter = new showdown.Converter(); const html = converter.makeHtml(readmeBody); let captchaHTML = ''; if (config.proxyCaptcha) { if (captchas.check(body?.key, body?.value)) { const key = proxyKeys.generate(); captchaHTML = `

Твой ключ от прокси - ${key}
Писать его в поле API Key в таверне и не забудь после этого нажать Connect.
Учти, если превысишь лимит в ${config.modelRateLimit} запросов в минуту - ключ превратится в тыкву и придётся идти за новым.

`; } else { const captcha = await captchas.generate(); captchaHTML = `

`; } } const pageBody = ` 2ch proxy.
${html}
Z
${captchaHTML}
${JSON.stringify(info, null, 2)}
V
`; return pageBody; }