Change how PASV process local IP

Now use local ip for local connection
This commit is contained in:
2019-07-21 21:15:02 +02:00
parent 3f80eef90b
commit 2d517ea090
2 changed files with 38 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Net;
namespace DearFTP.Connection.Commands namespace DearFTP.Connection.Commands
{ {
@@ -16,11 +17,43 @@ namespace DearFTP.Connection.Commands
int port = session.DataConnection.Port; int port = session.DataConnection.Port;
var portBytes = BitConverter.GetBytes((ushort)port).Reverse().Select(x => x.ToString()); var portBytes = BitConverter.GetBytes((ushort)port).Reverse().Select(x => x.ToString());
string remote = string.Join(',', session.Configuration.Server.Host.Split('.').Concat(portBytes));
string remote;
if (IsInternal(session.RemoteIP))
{
remote = string.Join(',', session.LocalIP.ToString().Split('.').Concat(portBytes));
}
else
{
remote = string.Join(',', session.Configuration.Server.Host.Split('.').Concat(portBytes));
}
stream.Send(ResponseCode.PassiveMode, $"Entering Passive Mode ({remote})"); stream.Send(ResponseCode.PassiveMode, $"Entering Passive Mode ({remote})");
session.DataConnection.AcceptClient(); 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

@@ -23,10 +23,11 @@ namespace DearFTP.Connection
public bool IsTlsProtected { get; private set; } public bool IsTlsProtected { get; private set; }
public string CurrentWorkingDirectory => NavigablePath.CurrentDirectory; public string CurrentWorkingDirectory => NavigablePath.CurrentDirectory;
public string IP => ((IPEndPoint)_client.Client.LocalEndPoint).Address.ToString(); public IPAddress LocalIP => ((IPEndPoint)_client.Client.LocalEndPoint).Address;
public IPAddress RemoteIP => ((IPEndPoint)_client.Client.RemoteEndPoint).Address;
private TcpClient _client; private readonly TcpClient _client;
private NetworkStream _networkStream; private readonly NetworkStream _networkStream;
private SslStream _sslStream; private SslStream _sslStream;
private bool _isRunning = true; private bool _isRunning = true;