From e8baee2d1e200d746345f5211d5eee204d751637 Mon Sep 17 00:00:00 2001 From: Eveldee Date: Sun, 21 Jul 2019 21:27:31 +0200 Subject: [PATCH] Move IsInternal() method to extension --- DearFTP/Connection/Commands/PassiveCommand.cs | 27 ++-------------- .../Utils/Extensions/IPAddressExtensions.cs | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 24 deletions(-) create mode 100644 DearFTP/Utils/Extensions/IPAddressExtensions.cs diff --git a/DearFTP/Connection/Commands/PassiveCommand.cs b/DearFTP/Connection/Commands/PassiveCommand.cs index c57e57a..91e4296 100644 --- a/DearFTP/Connection/Commands/PassiveCommand.cs +++ b/DearFTP/Connection/Commands/PassiveCommand.cs @@ -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 - /// - /// An extension method to determine if an IP address is internal, as specified in RFC1918 - /// - /// The IP address that will be tested - /// Returns true if the IP is internal, false if it is external - 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; - } - } } } diff --git a/DearFTP/Utils/Extensions/IPAddressExtensions.cs b/DearFTP/Utils/Extensions/IPAddressExtensions.cs new file mode 100644 index 0000000..563a234 --- /dev/null +++ b/DearFTP/Utils/Extensions/IPAddressExtensions.cs @@ -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 + /// + /// An extension method to determine if an IP address is internal, as specified in RFC1918 + /// + /// The IP address that will be tested + /// Returns true if the IP is internal, false if it is external + 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; + } + } + } +}