Beispiel #1
0
        public async void TestWebServerComms()
        {
            WebServer webServer = new WebServer();
            var       resStart  = await webServer.StartAsync();

            Assert.True(resStart);
            using (var httpClient = new OPHttpClient())
            {
                StoreName        storeName = StoreName.My; StoreLocation storeLocation = StoreLocation.LocalMachine;
                List <SSLCertVM> certs = await httpClient.RequestJsonAsync <List <SSLCertVM> >(HttpMethod.Get, "http://localhost:5024/config/getsslcerts?storename=" + storeName + "&storelocation=" + storeLocation, "Administrator", "Admin");

                Assert.NotNull(certs);

                WebServerConfig config = await httpClient.RequestJsonAsync <WebServerConfig>(HttpMethod.Get, "http://localhost:5024/config/load", "Administrator", "Admin");

                Assert.NotNull(config);

                var req2 = await httpClient.RequestStatusOnlyAsync(HttpMethod.Put, "http://localhost:5024/config/save", "Administrator", "Admin", new StringContent(JsonConvert.SerializeObject(config), Encoding.UTF8, "application/json"));

                Assert.Equal(HttpStatusCode.OK, req2);

                var req3 = await httpClient.RequestStatusOnlyAsync(HttpMethod.Put, "http://localhost:5024/config/save", "Administrator", "Admin", new StringContent("Some Random Data which is not a proper Json", Encoding.UTF8, "application/json"));

                Assert.Equal(HttpStatusCode.InternalServerError, req3);
            }
        }
Beispiel #2
0
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            httpServerConfigVM = (HttpServerConfigVM)uCHttpServerConfig.DataContext;
            Settings settings = Services.LoadConfigFile <Settings>("settings.json");

            configVM          = ((ServerConfigVM)this.DataContext);
            configVM.Host     = settings.LastHost;
            configVM.Username = settings.LastUsername;
            Services.Config   = settings;

            httpServerConfigVM.OnRefresh = async() =>
            {
                try
                {
                    Tuple <WebServerConfig, SSLCertVM, SSLCertVM> obj = await Services.OPHttpClient.RequestJsonAsync <Tuple <WebServerConfig, SSLCertVM, SSLCertVM> >(HttpMethod.Get, configVM.Host + "/config/load", configVM.Username, configVM.Password);

                    return(new Tuple <HttpServerConfig, SSLCertVM>(obj.Item1, obj.Item2));
                }
                catch (Exception ex)
                {
                    return(null);
                }
            };

            httpServerConfigVM.OnLogRefresh = async(lastLogReadUTC) =>
            {
                try
                {
                    List <LogStruct> logs = await Services.OPHttpClient.RequestJsonAsync <List <LogStruct> >(HttpMethod.Get, configVM.Host + "/config/getlogs?lastread=" + lastLogReadUTC.Ticks, configVM.Username, configVM.Password);

                    return(logs);
                }
                catch (Exception ex)
                {
                    return(null);
                }
            };
            httpServerConfigVM.CheckConnectionOK = () => { return(Task.FromResult(configVM.LoggedIn)); };

            httpServerConfigVM.OnSaveAndApply = async(httpServerConfig) =>
            {
                try
                {
                    UtilityConfig utilityConfig = new UtilityConfig()
                    {
                        AllowLocalhostConnectionsOnlyForHttp = configVM.ConfigLocalhostOnly,
                        SSLCertificateStoreName   = configVM.ConfigSSLCertificateStoreName,
                        SSLCertificateSubjectName = configVM.ConfigSSLCertificateSubjectName,
                        EnableHTTPS        = configVM.ConfigEnableHTTPS,
                        ConfigUsername     = configVM.ConfigUsername,
                        HTTPPort           = configVM.ConfigHTTPPort,
                        HTTPSPort          = configVM.ConfigHTTPSPort,
                        ConfigPasswordHash = configVM.ConfigPasswordHash
                    };
                    WebServerConfig webServerConfig = new WebServerConfig(httpServerConfig, utilityConfig);
                    var             res             = await Services.OPHttpClient.RequestStatusOnlyAsync(HttpMethod.Put, configVM.Host + "/config/save", configVM.Username, configVM.Password, webServerConfig);

                    if (res == HttpStatusCode.OK)
                    {
                        return(true);
                    }
                }
                catch
                {
                    return(false);
                }
                return(false);
            };

            httpServerConfigVM.OnSSLCertRefresh = async(storeName, storeLocation) =>
            {
                using (OPHttpClient httpClient = new OPHttpClient())
                {
                    List <SSLCertVM> Res = await httpClient.RequestJsonAsync <List <SSLCertVM> >(HttpMethod.Get, configVM.Host + "/config/getsslcerts?storename=" + storeName + "&storelocation=" + storeLocation, configVM.Username, configVM.Password);

                    return(Res);
                }
            };
            ((SelectSSLCertVM)uCConfigSelectSSLCert.DataContext).OnRefresh = (storeName, storeLocation) =>
            {
                return(httpServerConfigVM.OnSSLCertRefresh?.Invoke(storeName, storeLocation));
            };
        }