Files
SponsorBlockServer/src/middleware/userCounter.ts
2022-12-05 16:47:11 -05:00

17 lines
614 B
TypeScript

import axios from "axios";
import { Logger } from "../utils/logger";
import { config } from "../config";
import { getIP } from "../utils/getIP";
import { NextFunction, Request, Response } from "express";
export function userCounter(req: Request, res: Response, next: NextFunction): void {
if (req.method !== "OPTIONS") {
if (Math.random() < 1 / config.userCounterRatio) {
axios.post(`${config.userCounterURL}/api/v1/addIP?hashedIP=${getIP(req)}`)
.catch(() => Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`));
}
}
next();
}