Ejemplo n.º 1
0
        //public HttpServerConfigVM(HttpServerConfig httpServerConfig)
        //{
        //    this.EnableHTTP = httpServerConfig.EnableHTTP;
        //    this.HTTPPort = httpServerConfig.HTTPPort;
        //    this.EnableHTTPS = httpServerConfig.EnableHTTPS;
        //    this.HTTPSPort = httpServerConfig.HTTPSPort;
        //    this.EnableCache = httpServerConfig.EnableCache;
        //    this.OnlyCacheItemsLessThenMB = httpServerConfig.OnlyCacheItemsLessThenMB;
        //    this.MaximumCacheSizeInMB = httpServerConfig.MaximumCacheSizeInMB;
        //    this.SSLCertificateStoreName = httpServerConfig.SSLCertificateStoreName;
        //    this.SSLCertificateSubjectName = httpServerConfig.SSLCertificateSubjectName;
        //    this.WWWDirectory = httpServerConfig.WWWDirectory;
        //    this.LogDirectory = httpServerConfig.LogDirectory;
        //    this.AllowLocalhostConnectionsOnlyForHttp = httpServerConfig.AllowLocalhostConnectionsOnlyForHttp;
        //    this.Rules.Clear();
        //    foreach (var rule in httpServerConfig.Rules) this.Rules.Add(new RuleVM(rule, this));
        //}

        public void Update(HttpServerConfig httpServerConfig, ObservableCollection <string> sANs)
        {
            this.EnableHTTP  = httpServerConfig.EnableHTTP;
            this.HTTPPort    = httpServerConfig.HTTPPort;
            this.EnableHTTPS = httpServerConfig.EnableHTTPS;
            this.HTTPSPort   = httpServerConfig.HTTPSPort;
            this.EnableCache = httpServerConfig.EnableCache;
            this.OnlyCacheItemsLessThenMB  = httpServerConfig.OnlyCacheItemsLessThenMB;
            this.MaximumCacheSizeInMB      = httpServerConfig.MaximumCacheSizeInMB;
            this.SSLCertificateStoreName   = httpServerConfig.SSLCertificateStoreName;
            this.SSLCertificateSubjectName = httpServerConfig.SSLCertificateSubjectName;
            this.WWWDirectory = httpServerConfig.WWWDirectory;
            this.LogDirectory = httpServerConfig.LogDirectory;
            this.AllowLocalhostConnectionsOnlyForHttp = httpServerConfig.AllowLocalhostConnectionsOnlyForHttp;
            this.SubjectAlternativeNames.Clear();
            foreach (var sAN in sANs)
            {
                this.SubjectAlternativeNames.Add(sAN);
            }
            this.Rules.Clear();
            foreach (var rule in httpServerConfig.Rules)
            {
                this.Rules.Add(new RuleVM(rule, this));
            }
        }
