コード例 #1
0
        public SecurableConnection([NotNull] Stream source, PrivateKeyHolder sslKey)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            _sslKey = sslKey;

            Init(source);
        }
コード例 #2
0
            public TestConnection(ITestOutputHelper output)
            {
                (Stream a, Stream b) = PairedStream.Create();
                LocalStream          = new RedirectableStream(a);
                Reader = new StreamReader(LocalStream);
                Writer = new StreamWriter(LocalStream)
                {
                    AutoFlush = true
                };


                var builder = new ContainerBuilder();

                builder.RegisterAssemblyTypes(typeof(SmtpSession).GetTypeInfo().Assembly)
                .Where(t => t.GetTypeInfo().GetCustomAttribute <SmtpCommandAttribute>() != null)
                .Keyed <ISmtpCommand>(t => t.GetTypeInfo().GetCustomAttribute <SmtpCommandAttribute>().Name);
                builder.RegisterInstance(TestHelpers.MakeSettings("test.vaettir.net"))
                .As <AgentSettings>()
                .As <AgentSettings>();

                builder.RegisterType <SmtpSession>()
                .As <SmtpSession>()
                .As <ISmtpMessageChannel>()
                .As <IMailBuilder>()
                .As <IProtocolSession>();
                builder.RegisterType <SmtpAuthenticationTransport>()
                .As <IAuthenticationTransport>();

                builder.RegisterInstance(new SecurableConnection(b, PrivateKeyHolder.Fixed(s_serverCert)))
                .As <IConnectionSecurity>()
                .As <SecurableConnection>();

                builder.RegisterInstance(new TestOutputLogger(output))
                .As <ILogger>();

                builder.RegisterInstance(new ConnectionInformation("127.0.0.1", "128.0.0.1"));

                Container = builder.Build();

                Connection = Container.Resolve <SecurableConnection>();
                Session    = Container.Resolve <SmtpSession>();

                CancellationTokenSource = new CancellationTokenSource();
                CancellationToken token = CancellationTokenSource.Token;

                SessionTask = Task.Run(() => Session.RunAsync(token), token);
            }
