Spaces:
Running
Running
Update server.js
Browse files
server.js
CHANGED
@@ -14,8 +14,9 @@ const port = 7860;
|
|
14 |
const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
15 |
|
16 |
let requestDetails = [];
|
17 |
-
let apiKeyDetails = {};
|
18 |
|
|
|
19 |
const allowedModels = [
|
20 |
'gpt-3.5-turbo-0125',
|
21 |
'gpt-3.5-turbo',
|
@@ -51,7 +52,7 @@ function authenticateApiKey(req, res, next) {
|
|
51 |
|
52 |
const validApiKeys = (process.env.SECRET_API_KEYS || '').split(',');
|
53 |
const apiKeyMatch = validApiKeys.find(key => `Bearer ${key.trim()}` === receivedApiKey);
|
54 |
-
|
55 |
if (!apiKeyMatch) {
|
56 |
return res.status(401).send('Unauthorized: API key is invalid');
|
57 |
}
|
@@ -70,46 +71,38 @@ function authenticateApiKey(req, res, next) {
|
|
70 |
}
|
71 |
|
72 |
apiKeyDetails[apiKeyMatch].count++;
|
|
|
73 |
const logMessage = `API Key ${displayKey}... used ${apiKeyDetails[apiKeyMatch].count} times so far.`;
|
74 |
logAndEmit(logMessage, true);
|
75 |
-
|
76 |
req.apiKey = apiKeyMatch;
|
77 |
next();
|
78 |
}
|
79 |
|
80 |
-
|
81 |
-
const
|
82 |
-
|
83 |
-
|
|
|
|
|
84 |
}
|
|
|
85 |
next();
|
86 |
-
}
|
|
|
|
|
|
|
|
|
|
|
87 |
proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
|
88 |
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
89 |
return proxyReqOpts;
|
90 |
}
|
91 |
}));
|
92 |
|
93 |
-
|
94 |
-
let requestsInfo = requestDetails.map(detail =>
|
95 |
-
`Request ${detail.requestNumber} on ${detail.timestamp} from API Key ${detail.apiKey}... with text "${detail.text}"`).join('<br>');
|
96 |
-
res.send(`This is your OpenAI Reverse Proxy URL: ${baseUrl}.<br><br>Requests Overview:<br>${requestsInfo}`);
|
97 |
-
});
|
98 |
-
|
99 |
-
app.get('/logs', (req, res) => {
|
100 |
-
res.sendFile(__dirname + '/index.html');
|
101 |
-
});
|
102 |
-
|
103 |
-
function getExternalUrl(spaceId) {
|
104 |
-
try {
|
105 |
-
const [username, spacename] = spaceId.split("/");
|
106 |
-
return `https://${username}-${spacename.replace(/_/g, "-")}.hf.space/api/v1`;
|
107 |
-
} catch (e) {
|
108 |
-
return "Error generating external URL";
|
109 |
-
}
|
110 |
-
}
|
111 |
|
112 |
server.listen(port, () => {
|
113 |
const message = `Reverse proxy server and WebSocket running on port ${port}`;
|
114 |
-
logAndEmit(message, true);
|
115 |
});
|
|
|
14 |
const baseUrl = getExternalUrl(process.env.SPACE_ID);
|
15 |
|
16 |
let requestDetails = [];
|
17 |
+
let apiKeyDetails = {}; // Track API key usage and first use timestamp
|
18 |
|
19 |
+
// Allowed models array
|
20 |
const allowedModels = [
|
21 |
'gpt-3.5-turbo-0125',
|
22 |
'gpt-3.5-turbo',
|
|
|
52 |
|
53 |
const validApiKeys = (process.env.SECRET_API_KEYS || '').split(',');
|
54 |
const apiKeyMatch = validApiKeys.find(key => `Bearer ${key.trim()}` === receivedApiKey);
|
55 |
+
|
56 |
if (!apiKeyMatch) {
|
57 |
return res.status(401).send('Unauthorized: API key is invalid');
|
58 |
}
|
|
|
71 |
}
|
72 |
|
73 |
apiKeyDetails[apiKeyMatch].count++;
|
74 |
+
|
75 |
const logMessage = `API Key ${displayKey}... used ${apiKeyDetails[apiKeyMatch].count} times so far.`;
|
76 |
logAndEmit(logMessage, true);
|
77 |
+
|
78 |
req.apiKey = apiKeyMatch;
|
79 |
next();
|
80 |
}
|
81 |
|
82 |
+
function validateModel(req, res, next) {
|
83 |
+
const pathComponents = req.path.split('/');
|
84 |
+
const modelUsed = pathComponents.filter(component => allowedModels.includes(component))[0];
|
85 |
+
|
86 |
+
if (!modelUsed) {
|
87 |
+
return res.status(403).send("Access to this model is not allowed.");
|
88 |
}
|
89 |
+
|
90 |
next();
|
91 |
+
}
|
92 |
+
|
93 |
+
app.use('/api', authenticateApiKey, validateModel, proxy(targetUrl, {
|
94 |
+
proxyReqPathResolver: function(req) {
|
95 |
+
return require('url').parse(req.url).path;
|
96 |
+
},
|
97 |
proxyReqOptDecorator: function(proxyReqOpts, srcReq) {
|
98 |
proxyReqOpts.headers['Authorization'] = 'Bearer ' + openaiKey;
|
99 |
return proxyReqOpts;
|
100 |
}
|
101 |
}));
|
102 |
|
103 |
+
// Additional server endpoints remain unchanged
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
server.listen(port, () => {
|
106 |
const message = `Reverse proxy server and WebSocket running on port ${port}`;
|
107 |
+
logAndEmit(message, true); // true to include total requests in the log
|
108 |
});
|