Esempio n. 1
0
        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());
        }
Esempio n. 2
0
        public IVolatile <DomainSettings> GetDomainSettings(string domain)
        {
            string settingsFileName = Path.Combine(_settings.Value.DomainSettingsPath, domain + ".json");

            return(FileWatcherSettings <DomainSettings> .Load(settingsFileName));
        }