File size: 2,444 Bytes
2889205
 
 
7f84064
2889205
7f84064
2889205
177a238
2889205
65e323a
 
7f84064
 
315c31e
 
38265f9
315c31e
65e323a
315c31e
 
 
38265f9
65e323a
315c31e
38265f9
65e323a
315c31e
65e323a
 
315c31e
38265f9
65e323a
 
 
 
 
 
315c31e
65e323a
 
315c31e
 
65e323a
 
315c31e
65e323a
3e0d39c
177a238
65e323a
 
315c31e
3e0d39c
 
177a238
65e323a
d5b575b
 
65e323a
315c31e
65e323a
2889205
 
 
315c31e
3e0d39c
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
53
54
55
56
57
58
59
60
61
62
63
64
65
const express = require('express');
const proxy = require('express-http-proxy');
const app = express();

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

app.use('/api', (req, res, next) => {
  // Continue only if it's a POST to /v1/chat/completions
  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;

    if (ipRequestCounts[ip] > 10) {
      console.log(`IP ${ip} hit rate limit with ${ipRequestCounts[ip]} requests.`);
      return res.status(429).send('You have reached the 10 message limit. Please contact support for more information.');
    }

    console.log(`IP ${ip} has made ${ipRequestCounts[ip]} requests so far.`);
  }

  // Log request details
  const timestamp = new Date().toISOString();
  const requestData = {
    requestNumber: ipRequestCounts[req.connection.remoteAddress] || 0,
    ip: req.connection.remoteAddress,
    timestamp: timestamp,
    text: req.method + ' ' + req.url
  };
  requestDetails.push(requestData);
  console.log(`Request ${requestData.requestNumber} on ${requestData.timestamp} from IP ${requestData.ip} with method "${req.method}" and text "${requestData.text}"`);

  // Process the request as usual
  next();
}, proxy(targetUrl, {
  proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
    // Set authorization headers for OpenAI API
    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('<br>');
  res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}.<br><br>Requests Overview:<br>${requestsInfo}`);
});

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";
  }
}

app.listen(port, () => {
  console.log(`Reverse proxy server running on ${baseUrl}`);
});