Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local ip check update #729

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions websocket-sharp/Ext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,9 @@ public static bool IsLocal (this System.Net.IPAddress address)
return true;
}

if (address.IsInternal())
return true;

var name = System.Net.Dns.GetHostName ();
var addrs = System.Net.Dns.GetHostAddresses (name);

Expand All @@ -1510,6 +1513,30 @@ public static bool IsLocal (this System.Net.IPAddress address)
return false;
}

/// <summary>
/// An extension method to determine if an IP address is internal
/// Class A Private IP Range: 10.0.0.0 -> 10.255.255.255
/// Class B Private IP Range: 172.16.0.0 -> 172.31.255.255
/// Class C Private IP Range: 192.168.0.0 -> 192.168.255.25
/// </summary>
/// <param name="address">The IP address to check</param>
/// <returns>Returns true if the IP matches the internal ranges, false if not/returns>
public static bool IsInternal(this System.Net.IPAddress address)
{
byte[] bytes = address.GetAddressBytes();
switch( bytes[ 0 ] )
{
case 10:
return true;
case 172:
return bytes[ 1 ] < 32 && bytes[ 1 ] >= 16;
case 192:
return bytes[ 1 ] == 168;
default:
return false;
}
}

/// <summary>
/// Determines whether the specified string is <see langword="null"/> or
/// an empty string.
Expand Down