Skip to content

Commit

Permalink
Merge pull request #122 from workheap/zip_support
Browse files Browse the repository at this point in the history
Postal code added to IPv4.
  • Loading branch information
bluesmoon committed Mar 9, 2017
2 parents 70788f0 + eb0fbbf commit de3ea8d
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 8 deletions.
16 changes: 10 additions & 6 deletions README.md
Expand Up @@ -16,7 +16,7 @@ an internal binary format (note that this is different from the binary data form
binary file to lookup IP addresses and return the country, region and city that it maps to.

Both IPv4 and IPv6 addresses are supported, however since the GeoLite IPv6 database does not currently contain any city or region
information, city and region lookups are only supported for IPv4.
information, city, region and postal code lookups are only supported for IPv4.

philosophy
----------
Expand Down Expand Up @@ -45,11 +45,13 @@ var ip = "207.97.227.239";
var geo = geoip.lookup(ip);

console.log(geo);
{ range: [ 3479299040, 3479299071 ],
{ range: [ 3479297920, 3479301339 ],
country: 'US',
region: 'CA',
city: 'San Francisco',
ll: [37.7484, -122.4156] }
region: 'TX',
city: 'San Antonio',
ll: [ 29.4889, -98.3987 ],
metro: 641,
zip: 78218 }
```

installation
Expand Down Expand Up @@ -95,7 +97,9 @@ If the IP address was found, the `lookup` method returns an object with the foll
// ISO-3166-2 subcountry code for other countries, this is the
// FIPS 10-4 subcountry code
city: "City Name", // This is the full city name
ll: [<latitude>, <longitude>] // The latitude and longitude of the city
ll: [<latitude>, <longitude>], // The latitude and longitude of the city
metro: <metro code>, // Metro code
zip: <postal code> // Postal code (IPv4 only)
}
```

Expand Down
Binary file modified data/geoip-city-names.dat
Binary file not shown.
Binary file modified data/geoip-city.dat
Binary file not shown.
Binary file modified data/geoip-city6.dat
Binary file not shown.
Binary file modified data/geoip-country.dat
Binary file not shown.
Binary file modified data/geoip-country6.dat
Binary file not shown.
3 changes: 2 additions & 1 deletion lib/geoip.js
Expand Up @@ -107,7 +107,8 @@ function lookup4(ip) {
geodata.region = locBuffer.toString('utf8', (locId * locRecordSize) + 2, (locId * locRecordSize) + 4).replace(/\u0000.*/, '');
geodata.ll = [locBuffer.readInt32BE((locId * locRecordSize) + 4) / 10000, locBuffer.readInt32BE((locId * locRecordSize) + 8) / 10000];
geodata.metro = locBuffer.readInt32BE((locId * locRecordSize) + 12);
geodata.city = locBuffer.toString('utf8', (locId * locRecordSize) + 16, (locId * locRecordSize) + locRecordSize).replace(/\u0000.*/, '');
geodata.zip = locBuffer.readInt32BE((locId * locRecordSize) + 16);
geodata.city = locBuffer.toString('utf8', (locId * locRecordSize) + 20, (locId * locRecordSize) + locRecordSize).replace(/\u0000.*/, '');
}

return geodata;
Expand Down
7 changes: 6 additions & 1 deletion scripts/updatedb.js
Expand Up @@ -369,6 +369,7 @@ function processCityDataNames(src, dest, cb) {
var cc = fields[1];
var rg = fields[2];
var city = fields[3];
var zip = parseInt(fields[4]);
var lat = Math.round(parseFloat(fields[5]) * 10000);
var lon = Math.round(parseFloat(fields[6]) * 10000);
var metro = parseInt(fields[7]);
Expand All @@ -384,7 +385,11 @@ function processCityDataNames(src, dest, cb) {
b.writeInt32BE(metro, 12);
}

b.write(city, 16);
if(zip){
b.writeInt32BE(zip, 16);
}

b.write(city, 20);

fs.writeSync(datFile, b, 0, b.length, null);
}
Expand Down

0 comments on commit de3ea8d

Please sign in to comment.