FubarDevelopment / FtpServer

Portable FTP server written in .NET
http://fubardevelopment.github.io/FtpServer/
MIT License
473 stars 161 forks source link

Server does not send welcome message after initial socket connection #67

Closed Ansssss closed 5 years ago

Ansssss commented 5 years ago

I downloaded the latest code yesterday and built it. The TestFtpServer project will work for me (i.e. I can start it debugging from within Visual Studio and connect to it with a FileZilla client). However, when I tried to make a minimal server example using the library, the server never sends the welcome message after a client connects. I verified with WireShark, only the TCP connection is established, nothing beyond that.

I even tried using the same code another user posted for a fixed bug, but the problem remains the same. Here is the code I am currently using:

using System;
using System.IO;
using FubarDev.FtpServer;
using FubarDev.FtpServer.FileSystem.DotNet;
using Microsoft.Extensions.DependencyInjection;

namespace SimpleFtpConsoleTest
{
    class Program
    {

        static void Main(string[] args)
        {
            // Setup dependency injection
            var services = new ServiceCollection();

            // use %TEMP% as root folder
            services.Configure<DotNetFileSystemOptions>(opt => opt
                .RootPath = Path.GetTempPath());

            // Add FTP server services
            // DotNetFileSystemProvider = Use the .NET file system functionality
            // AnonymousMembershipProvider = allow only anonymous logins
            services.AddFtpServer(builder => builder
                .UseDotNetFileSystem() // Use the .NET file system functionality
                .EnableAnonymousAuthentication()); // allow anonymous logins

            // Configure the FTP server
            services.Configure<FtpServerOptions>(opt => opt.ServerAddress = "127.0.0.1");
            services.Configure<FtpServerOptions>(opt => opt.Port = 44421);

            // Build the service provider
            using (var serviceProvider = services.BuildServiceProvider())
            {
                // Initialize the FTP server
                var ftpServer = serviceProvider.GetRequiredService<IFtpServer>();

                // Start the FTP server
                ftpServer.Start();

                Console.WriteLine("Press ENTER/RETURN to close the test application.");
                Console.ReadLine();

                // Stop the FTP server
                ftpServer.Stop();
            }
        }
    }
}
fubar-coder commented 5 years ago

I cannot reproduce this problem.

Ansssss commented 5 years ago

It is not an anti-virus problem related to the port. To verify I just started up a netcat process listening on that port and connected to it and exchanged info without problem.

I generated a new csproj file and added it in the samples folder of the FubarDev.FtpServer solution to test it out. It is named ConsoleApp1.csproj and is attached (and github made me rename it to .txt). ConsoleApp1.txt

fubar-coder commented 5 years ago

Okay, I found the problem. I guess that you're using the source code (i.e. a clone of master)? There's a bug (issue #63) that's fixed in 2.2.1 (branch release/2.2.1).

This is the diff:

diff --git a/src/FubarDev.FtpServer/PasvListenerFactory.cs b/src/FubarDev.FtpServer/PasvListenerFactory.cs
index e5dd2cd..b8a71bf 100644
--- a/src/FubarDev.FtpServer/PasvListenerFactory.cs
+++ b/src/FubarDev.FtpServer/PasvListenerFactory.cs
@@ -33,7 +33,7 @@ namespace FubarDev.FtpServer
         /// </summary>
         /// <param name="serverOptions">FTPServer options.</param>
         /// <param name="logger">Logger instance.</param>
-        public PasvListenerFactory(IOptions<FtpServerOptions> serverOptions, ILogger<PasvListenerFactory> logger)
+        public PasvListenerFactory(IOptions<FtpServerOptions> serverOptions, ILogger<PasvListenerFactory> logger = null)
         {
             _log = logger;
             if (serverOptions.Value.PasvMinPort > 1023 &&
@@ -41,7 +41,7 @@ namespace FubarDev.FtpServer
             {
                 _pasvMinPort = serverOptions.Value.PasvMinPort;
                 _pasvMaxPort = serverOptions.Value.PasvMaxPort;
-                _log.LogInformation($"PASV port range set to {_pasvMinPort}:{_pasvMaxPort}");
+                _log?.LogInformation($"PASV port range set to {_pasvMinPort}:{_pasvMaxPort}");
                 _pasvPorts = Enumerable.Range(_pasvMinPort, _pasvMaxPort - _pasvMinPort + 1).ToArray();
             }

@@ -106,14 +106,14 @@ namespace FubarDev.FtpServer
                         // retry if the socket is already in use, else throw the underlying exception
                         if (se.SocketErrorCode != SocketError.AddressAlreadyInUse)
                         {
-                            _log.LogError(se, "Could not create listener");
+                            _log?.LogError(se, "Could not create listener");
                             throw;
                         }
                     }
                 }

                 // if we reach this point, we have not been able to create a listener within range
-                _log.LogWarning("No free ports available for data connection");
+                _log?.LogWarning("No free ports available for data connection");
                 throw new SocketException((int)SocketError.AddressAlreadyInUse);
             }
         }
Ansssss commented 5 years ago

You are correct, I just downloaded a zip of the source. I thought I had version 3.0.0 (that is the product version listed on the FubarDev.FtpServer.dll that was compiled), so I did not think issue #63 would affect that version of code. I must be confused by the branches and what the github page offers for the download. Is the fix not in the master branch?

fubar-coder commented 5 years ago

No, not yet. The master branch is a version between 2.2.1 and 3.0, but I hope to release 3.0 soon, which will be the first step of a larger refactoring effort.