geoip-lite / node-geoip

Native NodeJS implementation of MaxMind's GeoIP API -- works in node 0.6.3 and above, ask me about other versions
https://npmjs.org/package/geoip-lite
Other
2.32k stars 356 forks source link

Geoip-lite returns null for IPv6 representation of IPv4 addresses #84

Open chrisvdb opened 9 years ago

chrisvdb commented 9 years ago

Using my current IP address:

var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; console.log(ip); var geo = geoip.lookup(ip); console.log(geo);

yields

::ffff:123.202.30.78 null

My geoip-lite data directory contains:

-rw-r--r-- 1 root root 40M Feb 11 15:20 geoip-city-names.dat -rw-r--r-- 1 root root 23M Feb 11 15:20 geoip-city.dat -rw-r--r-- 1 root root 1.3M Feb 11 15:20 geoip-city6.dat -rw-r--r-- 1 root root 1001K Feb 11 15:20 geoip-country.dat -rw-r--r-- 1 root root 726K Feb 11 15:20 geoip-country6.dat

(root because it's in a docker container)

Looking up the IPv4 version (123.202.30.78) works as expected. Looking up ::ffff:123.202.30.78 on https://www.maxmind.com/en/geoip-demo works as expected too.

A workaround is:

ip = ip.split(:).slice(-1)[0]; // works both on IPv4 and IPv6 representations of IPv4 addresses

Rytiss commented 9 years ago

Confirming - I have the same issue. My workaround is ip.substr(7) since I always know that I'll get IPv4s, so I just drop IPv6 prefix.

ollyde commented 1 year ago

I did a test myself.

var geoip = require('geoip-lite');
var ip = "::ffff:123.202.30.78";
var geo = geoip.lookup(ip);
console.log(geo)

I get

{
  range: [ 2076843520, 2076843775 ],
  country: 'HK',
  region: 'HCW',
  eu: '0',
  timezone: 'Asia/Hong_Kong',
  city: 'Central',
  ll: [ 22.2908, 114.1501 ],
  metro: 0,
  area: 5
}

I'm using the premium database though.