mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-26 17:38:28 +03:00
Fix usercounter behind cloudflare
This commit is contained in:
@@ -12,7 +12,7 @@ export function userCounter(req: Request, res: Response, next: NextFunction): vo
|
|||||||
if (Math.random() < 1 / config.userCounterRatio) {
|
if (Math.random() < 1 / config.userCounterRatio) {
|
||||||
axios({
|
axios({
|
||||||
method: "post",
|
method: "post",
|
||||||
url: `${config.userCounterURL}/api/v1/addIP?hashedIP=${getIP(req)}`,
|
url: `${config.userCounterURL}/api/v1/addIP?hashedIP=${getIP(req, true)}`,
|
||||||
httpAgent
|
httpAgent
|
||||||
}).catch(() => /* instanbul skip next */ Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`));
|
}).catch(() => /* instanbul skip next */ Logger.debug(`Failing to connect to user counter at: ${config.userCounterURL}`));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { config } from "../config";
|
|||||||
import { Request } from "express";
|
import { Request } from "express";
|
||||||
import { IPAddress } from "../types/segments.model";
|
import { IPAddress } from "../types/segments.model";
|
||||||
|
|
||||||
export function getIP(req: Request): IPAddress {
|
export function getIP(req: Request, checkCloudflare = false): IPAddress {
|
||||||
// if in testing mode, return immediately
|
// if in testing mode, return immediately
|
||||||
if (config.mode === "test") return "127.0.0.1" as IPAddress;
|
if (config.mode === "test") return "127.0.0.1" as IPAddress;
|
||||||
|
|
||||||
@@ -10,11 +10,14 @@ export function getIP(req: Request): IPAddress {
|
|||||||
config.behindProxy = "X-Forwarded-For";
|
config.behindProxy = "X-Forwarded-For";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cloudflareIP = req.headers["cf-connecting-ip"] as IPAddress;
|
||||||
|
if (checkCloudflare && cloudflareIP) return cloudflareIP;
|
||||||
|
|
||||||
switch (config.behindProxy as string) {
|
switch (config.behindProxy as string) {
|
||||||
case "X-Forwarded-For":
|
case "X-Forwarded-For":
|
||||||
return req.headers["x-forwarded-for"] as IPAddress;
|
return req.headers["x-forwarded-for"] as IPAddress;
|
||||||
case "Cloudflare":
|
case "Cloudflare":
|
||||||
return req.headers["cf-connecting-ip"] as IPAddress;
|
return cloudflareIP;
|
||||||
case "X-Real-IP":
|
case "X-Real-IP":
|
||||||
return req.headers["x-real-ip"] as IPAddress;
|
return req.headers["x-real-ip"] as IPAddress;
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user