Ayeantics commited on
Commit
e4e9ad6
1 Parent(s): b09eec8

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +30 -12
server.js CHANGED
@@ -1,6 +1,11 @@
1
  const express = require('express');
 
2
  const proxy = require('express-http-proxy');
 
 
3
  const app = express();
 
 
4
 
5
  const targetUrl = 'https://api.openai.com';
6
  const openaiKey = process.env.OPENAI_KEY;
@@ -10,36 +15,45 @@ const baseUrl = getExternalUrl(process.env.SPACE_ID);
10
  let requestDetails = []; // Store details of each request
11
  let ipRequestCounts = {}; // Dictionary to keep track of requests per IP
12
 
 
 
 
 
 
 
 
 
 
13
  app.use('/api', (req, res, next) => {
14
- // Continue only if it's a POST to /v1/chat/completions
15
  if (req.method === 'POST' && req.url.startsWith('/v1/chat/completions')) {
16
  const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
17
  ipRequestCounts[ip] = (ipRequestCounts[ip] || 0) + 1;
18
 
19
  if (ipRequestCounts[ip] > 10) {
20
- console.log(`IP ${ip} hit rate limit with ${ipRequestCounts[ip]} requests.`);
 
21
  return res.status(429).send('You have reached the 10 message limit. Please contact support for more information.');
22
  }
23
-
24
- console.log(`IP ${ip} has made ${ipRequestCounts[ip]} requests so far.`);
 
25
  }
26
 
27
- // Log request details
28
  const timestamp = new Date().toISOString();
29
  const requestData = {
30
- requestNumber: ipRequestCounts[req.connection.remoteAddress] || 0,
31
- ip: req.connection.remoteAddress,
32
  timestamp: timestamp,
33
  text: req.method + ' ' + req.url
34
  };
35
  requestDetails.push(requestData);
36
- console.log(`Request ${requestData.requestNumber} on ${requestData.timestamp} from IP ${requestData.ip} with method "${req.method}" and text "${requestData.text}"`);
 
 
37
 
38
- // Process the request as usual
39
  next();
40
  }, proxy(targetUrl, {
41
  proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
42
- // Set authorization headers for OpenAI API
43
  proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
44
  return proxyReqOpts;
45
  }
@@ -51,6 +65,10 @@ app.get("/", (req, res) => {
51
  res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}.<br><br>Requests Overview:<br>${requestsInfo}`);
52
  });
53
 
 
 
 
 
54
  function getExternalUrl(spaceId) {
55
  try {
56
  const [username, spacename] = spaceId.split("/");
@@ -60,6 +78,6 @@ function getExternalUrl(spaceId) {
60
  }
61
  }
62
 
63
- app.listen(port, () => {
64
- console.log(`Reverse proxy server running on ${baseUrl}`);
65
  });
 
1
  const express = require('express');
2
+ const http = require('http');
3
  const proxy = require('express-http-proxy');
4
+ const socketIo = require('socket.io');
5
+
6
  const app = express();
7
+ const server = http.createServer(app);
8
+ const io = socketIo(server);
9
 
10
  const targetUrl = 'https://api.openai.com';
11
  const openaiKey = process.env.OPENAI_KEY;
 
15
  let requestDetails = []; // Store details of each request
16
  let ipRequestCounts = {}; // Dictionary to keep track of requests per IP
17
 
18
+ io.on('connection', (socket) => {
19
+ console.log('A user connected to the websocket for live logs.');
20
+ });
21
+
22
+ function logAndEmit(message) {
23
+ console.log(message); // Continue to log to the console
24
+ io.emit('log', message); // Emit this log to the frontend via WebSocket
25
+ }
26
+
27
  app.use('/api', (req, res, next) => {
 
28
  if (req.method === 'POST' && req.url.startsWith('/v1/chat/completions')) {
29
  const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
30
  ipRequestCounts[ip] = (ipRequestCounts[ip] || 0) + 1;
31
 
32
  if (ipRequestCounts[ip] > 10) {
33
+ const rateLimitMessage = `IP ${ip} hit rate limit with ${ipRequestCounts[ip]} requests.`;
34
+ logAndEmit(rateLimitMessage);
35
  return res.status(429).send('You have reached the 10 message limit. Please contact support for more information.');
36
  }
37
+
38
+ const logMessage = `IP ${ip} has made ${ipRequestCounts[ip]} requests so far.`;
39
+ logAndEmit(logMessage);
40
  }
41
 
 
42
  const timestamp = new Date().toISOString();
43
  const requestData = {
44
+ requestNumber: ipRequestCounts[req.headers['x-forwarded-for'] || req.connection.remoteAddress],
45
+ ip: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
46
  timestamp: timestamp,
47
  text: req.method + ' ' + req.url
48
  };
49
  requestDetails.push(requestData);
50
+
51
+ const detailedLog = `Request ${requestData.requestNumber} on ${requestData.timestamp} from IP ${requestData.ip} with method "${req.method}" and text "${requestData.text}"`;
52
+ logAndEmit(detailedLog);
53
 
 
54
  next();
55
  }, proxy(targetUrl, {
56
  proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
 
57
  proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
58
  return proxyReqOpts;
59
  }
 
65
  res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}.<br><br>Requests Overview:<br>${requestsInfo}`);
66
  });
67
 
68
+ app.get('/logs', (req, res) => {
69
+ res.sendFile(__dirname + '/index.html');
70
+ });
71
+
72
  function getExternalUrl(spaceId) {
73
  try {
74
  const [username, spacename] = spaceId.split("/");
 
78
  }
79
  }
80
 
81
+ server.listen(port, () => {
82
+ logAndEmit(`Reverse proxy server and WebSocket running on port ${port}`);
83
  });