Update server.js
Browse files
server.js
CHANGED
@@ -11,44 +11,44 @@ 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 |
-
//
|
15 |
-
if (req.method === 'POST' && req.url
|
16 |
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
17 |
-
|
18 |
|
19 |
-
if (
|
20 |
-
|
21 |
-
return res.status(429).send('You have reached the 10 message limit.
|
22 |
}
|
23 |
|
24 |
-
|
25 |
-
ipRequestCounts[ip] = currentCount + 1;
|
26 |
}
|
27 |
|
28 |
-
//
|
29 |
const timestamp = new Date().toISOString();
|
30 |
const requestData = {
|
31 |
-
requestNumber: ipRequestCounts[req.connection.remoteAddress] || 0,
|
32 |
ip: req.connection.remoteAddress,
|
33 |
timestamp: timestamp,
|
34 |
text: req.method + ' ' + req.url
|
35 |
};
|
36 |
-
|
37 |
requestDetails.push(requestData);
|
38 |
console.log(`Request ${requestData.requestNumber} on ${requestData.timestamp} from IP ${requestData.ip} with method "${req.method}" and text "${requestData.text}"`);
|
39 |
|
|
|
40 |
next();
|
41 |
}, proxy(targetUrl, {
|
42 |
-
proxyReqOptDecorator: (proxyReqOpts, srcReq)
|
|
|
43 |
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
44 |
return proxyReqOpts;
|
45 |
-
}
|
46 |
}));
|
47 |
|
48 |
app.get("/", (req, res) => {
|
49 |
let requestsInfo = requestDetails.map(detail =>
|
50 |
`Request ${detail.requestNumber} on ${detail.timestamp} from IP ${detail.ip} with text "${detail.text}"`).join('<br>');
|
51 |
-
res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}.<br><br>${requestsInfo}`);
|
52 |
});
|
53 |
|
54 |
function getExternalUrl(spaceId) {
|
@@ -56,10 +56,10 @@ function getExternalUrl(spaceId) {
|
|
56 |
const [username, spacename] = spaceId.split("/");
|
57 |
return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
|
58 |
} catch (e) {
|
59 |
-
return "";
|
60 |
}
|
61 |
}
|
62 |
|
63 |
app.listen(port, () => {
|
64 |
-
console.log(`Reverse proxy server running on ${baseUrl}
|
65 |
});
|
|
|
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 |
+
}
|
46 |
}));
|
47 |
|
48 |
app.get("/", (req, res) => {
|
49 |
let requestsInfo = requestDetails.map(detail =>
|
50 |
`Request ${detail.requestNumber} on ${detail.timestamp} from IP ${detail.ip} with text "${detail.text}"`).join('<br>');
|
51 |
+
res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}.<br><br>Requests Overview:<br>${requestsInfo}`);
|
52 |
});
|
53 |
|
54 |
function getExternalUrl(spaceId) {
|
|
|
56 |
const [username, spacename] = spaceId.split("/");
|
57 |
return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
|
58 |
} catch (e) {
|
59 |
+
return "Error generating external URL";
|
60 |
}
|
61 |
}
|
62 |
|
63 |
app.listen(port, () => {
|
64 |
+
console.log(`Reverse proxy server running on ${baseUrl}`);
|
65 |
});
|