Update plugins/antiban.js
Browse files- plugins/antiban.js +27 -9
plugins/antiban.js
CHANGED
@@ -3,34 +3,52 @@ import { Database } from '../database/database.js';
|
|
3 |
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
|
4 |
const AntibanRoutes = express.Router();
|
5 |
|
6 |
-
AntibanRoutes.
|
7 |
const dbClient = new Database("AkenoXJs");
|
8 |
const collection = dbClient.collection("ban_users");
|
|
|
|
|
9 |
try {
|
|
|
10 |
const userIdString = req.query.user_id;
|
11 |
-
const
|
12 |
const userIdNumber = Number(userIdString);
|
13 |
|
|
|
|
|
|
|
14 |
if (isNaN(userIdNumber)) {
|
15 |
return res.status(400).json({ error: "Invalid or missing user_id" });
|
16 |
}
|
17 |
|
18 |
const existingUser = await collection.findOne({ user_ban: userIdNumber });
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
if (existingUser) {
|
20 |
-
|
21 |
}
|
|
|
22 |
const userDocument = {
|
23 |
-
|
24 |
-
|
|
|
25 |
};
|
26 |
-
|
27 |
-
|
|
|
28 |
}
|
|
|
29 |
await collection.insertOne(userDocument);
|
30 |
-
res.json({ message: `
|
|
|
31 |
} catch (error) {
|
32 |
-
res.status(
|
33 |
}
|
34 |
});
|
35 |
|
|
|
36 |
export { AntibanRoutes };
|
|
|
3 |
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
|
4 |
const AntibanRoutes = express.Router();
|
5 |
|
6 |
+
AntibanRoutes.post("/api/v1/user/ban-user", authenticateApiKey, apiLimiter, async (req, res) => {
|
7 |
const dbClient = new Database("AkenoXJs");
|
8 |
const collection = dbClient.collection("ban_users");
|
9 |
+
const collectionKey = dbClient.collection("api_keys");
|
10 |
+
|
11 |
try {
|
12 |
+
const apiKey = req.headers["x-api-key"];
|
13 |
const userIdString = req.query.user_id;
|
14 |
+
const reasonString = req.query.reason;
|
15 |
const userIdNumber = Number(userIdString);
|
16 |
|
17 |
+
if (!apiKey) {
|
18 |
+
return res.status(400).json({ error: "Missing API key in headers" });
|
19 |
+
}
|
20 |
if (isNaN(userIdNumber)) {
|
21 |
return res.status(400).json({ error: "Invalid or missing user_id" });
|
22 |
}
|
23 |
|
24 |
const existingUser = await collection.findOne({ user_ban: userIdNumber });
|
25 |
+
const existingUserKey = await collectionKey.findOne({ key: apiKey });
|
26 |
+
|
27 |
+
if (!existingUserKey) {
|
28 |
+
return res.status(401).json({ message: "API key not found" });
|
29 |
+
}
|
30 |
+
|
31 |
if (existingUser) {
|
32 |
+
return res.status(200).json({ message: `User is already banned: ${existingUser.user_ban}`, is_ban: true });
|
33 |
}
|
34 |
+
|
35 |
const userDocument = {
|
36 |
+
key: existingUserKey.key,
|
37 |
+
user_ban: userIdNumber,
|
38 |
+
createdAt: new Date(),
|
39 |
};
|
40 |
+
|
41 |
+
if (reasonString) {
|
42 |
+
userDocument.reason = reasonString;
|
43 |
}
|
44 |
+
|
45 |
await collection.insertOne(userDocument);
|
46 |
+
res.json({ message: `User successfully banned: ${userIdNumber}`, is_ban: true });
|
47 |
+
|
48 |
} catch (error) {
|
49 |
+
res.status(500).json({ error: `Internal server error: ${error.message}` });
|
50 |
}
|
51 |
});
|
52 |
|
53 |
+
|
54 |
export { AntibanRoutes };
|