Update server.js
Browse files
server.js
CHANGED
|
@@ -9,66 +9,55 @@ const server = http.createServer(app);
|
|
| 9 |
const io = socketIo(server);
|
| 10 |
|
| 11 |
const targetUrl = 'https://api.openai.com';
|
| 12 |
-
const
|
| 13 |
-
let currentKeyIndex = 0;
|
| 14 |
const port = 7860;
|
| 15 |
const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
| 16 |
|
| 17 |
-
let requestDetails = [];
|
| 18 |
-
let ipRequestCounts = {};
|
| 19 |
|
| 20 |
io.on('connection', (socket) => {
|
| 21 |
console.log('A user connected to the websocket for live logs.');
|
| 22 |
});
|
| 23 |
|
| 24 |
function logAndEmit(message) {
|
| 25 |
-
console.log(message);
|
| 26 |
-
io.emit('log', message);
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
function getNextKey() {
|
| 30 |
-
// This function cycles through the keys in a round-robin manner
|
| 31 |
-
const key = openaiKeys[currentKeyIndex];
|
| 32 |
-
currentKeyIndex = (currentKeyIndex + 1) % openaiKeys.length;
|
| 33 |
-
return key;
|
| 34 |
}
|
| 35 |
|
| 36 |
app.use('/api', (req, res, next) => {
|
|
|
|
| 37 |
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
| 38 |
ipRequestCounts[ip] = (ipRequestCounts[ip] || 0) + 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
}, proxy(targetUrl, {
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
userResDecorator: function(proxyRes, proxyResData, userReq, userRes) {
|
| 59 |
-
// Check response for indication of invalid key
|
| 60 |
-
if (proxyRes.statusCode === 401 || proxyRes.statusCode === 403) {
|
| 61 |
-
logAndEmit(`Error detected with API key, switching keys...`);
|
| 62 |
-
getNextKey(); // Immediately get next key to avoid using the same failed key
|
| 63 |
-
}
|
| 64 |
-
return proxyResData; // Pass through the original response data
|
| 65 |
-
}
|
| 66 |
}));
|
| 67 |
|
| 68 |
app.get("/", (req, res) => {
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
});
|
| 73 |
|
| 74 |
app.get('/logs', (req, res) => {
|
|
@@ -86,4 +75,4 @@ function getExternalUrl(spaceId) {
|
|
| 86 |
|
| 87 |
server.listen(port, () => {
|
| 88 |
logAndEmit(`Reverse proxy server and WebSocket running on port ${port}`);
|
| 89 |
-
});
|
|
|
|
| 9 |
const io = socketIo(server);
|
| 10 |
|
| 11 |
const targetUrl = 'https://api.openai.com';
|
| 12 |
+
const openaiKey = process.env.OPENAI_KEY;
|
|
|
|
| 13 |
const port = 7860;
|
| 14 |
const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
| 15 |
|
| 16 |
+
let requestDetails = []; // Store details of each request
|
| 17 |
+
let ipRequestCounts = {}; // Dictionary to keep track of requests per IP
|
| 18 |
|
| 19 |
io.on('connection', (socket) => {
|
| 20 |
console.log('A user connected to the websocket for live logs.');
|
| 21 |
});
|
| 22 |
|
| 23 |
function logAndEmit(message) {
|
| 24 |
+
console.log(message); // Continue to log to the console
|
| 25 |
+
io.emit('log', message); // Emit this log to the frontend via WebSocket
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
app.use('/api', (req, res, next) => {
|
| 29 |
+
if (req.method === 'POST' && req.url.startsWith('/v1/chat/completions')) {
|
| 30 |
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
| 31 |
ipRequestCounts[ip] = (ipRequestCounts[ip] || 0) + 1;
|
| 32 |
+
|
| 33 |
+
const logMessage = `IP ${ip} has made ${ipRequestCounts[ip]} requests so far.`;
|
| 34 |
+
logAndEmit(logMessage);
|
| 35 |
+
}
|
| 36 |
|
| 37 |
+
const timestamp = moment().tz("Asia/Tbilisi").format("DD-MMM-YYYY HH:mm");
|
| 38 |
+
const requestData = {
|
| 39 |
+
requestNumber: ipRequestCounts[req.headers['x-forwarded-for'] || req.connection.remoteAddress],
|
| 40 |
+
ip: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
|
| 41 |
+
timestamp: timestamp,
|
| 42 |
+
text: req.method + ' ' + req.url
|
| 43 |
+
};
|
| 44 |
+
requestDetails.push(requestData);
|
| 45 |
+
|
| 46 |
+
const detailedLog = `Request ${requestData.requestNumber} on ${requestData.timestamp} from IP ${requestData.ip} with method "${req.method}" and text "${requestData.text}"`;
|
| 47 |
+
logAndEmit(detailedLog);
|
| 48 |
|
| 49 |
+
next();
|
| 50 |
}, proxy(targetUrl, {
|
| 51 |
+
proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
|
| 52 |
+
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
| 53 |
+
return proxyReqOpts;
|
| 54 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
}));
|
| 56 |
|
| 57 |
app.get("/", (req, res) => {
|
| 58 |
+
let requestsInfo = requestDetails.map(detail =>
|
| 59 |
+
`Request ${detail.requestNumber} on ${detail.timestamp} from IP ${detail.ip} with text "${detail.text}"`).join('<br>');
|
| 60 |
+
res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}.<br><br>Requests Overview:<br>${requestsInfo}`);
|
| 61 |
});
|
| 62 |
|
| 63 |
app.get('/logs', (req, res) => {
|
|
|
|
| 75 |
|
| 76 |
server.listen(port, () => {
|
| 77 |
logAndEmit(`Reverse proxy server and WebSocket running on port ${port}`);
|
| 78 |
+
});
|