Skip to content

Commit

Permalink
use geop-lite module instead of geoip
Browse files Browse the repository at this point in the history
  • Loading branch information
konklone committed Dec 23, 2018
1 parent 4619a83 commit 6c0c04a
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 766 deletions.
19 changes: 6 additions & 13 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@

// imbues Date with timezone abilities - must do this first
require('time')(Date);

// allow port to be passed in via command lne
var optimist = require('optimist');
var args = optimist.alias('port', 'p').argv;

var index = function(req, res) {
// this is only used as a fallback for clients who don't use JavaScript
// if I must pick a timezone for them, let's choose the opposite of the dateline
var utcTime = new Date();
utcTime.setTimezone("UTC");

var country = findCountry(req);

res.render('index', {
answer: Christmas.answer(country, utcTime),
answer: Christmas.answer(country),
country: country,

config: config,
Expand All @@ -40,8 +32,7 @@ var rss = function(req, res) {
/** helpers **/

// must have downloaded a country-level geoip dat file ahead of time
var geoip = require('geoip'),
countries = new geoip.Country('data/countries.dat');
var geoip = require('geoip-lite');

var findCountry = function(req) {
if (req.query.country) return req.query.country;
Expand All @@ -51,10 +42,12 @@ var findCountry = function(req) {

// debug: French IP
// ip = "193.51.208.14";
// debug: US IP
// ip = "207.97.227.239";
// console.log("Looking up IP [" + ip + "]");

var data = countries.lookupSync(ip);
var country = data ? data.country_code : null;
var data = geoip.lookup(ip);
var country = data ? data.country : null;

// console.log("Found country [" + country + "]");
return country;
Expand Down
54 changes: 46 additions & 8 deletions data/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
Place a Maxmind (or other GeoIP-compatible database) here for country-level lookups.
## GeoIP data for isitchristmas.com

This has been tested with the MaxMind `GeoIP Legacy Binary` format, which may be provided as a zipped `.dat` file.
This app uses the [`geoip-lite`](https://github.com/bluesmoon/node-geoip) library to process GeoIP2 CSV files into a binary `.dat` format, that is then read in directly by the app.

Rename the `.dat` file to be `countries.dat`, and place it in this directory. `data/countries.dat` is in this project's `.gitignore`.
Unfortunately, the process of doing this is a little complicated.

See [kuno/GeoIP](https://github.com/kuno/GeoIP) for more information.
1. Buy the paid GeoIP2 country CSV from Maxmind.
2. Download the free GeoLite2 city CSV from Maxmind. (Needed for parsing, not for looking up user city.)
3. Symlink the `data/` folder in this directory to be the local `data` folder inside `./node_modules/geoip-lite`, so that the results of the update script drop them here.
4. Update `geoip-lite`'s update script to use/download these CSVs during its update process.
5. Run the update script, which should create/update `geoip-city-name.dat`, `geoip-city.dat`, `geoip-city6.dat`, `geoip-country.dat`, and `geoip-country6.dat` inside the `/data` folder.

Note: Until `kuno/GeoIP` supports newer versions of Node (6.x and up), the project uses a fork that maintains an (outstanding pull request) for Node 6.x support.
When the app is run, running `require('geoip-lite')` should use the `.dat` files to populate the database.

* Fork: https://github.com/dankang/GeoIP
* Fork backup: https://github.com/konklone/GeoIP
* branch: `feature/support_nodejs_6x`

### Implementation notes

Example of database object to use in `updatedb.js`:

```javascript
var databases = [
{
type: 'country',
url: 'http://localhost:5000/GeoIP2-Country-CSV_20181218.zip',
src: [
'GeoIP2-Country-Locations-en.csv',
'GeoIP2-Country-Blocks-IPv4.csv',
'GeoIP2-Country-Blocks-IPv6.csv'
],
dest: [
'',
'geoip-country.dat',
'geoip-country6.dat'
]
},
{
type: 'city',
url: 'http://localhost:5000/GeoLite2-City-CSV_20181218.zip',
src: [
'GeoLite2-City-Locations-en.csv',
'GeoLite2-City-Blocks-IPv4.csv',
'GeoLite2-City-Blocks-IPv6.csv'
],
dest: [
'geoip-city-names.dat',
'geoip-city.dat',
'geoip-city6.dat'
]
}
];
```
Loading

0 comments on commit 6c0c04a

Please sign in to comment.