-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_ip.js
33 lines (29 loc) · 894 Bytes
/
client_ip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
var redis = require("redis");
const ipInt = require("ip-to-int");
const client = redis.createClient();
client.on("error", function(err) {
console.log("Error " + err);
});
function int_to_ip(ip) {
try {
const int_ip = ipInt(ip).toInt();
return int_ip
} catch (e) {
console.log("Error while converting IP to Int hax", e)
}
}
async function ip_redis(ip) {
await client.connect()
const ip_int = int_to_ip(ip)
const geoname_id = await client.zRangeByScore('ip-loc', ip_int, '+inf', {
LIMIT: {
count: 1,
offset: 0
}
})
const json_data = JSON.parse(geoname_id[0])
console.log("Geoname ID : ", json_data.geoname_id)
const city_data = await client.ft.search('idx:ipIdx', `@geoname_id:(${json_data.geoname_id})`)
console.log("City data for IP", city_data.documents)
}
ip_redis("1.0.0.1")