mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-25 08:58:23 +03:00
23 lines
812 B
TypeScript
23 lines
812 B
TypeScript
import { config } from "../config";
|
|
import { Request } from "express";
|
|
import { IPAddress } from "../types/segments.model";
|
|
|
|
export function getIP(req: Request): IPAddress {
|
|
// if in testing mode, return immediately
|
|
if (config.mode === "test") return "127.0.0.1" as IPAddress;
|
|
|
|
if (config.behindProxy === true || config.behindProxy === "true") {
|
|
config.behindProxy = "X-Forwarded-For";
|
|
}
|
|
|
|
switch (config.behindProxy as string) {
|
|
case "X-Forwarded-For":
|
|
return req.headers["x-forwarded-for"] as IPAddress;
|
|
case "Cloudflare":
|
|
return req.headers["cf-connecting-ip"] as IPAddress;
|
|
case "X-Real-IP":
|
|
return req.headers["x-real-ip"] as IPAddress;
|
|
default:
|
|
return req.socket?.remoteAddress as IPAddress;
|
|
}
|
|
} |