Skip to content

Commit

Permalink
changes to geoip location for ipv6
Browse files Browse the repository at this point in the history
  • Loading branch information
KelvinTegelaar committed Aug 10, 2023
1 parent 7fa37d6 commit 9829a33
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions Modules/CIPPCore/Public/Get-CIPPGeoIPLocation.ps1
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
function Get-CIPPGeoIPLocation {
[CmdletBinding()]
param (
$IP
[string]$IP
)
if ($IP -like '*:*') { $IP = $IP.split(':')[0] }
$CTX = new-azdatatablecontext -TableName geoipdb -ConnectionString 'TableEndpoint=https://cyberdraingeoipdb.table.core.windows.net/;SharedAccessSignature=sv=2022-11-02&ss=t&srt=o&sp=rl&se=2025-08-08T21:05:23Z&st=2023-08-08T13:05:23Z&spr=https&sig=89Bmk2Un89xqNzZPLkryFnLRCjHs9rCWGUJjhvf5mso%3D'
$GeoTable = @{ Context = $CTX }

$IPAsint = 0
$IP.Split(".") | ForEach-Object {
$IPAddressByte = 0
[int]::TryParse($_, [ref] $IPAddressByte) | Out-Null
$IPAsint = ([long]($IPAsint -shl 8)) -bor [byte]$_
if ($IP -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+$') {
$IP = $IP -replace ':\d+$', '' # Remove the port number if present
}

$location = (Get-AzDataTableEntity @GeoTable -Filter "PartitionKey eq 'GeoIP' and RowKey le '$IPAsint' and ipTo ge '$IPAsint'") | Select-Object -Last 1
$partitionKey = "GeoIP"
$IPAsint = [System.Numerics.BigInteger]::Zero
$ipAddress = [System.Net.IPAddress]::Parse($IP)
if ($ipAddress.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6) {
$partitionKey = "GeoIPv6"
$bytes = $ipAddress.GetAddressBytes()
for ($i = 0; $i -lt $bytes.Length; $i++) {
$IPAsint = $IPAsint -shl 8 -bor $bytes[$i]
}
}
else {
$IP.Split(".") | ForEach-Object {
$IPAddressByte = 0
[int]::TryParse($_, [ref] $IPAddressByte) | Out-Null
$IPAsint = ([long]($IPAsint -shl 8)) -bor [byte]$_
}
}

$CTX = New-AzDataTableContext -TableName geoipdb -ConnectionString 'TableEndpoint=https://cyberdraingeoipdb.table.core.windows.net/;SharedAccessSignature=sv=2022-11-02&ss=t&srt=o&sp=rl&se=2025-08-08T21:05:23Z&st=2023-08-08T13:05:23Z&spr=https&sig=89Bmk2Un89xqNzZPLkryFnLRCjHs9rCWGUJjhvf5mso%3D'
$GeoTable = @{ Context = $CTX }
$location = (Get-AzDataTableEntity @GeoTable -Filter "PartitionKey eq '$partitionKey' and RowKey le '$IPAsint' and ipTo ge '$IPAsint'") | Select-Object -Last 1

return $location
}

0 comments on commit 9829a33

Please sign in to comment.