Ejemplo n.º 2
0
        protected override async void OnStart(string[] args)
        {
            cancellationTokenSource = new CancellationTokenSource();
            HttpServerConfig config = new HttpServerConfig();

            try
            {
                string appDirectory = ConfigurationManager.AppSettings.Get("AppDirectory");
                appDirectory = appDirectory?.Replace("[ProgramDataDirectory]", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));

                Directory.CreateDirectory(appDirectory);
                string configFilePath = Path.Combine(appDirectory, "httpserver.json");
                if (File.Exists(configFilePath))
                {
                    string configJSONStr = File.ReadAllText(configFilePath);
                    config = JsonConvert.DeserializeObject <HttpServerConfig>(configJSONStr);
                }
                else
                {
                    string jsonString = JsonConvert.SerializeObject(config, Formatting.Indented);
                    File.WriteAllText(configFilePath, jsonString);
                }



                string[] myFiles = Directory.GetFiles(config.WWWDirectory);
                if (myFiles.FirstOrDefault(f => Path.GetFileName(f) == "index.html") == null)
                {
                    File.Copy(Path.Combine("Sample", "index.html"), Path.Combine(config.WWWDirectoryParsed(), "index.html"));
                }
                if (myFiles.FirstOrDefault(f => Path.GetFileName(f) == "test.html") == null)
                {
                    File.Copy(Path.Combine("Sample", "test.html"), Path.Combine(config.WWWDirectoryParsed(), "test.html"));
                }

                Log.Logger = new LoggerConfiguration()
                             .MinimumLevel.Debug()
                             .WriteTo.File(Path.Combine(config.LogDirectoryParsed(), "HTTPServer.log"), rollingInterval: RollingInterval.Day)
                             .CreateLogger();

                HttpServer vlixHttpServer = new HttpServer(cancellationTokenSource.Token, config);
                vlixHttpServer.OnErrorLog   = (log) => Log.Error(log);
                vlixHttpServer.OnInfoLog    = (log) => Log.Information(log);
                vlixHttpServer.OnWarningLog = (log) => Log.Warning(log);
                await vlixHttpServer.StartAsync();
            }
            catch (Exception ex)
            {
                var AppDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                Log.Error(ex.ToString() + "\r\n\r\nApp Directory = " + AppDir);
            }
        }
Ejemplo n.º 3
0
 private void CommonConstructor(CancellationToken cancellationToken, HttpServerConfig httpServerConfig)
 {
     if (httpServerConfig.WWWDirectory.Length < 1)
     {
         throw new Exception("path cannot be empty!");
     }
     if (httpServerConfig.WWWDirectory.EndsWith(Path.DirectorySeparatorChar))
     {
         httpServerConfig.WWWDirectory = httpServerConfig.WWWDirectory.Substring(0, httpServerConfig.WWWDirectory.Length - 1);
     }
     this.CancellationToken = cancellationToken;
     this.Config            = httpServerConfig;
 }
 public void Update(HttpServerConfig httpServerConfig)
 {
     this.LogDirectory = httpServerConfig.LogDirectory;
     this.Rules.Clear();
     foreach (var r in httpServerConfig.Rules)
     {
         this.Rules.Add(r);
     }
     this.EnableHTTP                           = httpServerConfig.EnableHTTP;
     this.HTTPPort                             = httpServerConfig.HTTPPort;
     this.EnableHTTPS                          = httpServerConfig.EnableHTTPS;
     this.HTTPSPort                            = httpServerConfig.HTTPSPort;
     this.EnableCache                          = httpServerConfig.EnableCache;
     this.MaximumCacheSizeInMB                 = httpServerConfig.MaximumCacheSizeInMB;
     this.OnlyCacheItemsLessThenMB             = httpServerConfig.OnlyCacheItemsLessThenMB;
     this.WWWDirectory                         = httpServerConfig.WWWDirectory;
     this.LogDirectory                         = httpServerConfig.LogDirectory;
     this.SSLCertificateSubjectName            = httpServerConfig.SSLCertificateSubjectName;
     this.SSLCertificateStoreName              = httpServerConfig.SSLCertificateStoreName;
     this.AllowLocalhostConnectionsOnlyForHttp = httpServerConfig.AllowLocalhostConnectionsOnlyForHttp;
 }
Ejemplo n.º 5
0
 public HttpServer(HttpServerConfig httpServerConfig) : base(httpServerConfig)
 {
     CommonConstructor((new CancellationTokenSource()).Token, httpServerConfig);
 }
Ejemplo n.º 6
0
 public HttpServer(CancellationToken cancellationToken, HttpServerConfig httpServerConfig) : base(httpServerConfig)
 {
     CommonConstructor(cancellationToken, httpServerConfig);
 }
Ejemplo n.º 7
0
 public WebServerConfig(HttpServerConfig httpServerConfig, UtilityConfig utilityConfig)
 {
     this.Update(httpServerConfig);
     this.UtilityConfig = utilityConfig;
 }
