|
| 1 | +import { promises as fs } from 'fs' |
| 2 | + |
| 3 | +async function holdOn(time) { |
| 4 | + return new Promise((resolve) => { |
| 5 | + setTimeout(resolve, time) |
| 6 | + }) |
| 7 | +} |
| 8 | + |
| 9 | +async function queryServerLocations(addresses) { |
| 10 | + return new Promise(async (resolve, reject) => { |
| 11 | + const response = await fetch('http://ip-api.com/batch?fields=lat,lon,query', { |
| 12 | + method: 'POST', |
| 13 | + body: JSON.stringify(addresses) |
| 14 | + }) |
| 15 | + |
| 16 | + const data = await response.json() |
| 17 | + resolve(data) |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | +function countIPs(addresses) { |
| 22 | + const counts = new Map() |
| 23 | + addresses.forEach((address) => { |
| 24 | + const base = address.split(":")[0] |
| 25 | + counts.set(base, (counts.get(base) || 0) + 1) |
| 26 | + }) |
| 27 | + |
| 28 | + return counts |
| 29 | +} |
| 30 | + |
| 31 | +const fileName = process.argv[2] |
| 32 | + |
| 33 | +fs.readFile(fileName).then(async (dataString) => { |
| 34 | + const data = JSON.parse(dataString) |
| 35 | + const counts = countIPs(data) |
| 36 | + |
| 37 | + const uniqueIPs = [...counts.keys()] |
| 38 | + const chunkSize = 100 |
| 39 | + let features = [] |
| 40 | + |
| 41 | + for (let i = 0; i < uniqueIPs.length; i += chunkSize) { |
| 42 | + const chunk = uniqueIPs.slice(i, i + chunkSize) |
| 43 | + const locations = await queryServerLocations(chunk) |
| 44 | + |
| 45 | + const locationFeatures = locations.map((data) => { |
| 46 | + return { |
| 47 | + "type": "Feature", |
| 48 | + "geometry": { |
| 49 | + "type": "Point", |
| 50 | + "coordinates": [data.lon, data.lat], |
| 51 | + }, |
| 52 | + "properties": { |
| 53 | + "count": counts.get(data.query), |
| 54 | + "ip": data.query |
| 55 | + } |
| 56 | + } |
| 57 | + }) |
| 58 | + features = features.concat(locationFeatures) |
| 59 | + console.log('Processed', features.length) |
| 60 | + await holdOn(5000) |
| 61 | + } |
| 62 | + await fs.writeFile(fileName.replace(".json", ".geojson"), JSON.stringify({ "type": "FeatureCollection", features: features })) |
| 63 | +}) |
| 64 | + |
| 65 | + |
0 commit comments