Add user agent blacklist

This commit is contained in:
soruly 2022-06-18 07:28:12 +00:00
parent 3592ef97d3
commit 33641ef6f2
No known key found for this signature in database
GPG Key ID: EF971E90F3D2521F
2 changed files with 9 additions and 2 deletions

View File

@ -1,2 +1,3 @@
SERVER_PORT=3000
SERVER_ADDR=127.0.0.1
SERVER_ADDR=127.0.0.1
BLACKLIST_UA=Bot|MSIE|Bytespider|Baidu|Sogou|FB_AN|FB_IOS|FB_IAB|Instagram

View File

@ -5,7 +5,7 @@ import crypto from "crypto";
import express from "express";
import rateLimit from "express-rate-limit";
const { SERVER_ADDR = "0.0.0.0", SERVER_PORT = 3000 } = process.env;
const { SERVER_ADDR = "0.0.0.0", SERVER_PORT = 3000, BLACKLIST_UA } = process.env;
const app = express();
@ -15,6 +15,12 @@ app.set("trust proxy", 1);
app.set("view engine", "ejs");
app.set("views", path.resolve("."));
app.use((req, res, next) => {
if (BLACKLIST_UA && req.headers["user-agent"]?.match(new RegExp(`(${BLACKLIST_UA})`, "i")))
return;
next();
});
app.use(express.json());
app.use((req, res, next) => {