Ejemplo n.º 8
0
        public HttpServerConfig ToModel()
        {
            HttpServerConfig httpServerConfig = new HttpServerConfig()
            {
                SSLCertificateStoreName   = this.SSLCertificateStoreName,
                SSLCertificateSubjectName = this.SSLCertificateSubjectName,
                EnableHTTPS = this.EnableHTTPS,
                HTTPSPort   = this.HTTPSPort,
                EnableHTTP  = this.EnableHTTP,
                HTTPPort    = this.HTTPPort,
                AllowLocalhostConnectionsOnlyForHttp = this.AllowLocalhostConnectionsOnlyForHttp,
                EnableCache              = this.EnableCache,
                LogDirectory             = this.LogDirectory,
                MaximumCacheSizeInMB     = this.MaximumCacheSizeInMB,
                OnlyCacheItemsLessThenMB = this.OnlyCacheItemsLessThenMB,
                WWWDirectory             = this.WWWDirectory,
            };

            foreach (var ruleVM in this.Rules)
            {
                Rule r = new Rule()
                {
                    Name         = ruleVM.Name,
                    Enable       = ruleVM.Enable,
                    RequestMatch = new RequestMatch()
                    {
                        CheckHostName     = ruleVM.RequestMatch.CheckHostName,
                        CheckPath         = ruleVM.RequestMatch.CheckPath,
                        CheckPort         = ruleVM.RequestMatch.CheckPort,
                        HostNameMatch     = ruleVM.RequestMatch.HostNameMatch,
                        HostNameMatchType = ruleVM.RequestMatch.HostNameMatchType,
                        PathMatch         = ruleVM.RequestMatch.PathMatch,
                        PathMatchType     = ruleVM.RequestMatch.PathMatchType,
                        Port = ruleVM.RequestMatch.Port
                    }
                };
                switch (ruleVM.ResponseAction.ActionType)
                {
                case ActionType.AlternativeWWWDirectory:
                    r.ResponseAction = new AlternativeWWWDirectoryAction()
                    {
                        AlternativeWWWDirectory = ruleVM.ResponseAction.AlternativeWWWDirectory
                    };
                    break;

                case ActionType.Deny:
                    r.ResponseAction = new DenyAction();
                    break;

                case ActionType.Redirect:
                    r.ResponseAction = new RedirectAction()
                    {
                        Scheme      = ruleVM.ResponseAction.RedirectScheme,
                        SetScheme   = ruleVM.ResponseAction.SetRedirectScheme,
                        HostName    = ruleVM.ResponseAction.RedirectHostName,
                        SetHostName = ruleVM.ResponseAction.SetRedirectHostName,
                        Path        = ruleVM.ResponseAction.RedirectPath,
                        SetPath     = ruleVM.ResponseAction.SetRedirectPath,
                        Port        = ruleVM.ResponseAction.RedirectPort,
                        SetPort     = ruleVM.ResponseAction.SetRedirectPort
                    };
                    break;

                case ActionType.ReverseProxy:
                    r.ResponseAction = new ReverseProxyAction()
                    {
                        SetScheme       = ruleVM.ResponseAction.SetReverseProxyScheme,
                        Scheme          = ruleVM.ResponseAction.ReverseProxyScheme,
                        SetHostName     = ruleVM.ResponseAction.SetReverseProxyHostName,
                        HostName        = ruleVM.ResponseAction.ReverseProxyHostName,
                        SetPort         = ruleVM.ResponseAction.SetReverseProxyPort,
                        Port            = ruleVM.ResponseAction.ReverseProxyPort,
                        UsePathVariable = ruleVM.ResponseAction.ReverseProxyUsePathVariable,
                        SetPath         = ruleVM.ResponseAction.SetReverseProxyPath,
                        PathAndQuery    = ruleVM.ResponseAction.ReverseProxyPathAndQuery
                    };
                    break;
                }
                httpServerConfig.Rules.Add(r);
            }
            return(httpServerConfig);
        }