UNPKG

client-request-inspector

Version:

A Node.js library designed for efficient server-side extraction of client request data, providing easy access to client information such as IP address, browser type and location

1 lines 2.04 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.IpAddressService=void 0;class IpAddressService{static ip(value){return!!value&&this.regexes.ipv4.test(value)||this.regexes.ipv6.test(value)}static string(value){return Object.prototype.toString.call(value)==="[object String]"}static getClientIpFromXForwardedFor(value){if(!value){return null}if(!this.string(value)){throw new TypeError(`Expected a string, got "${typeof value}"`)}const forwardedIps=value.split(",").map((e=>{const ip=e.trim();if(ip.includes(":")){const splitted=ip.split(":");if(splitted.length===2){return splitted[0]}}return ip}));for(const element of forwardedIps){if(this.ip(element)){return element}}return null}static getClientIp(req){if(req&&req.headers){const xForwardedFor=this.getClientIpFromXForwardedFor(req.headers["x-forwarded-for"]);if(this.ip(xForwardedFor)){return xForwardedFor}const headerParam=this.headersParams.find((param=>this.ip(req.headers[param])));if(!!headerParam){return req.headers[headerParam]}}if(!!req.socket&&this.ip(req.socket.remoteAddress)){return req.socket.remoteAddress}if(!!req.info&&this.ip(req.info.remoteAddress)){return req.info.remoteAddress}if(!!req.requestContext&&!!req.requestContext.identity&&this.ip(req.requestContext.identity.sourceIp)){return req.requestContext.identity.sourceIp}if(req.headers){if(this.ip(req.headers["Cf-Pseudo-IPv4"])){return req.headers["Cf-Pseudo-IPv4"]}}if(!!req.raw){return this.getClientIp(req.raw)}return null}}exports.IpAddressService=IpAddressService;IpAddressService.regexes={ipv4:/^(?:(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/,ipv6:/^((?=.*::)(?!.*::.+::)(::)?([\dA-F]{1,4}:(:|\b)|){5}|([\dA-F]{1,4}:){6})((([\dA-F]{1,4}((?!\3)::|:\b|$))|(?!\2\3)){2}|(((2[0-4]|1\d|[1-9])?\d|25[0-5])\.?\b){4})$/i};IpAddressService.headersParams=["x-client-ip","cf-connecting-ip","do-connecting-ip","fastly-client-ip","true-client-ip","x-real-ip","x-cluster-client-ip","x-forwarded","forwarded-for","forwarded","x-appengine-user-ip"];