Skip to content

Commit

Permalink
Merge pull request #44 from ipfs/geoip-over-ipfs
Browse files Browse the repository at this point in the history
Make geoip requests over ipfs
  • Loading branch information
jbenet committed May 5, 2015
2 parents b2c633a + 43f652d commit a150ec4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
14 changes: 9 additions & 5 deletions js/getlocation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

var $ = require('jquery')
var geoip = require('ipfs-geoip')

function isLocal (address) {
var split = address.split('.')
Expand All @@ -11,14 +11,18 @@ function isLocal (address) {
return false
}

var getLocation = module.exports = function (multiaddrs, cb) {
var getLocation = module.exports = function (ipfs, multiaddrs, cb) {
if (multiaddrs.length === 0) return cb(null, null)
var address = multiaddrs[0].split('/')[2]
if (isLocal(address)) return getLocation(multiaddrs.slice(1), cb)
if (isLocal(address)) return getLocation(ipfs, multiaddrs.slice(1), cb)

geoip.lookup(ipfs, address, function (err, res) {
if (err) {
throw err
}

$.get('http://freegeoip.net/json/' + address, function (res) {
if (!res.country_name && multiaddrs.length > 1) {
return getLocation(multiaddrs.slice(1), cb)
return getLocation(ipfs, multiaddrs.slice(1), cb)
}

var location = 'Earth'
Expand Down
2 changes: 1 addition & 1 deletion js/pages/connections.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var Connections = React.createClass({
t.props.ipfs.id(peer.ID, function (err, id) {
if (err) return console.error(err)

getLocation(id.Addresses, function (err, res) {
getLocation(t.props.ipfs, id.Addresses, function (err, res) {
if (err) return console.error(err)
// If we've unmounted, abort
if (!t.isMounted()) return
Expand Down
2 changes: 1 addition & 1 deletion js/pages/home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var Home = React.createClass({
}
})

getLocation(peer.Addresses, function (err, location) {
getLocation(t.props.ipfs, peer.Addresses, function (err, location) {
if (err || !location) return console.error(err)
t.setState({
node: {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"font-awesome": "^4.3.0",
"font-awesome-webpack": "0.0.3",
"ipfs-api": "^1.2.0",
"ipfs-geoip": "^1.4.0",
"jquery": "^2.1.3",
"lodash": "^3.6.0",
"react": "^0.13.1",
Expand Down

0 comments on commit a150ec4

Please sign in to comment.