The portable FTP server
Inheritance: IDisposable
Beispiel #1
0
        public FtpServer(IFileSystem sys, string host = "127.0.0.1", int port = 21)
        {
            var fsp = new FtpSystemFactory(() => new FtpSystem(sys));
            var msp = new VirtualMemberProvider();
            var ass = typeof(Server).Assembly;
            var cmd = new AssemblyFtpCommandHandlerFactory(ass, new Assembly[0]);

            server            = new Server(fsp, msp, host, port, cmd);
            server.LogManager = new FtpLogManager();
            server.Start();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FtpConnection"/> class.
        /// </summary>
        /// <param name="server">The server this connection belongs to</param>
        /// <param name="socket">The socket to use to communicate with the client</param>
        /// <param name="encoding">The encoding to use for the LIST/NLST commands</param>
        public FtpConnection([NotNull] FtpServer server, [NotNull] ITcpSocketClient socket, [NotNull] Encoding encoding)
        {
            Server        = server;
            _socket       = socket;
            RemoteAddress = new Address(socket.RemoteAddress, socket.RemotePort);
            SocketStream  = OriginalStream = socket.GetStream();
            Encoding      = encoding;
            Data          = new FtpConnectionData(this);
            var commandHandlers = Server.CommandsHandlerFactory.CreateCommandHandlers(this).ToList();

            CommandHandlers = commandHandlers
                              .SelectMany(x => x.Names, (item, name) => new { Name = name, Item = item })
                              .ToDictionary(x => x.Name, x => x.Item, StringComparer.OrdinalIgnoreCase);

            // Add stand-alone extensions
            AddExtensions(Server.CommandsHandlerFactory.CreateCommandHandlerExtensions(this));

            // Add extensions provided by command handlers
            foreach (var commandHandler in commandHandlers)
            {
                AddExtensions(commandHandler.GetExtensions());
            }
        }
Beispiel #3
0
        public void TestCreated()
        {
            var host = "127.0.0.1";

            // allow only anonymous logins
            var membershipProvider = new AnonymousMembershipProvider();

            // use %TEMP%/TestFtpServer as root folder
            var fsProvider = new DotNetFileSystemProvider(Path.Combine(Path.GetTempPath(), "TestFtpServer"), false);

            // Initialize the FTP server
            var ftpServer = new FtpServer(fsProvider, membershipProvider, host);

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

            VerifyListing(host, "anonymous", "*****@*****.**");

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

            // Stop the FTP server
            ftpServer.Stop();
        }
 public BackgroundTransferWorker(FtpServer server)
 {
     _log  = server?.LogManager?.CreateLog(GetType());
     Queue = new BackgroundTransferQueue(_event);
 }
 public BackgroundTransferWorker(FtpServer server)
 {
     _log = server?.LogManager?.CreateLog(GetType());
     Queue = new BackgroundTransferQueue(_event);
 }
Beispiel #6
-1
        private static void Main()
        {
            // Load server certificate
            var cert = new X509Certificate2("test.pfx");
            AuthTlsCommandHandler.ServerCertificate = cert;

            // Only allow anonymous login
            var membershipProvider = new AnonymousMembershipProvider(new NoValidation());

            // Use the .NET file system
            var fsProvider = new DotNetFileSystemProvider(Path.Combine(Path.GetTempPath(), "TestFtpServer"));

            // Use all commands from the FtpServer assembly and the one(s) from the AuthTls assembly
            var commandFactory = new AssemblyFtpCommandHandlerFactory(typeof(FtpServer).Assembly, typeof(AuthTlsCommandHandler).Assembly);

            // Initialize the FTP server
            using (var ftpServer = new FtpServer(fsProvider, membershipProvider, "127.0.0.1", Port, commandFactory)
            {
                DefaultEncoding = Encoding.ASCII,
                LogManager = new FtpLogManager(),
            })
            {
#if USE_FTPS_IMPLICIT
                // Use an implicit SSL connection (without the AUTHTLS command)
                ftpServer.ConfigureConnection += (s, e) =>
                {
                    var sslStream = new FixedSslStream(e.Connection.OriginalStream);
                    sslStream.AuthenticateAsServer(cert);
                    e.Connection.SocketStream = sslStream;
                };
#endif

                // Create the default logger
                var log = ftpServer.LogManager?.CreateLog(typeof(Program));

                try
                {
                    // Start the FTP server
                    ftpServer.Start();
                    Console.WriteLine("Press ENTER/RETURN to close the test application.");
                    Console.ReadLine();

                    // Stop the FTP server
                    ftpServer.Stop();
                }
                catch (Exception ex)
                {
                    log?.Error(ex, "Error during main FTP server loop");
                }
            }
        }