geoip lookup over ipfs
npm install --save ipfs-geoip
Instead of a local installation (and browserification) you may request a remote copy from jsDelivr:
<!-- loading the minified version using jsDelivr -->
<script src="https://cdn.jsdelivr.net/npm/ipfs-geoip@4.1.0/dist/index.min.js"></script>
When using prebuilt bundle from CDN, ipfs-geoip
will be exposed under window.IpfsGeoip
const geoip = require('ipfs-geoip')
const ipfs = require('ipfs-http-client')()
const exampleIp = '89.114.95.36'
try {
const result = await geoip.lookup(ipfs, exampleIp)
console.log('Result: ', result)
} catch (err) {
console.log('Error: ' + err)
}
try {
const result = await geoip.lookupPretty(ipfs, '/ip4/' + exampleIp)
console.log('Pretty result: %s', result.formatted)
} catch (err) {
console.log('Error: ' + err)
}
Returns a promise that resolves to an object of the form
{
"country_code": "US",
"country_name": "United States",
"region_code": "CA",
"city": "Mountain View",
"postal_code": "94040",
"latitude": 37.3860,
"longitude": -122.0838,
"metro_code": "807",
"area_code": "650",
"planet": "Earth"
}
Provides the same results as lookup
with the addition of
a formatted
property that looks like this: Mountain View, CA, United States, Earth
.
The utility geoip-gen reads csv files provided from GeoLite, and turns them into a 32-way branching b-tree, which is stored as ipfs json objects.
There is a generator included, that can be run with
$ npm run generate
This takes quite a long time to import, but you only need to do it once globally to use the lookup feature.
You can find an example of how to use this in example/lookup.js
, which you can use like this:
$ node example/lookup.js 8.8.8.8
Result: {
"country_name": "United States",
"country_code": "US",
"region_code": "CA",
"city": "Mountain View",
"postal_code": "94040",
"latitude": 37.386,
"longitude": -122.0838,
"metro_code": "807",
"area_code": "650",
"planet": "Earth"
}
Pretty result: Mountain View, CA, United States, Earth
The current root hash for lookups is QmRn43NNNBEibc6m7zVNcS6UusB1u3qTTfyoLmkugbeeGJ
.
Feel free to join in. All welcome. Open an issue!
This repository falls under the IPFS Code of Conduct.