Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions test/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,54 @@ describe('RPC integration', () => {
}
})
}).timeout(7000)

it('geo-ip: retrieves ips with accuracy_radius < 500km has confidence score > 0', (done) => {
const query = {
action: 'getIpGeo',
args: ['2.125.160.216']
}

client.request(query, (err, data) => {
try {
if (err) throw err

assert.strictEqual(
data[0], '2.125.160.216', 'result contains queried ip'
)

const res = data[1]
assert.ok(res.area < 500)
assert.ok(res.confidenceScore > 0 && res.confidenceScore < 1)
assert.strictEqual(res.country, 'GB')
done()
} catch (err) {
done(err)
}
})
}).timeout(7000)

it('geo-ip: retrieves ips with accuracy_radius > 500km has confidence score=0', (done) => {
const query2 = {
action: 'getIpGeo',
args: ['8.8.8.8']
}

client.request(query2, (err, data) => {
try {
if (err) throw err

assert.strictEqual(
data[0], '8.8.8.8', 'result contains queried ip'
)

const res = data[1]
assert.ok(res.area > 500)
assert.strictEqual(res.confidenceScore, 0)
assert.strictEqual(res.country, 'US')
done()
} catch (err) {
done(err)
}
})
}).timeout(7000)
})
30 changes: 26 additions & 4 deletions workers/loc.api/net.util.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ class UtilNet extends Api {
}

getIpInfo (space, ip, cb) {
const geoData = this.ctx.geoIp.lookup(ip)
const geoData = this._getGeoIp(ip)
const asnData = this.ctx.asnDb.get(ip)
const ispData = this.ctx.ispDb.get(ip)
const connectionTypeData = this.ctx.connectionTypeDb.get(ip)

dns.reverse(ip, (err, dnsData) => {
if (err) return cb(err)

const confidenceScore = this._calculateConfidenceScore(geoData)
const res = [
ip,
{ geo: geoData, dns: dnsData, asn: asnData, isp: ispData, connectionType: connectionTypeData }
{ geo: geoData, dns: dnsData, asn: asnData, isp: ispData, connectionType: connectionTypeData, confidenceScore }
]

cb(null, res)
Expand All @@ -46,7 +47,7 @@ class UtilNet extends Api {
}

getIpGeo (space, ip, cb) {
const res = this.ctx.geoIp.lookup(ip)
const res = this._getGeoIp(ip)
cb(null, [ip, res])
}

Expand Down Expand Up @@ -74,7 +75,7 @@ class UtilNet extends Api {
}

const res = ips.map((ip) => {
return [ip, this.ctx.geoIp.lookup(ip)]
return [ip, this._getGeoIp(ip)]
})

cb(null, res)
Expand All @@ -87,6 +88,27 @@ class UtilNet extends Api {
cb(null, [ip, data])
})
}

_getGeoIp (ip) {
const res = this.ctx.geoIp.lookup(ip)
const confidenceScore = this._calculateConfidenceScore(res)
return { ...res, confidenceScore }
}

_calculateConfidenceScore (geo) {
if (!geo || geo.area == null) {
return 0
}
const accuracyRadius = geo.area

// Define a max expected radius beyond which confidence is zero
const maxExpectedRadius = 500 // kilometers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to config please

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved!


// Simple linear mapping of radius to confidence score
const confidence = Math.max(0, Math.min(1, 1 - (accuracyRadius / maxExpectedRadius)))

return confidence
}
}

module.exports = UtilNet