public async Task OnAuthenticated(string username) { IsAuthenticated = true; ActionsTracker.UserAuthenticated(null, new UserAuthenticatedEventArgs() { EndPoint = ClientInitialRemoteEndPoint, UserName = username }); FileSystemProvider.Initialize(username); }
public async Task OnDataChannelEncryptionDisabled() { ClientDataConnection.DisableEncryption(); ActionsTracker.ConnectionSecurityChanged(null, new ConnectionSecurityChangedEventArgs() { EndPoint = ClientInitialRemoteEndPoint, Security = ConnectionFlags.HasFlag(ControlConnectionFlags.UsingTLSorSSL) ? ConnectionSecurity.ControlConnectionSecured : ConnectionSecurity.NonSecure }); }
/// <summary> /// Enables encryption of command channel. /// Usually called after AUTH command is sent /// </summary> /// <param name="parameter"></param> /// <returns></returns> public async Task OnEncryptionEnabled() { if (!IsEncryptionSupported) { SendResponse(new FtpReply() { ReplyCode = FtpReplyCode.NotImplemented, Message = "Server is not configured to support SSL/TLS." }, false); return; } SendResponse(new FtpReply() { ReplyCode = FtpReplyCode.ServiceReady, Message = "Service is ready." }, false); ConnectionFlags |= ControlConnectionFlags.UsingTLSorSSL; var authStream = new FtpSslStream(ClientCommandStream); var certificate = new X509Certificate2(DefaultServerValues.CertificateLocation, string.Empty); await authStream.AuthenticateAsServerAsync(certificate); ClientCommandStream = authStream; CommandStreamReader = new StreamReader(ClientCommandStream, ServerEncoding); ActionsTracker.ConnectionSecurityChanged(null, new ConnectionSecurityChangedEventArgs() { EndPoint = ClientInitialRemoteEndPoint, Security = ClientDataConnection.IsEncryptionActivated ? ConnectionSecurity.Both : ConnectionSecurity.ControlConnectionSecured }); Logger.Log($"Successfully authenticated via TLS : {ClientInitialRemoteEndPoint.ToString()}" , RecordKind.Status); }
/// <summary> /// Call this method if you want to start listening for incoming requests /// and allow users to manage their virtual storage /// </summary> /// <returns></returns> /// <exception cref="ApplicationException">Server didn't start</exception> public async Task Start(bool IsEncryptionEnabled) { try { DiContainer.ValidateProvider(); Initialize(); } catch (ApplicationException ex) { logger.Log($"Server didn't start. Detailed error: {ex.Message}", RecordKind.Error); throw; } ConnectionsListener = new TcpListener(IPAddress.Any, DefaultServerValues.FtpControlPort); ConnectionsListener.Start(); logger.Log($"Started the server at {DefaultServerValues.ServerExternalIP}:{DefaultServerValues.FtpControlPort}", RecordKind.Status); while (true) { try { var connectedClient = ConnectionsListener.AcceptTcpClient(); ActionsTracker.UserConnected(null, connectedClient.Client.RemoteEndPoint); var controlConnection = DiContainer.Provider.Resolve <ControlConnection>(); controlConnection.Initialize(connectedClient, IsEncryptionEnabled); var cts = new CancellationTokenSource(); cts.Token.Register(() => controlConnection.Dispose()); connections.Add(Task.Run(() => controlConnection.InitiateConnection()), cts); } catch (DirectoryNotFoundException) { logger.Log("Wrong server base directory was provided.", RecordKind.Error); Dispose(); return; }// when server is disposed, these exceptions are thrown. }
private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { if (ClientCommandStream is FtpSslStream) { (ClientCommandStream as FtpSslStream)?.Close(); } ActionsTracker.UserDisconnected(null, ClientInitialRemoteEndPoint); ConnectedClient.Close(); } ClientCommandStream = null; FileSystemProvider = null; AuthenticationProvider = null; CommandStreamReader = null; disposedValue = true; } }
public async Task OnDataChannelEncryptionEnabled() { if (!IsEncryptionSupported) { SendResponse(new FtpReply() { ReplyCode = FtpReplyCode.NotImplemented, Message = "Server is not configured to support SSL/TLS." }, false); return; } ClientDataConnection.ActivateEncryption(); ActionsTracker.ConnectionSecurityChanged(null, new ConnectionSecurityChangedEventArgs() { EndPoint = ClientInitialRemoteEndPoint, Security = ConnectionFlags.HasFlag(ControlConnectionFlags.UsingTLSorSSL) ? ConnectionSecurity.Both : ConnectionSecurity.DataChannelSecured }); Logger.Log($"Enabled encryption for datachannel : {ClientInitialRemoteEndPoint.ToString()}" , RecordKind.Status); }