const express = require('express'); const proxy = require('express-http-proxy'); const app = express(); const bodyParser = require('body-parser'); const targetUrl = 'https://openrouter.ai'; const openaiKey = process.env.OPENAI_KEY; const proxyKey = process.env.PROXY_KEY; // Your secret proxy key const port = 7860; const baseUrl = getExternalUrl(process.env.SPACE_ID); app.use(bodyParser.json({ limit: '50mb' })); // Middleware to log request details app.use((req, res, next) => { console.log(`Incoming Request - Method: ${req.method}, URL: ${req.url}`); console.log('Headers:', req.headers); console.log('Body:', req.body); next(); }); // Middleware to authenticate requests with the proxy key and check the model function authenticateProxyKeyAndModel(req, res, next) { const providedKey = req.headers['auro']; // Assuming the key is sent in the 'x-proxy-key' header const requestedModel = req.body.model; // List of allowed models const allowedModels = ['gryphe/mythomist-7b', 'gryphe/mythomax-l2-13b']; if (providedKey && providedKey === proxyKey && allowedModels.includes(requestedModel)) { // If the provided key matches the expected key and the requested model is allowed, allow the request to proceed next(); } else { // If the key is missing or incorrect, or the model is not allowed, reject the request with an error response res.status(401).json({ error: 'Unauthorized or invalid model' }); } } app.use('/api', authenticateProxyKeyAndModel, proxy(targetUrl, { proxyReqPathResolver: (req) => '/api/v1/chat/completions', proxyReqOptDecorator: (proxyReqOpts, srcReq) => { // Modify the request headers if necessary proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey; return proxyReqOpts; }, })); app.get("/", (req, res) => { // res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}`); }); function getExternalUrl(spaceId) { try { const [username, spacename] = spaceId.split("/"); return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`; } catch (e) { return ""; } } app.listen(port, () => { console.log(`Reverse proxy server running on ${baseUrl}`); });