Merge branch 'bugfix/interface_detection'

This commit is contained in:
Christian Stein 2020-02-14 09:34:41 +01:00
commit 90a8fab9cb
3 changed files with 129 additions and 75 deletions

View file

@ -24,6 +24,7 @@ function ConvertTo-IcingaIPBinaryString()
param(
$IP
);
if ($IP -like '*.*') {
$IP = ConvertTo-IcingaIPv4BinaryString -IP $IP;
} elseif ($IP -like '*:*') {

View file

@ -25,11 +25,17 @@ function ConvertTo-IcingaIPv4BinaryString()
[string]$IP
);
try {
$IP = $IP -split '\.' | ForEach-Object {
[System.Convert]::ToString($_, 2).PadLeft(8, '0');
}
$IP = $IP -join '';
$IP = $IP -replace '\s','';
} catch {
# Todo: Should we handle errors? It might happen due to faulty routes or unhandled route config
# we throw errors which should have no effect at all
return $null;
}
return @{
'value' = $IP;

View file

@ -37,7 +37,7 @@ function Get-IcingaNetworkInterface()
}
try {
$IP = ([System.Net.Dns]::GetHostAddresses($IP)).IPAddressToString;
[array]$IP = ([System.Net.Dns]::GetHostAddresses($IP)).IPAddressToString;
} catch {
Write-Host 'Invalid IP was provided!';
return $null;
@ -54,21 +54,47 @@ function Get-IcingaNetworkInterface()
$Counter++;
$Divide = $Info.DestinationPrefix;
$IP,$Mask = $Divide.Split('/');
$IP, $Mask = $Divide.Split('/');
foreach ($destinationIP in $IPBinStringMaster) {
[string]$Key = '';
[string]$MaskKey = '';
############################################################################
################################ IPv4 ####################################
############################################################################
if ($IPBinStringMaster.name -eq 'IPv4') {
if ($destinationIP.name -eq 'IPv4') {
if ($IP -like '*.*') {
############################################################################
if ([int]$Mask -lt 10) {
[string]$MaskKey = [string]::Format('00{0}', $Mask);
$MaskKey = [string]::Format('00{0}', $Mask);
} else {
[string]$MaskKey = [string]::Format('0{0}', $Mask);
$MaskKey = [string]::Format('0{0}', $Mask);
}
############################################################################
}
}
############################################################################
################################ IPv6 ####################################
############################################################################
if ($destinationIP.name -eq 'IPv6') {
if ($IP -like '*:*') {
############################################################################
if ([int]$Mask -lt 10) {
$MaskKey = [string]::Format('00{0}', $Mask);
} elseif ([int]$Mask -lt 100) {
$MaskKey = [string]::Format('0{0}', $Mask);
} else {
$MaskKey = $Mask;
}
############################################################################
}
}
[string]$Key = [string]::Format('{0}-{1}', $MaskKey, $Counter);
$Key = [string]::Format('{0}-{1}', $MaskKey, $Counter);
if ($InterfaceData.ContainsKey($Key)) {
continue;
}
$InterfaceData.Add(
$Key, @{
@ -77,48 +103,69 @@ function Get-IcingaNetworkInterface()
'Interface' = $Info.ifIndex;
}
);
############################################################################
}
}
############################################################################
################################ IPv4 ####################################
############################################################################
if ($IPBinStringMaster.name -eq 'IPv6') {
if ($IP -like '*:*') {
############################################################################
if ([int]$Mask -lt 10) {
[string]$MaskKey = [string]::Format('00{0}', $Mask);
} elseif ([int]$Mask -lt 100) {
[string]$MaskKey = [string]::Format('0{0}', $Mask);
} else {
[string]$MaskKey = $Mask;
}
[string]$Key = [string]::Format('{0}-{1}', $MaskKey, $Counter);
$InterfaceData.Add(
$Key, @{
'Binary IP String' = (ConvertTo-IcingaIPBinaryString -IP $IP);
'Mask' = $Mask;
'Interface' = $Info.ifIndex;
}
);
############################################################################
}
}
}
$InterfaceDataOrdered = $InterfaceData.GetEnumerator() | Sort-Object -Property Name -Descending;
$ExternalInterfaces = @{};
foreach ( $Route in $InterfaceDataOrdered ) {
foreach ($destinationIP in $IPBinStringMaster) {
[string]$RegexPattern = [string]::Format("^.{{{0}}}", $Route.Value.Mask);
[string]$ToBeMatched = $Route.Value."Binary IP String";
if ($null -eq $ToBeMatched) {
continue;
}
$Match1=[regex]::Matches($ToBeMatched, $RegexPattern).Value;
$Match2=[regex]::Matches($IPBinStringMaster.Value, $RegexPattern).Value;
$Match2=[regex]::Matches($destinationIP.Value, $RegexPattern).Value;
If ($Match1 -like $Match2) {
return ((Get-NetIPAddress -InterfaceIndex $Route.Value.Interface -AddressFamily $IPBinStringMaster.name).IPAddress);
$ExternalInterface = ((Get-NetIPAddress -InterfaceIndex $Route.Value.Interface -AddressFamily $destinationIP.Name -ErrorAction SilentlyContinue).IPAddress);
# If no interface was found -> skip this entry
if ($null -eq $ExternalInterface) {
continue;
}
if ($ExternalInterfaces.ContainsKey($ExternalInterface)) {
$ExternalInterfaces[$ExternalInterface].count += 1;
} else {
$ExternalInterfaces.Add(
$ExternalInterface,
@{
'count' = 1
}
);
}
}
return ((Get-NetIPAddress -InterfaceIndex (Get-NetRoute | Where-Object -Property DestinationPrefix -like '0.0.0.0/0')[0].IfIndex -AddressFamily $IPBinStringMaster.name).IPAddress).split('%')[0];
}
}
if ($ExternalInterfaces.Count -eq 0) {
foreach ($destinationIP in $IPBinStringMaster) {
$ExternalInterface = ((Get-NetIPAddress -InterfaceIndex (Get-NetRoute | Where-Object -Property DestinationPrefix -like '0.0.0.0/0')[0].IfIndex -AddressFamily $destinationIP.name).IPAddress).split('%')[0];
if ($ExternalInterfaces.ContainsKey($ExternalInterface)) {
$ExternalInterfaces[$ExternalInterface].count += 1;
} else {
$ExternalInterfaces.Add(
$ExternalInterface,
@{
'count' = 1
}
);
}
}
}
$InternalCount = 0;
$UseInterface = '';
foreach ($interface in $ExternalInterfaces.Keys) {
$currentCount = $ExternalInterfaces[$interface].count;
if ($currentCount -gt $InternalCount) {
$InternalCount = $currentCount;
$UseInterface = $interface;
}
}
return $UseInterface;
}