Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

v0.4.4 rolling out

  • Loading branch information...
commit 4206dffb42050a0bfd48c9ae8dba3764c30aa36b 1 parent 028b4cc
kuno authored
Showing with 199 additions and 36 deletions.
  1. +79 −27 README.md
  2. +56 −0 benchmark/city6.js
  3. +1 −1  package.json
  4. +63 −8 sample/sample.js
106 README.md
View
@@ -70,7 +70,7 @@ From v0.4.0, geoip need nodejs >= 0.4.0, if you want to use it on old nodejs, yo
###Country
-ipv4
+ipv4 address lookup
// Open the country data file
var Country = geoip.Country;
@@ -97,7 +97,7 @@ ipv4
}
});
-ipv6 (Currently only Country module supports ipv6)
+ipv6 address lookup (Currently only Country and City module supports ipv6)
// Open the ipv6 edition of country module
var Country6 = geoip.Country6;
@@ -136,41 +136,93 @@ ipv6 (Currently only Country module supports ipv6)
###City
+ipv4 address lookup
+
// Open the GeoLiteCity.dat file first.
var City = geoip.City;
var city = new City('/path/to/GeoLiteCity.dat');
-Synchronous method:
-
- var city_obj = city.lookupSync('8.8.8.8');
- console.log(city_obj);
- /*
- Return an object of city information
- {
- "country_code":"US",
- "country_code3":"USA",
- "country_name":"United States",
- "continet_code":"NA",
- "region":"CA",
- "city":"Mountain View",
- "postal_code":"94043",
- "latitude":37.41919999999999,
- "longitude":-122.0574,
- "dma_code":807,
- "metro_code":807,
- "area_code":650
+ Synchronous method:
+
+ var city_obj = city.lookupSync('8.8.8.8');
+ console.log(city_obj);
+ /*
+ Return an object of city information
+ {
+ "country_code":"US",
+ "country_code3":"USA",
+ "country_name":"United States",
+ "continet_code":"NA",
+ "region":"CA",
+ "city":"Mountain View",
+ "postal_code":"94043",
+ "latitude":37.41919999999999,
+ "longitude":-122.0574,
+ "dma_code":807,
+ "metro_code":807,
+ "area_code":650
+ }
+ */
+
+ Asynchronous method:
+
+ city.lookup('www.google.com', function(err, data) {
+ if (err) {throw err;}
+ if (data) {
+ console.log(data);
+ }
+ });
+
+
+ipv6 address lookup
+
+ // Open the GeoLiteCityv6.dat file first.
+ var City6 = geoip.City6;
+ var city6 = new City6('/path/to/GeoLiteCityv6.dat');
+
+ Synchronous method:
+
+ var city6_obj = city6.lookupSync('8.8.8.8');
+ console.log(city6_obj);
+ /*
+ Return an object of city information
+ {
+ "country_code":"US",
+ "country_code3":"USA",
+ "country_name":"United States",
+ "continet_code":"NA",
+ "region":"CA",
+ "city":"Mountain View",
+ "postal_code":"94043",
+ "latitude":37.41919999999999,
+ "longitude":-122.0574,
+ "dma_code":807,
+ "metro_code":807,
+ "area_code":650
}
- */
+ */
-Asynchronous method:
+ Asynchronous method:
- city.lookup('www.google.com', function(err, data) {
+ city6.lookup('2002:c6aa:2dc8:b719:4314:2a7d:e31c:fe73', function(err, data) {
if (err) {throw err;}
if (data) {
console.log(data);
- }
- });
-
+ /* As expected ,return an object that contains city information
+ {
+ country_code: 'US',
+ country_code3: 'USA',
+ country_name: 'United States',
+ region: 'CO',
+ city: 'Englewood',
+ postal_code: '80112',
+ latitude: 39.569000244140625,
+ longitude: -104.85820007324219,
+ continent_code: 'NA'
+ }
+ */
+ }
+ });
###Organization
56 benchmark/city6.js
View
@@ -0,0 +1,56 @@
+#!/usr/bin/env node
+
+var geoip = require('../index.js');
+var util = require('util');
+var City6 = geoip.City6;
+var c6 = new City6('../database/GeoLiteCityv6.dat', true);
+
+var ip128 = function() {
+ var ip = '', hex;
+ for (var i = 0; i < 8; i++) {
+ hex = (Math.floor(Math.random() * (Math.pow(2, 16)))).toString(16);
+ ip += (hex.length = 4) ? hex : (hex + '0');
+ if (i <= 6) {
+ ip += ':';
+ }
+ }
+
+ return ip
+}
+
+/*
+var start = new Date().getTime();
+for (var i = 0; i < 100000; ++i) {
+ var addr = '109.161.37.182'; //ip32();
+ // console.log(addr);
+ var l = c.lookupSync(addr);
+// console.log(l);
+}
+var end = new Date().getTime();
+
+util.puts((end - start) / 1000);
+*/
+
+var start6 = new Date().getTime();
+for (var i = 0; i < 100000; i++) {
+ var addr6 = ip128();
+ //console.log(addr6);
+ var l6 = c6.lookupSync(addr6);
+ if (l6) { console.log('addr6 is ' + addr6); console.log(l6);}
+}
+var end6 = new Date().getTime();
+
+util.puts((end6 - start6) / 1000);
+
+c6.update('../database/GeoLiteCityv6.dat');
+
+start6 = new Date().getTime();
+for (var j = 0; j < 100000; j++) {
+ var addr6 = ip128();
+ //console.log(addr6);
+ var l6 = c6.lookupSync(addr6);
+ if (l6) { console.log('addr6 is ' + addr6); console.log(l6);}
+}
+end6 = new Date().getTime();
+
+util.puts((end6 - start6) / 1000);
2  package.json
View
@@ -1,7 +1,7 @@
{
"name":"geoip",
"description":"GeoIP binding for node",
- "version":"0.5.0pre",
+ "version":"0.4.4",
"homepage":"http://github.com/kuno/GeoIP",
"author":"Guan 'kuno' Qing <neokuno at Gmail dot com>",
"contributors":
71 sample/sample.js
View
@@ -1,15 +1,17 @@
var geoip = require('geoip');
-// Data
+// !! Important !!
// Befor you can use this package, you need to download or buy some data fro http://www.maxmind.com/app/ip-location.
// There are some free databases among some commercial versions, the free databases can be found http://geolite.maxmind.com/download/geoip/database/.
+////////////////////////////////////////////////////////////////////////////////
// Country
-// Open the country data file
+////////////////////////////////////////////////////////////////////////////////
+/////////////////////////
+// ipv4 address lookup //
+/////////////////////////
var Country = geoip.Country;
-
-// ipv4
var country = new Country('/path/to/GeoIP.dat');
// Synchronous method(the recommended way):
@@ -32,14 +34,16 @@ country.lookup('www.google.com', function(err, data) {
}
});
-// ipv6 (Currently only Country module supports ipv6)
+/////////////////////////
+// ipv6 address lookup //
+/////////////////////////
var Coutry6 = geoip.Country6;
var country_v6 = new Country6('/path/to/GeoIPv6.dat');
// Synchronous method
var country_obj_v6 = country_v6.lookupSync('2607:f0d0:1002:0051:0000:0000:0000:0004');
-console.log(country_obj_v6); // Same as ipv4
+console.log(country_obj_v6);
/*
{ country_code: 'US',
country_code3: 'USA',
@@ -62,12 +66,17 @@ country_v6.lookup('2400:2352:b0f1:36c5:aa9d:694a:2f98:40bd', function(err, data_
country_name: 'Japan',
continent_code: 'AS'
}
-
*/
}
});
+
+////////////////////////////////////////////////////////////////////////////////
// City
+////////////////////////////////////////////////////////////////////////////////
+//////////////////////////
+// ipv4 address lookup ///
+//////////////////////////
var City = geoip.City;
var city = new City('/path/to/GeoLiteCity.dat');
@@ -102,8 +111,51 @@ city.lookup('www.google.com', function(err, data) {
}
});
+/////////////////////////
+// ipv6 address lookup //
+/////////////////////////
+var City6 = geoip.City6;
+var city6 = new City6('/path/to/GeoLiteCityv6.dat');
+
+// Synchronous method
+var city6_obj = city6.lookupSync('2002:a46f:f395:14de:f876:542b:41cb:5bbe');
+console.log(city6_obj);
+/*
+Return an object of city information
+{ country_code: 'US',
+ country_code3: 'USA',
+ country_name: 'United States',
+ region: 'AL',
+ city: 'Birmingham',
+ postal_code: '35294',
+ latitude: 33.54460144042969,
+ longitude: -86.92919921875,
+ continent_code: 'NA' }
+*/
+
+city6.update('/path/to/new/GeoLiteCityv6.dat');
+// Asynchronous method
+city6.lookup('2001:b565:487a:c05d:5f07:5662:c3cd:cfcf', function(err, data) {
+ if (err) {throw err;}
+ if (data) {
+ console.log(data);
+ /*
+ { country_code: 'TW',
+ country_code3: 'TWN',
+ country_name: 'Taiwan',
+ latitude: 23.5,
+ longitude: 121,
+ continent_code: 'AS'
+ }
+ */
+ }
+});
+
+
+////////////////////////////////////////////////////////////////////////////////
// Organization
+////////////////////////////////////////////////////////////////////////////////
var Org = geoip.Org;
var org = new Org('/path/to/database'); // Org module can open three edition database 'org', 'asnum', 'isp'
@@ -131,7 +183,9 @@ org.lookup('www.google.com', function(err, data) {
});
+////////////////////////////////////////////////////////////////////////////////
// Region
+////////////////////////////////////////////////////////////////////////////////
var Region = geoip.Region;
var region = new Region('/path/to/GeoIPRegion.dat');
@@ -155,8 +209,9 @@ region.lookup('www.google.com', function(data) {
});
-
+////////////////////////////////////////////////////////////////////////////////
// NetSpeed
+////////////////////////////////////////////////////////////////////////////////
var NetSpeed = geoip.NetSpeed;
var netspeed = new NetSpeed('/path/to/GeoIPNetSpeed.dat');
Please sign in to comment.
Something went wrong with that request. Please try again.