Move IsInternal() method to extension

This commit is contained in:
2019-07-21 21:27:31 +02:00
parent 2d517ea090
commit e8baee2d1e
2 changed files with 35 additions and 24 deletions

View File

@@ -1,4 +1,5 @@
using System;
using DearFTP.Utils.Extensions;
using System;
using System.Linq;
using System.Net;
@@ -20,7 +21,7 @@ namespace DearFTP.Connection.Commands
string remote;
if (IsInternal(session.RemoteIP))
if (session.RemoteIP.IsInternal())
{
remote = string.Join(',', session.LocalIP.ToString().Split('.').Concat(portBytes));
}
@@ -33,27 +34,5 @@ namespace DearFTP.Connection.Commands
session.DataConnection.AcceptClient();
}
// Source: https://stackoverflow.com/a/39120248
/// <summary>
/// An extension method to determine if an IP address is internal, as specified in RFC1918
/// </summary>
/// <param name="toTest">The IP address that will be tested</param>
/// <returns>Returns true if the IP is internal, false if it is external</returns>
public bool IsInternal(IPAddress toTest)
{
byte[] bytes = toTest.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;
}
}
}
}

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
namespace DearFTP.Utils.Extensions
{
public static class IPAddressExtensions
{
// Source: https://stackoverflow.com/a/39120248
/// <summary>
/// An extension method to determine if an IP address is internal, as specified in RFC1918
/// </summary>
/// <param name="toTest">The IP address that will be tested</param>
/// <returns>Returns true if the IP is internal, false if it is external</returns>
public static bool IsInternal(this IPAddress toTest)
{
byte[] bytes = toTest.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;
}
}
}
}