コード例 #3
0
ファイル: Program.cs プロジェクト: garath/mail-server
        private static async Task <IContainer> BuildContainer(Options options)
        {
            var builder = new ContainerBuilder();

            CancellationTokenSource abort = new CancellationTokenSource();

            builder.Register(c => abort.Token);

            builder.RegisterInstance(options);
            builder.RegisterType <UserHandler>().Keyed <CommandHandler>("user");
            builder.RegisterType <AgentHandler>().Keyed <CommandHandler>("agent");

            FileWatcherSettings <AgentSettings> settings = FileWatcherSettings <AgentSettings> .Load(options.SettingsPath);

            var certificates = ImmutableDictionary.CreateBuilder <string, PrivateKeyHolder>();

            foreach (var connection in settings.Value.Connections)
            {
                if (string.IsNullOrEmpty(connection.Certificate))
                {
                    continue;
                }
                certificates.Add(connection.Certificate, await PrivateKeyHolder.LoadAsync(connection.Certificate, abort.Cancel));
            }

            builder.RegisterInstance(new PrivateKeyProvider(certificates.ToImmutable()));

            IDictionary <string, LogSettings> logSettings = settings.Value.Logging;

            if (logSettings != null)
            {
                LogSettings defaultSettings = null;
                string      defaultKey      = null;
                foreach (KeyValuePair <string, LogSettings> log in logSettings)
                {
                    switch (log.Key.ToLowerInvariant())
                    {
                    case "":
                    case "default":
                        defaultKey      = log.Key;
                        defaultSettings = log.Value;
                        break;

                    case "console":
                    case "con":
                        builder.RegisterInstance(ChildWatcherSettings.Create(settings, s => s.Logging.GetValueOrDefault(log.Key)))
                        .Keyed <IVolatile <LogSettings> >("console");
                        builder.RegisterType <ConsoleLogger>()
                        .As <ILogSync>();
                        break;
                    }
                }

                if (defaultSettings != null)
                {
                    builder.RegisterInstance(ChildWatcherSettings.Create(settings, s => s.Logging.GetValueOrDefault(defaultKey)))
                    .As <IVolatile <LogSettings> >();
                }
            }

            builder.Register(c => new CompositeLogger(c.Resolve <IEnumerable <ILogSync> >())).As <ILogger>();

            builder.RegisterType <DnsClientResolver>()
            .As <IDnsResolve>()
            .SingleInstance();

            builder.RegisterType <WrappedTcpClientProvider>()
            .As <ITcpConnectionProvider>()
            .SingleInstance();

            builder.RegisterType <SmtpSession>()
            .Keyed <IProtocolSession>("smtp")
            .As <ISmtpMessageChannel>()
            .As <IMailBuilder>()
            .InstancePerMatchingLifetimeScope(ProtocolListener.ConnectionScopeTag);

            builder.RegisterType <SmtpAuthenticationTransport>()
            .Keyed <IAuthenticationTransport>("smtp")
            .InstancePerMatchingLifetimeScope(ProtocolListener.ConnectionScopeTag);

            builder.RegisterType <ImapSession>()
            .Keyed <IProtocolSession>("imap")
            .Keyed <IAuthenticationTransport>("imap")
            .As <IImapMessageChannel>()
            .As <IImapMailboxPointer>()
            .InstancePerMatchingLifetimeScope(ProtocolListener.ConnectionScopeTag);

            builder.RegisterType <MailTransfer>();
            builder.RegisterType <MailDispatcher>();

            builder.RegisterType <ProtocolListener>();

            builder.RegisterType <FileSystemMailQueue>().As <IMailQueue>().SingleInstance();
            builder.RegisterType <HashedPasswdUserStore>().As <IUserStore>().SingleInstance();
            builder.RegisterType <FileSystemDomainResolver>().As <IDomainSettingResolver>().SingleInstance();
            builder.RegisterType <FileSystemMailTransferQueue>().As <IMailTransferQueue>();
            builder.RegisterType <FileSystemMailboxStore>().As <IMailboxStore>().As <IMailboxDeliveryStore>();
            builder.RegisterType <FileSystemMailSendFailureManager>().As <IMailSendFailureManager>();
            AgentSettings initialValue = settings.Value;

            builder.RegisterInstance(initialValue)
            .As <AgentSettings>()
            .As <AgentSettings>();
            builder.RegisterInstance(settings)
            .As <IVolatile <AgentSettings> >()
            .As <IVolatile <AgentSettings> >();

            builder.RegisterAssemblyTypes(typeof(SmtpSession).GetTypeInfo().Assembly)
            .Where(t => t.GetTypeInfo().GetCustomAttribute <SmtpCommandAttribute>() != null)
            .Keyed <ISmtpCommand>(t => t.GetTypeInfo().GetCustomAttribute <SmtpCommandAttribute>().Name);

            var imapCommands = typeof(ImapSession)
                               .Assembly
                               .DefinedTypes
                               .Select(t => (t, md: t.GetTypeInfo().GetCustomAttribute <ImapCommandAttribute>()))
                               .Where(p => p.md != null);

            foreach (var(t, md) in imapCommands)
            {
                builder.RegisterType(t)
                .Keyed <IImapCommand>(md.Name)
                .WithMetadata <ImapCommandMetadata>(m =>
                {
                    m.For(x => x.Name, md.Name);
                    m.For(x => x.MinimumState, md.MinimumState);
                });
            }

            builder.RegisterType <CapabilityCommand>().As <IImapCommand>();

            var authMechs = typeof(IAuthenticationSession)
                            .Assembly
                            .DefinedTypes
                            .Select(t => (t, md: t.GetTypeInfo().GetCustomAttribute <AuthenticationMechanismAttribute>()))
                            .Where(p => p.md != null);

            foreach (var(t, md) in authMechs)
            {
                builder.RegisterType(t)
                .WithMetadata <AuthencticationMechanismMetadata>(m =>
                {
                    m.For(x => x.Name, md.Name);
                    m.For(x => x.RequiresEncryption, md.RequiresEncryption);
                })
                .As <IAuthenticationSession>()
                .Keyed <IAuthenticationSession>(md.Name);
            }

            return(builder.Build());
        }