<commit_before>'use strict'

var geoip = require('ipfs-geoip')

function isLocal (address) {
  // IP6
  if (address === '::1') return true

  var split = address.split('.')
  if (split[0] === '10') return true
  if (split[0] === '127') return true
  if (split[0] === '192' && split[1] === '168') return true
  if (split[0] === '172' && +split[1] >= 16 && +split[1] <= 31) return true
  return false
}

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(ipfs, multiaddrs.slice(1), cb)

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

    if (!res.country_name && multiaddrs.length > 1) {
      return getLocation(ipfs, multiaddrs.slice(1), cb)
    }

    var location = 'Earth'
    if (res.country_name) location = res.country_name + ', ' + location
    if (res.region_code) location = res.region_code + ', ' + location
    if (res.city) location = res.city + ', ' + location

    res.formatted = location
    cb(null, res)
  })
}<commit_msg>fix: getLocation only works on ip4 addresses.<commit_after>'use strict'

var geoip = require('ipfs-geoip')

function isLocal (address) {
  var split = address.split('.')
  if (split[0] === '10') return true
  if (split[0] === '127') return true
  if (split[0] === '192' && split[1] === '168') return true
  if (split[0] === '172' && +split[1] >= 16 && +split[1] <= 31) return true
  return false
}

var getLocation = module.exports = function (ipfs, multiaddrs, cb) {
  if (multiaddrs.length === 0) return cb(null, null)
  var current = multiaddrs[0].split('/')
  var address = current[2]
  // No support for ip6 lookups currently
  if (isLocal(address) || current[1] === 'ip6') return getLocation(ipfs, multiaddrs.slice(1), cb)

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

    if (!res.country_name && multiaddrs.length > 1) {
      return getLocation(ipfs, multiaddrs.slice(1), cb)
    }

    var location = 'Earth'
    if (res.country_name) location = res.country_name + ', ' + location
    if (res.region_code) location = res.region_code + ', ' + location
    if (res.city) location = res.city + ', ' + location

    res.formatted = location
    cb(null, res)
  })
}
{'fraction_non_alphanumeric': 0.171828595672465, 'fraction_numerical': 0.028850233347475603, 'mean_word_length': 3.4323308270676693, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2800940', 'n_tokens_mistral': 861, 'n_tokens_neox': 816, 'n_words': 223}