@nka212bg/backend-utils 0.1.60 → 0.1.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/location/index.js CHANGED
@@ -5,21 +5,24 @@ const { locales, timeZones } = require(`./localelib`);
5
5
  const Reader = require("@maxmind/geoip2-node").Reader;
6
6
  const GeoDB = Reader.openBuffer(readFileSync(`${__dirname}/GeoLite2-City.mmdb`));
7
7
 
8
- exports.userIp = (req) => {
8
+ // x-forwarded-for is client-controlled, trust it only behind a reverse proxy (nginx, Cloudflare...)
9
+ exports.userIp = (req, { trustProxy = false } = {}) => {
9
10
  if (!req) throw new Error("Request object is missing, can't work like that! That's absurd!!!");
10
11
 
11
- let ip = (req.headers["x-forwarded-for"] || req.socket?.remoteAddress || req.connection?.remoteAddress || req.ip)?.split(",")[0].replace(/::ffff:/gi, "");
12
+ let ip = ((trustProxy && req.headers["x-forwarded-for"]) || req.socket?.remoteAddress || req.connection?.remoteAddress || req.ip)
13
+ ?.split(",")[0]
14
+ .trim()
15
+ .replace(/::ffff:/gi, "");
12
16
 
13
- // if (["127.0.0.1", "::1"].includes(ip)) return "146.70.185.8";
14
17
  return ip;
15
18
  };
16
19
 
17
20
  // user geo
18
- exports.location = ({ req, ip }) => {
21
+ exports.location = ({ req, ip, trustProxy }) => {
19
22
  let userIP;
20
23
  try {
21
24
  if (!ip && !req) throw "missing_required";
22
- userIP = ip || this.userIp(req);
25
+ userIP = ip || this.userIp(req, { trustProxy });
23
26
  const geo = GeoDB.city(userIP);
24
27
 
25
28
  const country = locales[geo.country.isoCode];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nka212bg/backend-utils",
3
3
  "author": "nka212bg",
4
- "version": "0.1.60",
4
+ "version": "0.1.61",
5
5
  "main": "index.js",
6
6
  "dependencies": {
7
7
  "@maxmind/geoip2-node": "^5.0.0",