Update server.js
Browse files
server.js
CHANGED
@@ -1,62 +1,60 @@
|
|
1 |
const express = require('express');
|
2 |
const proxy = require('express-http-proxy');
|
3 |
-
const bodyParser = require('body-parser');
|
4 |
const app = express();
|
5 |
|
6 |
const targetUrl = 'https://api.openai.com';
|
7 |
const openaiKey = process.env.OPENAI_KEY;
|
8 |
-
const adminPassword = 'securepassword'; // Never use hard-coded sensitive passwords in production
|
9 |
const port = 7860;
|
|
|
10 |
|
11 |
-
|
|
|
12 |
|
13 |
-
// In-memory configuration "database"
|
14 |
-
let ipConfigurations = {};
|
15 |
-
|
16 |
-
// Rate limit middleware
|
17 |
app.use('/api', (req, res, next) => {
|
18 |
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
19 |
-
const
|
20 |
-
|
21 |
-
if (
|
22 |
-
return res.status(429).
|
23 |
-
error: true,
|
24 |
-
message: 'You have reached your maximum request limit.',
|
25 |
-
nextAvailableRequestTime: getNextAvailableRequestTime(config)
|
26 |
-
});
|
27 |
}
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
next();
|
34 |
}, proxy(targetUrl, {
|
35 |
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
36 |
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
37 |
return proxyReqOpts;
|
38 |
},
|
39 |
-
|
40 |
-
|
41 |
-
//
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
if (password !== adminPassword) {
|
46 |
-
return res.status(403).send("Unauthorized");
|
47 |
}
|
|
|
48 |
|
49 |
-
|
50 |
-
|
|
|
|
|
51 |
});
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
return resetTime.toISOString();
|
57 |
}
|
58 |
|
59 |
-
// Start the express server
|
60 |
app.listen(port, () => {
|
61 |
-
console.log(`Reverse proxy server running on
|
62 |
});
|
|
|
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;
|
|
|
7 |
const port = 7860;
|
8 |
+
const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
9 |
|
10 |
+
let requestDetails = [];
|
11 |
+
let ipRequestCounts = {};
|
12 |
|
|
|
|
|
|
|
|
|
13 |
app.use('/api', (req, res, next) => {
|
14 |
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
15 |
+
const currentCount = ipRequestCounts[ip] || 0;
|
16 |
+
|
17 |
+
if (currentCount >= 2) {
|
18 |
+
return res.status(429).send('You have reached your request limit of 10 messages. Please contact support for more details.');
|
|
|
|
|
|
|
|
|
19 |
}
|
20 |
|
21 |
+
ipRequestCounts[ip] = currentCount + 1;
|
22 |
+
const timestamp = new Date().toISOString();
|
23 |
+
const requestData = {
|
24 |
+
requestNumber: ipRequestCounts[ip],
|
25 |
+
ip: ip,
|
26 |
+
timestamp: timestamp,
|
27 |
+
text: req.method + ' ' + req.url
|
28 |
+
};
|
29 |
+
requestDetails.push(requestData);
|
30 |
+
console.log(`Request ${requestData.requestNumber} from IP ${requestData.ip} on ${requestData.timestamp}`);
|
31 |
+
|
32 |
next();
|
33 |
}, proxy(targetUrl, {
|
34 |
proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
|
35 |
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
36 |
return proxyReqOpts;
|
37 |
},
|
38 |
+
userResDecorator: (proxyRes, proxyResData, userReq, userRes) => {
|
39 |
+
if (proxyRes.statusCode === 429) {
|
40 |
+
// Return a custom message for the 429 error from the proxy attempt
|
41 |
+
return Buffer.from('Request limit reached on the proxied service. Please try again later.');
|
42 |
+
}
|
43 |
+
return proxyResData; // Return original data if response is not 429
|
|
|
|
|
44 |
}
|
45 |
+
}));
|
46 |
|
47 |
+
app.get("/", (req, res) => {
|
48 |
+
let requestsInfo = requestDetails.map(detail =>
|
49 |
+
`Request ${detail.requestNumber} from IP ${detail.ip} on ${detail.timestamp}`).join('<br>');
|
50 |
+
res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}.<br><br>${requestsInfo}`);
|
51 |
});
|
52 |
|
53 |
+
function getExternalUrl(spaceId) {
|
54 |
+
const [username, spacename] = spaceId.split("/");
|
55 |
+
return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
|
|
|
56 |
}
|
57 |
|
|
|
58 |
app.listen(port, () => {
|
59 |
+
console.log(`Reverse proxy server running on ${baseUrl}.`);
|
60 |
});
|