-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented geoliteDB.get(db), releasing 1.0.0
various formatting improvements, added accuracy_radius because I found it was still relevant in the free data (my bad!) updated tests to include and validate some data
- Loading branch information
Showing
13 changed files
with
423 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
get: (t, geoname_id) => t.one('SELECT continent_code, country_iso_code, subdivision_1_iso_code, subdivision_2_iso_code, metro_code, time_zone FROM geolite2_city_locations WHERE geoname_id = $1', geoname_id), | ||
getCountry: (t, geoname_id) => t.one('SELECT country_iso_code FROM geolite2_city_locations WHERE geoname_id = $1', geoname_id), | ||
getLocales: (t, geoname_id) => t.many('SELECT locale_code, continent_name, country_name, subdivision_1_name, subdivision_2_name, city_name FROM geolite2_city_locations_locales WHERE geoname_id = $1 ORDER BY locale_code ASC', geoname_id), | ||
getCountryLocales: (t, geoname_id) => t.many('SELECT locale_code, country_name FROM geolite2_city_locations_locales WHERE geoname_id = $1 ORDER BY locale_code ASC', geoname_id), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
'use strict'; | ||
|
||
const ip = require('ip-address'); | ||
|
||
const invalid_v4 = [ | ||
new ip.Address4('0.0.0.0/8'), | ||
new ip.Address4('10.0.0.0/8'), | ||
new ip.Address4('100.64.0.0/10'), | ||
new ip.Address4('127.0.0.0/8'), | ||
new ip.Address4('127.0.53.53'), | ||
new ip.Address4('169.254.0.0/16'), | ||
new ip.Address4('172.16.0.0/12'), | ||
new ip.Address4('192.0.0.0/24'), | ||
new ip.Address4('192.0.2.0/24'), | ||
new ip.Address4('192.168.0.0/16'), | ||
new ip.Address4('198.18.0.0/15'), | ||
new ip.Address4('198.51.100.0/24'), | ||
new ip.Address4('203.0.113.0/24'), | ||
new ip.Address4('224.0.0.0/4'), | ||
new ip.Address4('240.0.0.0/4'), | ||
new ip.Address4('255.255.255.255/32'), | ||
]; | ||
|
||
const invalid_v6 = [ | ||
new ip.Address6('::/128'), | ||
new ip.Address6('::1/128'), | ||
new ip.Address6('::ffff:0:0/96'), | ||
new ip.Address6('::/96'), | ||
new ip.Address6('100::/64'), | ||
new ip.Address6('2001:10::/28'), | ||
new ip.Address6('2001:db8::/32'), | ||
new ip.Address6('fc00::/7'), | ||
new ip.Address6('fe80::/10'), | ||
new ip.Address6('fec0::/10'), | ||
new ip.Address6('2002::/24'), | ||
new ip.Address6('2002:a00::/24'), | ||
new ip.Address6('2002:7f00::/24'), | ||
new ip.Address6('2002:a9fe::/32'), | ||
new ip.Address6('2002:ac10::/28'), | ||
new ip.Address6('2002:c000::/40'), | ||
new ip.Address6('2002:c000:200::/40'), | ||
new ip.Address6('2002:c0a8::/32'), | ||
new ip.Address6('2002:c612::/31'), | ||
new ip.Address6('2002:c633:6400::/40'), | ||
new ip.Address6('2002:cb00:7100::/40'), | ||
new ip.Address6('2002:e000::/20'), | ||
new ip.Address6('2002:f000::/20'), | ||
new ip.Address6('2002:ffff:ffff::/48'), | ||
new ip.Address6('2001::/40'), | ||
new ip.Address6('2001:0:a00::/40'), | ||
new ip.Address6('2001:0:7f00::/40'), | ||
new ip.Address6('2001:0:a9fe::/48'), | ||
new ip.Address6('2001:0:ac10::/44'), | ||
new ip.Address6('2001:0:c000::/56'), | ||
new ip.Address6('2001:0:c000:200::/56'), | ||
new ip.Address6('2001:0:c0a8::/48'), | ||
new ip.Address6('2001:0:c612::/47'), | ||
new ip.Address6('2001:0:c633:6400::/56'), | ||
new ip.Address6('2001:0:cb00:7100::/56'), | ||
new ip.Address6('2001:0:e000::/36'), | ||
new ip.Address6('2001:0:f000::/36'), | ||
new ip.Address6('2001:0:ffff:ffff::/64'), | ||
]; | ||
|
||
module.exports = (ipaddr) => { | ||
const ipv4 = new ip.Address4(ipaddr); | ||
const ipv6 = new ip.Address6(ipaddr); | ||
if (ipv4.isValid()) { | ||
if (invalid_v4.every((subnet) => !ipv4.isInSubnet(subnet))) { | ||
return ipv4; | ||
} | ||
} else if (ipv6.isValid()) { | ||
if (invalid_v6.every((subnet) => !ipv6.isInSubnet(subnet))) { | ||
return ipv6; | ||
} | ||
} | ||
return false; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict'; | ||
|
||
const getIP = require('./getip'); | ||
const pretty = require('./pretty'); | ||
|
||
module.exports = (db) => { | ||
const ip = require('./ip'); | ||
const geonames = require('./geonames'); | ||
|
||
const queries = { | ||
get: (ipaddr) => new Promise((resolve, reject) => { | ||
ipaddr = getIP(ipaddr); | ||
if (ipaddr) { | ||
if (ipaddr.v4) { | ||
ip.getv4(db, ipaddr.address) | ||
.then(iploc => { | ||
db.task(t => { | ||
const ops = [ip.getASNv4(t, ipaddr.address), geonames.get(t, iploc.geoname_id), geonames.getLocales(t, iploc.geoname_id)]; | ||
if (iploc.registered_country_geoname_id !== null) { | ||
ops.push(geonames.getCountry(t, iploc.registered_country_geoname_id)); | ||
ops.push(geonames.getCountryLocales(t, iploc.registered_country_geoname_id)); | ||
} | ||
if (iploc.represented_country_geoname_id !== null) { | ||
ops.push(geonames.getCountry(t, iploc.represented_country_geoname_id)); | ||
ops.push(geonames.getCountryLocales(t, iploc.represented_country_geoname_id)); | ||
} | ||
return t.batch(ops); | ||
}) | ||
.then(data => { | ||
resolve(pretty(iploc, data)); | ||
}) | ||
.catch(reject); | ||
}) | ||
.catch(reject); | ||
} else { | ||
ip.getv6(db, ipaddr.address) | ||
.then(iploc => { | ||
db.task(t => { | ||
const ops = [ip.getASNv6(t, ipaddr.address), geonames.get(t, iploc.geoname_id), geonames.getLocales(t, iploc.geoname_id)]; | ||
if (iploc.registered_country_geoname_id !== null) { | ||
ops.push(geonames.getCountry(t, iploc.registered_country_geoname_id)); | ||
ops.push(geonames.getCountryLocales(t, iploc.registered_country_geoname_id)); | ||
} | ||
if (iploc.represented_country_geoname_id !== null) { | ||
ops.push(geonames.getCountry(t, iploc.represented_country_geoname_id)); | ||
ops.push(geonames.getCountryLocales(t, iploc.represented_country_geoname_id)); | ||
} | ||
return t.batch(ops); | ||
}) | ||
.then(data => { | ||
resolve(pretty(iploc, data)); | ||
}) | ||
.catch(reject); | ||
}) | ||
.catch(reject); | ||
} | ||
} else { | ||
reject({ | ||
error: 'invalid_ip', | ||
}); | ||
} | ||
}), | ||
}; | ||
|
||
return queries; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
getv4: (t, ip) => t.one('SELECT geoname_id, registered_country_geoname_id, represented_country_geoname_id, postal_code, latitude, longitude, accuracy_radius FROM geolite2_city_ipv4 WHERE $1::inet <<= network', ip), | ||
getv6: (t, ip) => t.one('SELECT geoname_id, registered_country_geoname_id, represented_country_geoname_id, postal_code, latitude, longitude, accuracy_radius FROM geolite2_city_ipv6 WHERE $1::inet <<= network', ip), | ||
getASNv4: (t, ip) => t.one('SELECT asn, org FROM geolite_asn WHERE $1::inet BETWEEN ip_start AND ip_end', ip), | ||
getASNv6: (t, ip) => t.one('SELECT asn, org FROM geolite_asn_ipv6 WHERE $1::inet <<= network', ip), | ||
}; |
Oops, something went wrong.