Giter Club home page Giter Club logo

geoip's Introduction

##GeoIP API for node.

####Emergent Update####

Warning: Due to the Asynchronous programming problome,

Any version that below the 0.1.3 dosen't work in any real script.

I highly recommond that every user should update to at least v0.1.3!

###Description

Get geolocation information based on domain or IP address.

###Compatibility###

v0.3.1-1, Passed test on nodejs v0.2.0 ~ 0.2.6, v0.3.0 ~ 0.3.3.

###Architecture

architecture

###Data

Befor you can use this package, you need to download or buy some data from www.maxmind.com.

There are three free versions data among with some commercial versions.

Note: This package only support binary data, not any other formats.

GeoIP Country Lite Edition Download.

GeoIP City Lite Edition Download

GeoIP ASN Edition download

###Install

npm install geoip

###Usage

####Country Information

  // Open the country data file
  var country_data = geoip.open('/path/to/GeoIP.dat');

1, Synchronous methods, network independence.

  geoip.Country.code_by_addr(country_data, '8.8.8.8'); // Return 'US'

  geoip.Country.name_by_addr(country_data, '8.8.8.8'); // Return  'United States'

2, Asynchronous methods, depends on node's async-style dns module.

  geoip.Country.code_by_domain(country_data, 'www.google.com', function(err, code) {
        if (err) {throw err;}
        console.log(code);  // prints 'US'
  });

  geoip.Country.name_by_domain(country_data, 'www.google.com', function(err, name) {
        if (err) {throw err;}
        console.log(name);  // prints 'United States'
  });

  //Close the opened file.
  geoip.close(country_data);

####City Information

  // Open the GeoLiteCity.dat file first.
  var city_data = geoip.open('/path/to/GeoLiteCity.dat');

1, Synchronous method geoip.City.record_by_addr(city_data, '8.8.8.8'); // 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 // }

2, Asynchronous method

  geoip.City.record_by_domain(city_data, 'www.google.com', function(err, reord) {
        if (err) {throw err;}
        var keys = Object.keys(record);
        keys.forEach(function(k) {  // Same as record_by_addr
            console.log(k + ':' + record[k]);
        });   
  });

  geoip.close(city_data);

####Organization Information

#####Get Organization Information#####

  // Open the GeoIPOrg.dat first.
  var org_data = geoip.open('/path/to/GeoIPOrg.dat');

1, Synchronous method

  geoip.Org.org_by_addr(org_data, '8.8.8.8');
  // Return an array of the names of organization
  // [
  // 'Genuity',
  // 'The United Way',
  // 'Education Management Corporation,
  // 'International Generating Co. (Intergen)'
  // ]    

  geoip.close(org_data);

2, Asynchronous method

  // This method has a small bug that not resovled yet, not recommend use it.
  geoip.Org.org_by_domain(org_data, 'www.google.com', function(err, org) {
      if (err) {throw err;}
      if (typeof org === 'string') {
          console.log(org);  // Organization Not Found
      } else {  // Same as org_by_addr
          org.foreach(function(o) {
          console.log(o[0] + ':' + o[1]);
      });
      }
  });

#####Get ASN informatioin######

  // Open the GeoIPASNum.dat first.

  var asn_data = geoip.open('/path/to/GeoIPASNum.dat');

1, Synchronous method

  geoip.Org.asn_by_addr(asn_data, '8.8.8.8');
  // Return an array of asn objects
  //[ 
  //  { number: 'AS15169', description: 'Google Inc.' },
  //  { number: 'AS36597',
  //    description: 'OmniTI Computer Consulting Inc.' },
  //  { number: 'AS26471',
  //    description: 'Smart City Networks' } 
  //]

2, Asynchronous method

  geoip.Org.asn_by_domain(asn_data, 'www.google.com', function(err, asn) {
      if (err) {throw err;}
      if (typeof ans === 'string') {
          console.log(asn)  // ASNumber Not Found
      } else {  // Same as asn_by_addr
          asn.forEach(function(a) {
              var keys = object.keys(a);
              console.log(a[keys[0]] + ' : ' + a[keys[1]]);
          });
      }
  });

  geoip.close(asn_data);

####Region Information

  // Open the GeoIPRegion.dat first.
  var region_data = geoip.open('/path/to/GeoIPRegion.dat');

1, Synchronous method

  geoip.Region.region_by_addr(region_data, '8.8.8.8');  // Return 'US,CO'

2, Asynchronous method

  geoip.Region.region_by_domain(region_data, 'www.google.com', function(err, region) {
      if (err) {throw err;}
      console.log(region);  // Maybe different from region_by_addr
  });

  geoip.close(region_data);

####NetSpeed Information

  // Open the GeoIPNetSpeed.dat first.
  var netspeed_data = geoip.open('/path/to/GeoIPNetSpeed.dat');

1, Synchronous method

  geoip.NetSpeed.speed_by_addr(netspeed_data, '8.8.8.8');  // Return 'Dailup'

2, Asynchronous method

  NetSpeed.speed_by_domain(data, 'www.google.com', function(err, speed) {
      if (err) {throw err;}
      console.log(speed);  // Maybe different from speed_by_addr
  });

  geoip.close(netspeed_data);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.