Repository navigation
Expand file tree
/
Copy pathipGeolocation.js
More file actions
48 lines (44 loc) · 1.57 KB
/
Copy pathipGeolocation.js
File metadata and controls
48 lines (44 loc) · 1.57 KB
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
const https = require('https')
function fetchIpLocationResults(ips, apiKey) {
if (!apiKey) {
throw new Error('IPGeolocation API key not found. Please set it in Settings > API Keys')
}
const list = Array.isArray(ips) ? ips.filter(Boolean) : [ips].filter(Boolean)
if (!list.length) {
throw new Error('At least one IP address is required')
}
return Promise.all(
list.map((ip) => new Promise((resolve) => {
const url = `https://api.ipgeolocation.io/ipgeo?apiKey=${apiKey}&ip=${encodeURIComponent(ip)}`
https.get(url, { timeout: 10000 }, (res) => {
let data = ''
res.on('data', (chunk) => { data += chunk })
res.on('end', () => {
try {
const result = JSON.parse(data)
if (result.message || result.error) {
resolve({ ip, error: result.message || result.error || 'Invalid IP address' })
} else {
resolve({
ip: result.ip || ip,
country: result.country_name,
region: result.state_prov,
city: result.city,
zip: result.zipcode,
lat: result.latitude,
lon: result.longitude,
isp: result.isp,
org: result.organization || result.isp
})
}
} catch {
resolve({ ip, error: 'Failed to parse location data' })
}
})
}).on('error', (error) => {
resolve({ ip, error: error.message || 'Network error' })
})
}))
)
}
module.exports = { fetchIpLocationResults }