Exemplo n.º 1
0
        public WebInterface()
        {
            if (WebInterfaceEnabled)
            {
                this.server = new Server();
                this.interpreter = new ManagedFileModule();
                this.server.Add(this.interpreter);
                this.reader = new FileModule();
                this.resource = new FileResources("/", Path.Combine(Directory.GetCurrentDirectory(), "WebInterface"));
                this.reader.Resources.Add(resource);
                this.server.Add(this.reader);
                this.server.Add(new SimpleRouter("/", "/index.html"));

                if (WebInterfaceDebug)
                {
                    if (UseSSL)
                    {
                        //
                    }
                    else
                    {
                        //
                    }
                }

                if (UseSSL)
                {
                    try
                    {
                        this.certificate = new X509Certificate2(CertificatePath);
                    }
                    catch (DirectoryNotFoundException)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("({0}) <Web Interface> Error: The directory specified could not be found.", DateTime.Now.ToString("hh:mm"));
                    }
                    catch (IOException)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("({0}) <Web Interface> Error: A file in the directory could not be accessed.", DateTime.Now.ToString("hh:mm"));
                    }
                    catch (NullReferenceException)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("({0}) <Web Interface> File must be a .cer file. Program does not have access to that type of file.", DateTime.Now.ToString("hh:mm"));
                    }
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("({0}) <Web Interface> Loaded Certificate: {1} Valid Date: {2} Expiry Date: {3}", DateTime.Now.ToString("hh:mm"), Path.GetFileName(CertificatePath), this.certificate.NotBefore, this.certificate.NotAfter);
                    this.securelistener = (SecureHttpListener) HttpListener.Create(IPAddress.Parse(ListenAddress), Port, this.certificate);
                    this.securelistener.UseClientCertificate = true;
                    this.server.Add(this.securelistener);
                }
                else
                {
                    this.listener = HttpListener.Create(IPAddress.Parse(ListenAddress), Port);
                    this.server.Add(this.listener);
                }
                this.reader.ContentTypes.Clear();
                this.reader.ContentTypes.Add("default", new ContentTypeHeader("application/octet-stream"));
                foreach (var mimetype in AllowedMimeTypes)
                {
                    var sbstr = mimetype.Split(',');
                    switch (sbstr.Length)
                    {
                        case 2:
                            if (sbstr[0].Length != 0 && sbstr[1].Length != 0)
                            {
                                try
                                {
                                    this.reader.ContentTypes.Add(sbstr[0], new ContentTypeHeader(sbstr[1]));
                                }
                                catch (ArgumentException)
                                {
                                    Console.ForegroundColor = ConsoleColor.Yellow;
                                    Console.WriteLine("({0}) <Web Interface> Config.xml contains duplicate Mime Types.", DateTime.Now.ToString("hh:mm"));
                                }
                            }
                            else
                            {
                                Console.ForegroundColor = ConsoleColor.Yellow;
                                Console.WriteLine("({0}) <Web Interface> Config.xml contains invalid Mime Types.", DateTime.Now.ToString("hh:mm"));
                            }
                            break;
                        default:
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.WriteLine("({0}) <Web Interface> Config.xml contains invalid Mime Types.", DateTime.Now.ToString("hh:mm"));
                            break;
                    }
                }
                try
                {
                    this.server.Start(5);
                    SessionManager.Start(this.server);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("({0}) <Web Interface> Running on Port {1}...", DateTime.Now.ToString("hh:mm"), Port);
                }
                catch (SocketException e)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("({0}) <Web Interface> {1}", DateTime.Now.ToString("hh:mm"), e);
                }
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("({0}) <Web Interface> Disabled", DateTime.Now.ToString("hh:mm"));
            }
        }
Exemplo n.º 2
0
        public WebInterface()
        {
            if (WebInterfaceEnabled)
            {
                this.server      = new Server();
                this.interpreter = new ManagedFileModule();
                this.server.Add(this.interpreter);
                this.reader   = new FileModule();
                this.resource = new FileResources("/", Path.Combine(Directory.GetCurrentDirectory(), "WebInterface"));
                this.reader.Resources.Add(resource);
                this.server.Add(this.reader);
                this.server.Add(new SimpleRouter("/", "/index.html"));

                if (WebInterfaceDebug)
                {
                    if (UseSSL)
                    {
                        //
                    }
                    else
                    {
                        //
                    }
                }

                if (UseSSL)
                {
                    try
                    {
                        this.certificate = new X509Certificate2(CertificatePath);
                    }
                    catch (DirectoryNotFoundException)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("({0}) <Web Interface> Error: The directory specified could not be found.", DateTime.Now.ToString("hh:mm"));
                    }
                    catch (IOException)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("({0}) <Web Interface> Error: A file in the directory could not be accessed.", DateTime.Now.ToString("hh:mm"));
                    }
                    catch (NullReferenceException)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("({0}) <Web Interface> File must be a .cer file. Program does not have access to that type of file.", DateTime.Now.ToString("hh:mm"));
                    }
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("({0}) <Web Interface> Loaded Certificate: {1} Valid Date: {2} Expiry Date: {3}", DateTime.Now.ToString("hh:mm"), Path.GetFileName(CertificatePath), this.certificate.NotBefore, this.certificate.NotAfter);
                    this.securelistener = (SecureHttpListener)HttpListener.Create(IPAddress.Parse(ListenAddress), Port, this.certificate);
                    this.securelistener.UseClientCertificate = true;
                    this.server.Add(this.securelistener);
                }
                else
                {
                    this.listener = HttpListener.Create(IPAddress.Parse(ListenAddress), Port);
                    this.server.Add(this.listener);
                }
                this.reader.ContentTypes.Clear();
                this.reader.ContentTypes.Add("default", new ContentTypeHeader("application/octet-stream"));
                foreach (var mimetype in AllowedMimeTypes)
                {
                    var sbstr = mimetype.Split(',');
                    switch (sbstr.Length)
                    {
                    case 2:
                        if (sbstr[0].Length != 0 && sbstr[1].Length != 0)
                        {
                            try
                            {
                                this.reader.ContentTypes.Add(sbstr[0], new ContentTypeHeader(sbstr[1]));
                            }
                            catch (ArgumentException)
                            {
                                Console.ForegroundColor = ConsoleColor.Yellow;
                                Console.WriteLine("({0}) <Web Interface> Config.xml contains duplicate Mime Types.", DateTime.Now.ToString("hh:mm"));
                            }
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.WriteLine("({0}) <Web Interface> Config.xml contains invalid Mime Types.", DateTime.Now.ToString("hh:mm"));
                        }
                        break;

                    default:
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("({0}) <Web Interface> Config.xml contains invalid Mime Types.", DateTime.Now.ToString("hh:mm"));
                        break;
                    }
                }
                try
                {
                    this.server.Start(5);
                    SessionManager.Start(this.server);
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("({0}) <Web Interface> Running on Port {1}...", DateTime.Now.ToString("hh:mm"), Port);
                }
                catch (SocketException e)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine("({0}) <Web Interface> {1}", DateTime.Now.ToString("hh:mm"), e);
                }
            }
            else
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("({0}) <Web Interface> Disabled", DateTime.Now.ToString("hh:mm"));
            }
        }