Move IsInternal() method to extension
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
32
DearFTP/Utils/Extensions/IPAddressExtensions.cs
Normal file
32
DearFTP/Utils/Extensions/IPAddressExtensions.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user