Beispiel #1
0
 public async Task ApplyStreamFilterAsync(Func <Stream, Task <Stream> > filter)
 {
     await ConnectionChannel.ApplyStreamFilterAsync(filter);
 }
Beispiel #2
0
        public async Task ProcessAsync()
        {
            try
            {
                Server.Behaviour.OnSessionStarted(this, Session);
                SetReaderEncoding(Server.Behaviour.GetDefaultEncoding(this));

                if (Server.Behaviour.IsSSLEnabled(this))
                {
                    await ConnectionChannel.ApplyStreamFilterAsync(async s =>
                    {
                        SslStream sslStream = new SslStream(s);
                        await sslStream.AuthenticateAsServerAsync(Server.Behaviour.GetSSLCertificate(this));
                        return(sslStream);
                    });

                    Session.SecureConnection = true;
                }

                await WriteResponseAsync(new SmtpResponse(StandardSmtpResponseCode.ServiceReady,
                                                          Server.Behaviour.DomainName + " smtp4dev ready"));

                int numberOfInvalidCommands = 0;
                while (ConnectionChannel.IsConnected)
                {
                    bool        badCommand = false;
                    SmtpCommand command    = new SmtpCommand(await ReadLineAsync());
                    Server.Behaviour.OnCommandReceived(this, command);

                    if (command.IsValid)
                    {
                        IVerb verbProcessor = VerbMap.GetVerbProcessor(command.Verb);

                        if (verbProcessor != null)
                        {
                            try
                            {
                                await verbProcessor.ProcessAsync(this, command);
                            }
                            catch (SmtpServerException exception)
                            {
                                await WriteResponseAsync(exception.SmtpResponse);
                            }
                        }
                        else
                        {
                            badCommand = true;
                        }
                    }
                    else if (command.IsEmpty)
                    {
                    }
                    else
                    {
                        badCommand = true;
                    }

                    if (badCommand)
                    {
                        numberOfInvalidCommands++;

                        if (Server.Behaviour.MaximumNumberOfSequentialBadCommands > 0 &&
                            numberOfInvalidCommands >= Server.Behaviour.MaximumNumberOfSequentialBadCommands)
                        {
                            await WriteResponseAsync(new SmtpResponse(StandardSmtpResponseCode.ClosingTransmissionChannel, "Too many bad commands. Bye!"));
                            await CloseConnectionAsync();
                        }
                        else
                        {
                            await WriteResponseAsync(new SmtpResponse(StandardSmtpResponseCode.SyntaxErrorCommandUnrecognised,
                                                                      "Command unrecognised"));
                        }
                    }
                }
            }
            catch (IOException ioException)
            {
                Session.SessionError     = ioException;
                Session.SessionErrorType = SessionErrorType.NetworkError;
            }
            catch (Exception exception)
            {
                Session.SessionError     = exception;
                Session.SessionErrorType = SessionErrorType.UnexpectedException;
            }

            await CloseConnectionAsync();

            Session.EndDate = DateTime.Now;
            Server.Behaviour.OnSessionCompleted(this, Session);
        }