FubarDevelopment / FtpServer

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

Is it possible to have the FTP server shut down after a period of inactivity? #118

Closed jcorjaycee closed 2 years ago

jcorjaycee commented 3 years ago

Total noob question here. I took a look at all the options provided at each step in the example Main configuration, but I couldn't find anything.

What I'm looking to do is have the server shut down if no requests are received after x number of seconds. Right now, I'm looking to do this with a Stopwatch object:

private static readonly Stopwatch AStopwatch = new();

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

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

            // 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 = "192.168.0.152";
                opt.Port = 3000;
            });

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

            // Start the FTP server
            ftpServerHost.StartAsync(CancellationToken.None).Wait();

            AStopwatch.Start();

            var tk = new Thread(TimeKeeper);
            tk.Start();

            while (AStopwatch.IsRunning)
            {
                // do nothing
            }

            ftpServerHost.StopAsync(CancellationToken.None).Wait();
            Console.WriteLine("FTP server stopped");
        }

        private static void TimeKeeper()
        {
            while (AStopwatch.IsRunning)
            {
                if (!(AStopwatch.Elapsed.TotalSeconds > 30)) continue;
                AStopwatch.Stop();
                return;
            }
        }

However, in order for this to work, I need the server to provide something akin to an EventHandler, so that when a new FTP message/request is received, this Stopwatch can be reset to 0 so the server keeps running. Does FtpServer offer this somewhere in the setup process?

If there is a better way to have the server shut down after inactivity, please let me know!

jcorjaycee commented 3 years ago
services.Configure<FtpConnectionOptions>(opt =>
                opt.InactivityTimeout = TimeSpan.FromSeconds(30));
jcorjaycee commented 3 years ago

Looks like I got that wrong, actually -- that's the device inactivity time out, and does nothing to shut down the server itself. Still looking for a solution!

fubar-coder commented 2 years ago

You can reconfigure the connection timeout through FtpConnectionOptions