sourceappblock-dodgy-uas.js

import { readFileSync } from "fs";
import * as mmdb from "mmdb-lib";

import { createError } from "./handmade.js";

const asnDb = readFileSync("./db/asns.mmdb");
const asnReader = new mmdb.Reader(asnDb);

const blockDodgyUAs = (req, res, next) => {
	const asn = asnReader.get(req.headers["x-real-ip"] ?? req.ip);

	// Some sort of dodgy scraping operation run out of Tencent
	if (
		req.headers["user-agent"] &&
		req.headers["user-agent"].includes("iPhone OS 13_2_3") &&
		req.headers["user-agent"].includes("Mobile/15E148")
	) {
		return next(createError(403));
	}

	// Russian VPS provider responsible for giving the Fred Fuckstone joke page a thousand hits a month
	if (asn?.asn == 48282) {
		return next(createError(403));
	}

	next();
};

export default blockDodgyUAs;