-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResolve-GeoIP.ps1
More file actions
425 lines (369 loc) · 16.6 KB
/
Copy pathResolve-GeoIP.ps1
File metadata and controls
425 lines (369 loc) · 16.6 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
function Resolve-GeoIP
{
<#
.SYNOPSIS
Resolves IP addresses to geographic location information.
.DESCRIPTION
Queries IP geolocation services to retrieve geographic information for IP addresses
including country, region, city, coordinates, timezone, ISP, and other location data.
Supports multiple free geolocation API services with automatic fallback:
- ipapi.co (default) - Comprehensive data with 1000 requests/day free tier
- ip-api.com - No key required, detailed info, 45 requests/minute
- ipinfo.io - Clean API with city/region/country
- ipwhois.app - No registration required
Compatible with PowerShell Desktop 5.1+ on Windows, macOS, and Linux.
.PARAMETER IPAddress
The IP address(es) to geolocate. Supports both IPv4 and IPv6.
Accepts pipeline input for bulk lookups.
If not specified, resolves the current public IP address.
.PARAMETER Service
The geolocation service to use.
Valid values: 'ipapi', 'ip-api', 'ipinfo', 'ipwhois', 'auto'
Default is 'auto' which tries multiple services for reliability.
.PARAMETER Timeout
Request timeout in seconds.
Default is 10 seconds. Valid range: 5-30 seconds.
.PARAMETER IncludeRaw
Include the raw JSON response from the API in the output.
Useful for debugging or accessing additional fields not parsed by default.
.EXAMPLE
PS > Resolve-GeoIP
IP : 201.XX.XX.XX
City : Austin
Region : Texas
RegionCode : TX
Country : United States
CountryCode : US
Continent : NA
Latitude : 99.9999
Longitude : -99.9999
Timezone : America/Chicago
ISP : CHARTER
Organization : CHARTER
ASN : AS20115
PostalCode : XXXXX
Service : ipapi.co
Gets geolocation information for your current public IP address.
.EXAMPLE
PS > Resolve-GeoIP -IPAddress '8.8.8.8'
Gets geolocation information for Google's public DNS server.
.EXAMPLE
PS > Resolve-GeoIP -IPAddress '1.1.1.1' -Service ipapi
Gets geolocation for Cloudflare DNS using the ipapi.co service.
.EXAMPLE
PS > @('8.8.8.8', '1.1.1.1', '208.67.222.222') | Resolve-GeoIP
Gets geolocation for multiple IP addresses using pipeline input.
.EXAMPLE
PS > Resolve-GeoIP -IPAddress '8.8.8.8' -IncludeRaw
Gets geolocation with raw API response included.
.EXAMPLE
PS > Resolve-GeoIP -IPAddress '2001:4860:4860::8888'
Gets geolocation for an IPv6 address.
.EXAMPLE
PS > Get-Content ./failed-logins.txt | Resolve-GeoIP -Service ipwhois | Select-Object IP,Country,ISP
Enriches a list of suspicious IPs captured from application logs before blocking them in a firewall rule.
.OUTPUTS
System.Management.Automation.PSCustomObject
Returns objects with IP, City, Region, Country, Latitude, Longitude, Timezone, ISP, and other properties.
.LINK
https://ipapi.co/api/
.LINK
https://ip-api.com/docs
.LINK
https://ipinfo.io/developers
.NOTES
API Service Details:
- ipapi.co: 1000 requests/day free, HTTPS, detailed data
- ip-api.com: 45 requests/minute, no key required, comprehensive
- ipinfo.io: 50k requests/month, simple and reliable
- ipwhois.app: No registration, unlimited for non-commercial
Rate Limiting: Free tiers have rate limits. For bulk queries, add delays
between requests or use the auto service which handles fallback.
Privacy: IP geolocation queries send IP addresses to external services.
Author: Jon LaBelle
License: MIT
.LINK
https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/NetworkAndDns/Resolve-GeoIP.ps1
Source: https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/NetworkAndDns/Resolve-GeoIP.ps1
#>
[CmdletBinding()]
[OutputType([System.Management.Automation.PSCustomObject])]
param(
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, Position = 0)]
[AllowEmptyString()]
[AllowNull()]
[Alias('IP', 'Address')]
[String[]]$IPAddress,
[Parameter()]
[ValidateSet('ipapi', 'ip-api', 'ipinfo', 'ipwhois', 'auto')]
[String]$Service = 'auto',
[Parameter()]
[ValidateRange(5, 30)]
[Int32]$Timeout = 10,
[Parameter()]
[Switch]$IncludeRaw
)
begin
{
Write-Verbose 'Initializing IP geolocation lookup'
# API service configurations
$services = @{
'ipapi' = @{
Name = 'ipapi.co'
UrlBase = 'https://ipapi.co'
Format = 'json'
}
'ip-api' = @{
Name = 'ip-api.com'
UrlBase = 'http://ip-api.com/json'
Format = 'json'
}
'ipinfo' = @{
Name = 'ipinfo.io'
UrlBase = 'https://ipinfo.io'
Format = 'json'
}
'ipwhois' = @{
Name = 'ipwhois.app'
UrlBase = 'https://ipwhois.app/json'
Format = 'json'
}
}
# Order for auto fallback
$serviceOrder = @('ipapi', 'ip-api', 'ipinfo', 'ipwhois')
}
process
{
# If no IP specified, get current public IP
if (-not $IPAddress)
{
Write-Verbose 'No IP address specified, resolving current public IP'
$IPAddress = @('') # Empty string means current IP for most services
}
foreach ($ip in $IPAddress)
{
Write-Verbose "Resolving geolocation for IP: $(if ($ip) { $ip } else { 'current public IP' })"
$httpClient = $null
try
{
$httpClient = [System.Net.Http.HttpClient]::new()
$httpClient.Timeout = [TimeSpan]::FromSeconds($Timeout)
$httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("PowerShell/$($PSVersionTable.PSVersion.ToString())")
# Determine which services to try
$servicesToTry = if ($Service -eq 'auto')
{
$serviceOrder
}
else
{
@($Service)
}
$success = $false
foreach ($svc in $servicesToTry)
{
try
{
$config = $services[$svc]
Write-Verbose "Trying service: $($config.Name)"
# Build URL
$url = switch ($svc)
{
'ipapi'
{
if ($ip)
{
"$($config.UrlBase)/$ip/json/"
}
else
{
"$($config.UrlBase)/json/"
}
}
'ip-api'
{
if ($ip)
{
"$($config.UrlBase)/$ip"
}
else
{
"$($config.UrlBase)"
}
}
'ipinfo'
{
if ($ip)
{
"$($config.UrlBase)/$ip/json"
}
else
{
"$($config.UrlBase)/json"
}
}
'ipwhois'
{
if ($ip)
{
"$($config.UrlBase)/$ip"
}
else
{
"$($config.UrlBase)"
}
}
}
Write-Verbose "Query URL: $url"
# Send request
$response = $httpClient.GetAsync($url).GetAwaiter().GetResult()
if ($response.IsSuccessStatusCode)
{
$jsonContent = $response.Content.ReadAsStringAsync().GetAwaiter().GetResult()
$data = $jsonContent | ConvertFrom-Json
# Check for API errors
$hasError = $false
if ($svc -eq 'ip-api' -and $data.status -eq 'fail')
{
Write-Verbose "Service returned error: $($data.message)"
$hasError = $true
}
elseif ($svc -eq 'ipapi' -and $data.error)
{
Write-Verbose "Service returned error: $($data.reason)"
$hasError = $true
}
if (-not $hasError)
{
# Parse response based on service format
$result = [PSCustomObject]@{
IP = $null
City = $null
Region = $null
RegionCode = $null
Country = $null
CountryCode = $null
Continent = $null
Latitude = $null
Longitude = $null
Timezone = $null
ISP = $null
Organization = $null
ASN = $null
PostalCode = $null
Service = $config.Name
}
# Map fields based on service
switch ($svc)
{
'ipapi'
{
$result.IP = $data.ip
$result.City = $data.city
$result.Region = $data.region
$result.RegionCode = $data.region_code
$result.Country = $data.country_name
$result.CountryCode = $data.country_code
$result.Continent = $data.continent_code
$result.Latitude = $data.latitude
$result.Longitude = $data.longitude
$result.Timezone = $data.timezone
$result.ISP = $data.org
$result.Organization = $data.org
$result.ASN = $data.asn
$result.PostalCode = $data.postal
}
'ip-api'
{
$result.IP = $data.query
$result.City = $data.city
$result.Region = $data.regionName
$result.RegionCode = $data.region
$result.Country = $data.country
$result.CountryCode = $data.countryCode
$result.Continent = $data.continent
$result.Latitude = $data.lat
$result.Longitude = $data.lon
$result.Timezone = $data.timezone
$result.ISP = $data.isp
$result.Organization = $data.org
$result.ASN = $data.as
$result.PostalCode = $data.zip
}
'ipinfo'
{
$result.IP = $data.ip
$result.City = $data.city
$result.Region = $data.region
$result.Country = $data.country
$result.Organization = $data.org
# Parse location coordinates
if ($data.loc)
{
$coords = $data.loc -split ','
if ($coords.Count -eq 2)
{
$result.Latitude = [decimal]$coords[0]
$result.Longitude = [decimal]$coords[1]
}
}
$result.Timezone = $data.timezone
$result.PostalCode = $data.postal
}
'ipwhois'
{
$result.IP = $data.ip
$result.City = $data.city
$result.Region = $data.region
$result.Country = $data.country
$result.CountryCode = $data.country_code
$result.Continent = $data.continent
$result.Latitude = $data.latitude
$result.Longitude = $data.longitude
$result.Timezone = $data.timezone
$result.ISP = $data.isp
$result.Organization = $data.org
$result.ASN = $data.asn
}
}
# Add raw response if requested
if ($IncludeRaw)
{
$result | Add-Member -NotePropertyName 'RawResponse' -NotePropertyValue $data
}
Write-Output $result
$success = $true
$response.Dispose()
break
}
}
$response.Dispose()
}
catch
{
Write-Verbose "Service $($config.Name) failed: $($_.Exception.Message)"
# Continue to next service
}
}
if (-not $success)
{
Write-Error "Failed to resolve geolocation for IP '$(if ($ip) { $ip } else { 'current' })' from all available services"
}
}
catch
{
Write-Error "Geolocation lookup failed for IP '$(if ($ip) { $ip } else { 'current' })': $($_.Exception.Message)"
}
finally
{
if ($httpClient)
{
$httpClient.Dispose()
}
}
}
}
end
{
Write-Verbose 'IP geolocation lookup completed'
}
}