fix add api key ban and unban
Browse files- index.js +118 -50
- middleware/midware.js +8 -0
- models.js +1 -0
index.js
CHANGED
@@ -133,56 +133,10 @@ const AllJsonReques = {
|
|
133 |
},
|
134 |
}
|
135 |
|
136 |
-
|
137 |
app.get("/api/v1/json/all", async (req, res) => {
|
138 |
res.json(AllJsonReques);
|
139 |
});
|
140 |
|
141 |
-
app.get('/api/v1/nsfw', async (req, res) => {
|
142 |
-
try {
|
143 |
-
const { url } = req.query;
|
144 |
-
if (!url) {
|
145 |
-
return res.status(400).json({ message: 'Invalid URL.' });
|
146 |
-
}
|
147 |
-
|
148 |
-
const response = await axios.get(url, { responseType: 'arraybuffer' });
|
149 |
-
const imageBuffer = Buffer.from(response.data);
|
150 |
-
|
151 |
-
let imageTensor;
|
152 |
-
|
153 |
-
if (url.endsWith('.gif')) {
|
154 |
-
const jpgBuffer = await sharp(imageBuffer)
|
155 |
-
.resize({ width: 299, height: 299 })
|
156 |
-
.toFormat('jpeg')
|
157 |
-
.toBuffer();
|
158 |
-
|
159 |
-
imageTensor = tf.node.decodeImage(jpgBuffer, 3);
|
160 |
-
} else {
|
161 |
-
imageTensor = tf.node.decodeImage(imageBuffer, 3);
|
162 |
-
}
|
163 |
-
|
164 |
-
const predictions = await model.classify(imageTensor);
|
165 |
-
imageTensor.dispose();
|
166 |
-
|
167 |
-
const formattedPredictions = predictions.reduce((acc, { className, probability }) => {
|
168 |
-
acc[className] = probability;
|
169 |
-
return acc;
|
170 |
-
}, {});
|
171 |
-
|
172 |
-
res.json(formattedPredictions);
|
173 |
-
} catch (error) {
|
174 |
-
console.error('Error processing image:', error);
|
175 |
-
|
176 |
-
if (error.response) {
|
177 |
-
return res.status(error.response.status).json({ message: 'Error fetching image from URL.', details: error.message });
|
178 |
-
} else if (error.code === 'ERR_INVALID_URL') {
|
179 |
-
return res.status(400).json({ message: 'Invalid image URL.', details: error.message });
|
180 |
-
} else {
|
181 |
-
return res.status(500).json({ message: 'Internal server error.', details: error.message });
|
182 |
-
}
|
183 |
-
}
|
184 |
-
});
|
185 |
-
|
186 |
app.get('/broadcast-user', async (req, res) => {
|
187 |
const dbClient = new Database("AkenoXJs");
|
188 |
const collection = dbClient.collection("api_keys");
|
@@ -249,10 +203,6 @@ app.delete("/api/v1/delete-key", async (req, res) => {
|
|
249 |
}
|
250 |
});
|
251 |
|
252 |
-
app.get("/api/v1/test", authenticateApiKey, apiLimiter, async (req, res) => {
|
253 |
-
res.json({ message: "access key" });
|
254 |
-
})
|
255 |
-
|
256 |
app.get("/runtime", async (req, res) => {
|
257 |
res.send("Running lifetime");
|
258 |
})
|
@@ -269,6 +219,12 @@ app.post('/api/v2/generate-key', async (req, res) => {
|
|
269 |
|
270 |
const existingUser = await ApiKey.findOne({ owner: userIdNumber });
|
271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
if (existingUser) {
|
273 |
return res.status(200).json({
|
274 |
apiKey: existingUser.key,
|
@@ -288,6 +244,7 @@ app.post('/api/v2/generate-key', async (req, res) => {
|
|
288 |
expiresAt: expirationDate,
|
289 |
owner: userIdNumber,
|
290 |
type: "premium",
|
|
|
291 |
email: email || null
|
292 |
});
|
293 |
await userDocument.save();
|
@@ -312,6 +269,12 @@ app.post("/api/v1/revoked-key", async (req, res) => {
|
|
312 |
|
313 |
const existingUser = await collection.findOne({ owner: userIdNumber });
|
314 |
|
|
|
|
|
|
|
|
|
|
|
|
|
315 |
if (!existingUser) {
|
316 |
return res.status(404).json({ error: "API key not found" });
|
317 |
}
|
@@ -340,6 +303,99 @@ app.post("/api/v1/revoked-key", async (req, res) => {
|
|
340 |
}
|
341 |
});
|
342 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
343 |
app.post('/api/v1/generate-key', async (req, res) => {
|
344 |
const dbClient = new Database("AkenoXJs");
|
345 |
const collection = dbClient.collection('api_keys');
|
@@ -353,8 +409,19 @@ app.post('/api/v1/generate-key', async (req, res) => {
|
|
353 |
if (isNaN(userIdNumber)) {
|
354 |
return res.status(400).json({ error: "Invalid or missing user_id" });
|
355 |
}
|
|
|
|
|
|
|
|
|
|
|
356 |
const existingUser = await collection.findOne({ owner: userIdNumber });
|
357 |
|
|
|
|
|
|
|
|
|
|
|
|
|
358 |
if (existingUser) {
|
359 |
return res.status(200).json({
|
360 |
apiKey: existingUser.key,
|
@@ -367,6 +434,7 @@ app.post('/api/v1/generate-key', async (req, res) => {
|
|
367 |
createdAt: new Date(),
|
368 |
owner: userIdNumber,
|
369 |
type: "free",
|
|
|
370 |
username: username,
|
371 |
};
|
372 |
if (email) {
|
|
|
133 |
},
|
134 |
}
|
135 |
|
|
|
136 |
app.get("/api/v1/json/all", async (req, res) => {
|
137 |
res.json(AllJsonReques);
|
138 |
});
|
139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
140 |
app.get('/broadcast-user', async (req, res) => {
|
141 |
const dbClient = new Database("AkenoXJs");
|
142 |
const collection = dbClient.collection("api_keys");
|
|
|
203 |
}
|
204 |
});
|
205 |
|
|
|
|
|
|
|
|
|
206 |
app.get("/runtime", async (req, res) => {
|
207 |
res.send("Running lifetime");
|
208 |
})
|
|
|
219 |
|
220 |
const existingUser = await ApiKey.findOne({ owner: userIdNumber });
|
221 |
|
222 |
+
if (existingUser) {
|
223 |
+
return res.status(200).json({
|
224 |
+
is_banned: existingUser.is_banned || false
|
225 |
+
});
|
226 |
+
}
|
227 |
+
|
228 |
if (existingUser) {
|
229 |
return res.status(200).json({
|
230 |
apiKey: existingUser.key,
|
|
|
244 |
expiresAt: expirationDate,
|
245 |
owner: userIdNumber,
|
246 |
type: "premium",
|
247 |
+
is_banned: false,
|
248 |
email: email || null
|
249 |
});
|
250 |
await userDocument.save();
|
|
|
269 |
|
270 |
const existingUser = await collection.findOne({ owner: userIdNumber });
|
271 |
|
272 |
+
if (existingUser) {
|
273 |
+
return res.status(200).json({
|
274 |
+
is_banned: existingUser.is_banned || false
|
275 |
+
});
|
276 |
+
}
|
277 |
+
|
278 |
if (!existingUser) {
|
279 |
return res.status(404).json({ error: "API key not found" });
|
280 |
}
|
|
|
303 |
}
|
304 |
});
|
305 |
|
306 |
+
app.post("/api/v1/api-key-unban", async (req, res) => {
|
307 |
+
const dbClient = new Database("AkenoXJs");
|
308 |
+
const collection = dbClient.collection("api_keys");
|
309 |
+
|
310 |
+
try {
|
311 |
+
const userIdString = req.query.user_id;
|
312 |
+
const userIdNumber = Number(userIdString);
|
313 |
+
|
314 |
+
if (isNaN(userIdNumber)) {
|
315 |
+
return res.status(400).json({ error: "Invalid or missing user_id" });
|
316 |
+
}
|
317 |
+
|
318 |
+
if (userIdNumber === 0) {
|
319 |
+
return res.status(400).json({ error: "Invalid user_id" });
|
320 |
+
}
|
321 |
+
|
322 |
+
if (userIdNumber === 6477856957) {
|
323 |
+
return res.status(400).json({ error: "sorry only developer" });
|
324 |
+
}
|
325 |
+
|
326 |
+
const existingUser = await collection.findOne({ owner: userIdNumber });
|
327 |
+
|
328 |
+
if (!existingUser) {
|
329 |
+
return res.status(404).json({ error: "API key not found" });
|
330 |
+
}
|
331 |
+
|
332 |
+
const result = await collection.updateOne(
|
333 |
+
{ owner: userIdNumber },
|
334 |
+
{ $set: { is_banned: false } },
|
335 |
+
{ upsert: false }
|
336 |
+
);
|
337 |
+
|
338 |
+
if (result.modifiedCount > 0) {
|
339 |
+
res.json({
|
340 |
+
message: "API key successfully unbanned",
|
341 |
+
owner: userIdNumber
|
342 |
+
});
|
343 |
+
} else {
|
344 |
+
res.status(500).json({ error: "Failed to unban API key" });
|
345 |
+
}
|
346 |
+
|
347 |
+
} catch (err) {
|
348 |
+
res.status(500).json({ error: `Key generation failed: ${err.message}` });
|
349 |
+
}
|
350 |
+
});
|
351 |
+
|
352 |
+
app.post("/api/v1/api-key-ban", async (req, res) => {
|
353 |
+
const dbClient = new Database("AkenoXJs");
|
354 |
+
const collection = dbClient.collection("api_keys");
|
355 |
+
|
356 |
+
try {
|
357 |
+
const userIdString = req.query.user_id;
|
358 |
+
const userIdNumber = Number(userIdString);
|
359 |
+
|
360 |
+
if (isNaN(userIdNumber)) {
|
361 |
+
return res.status(400).json({ error: "Invalid or missing user_id" });
|
362 |
+
}
|
363 |
+
|
364 |
+
if (userIdNumber === 0) {
|
365 |
+
return res.status(400).json({ error: "Invalid user_id" });
|
366 |
+
}
|
367 |
+
|
368 |
+
if (userIdNumber === 6477856957) {
|
369 |
+
return res.status(400).json({ error: "sorry only developer" });
|
370 |
+
}
|
371 |
+
|
372 |
+
const existingUser = await collection.findOne({ owner: userIdNumber });
|
373 |
+
|
374 |
+
if (!existingUser) {
|
375 |
+
return res.status(404).json({ error: "API key not found" });
|
376 |
+
}
|
377 |
+
|
378 |
+
const result = await collection.updateOne(
|
379 |
+
{ owner: userIdNumber },
|
380 |
+
{ $set: { is_banned: true } },
|
381 |
+
{ upsert: false }
|
382 |
+
);
|
383 |
+
|
384 |
+
if (result.modifiedCount > 0) {
|
385 |
+
res.json({
|
386 |
+
message: "API key successfully banned",
|
387 |
+
owner: userIdNumber
|
388 |
+
});
|
389 |
+
} else {
|
390 |
+
res.status(500).json({ error: "Failed to ban API key" });
|
391 |
+
}
|
392 |
+
|
393 |
+
} catch (err) {
|
394 |
+
res.status(500).json({ error: `Key generation failed: ${err.message}` });
|
395 |
+
}
|
396 |
+
});
|
397 |
+
|
398 |
+
|
399 |
app.post('/api/v1/generate-key', async (req, res) => {
|
400 |
const dbClient = new Database("AkenoXJs");
|
401 |
const collection = dbClient.collection('api_keys');
|
|
|
409 |
if (isNaN(userIdNumber)) {
|
410 |
return res.status(400).json({ error: "Invalid or missing user_id" });
|
411 |
}
|
412 |
+
|
413 |
+
if (userIdNumber === 0) {
|
414 |
+
return res.status(400).json({ error: "Invalid user_id" });
|
415 |
+
}
|
416 |
+
|
417 |
const existingUser = await collection.findOne({ owner: userIdNumber });
|
418 |
|
419 |
+
if (existingUser) {
|
420 |
+
return res.status(200).json({
|
421 |
+
is_banned: existingUser.is_banned || false
|
422 |
+
});
|
423 |
+
}
|
424 |
+
|
425 |
if (existingUser) {
|
426 |
return res.status(200).json({
|
427 |
apiKey: existingUser.key,
|
|
|
434 |
createdAt: new Date(),
|
435 |
owner: userIdNumber,
|
436 |
type: "free",
|
437 |
+
is_banned: false,
|
438 |
username: username,
|
439 |
};
|
440 |
if (email) {
|
middleware/midware.js
CHANGED
@@ -18,6 +18,10 @@ const authenticateApiKeyPremium = async (req, res, next) => {
|
|
18 |
return res.status(403).json({ error: 'Invalid or non-premium API Key' });
|
19 |
}
|
20 |
|
|
|
|
|
|
|
|
|
21 |
if (keyData.expiresAt && new Date() > keyData.expiresAt) {
|
22 |
return res.status(403).json({ error: 'Premium API Key has expired' });
|
23 |
}
|
@@ -41,6 +45,10 @@ const authenticateApiKey = async (req, res, next) => {
|
|
41 |
if (!keyDoc) {
|
42 |
return res.status(403).json({ error: 'Invalid API Key' });
|
43 |
}
|
|
|
|
|
|
|
|
|
44 |
next();
|
45 |
} catch (err) {
|
46 |
res.status(500).json({ error: 'Server error' });
|
|
|
18 |
return res.status(403).json({ error: 'Invalid or non-premium API Key' });
|
19 |
}
|
20 |
|
21 |
+
if (keyData.is_banned === true) {
|
22 |
+
return res.status(403).json({ error: 'Banned API Key' });
|
23 |
+
}
|
24 |
+
|
25 |
if (keyData.expiresAt && new Date() > keyData.expiresAt) {
|
26 |
return res.status(403).json({ error: 'Premium API Key has expired' });
|
27 |
}
|
|
|
45 |
if (!keyDoc) {
|
46 |
return res.status(403).json({ error: 'Invalid API Key' });
|
47 |
}
|
48 |
+
if (keyDoc.is_banned === true) {
|
49 |
+
return res.status(403).json({ error: 'Banned API Key' });
|
50 |
+
}
|
51 |
+
|
52 |
next();
|
53 |
} catch (err) {
|
54 |
res.status(500).json({ error: 'Server error' });
|
models.js
CHANGED
@@ -3,6 +3,7 @@ import mongoose from "mongoose";
|
|
3 |
const apiKeySchema = new mongoose.Schema({
|
4 |
key: { type: String, required: true, unique: true },
|
5 |
owner: { type: Number, required: true, unique: true },
|
|
|
6 |
email: { type: String },
|
7 |
createdAt: { type: Date, default: Date.now },
|
8 |
type: { type: String, enum: ["free", "premium"], default: "free" },
|
|
|
3 |
const apiKeySchema = new mongoose.Schema({
|
4 |
key: { type: String, required: true, unique: true },
|
5 |
owner: { type: Number, required: true, unique: true },
|
6 |
+
is_banned: { type: Boolean, default: false },
|
7 |
email: { type: String },
|
8 |
createdAt: { type: Date, default: Date.now },
|
9 |
type: { type: String, enum: ["free", "premium"], default: "free" },
|