Ejemplo n.º 1
0
        public async Task <bool> StartAsync()
        {
            this.OnInfoLog?.Invoke("Starting Vlix Web Server...");

            _listener = new HttpListener();

            if (this.Config.EnableHTTP)
            {
                _listener.Prefixes.Add("http://*:" + this.Config.HTTPPort.ToString() + "/");
            }
            if (this.Config.EnableHTTPS)
            {
                if (this.Config.EnableHTTP && (this.Config.HTTPPort == this.Config.HTTPSPort))
                {
                    this.OnErrorLog?.Invoke("Failed to start HTTPS Web Server (HTTPS Port cannot be the same as HTTP Port)!");
                    throw new Exception("Failed to start HTTPS Web Server (HTTPS Port cannot be the same as HTTP Port)!");
                }
                if (!await SSLCertificateServices.TryFindAndBindLatestSSLCertToPort(this.Config.HTTPSPort, this.Config.SSLCertificateSubjectName, this.Config.SSLCertificateStoreName, (log) => this.OnInfoLog?.Invoke(log), (log) => this.OnErrorLog?.Invoke(log)).ConfigureAwait(false))
                {
                    this.OnErrorLog?.Invoke("Failed to Start Web Server (Unable to bind SSL Cert to Port)!");
                    throw new Exception("Failed to Start Web Server (Unable to bind SSL Cert to Port)!");
                }
                ;
                _listener.Prefixes.Add("https://*:" + this.Config.HTTPSPort.ToString() + "/");
                _ = Task.Run(async() =>
                {
                    while (true)
                    {
                        await Task.Delay(60000 * 5).ConfigureAwait(false);
                        if (!await SSLCertificateServices.TryFindAndBindLatestSSLCertToPort(this.Config.HTTPSPort, this.Config.SSLCertificateSubjectName, this.Config.SSLCertificateStoreName, (log) => this.OnInfoLog?.Invoke(log), (log) => this.OnErrorLog?.Invoke(log)).ConfigureAwait(false))
                        {
                            this.OnErrorLog?.Invoke("Failed to Start Web Server (Unable to bind SSL Cert to Port)!");
                            throw new Exception("Failed to Start Web Server (Unable to bind SSL Cert to Port)!");
                        }
                        ;
                    }
                });
            }
            if (!this.Config.EnableHTTP && !this.Config.EnableHTTPS)
            {
                this.OnInfoLog?.Invoke("Unable to start as both HTTP (Port " + this.Config.HTTPPort + ") and HTTPS (Port " + this.Config.HTTPSPort + ") is disabled");
                return(false);
            }
            if (this.Config.EnableHTTP && !this.Config.EnableHTTPS)
            {
                this.OnInfoLog?.Invoke("Listening to port " + this.Config.HTTPPort + "(HTTP), Directory = '" + this.Config.WWWDirectoryParsed() + "'");
            }
            if (!this.Config.EnableHTTP && this.Config.EnableHTTPS)
            {
                this.OnInfoLog?.Invoke("Listening to port " + this.Config.HTTPSPort + "(HTTPS), Directory = '" + this.Config.WWWDirectoryParsed() + "'");
            }
            if (this.Config.EnableHTTP && this.Config.EnableHTTPS)
            {
                this.OnInfoLog?.Invoke("Listening to port " + this.Config.HTTPPort + "(HTTP) and " + this.Config.HTTPSPort + "(HTTPS), Directory = '" + this.Config.WWWDirectoryParsed() + "'");
            }

            _listener.Start();
            _listener.BeginGetContext(OnContext, null); //The thread stops here waiting for content to come

            this.OnInfoLog?.Invoke("Vlix HTTP Server Started!");
            return(true);
        }