17 lines
515 B
TypeScript
17 lines
515 B
TypeScript
import { Request } from 'express';
|
|
|
|
export interface RealIpInfo {
|
|
ip: string | undefined;
|
|
countryCode?: string;
|
|
}
|
|
|
|
export function getRealIpInfo(req: Request): RealIpInfo {
|
|
const ip =
|
|
(req.headers['cf-connecting-ip'] as string) ||
|
|
(req.headers['x-real-ip'] as string) ||
|
|
(typeof req.headers['x-forwarded-for'] === 'string' ? req.headers['x-forwarded-for'].split(',')[0] : req.connection.remoteAddress);
|
|
const countryCode = req.headers['cf-ipcountry'] as string;
|
|
|
|
return { ip, countryCode };
|
|
}
|