예제 #1
0
파일: Program.cs 프로젝트: epoulsen/McNNTP
        /// <summary>
        /// The main program message loop
        /// </summary>
        /// <returns>An error code, indicating an error condition when the value returned is non-zero</returns>
        /// <exception cref="FormatException">Thrown when an attempt to construct a format string fails to properly format a finalized message</exception>
        /// <exception cref="IOException">Thrown when the process is unable to write status to the console window</exception>
        /// <exception cref="ArgumentNullException">Thrown when a 'null' value is attempted to be written to the console window</exception>
        /// <exception cref="ConfigurationErrorsException">Thrown when the configuration file for the process cannot be parsed</exception>
        /// <exception cref="SecurityException">Thrown when the X.509 certificate store cannot be opened or enumerated when constructing SSL ports</exception>
        public static int Main()
        {
            var version = Assembly.GetEntryAssembly().GetName().Version;

            Console.WriteLine("McNNTP Server Console Harness v{0}", version);

            try
            {
                // Setup LOG4NET
                XmlConfigurator.Configure();

                var logger = LogManager.GetLogger(typeof(Program));

                // Load configuration
                var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var mcnntpConfigurationSection = (McNNTPConfigurationSection)config.GetSection("mcnntp");
                logger.InfoFormat("Loaded configuration from {0}", config.FilePath);

                // Verify Database
                if (DatabaseUtility.VerifyDatabase())
                {
                    DatabaseUtility.UpdateSchema();
                }
                else if (DatabaseUtility.UpdateSchema() && !DatabaseUtility.VerifyDatabase(true))
                {
                    Console.WriteLine(
                        "Unable to verify a database.  Would you like to create and initialize a database?");
                    var resp = Console.ReadLine();
                    if (resp != null && new[] { "y", "yes" }.Contains(resp.ToLowerInvariant().Trim()))
                    {
                        DatabaseUtility.RebuildSchema();
                    }
                }

                _ircServer = new IrcServer
                {
                    SslGenerateSelfSignedServerCertificate =
                        mcnntpConfigurationSection.Ssl == null ||
                        mcnntpConfigurationSection.Ssl.GenerateSelfSignedServerCertificate,
                    SslServerCertificateThumbprint =
                        mcnntpConfigurationSection.Ssl == null
                            ? null
                            : mcnntpConfigurationSection.Ssl.ServerCertificateThumbprint
                };

                _ircServer.Start();

                _imapServer = new ImapServer
                {
                    AllowPosting   = true,
                    ImapClearPorts =
                        mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ClearText" && p.Protocol == "imap")
                        .Select(p => p.Port)
                        .ToArray(),
                    ImapExplicitTLSPorts =
                        mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ExplicitTLS" && p.Protocol == "imap")
                        .Select(p => p.Port)
                        .ToArray(),
                    ImapImplicitTLSPorts =
                        mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ImplicitTLS" && p.Protocol == "imap")
                        .Select(p => p.Port)
                        .ToArray(),
                    PathHost = mcnntpConfigurationSection.PathHost,
                    SslGenerateSelfSignedServerCertificate =
                        mcnntpConfigurationSection.Ssl == null ||
                        mcnntpConfigurationSection.Ssl.GenerateSelfSignedServerCertificate,
                    SslServerCertificateThumbprint =
                        mcnntpConfigurationSection.Ssl == null
                                             ? null
                                             : mcnntpConfigurationSection.Ssl.ServerCertificateThumbprint
                };

                _imapServer.Start();

                _nntpServer = new NntpServer
                {
                    AllowPosting  = true,
                    IrcClearPorts =
                        mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ClearText" && p.Protocol == "irc")
                        .Select(p => p.Port)
                        .ToArray(),
                    IrcImplicitTLSPorts =
                        mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ImplicitTLS" && p.Protocol == "irc")
                        .Select(p => p.Port)
                        .ToArray(),
                    NntpClearPorts =
                        mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ClearText" && p.Protocol == "nntp")
                        .Select(p => p.Port)
                        .ToArray(),
                    NntpExplicitTLSPorts =
                        mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ExplicitTLS" && p.Protocol == "nntp")
                        .Select(p => p.Port)
                        .ToArray(),
                    NntpImplicitTLSPorts =
                        mcnntpConfigurationSection.Ports.Where(p => p.Ssl == "ImplicitTLS" && p.Protocol == "nntp")
                        .Select(p => p.Port)
                        .ToArray(),
                    LdapDirectoryConfiguration =
                        mcnntpConfigurationSection.Authentication.UserDirectories
                        .OfType <LdapDirectoryConfigurationElement>()
                        .OrderBy(l => l.Priority)
                        .FirstOrDefault(),
                    PathHost = mcnntpConfigurationSection.PathHost,
                    SslGenerateSelfSignedServerCertificate =
                        mcnntpConfigurationSection.Ssl == null ||
                        mcnntpConfigurationSection.Ssl.GenerateSelfSignedServerCertificate,
                    SslServerCertificateThumbprint =
                        mcnntpConfigurationSection.Ssl == null
                                         ? null
                                         : mcnntpConfigurationSection.Ssl.ServerCertificateThumbprint
                };

                _nntpServer.Start();

                Console.WriteLine("Type QUIT and press Enter to end the server.");

                while (true)
                {
                    Console.Write("\r\n> ");
                    var input = Console.ReadLine();
                    if (input == null || !_CommandDirectory.ContainsKey(input.Split(' ')[0].ToUpperInvariant()))
                    {
                        Console.WriteLine("Unrecongized command.  Type HELP for a list of available commands.");
                        continue;
                    }

                    if (!_CommandDirectory[input.Split(' ')[0].ToUpperInvariant()].Invoke(input))
                    {
                        continue;
                    }

                    _imapServer.Stop();
                    _nntpServer.Stop();
                    return(0);
                }
            }
            catch (AggregateException ae)
            {
                foreach (var ex in ae.InnerExceptions)
                {
                    Console.WriteLine(ex.ToString());
                }
                Console.ReadLine();
                return(-2);
            }
            catch (SecurityException sex)
            {
                Console.WriteLine(sex.ToString());
                Console.ReadLine();
                return(-3);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadLine();
                return(-1);
            }
        }
예제 #2
0
 protected override void OnStop()
 {
     server.Stop();
     base.OnStop();
 }
예제 #3
0
파일: Program.cs 프로젝트: epoulsen/McNNTP
 /// <summary>
 /// The Quit command handler, which causes the nntpServer process to stop and a value to be returned as 'true',
 /// indicating to the caller the nntpServer should terminate.
 /// </summary>
 /// <returns><c>true</c></returns>
 private static bool Quit()
 {
     _nntpServer.Stop();
     return(true);
 }