const express = require('express'); const http = require('http'); const proxy = require('express-http-proxy'); const socketIo = require('socket.io'); const moment = require('moment-timezone'); const app = express(); const server = http.createServer(app); const io = socketIo(server); const targetUrl = 'https://api.openai.com'; const openaiKey = process.env.OPENAI_KEY; const port = 7860; const baseUrl = getExternalUrl(process.env.SPACE_ID); let requestDetails = []; // Store details of each request let ipRequestCounts = {}; // Dictionary to keep track of requests per IP io.on('connection', (socket) => { console.log('A user connected to the websocket for live logs.'); }); function logAndEmit(message) { console.log(message); // Continue to log to the console io.emit('log', message); // Emit this log to the frontend via WebSocket } app.use('/api', (req, res, next) => { if (req.method === 'POST' && req.url.startsWith('/v1/chat/completions')) { const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; ipRequestCounts[ip] = (ipRequestCounts[ip] || 0) + 1; const logMessage = `IP ${ip} has made ${ipRequestCounts[ip]} requests so far.`; logAndEmit(logMessage); } const timestamp = moment().tz("Asia/Tbilisi").format("DD-MMM-YYYY HH:mm"); const requestData = { requestNumber: ipRequestCounts[req.headers['x-forwarded-for'] || req.connection.remoteAddress], ip: req.headers['x-forwarded-for'] || req.connection.remoteAddress, timestamp: timestamp, text: req.method + ' ' + req.url }; requestDetails.push(requestData); const detailedLog = `Request ${requestData.requestNumber} on ${requestData.timestamp} from IP ${requestData.ip} with method "${req.method}" and text "${requestData.text}"`; logAndEmit(detailedLog); next(); }, proxy(targetUrl, { proxyReqOptDecorator: function(proxyReqOpts, srcReq) { proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey; return proxyReqOpts; } })); app.get("/", (req, res) => { let requestsInfo = requestDetails.map(detail => `Request ${detail.requestNumber} on ${detail.timestamp} from IP ${detail.ip} with text "${detail.text}"`).join('
'); res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}.

Requests Overview:
${requestsInfo}`); }); app.get('/logs', (req, res) => { res.sendFile(__dirname + '/index.html'); }); function getExternalUrl(spaceId) { try { const [username, spacename] = spaceId.split("/"); return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`; } catch (e) { return "Error generating external URL"; } } server.listen(port, () => { logAndEmit(`Reverse proxy server and WebSocket running on port ${port}`); });