Esempio n. 1
0
        public Smtp4devServer(Func <Smtp4devDbContext> dbContextFactory, IOptions <ServerOptions> serverOptions,
                              MessagesHub messagesHub, SessionsHub sessionsHub)
        {
            this.dbContextFactory = dbContextFactory;

            smtpServer = new DefaultServer(serverOptions.Value.AllowRemoteConnections, serverOptions.Value.Port);
            smtpServer.MessageReceived  += OnMessageReceived;
            smtpServer.SessionCompleted += OnSessionCompleted;

            this.messagesHub = messagesHub;
            this.sessionsHub = sessionsHub;
        }
Esempio n. 2
0
        public Smtp4devServer(Func <Smtp4devDbContext> dbContextFactory, IOptions <ServerOptions> serverOptions, MessagesHub messagesHub, SessionsHub sessionsHub)
        {
            this.messagesHub      = messagesHub;
            this.sessionsHub      = sessionsHub;
            this.serverOptions    = serverOptions;
            this.dbContextFactory = dbContextFactory;


            System.Security.Cryptography.X509Certificates.X509Certificate2 cert = null;

            Console.WriteLine($"\nTLS mode: {serverOptions.Value.TlsMode}");

            if (serverOptions.Value.TlsMode != TlsMode.None)
            {
                if (!string.IsNullOrEmpty(serverOptions.Value.TlsCertificate))
                {
                    Console.WriteLine($"Using certificate from {serverOptions.Value.TlsCertificate}");
                    cert = new X509Certificate2(File.ReadAllBytes(serverOptions.Value.TlsCertificate), "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
                }
                else
                {
                    string pfxPath = Path.GetFullPath("selfsigned-certificate.pfx");
                    string cerPath = Path.GetFullPath("selfsigned-certificate.cer");

                    if (File.Exists(pfxPath))
                    {
                        cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);

                        if (cert.Subject != $"CN={serverOptions.Value.HostName}" || DateTime.Parse(cert.GetExpirationDateString()) < DateTime.Now.AddDays(30))
                        {
                            cert = null;
                        }
                        else
                        {
                            Console.WriteLine($"Using existing self-signed certificate with subject name '{serverOptions.Value.HostName} and expiry date {cert.GetExpirationDateString()}");
                        }
                    }

                    if (cert == null)
                    {
                        cert = CreateSelfSignedCertificate();
                        File.WriteAllBytes(pfxPath, cert.Export(X509ContentType.Pkcs12));
                        File.WriteAllBytes(cerPath, cert.Export(X509ContentType.Cert));
                        Console.WriteLine($"Generated new self-signed certificate with subject name '{serverOptions.Value.HostName} and expiry date {cert.GetExpirationDateString()}");
                    }

                    Console.WriteLine($"Ensure that the hostname you enter into clients and '{serverOptions.Value.HostName}' from ServerOptions:HostName configuration match exactly");
                    Console.WriteLine($"and trust the issuer certificate at {cerPath} in your client/OS to avoid certificate validation errors.");
                }
            }
            else
            {
                Console.WriteLine("SSL/TLS is now disabled by default");
                Console.WriteLine("To enable use set TlsMode option (values 'ImplicitTls' or 'StartTls') on command line or in appsettings.json and follow the instruction about hostname and certificate trust on first startup.");
            }
            Console.WriteLine();

            ServerOptions serverOptionsValue = serverOptions.Value;

            this.smtpServer = new DefaultServer(serverOptionsValue.AllowRemoteConnections, serverOptionsValue.Port,
                                                serverOptionsValue.TlsMode == TlsMode.ImplicitTls ? cert : null,
                                                serverOptionsValue.TlsMode == TlsMode.StartTls ? cert : null
                                                );
            this.smtpServer.MessageReceivedEventHandler  += OnMessageReceived;
            this.smtpServer.SessionCompletedEventHandler += OnSessionCompleted;
            this.smtpServer.SessionStartedHandler        += OnSessionStarted;
            this.smtpServer.AuthenticationCredentialsValidationRequiredEventHandler += OnAuthenticationCredentialsValidationRequired;
        }
Esempio n. 3
0
 public SessionsController(Smtp4devDbContext dbContext, SessionsHub sessionsHub)
 {
     _dbContext       = dbContext;
     this.sessionsHub = sessionsHub;
 }