forked from akash-network/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipLocationProvider.ts
72 lines (58 loc) · 1.83 KB
/
ipLocationProvider.ts
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { Provider } from "@src/../../shared/dbSchemas/akash";
import { sleep } from "@src/shared/utils/delay";
import axios from "axios";
import dns from "dns/promises";
const IpLookupDelay = 2_000;
async function getIpLocation(ip: string) {
const response = await axios.get(`http://ip-api.com/json/${ip}`);
if (response.data.status !== "success") {
throw new Error(`Could not get location for ip ${ip}`);
}
return {
region: response.data.regionName,
regionCode: response.data.region,
country: response.data.country,
countryCode: response.data.countryCode,
lat: response.data.lat,
lon: response.data.lon
};
}
export async function updateProvidersLocation() {
const providers = await Provider.findAll({
where: {
isOnline: true
}
});
console.log(`${providers.length} providers to lookup`);
for (const provider of providers) {
try {
const parsedUri = new URL(provider.hostUri);
const ips = await dns.resolve4(parsedUri.hostname);
if (ips.length === 0) {
console.log(`Could not resolve ip for ${provider.hostUri}`);
continue;
}
const ip = ips.sort()[0]; // Always use the first ip
if (provider.ip === ip) {
console.log(`Ip for ${provider.hostUri} is the same`);
continue;
}
const location = await getIpLocation(ip);
console.log(`${provider.hostUri} ip lookup: ${location.region}, ${location.country}`);
if (location) {
await provider.update({
ip: ip,
ipRegion: location.region,
ipRegionCode: location.regionCode,
ipCountry: location.country,
ipCountryCode: location.countryCode,
ipLat: location.lat,
ipLon: location.lon
});
}
await sleep(IpLookupDelay);
} catch (e) {
console.error(e);
}
}
}