Exemplo n.º 1
0
        public async Task <int> GetSensorIdForHost(string hostname)
        {
            using (CbClient cbClient = new CbClient(this.ServerUri, this.ApiToken, false))
            {
                var queryForSensorIdResponse = await cbClient.HttpGetAsDynamicAsync(String.Format("/api/v1/sensor?hostname={0}", hostname));

                if (queryForSensorIdResponse.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var sensor = ((IEnumerable)queryForSensorIdResponse.Response).Cast <dynamic>()
                                 .FirstOrDefault();
                    if (sensor != null)
                    {
                        return(sensor.id);
                    }
                    else
                    {
                        return(-1);
                    }
                }
                else
                {
                    throw new ApplicationException(String.Format("Could not find sensor with hostname: '{0}' - Http Code {1}", hostname, queryForSensorIdResponse.StatusCode));
                }
            }
        }
Exemplo n.º 2
0
        static void CbClientConsole()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Synchronous API Examples");
            Console.ForegroundColor = ConsoleColor.White;

            // Creates an instance of CbClient in a using (to ensure that it is disposed of properly)
            using (CbClient client = new CbClient("https://192.168.43.164/", "999b934fb2236d1465ecc1577d4c44e9a87128d1", false))
            {
                // Get the server information as a string
                Console.WriteLine("/api/info");
                var infoStringResponse = client.HttpGetAsString("/api/info");
                WriteStringResponse(infoStringResponse);

                // Get the sensor statistics as a dictionary
                Console.WriteLine("/api/v1/sensor/statistics");
                var sensorStatsResponse = client.HttpGetAsDictionary("/api/v1/sensor/statistics");
                WriteDictionaryResponse(sensorStatsResponse);

                // Get up to 5 processes as dynamic
                Console.WriteLine("/api/v1/process?rows=5");
                var processSearchResponse = client.HttpGetAsDynamic("/api/v1/process?rows=5");
                Console.WriteLine("  Status: {0}", processSearchResponse.StatusCode);
                Console.WriteLine("  Content:");
                foreach (dynamic result in processSearchResponse.Response.results)
                {
                    Console.WriteLine("    {0} - {1}", result.process_name, result.process_md5);
                }
                Console.WriteLine();
            }

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine();
            Console.WriteLine();
        }
Exemplo n.º 3
0
        static async Task CbClientConsoleAsync()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Asynchronous API Examples");
            Console.ForegroundColor = ConsoleColor.White;

            // Creates an instance of CbClient in a using (to ensure that it is disposed of properly)
            using (CbClient client = new CbClient("https://192.168.43.164/", "999b934fb2236d1465ecc1577d4c44e9a87128d1", false))
            {
                // Get the server information as a string
                Console.WriteLine("/api/info");
                var infoStringResponse = await client.HttpGetAsStringAsync("/api/info");
                WriteStringResponse(infoStringResponse);

                // Get the sensor statistics as a dictionary
                Console.WriteLine("/api/v1/sensor/statistics");
                var sensorStatsResponse = await client.HttpGetAsDictionaryAsync("/api/v1/sensor/statistics");
                WriteDictionaryResponse(sensorStatsResponse);

                // Get up to 5 processes as dynamic
                Console.WriteLine("/api/v1/process?rows=5");
                var processSearchResponse = await client.HttpGetAsDynamicAsync("/api/v1/process?rows=5");
                Console.WriteLine("  Status: {0}", processSearchResponse.StatusCode);
                Console.WriteLine("  Content:");
                foreach (dynamic result in processSearchResponse.Response.results)
                {
                    Console.WriteLine("    {0} - {1}", result.process_name, result.process_md5);
                }
                Console.WriteLine();
            }

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine();
            Console.WriteLine();
        }
Exemplo n.º 4
0
        public async Task <int> UpdateFilesBatch(ObservableFileSystem fileSystem, int sensorId, int start, int rows, CancellationToken cancelToken = default(CancellationToken))
        {
            using (CbClient cbClient = new CbClient(this.ServerUri, this.ApiToken, false))
            {
                var queryForPidsResponse = await cbClient.HttpGetAsDynamicAsync(String.Format("/api/v1/process?start={0}&rows={1}&q=sensor_id:{2} and filemod_count:[1 TO *]&sort=start asc",
                                                                                              start, rows, sensorId));

                if (cancelToken.IsCancellationRequested)
                {
                    return(-1);
                }

                if (queryForPidsResponse.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    throw new ApplicationException(String.Format("Could not get process batch for sensor id:{0}, start:{1}, rows:{2} - HTTP Code {3}",
                                                                 sensorId, start, rows, queryForPidsResponse.StatusCode));
                }
                else
                {
                    int resultCount = 0;
                    foreach (var result in queryForPidsResponse.Response.results)
                    {
                        var processId = result.id;
                        var segmentId = result.segment_id;

                        var queryForEventsResponse = await cbClient.HttpGetAsDynamicAsync(String.Format("/api/v1/process/{0}/{1}/event", processId, segmentId));

                        if (queryForEventsResponse.StatusCode != System.Net.HttpStatusCode.OK)
                        {
                            // do something
                        }
                        else
                        {
                            foreach (string evt in queryForEventsResponse.Response.process.filemod_complete)
                            {
                                var evtParts = evt.Split('|');
                                int type     = Convert.ToInt32(evtParts[0]);
                                fileSystem.AddFileSystemItem(evtParts[2], evtParts[1], type);
                            }
                        }
                        resultCount++;

                        if (cancelToken.IsCancellationRequested)
                        {
                            return(resultCount);
                        }
                    }

                    return(resultCount);
                }
            }
        }
Exemplo n.º 5
0
        public async Task<int> UpdateFilesBatch(ObservableFileSystem fileSystem, int sensorId, int start, int rows, CancellationToken cancelToken = default(CancellationToken))
        {
            using (CbClient cbClient = new CbClient(this.ServerUri, this.ApiToken, false))
            {
                var queryForPidsResponse = await cbClient.HttpGetAsDynamicAsync(String.Format("/api/v1/process?start={0}&rows={1}&q=sensor_id:{2} and filemod_count:[1 TO *]&sort=start asc",
                    start, rows, sensorId));
                if (cancelToken.IsCancellationRequested)
                {
                    return -1;
                }

                if (queryForPidsResponse.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    throw new ApplicationException(String.Format("Could not get process batch for sensor id:{0}, start:{1}, rows:{2} - HTTP Code {3}", 
                        sensorId, start, rows, queryForPidsResponse.StatusCode));
                }
                else
                {
                    int resultCount = 0;
                    foreach (var result in queryForPidsResponse.Response.results)
                    {
                        var processId = result.id;
                        var segmentId = result.segment_id;

                        var queryForEventsResponse = await cbClient.HttpGetAsDynamicAsync(String.Format("/api/v1/process/{0}/{1}/event", processId, segmentId));
                        if (queryForEventsResponse.StatusCode != System.Net.HttpStatusCode.OK)
                        {
                            // do something
                        }
                        else
                        {
                            foreach (string evt in queryForEventsResponse.Response.process.filemod_complete)
                            {
                                var evtParts = evt.Split('|');
                                int type = Convert.ToInt32(evtParts[0]);
                                fileSystem.AddFileSystemItem(evtParts[2], evtParts[1], type);
                            }
                        }
                        resultCount++;

                        if (cancelToken.IsCancellationRequested)
                        {
                            return resultCount;
                        }
                    }

                    return resultCount;
                }
            }
        }
Exemplo n.º 6
0
        public void Constructor1_should_initialize_http_client_properties()
        {
            string serverUri = "http://serveruri";
            string token = "token";

            var client = new CbClient(serverUri: serverUri, token: token, sslVerify: true);

            client.Token.ShouldEqual(token);
            client.ServerUri.ShouldEqual(new Uri(serverUri, UriKind.Absolute));
            client.HttpClient.ShouldNotBeNull();
            client.HttpClient.BaseAddress.ShouldEqual(new Uri(serverUri, UriKind.Absolute));
            client.HttpClient.DefaultRequestHeaders.Where(x => x.Key == "X-Auth-Token" && x.Value.Contains(token)).ShouldHaveCountOf(1);
            client.HttpClient.DefaultRequestHeaders.Accept.ShouldHaveCountOf(1);
            client.HttpClient.DefaultRequestHeaders.Accept.First().MediaType.ShouldEqualIgnoringCase("application/json");
        }
Exemplo n.º 7
0
 public async Task<int> GetProcessCountForHost(int sensorId)
 {
     using (CbClient cbClient = new CbClient(this.ServerUri, this.ApiToken, false))
     {
         var queryForCountResponse = await cbClient.HttpGetAsDynamicAsync(String.Format("/api/v1/process?rows=0&q=sensor_id:{0} and filemod_count:[1 TO *]", sensorId.ToString()));
         if (queryForCountResponse.StatusCode == System.Net.HttpStatusCode.OK)
         {
             return queryForCountResponse.Response.total_results;
         }
         else
         {
             throw new ApplicationException(String.Format("Could not get count of processes for sensor id:{0} - HTTP Code {1}", sensorId, queryForCountResponse.StatusCode));
         }
     }
 }
Exemplo n.º 8
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CbClient.Focus();

            TxtInvoiceNumber.Text = invNumber;

            TxtBillAmount.SelectionStart = TxtBillAmount.Text.Length;
            TxtDrawingFee.SelectionStart = TxtDrawingFee.Text.Length;

            CbFinalized.IsChecked = false;

            LoadClients();

            LoadInvoice();
        }
Exemplo n.º 9
0
        public async Task <int> GetProcessCountForHost(int sensorId)
        {
            using (CbClient cbClient = new CbClient(this.ServerUri, this.ApiToken, false))
            {
                var queryForCountResponse = await cbClient.HttpGetAsDynamicAsync(String.Format("/api/v1/process?rows=0&q=sensor_id:{0} and filemod_count:[1 TO *]", sensorId.ToString()));

                if (queryForCountResponse.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    return(queryForCountResponse.Response.total_results);
                }
                else
                {
                    throw new ApplicationException(String.Format("Could not get count of processes for sensor id:{0} - HTTP Code {1}", sensorId, queryForCountResponse.StatusCode));
                }
            }
        }
Exemplo n.º 10
0
 public async Task<List<Hostname>> GetHostnames()
 {
     using (CbClient cbClient = new CbClient(this.ServerUri, this.ApiToken, false))
     {
         var hostnameResponse = await cbClient.HttpGetAsDynamicAsync("/api/v1/sensor");
         if (hostnameResponse.StatusCode == System.Net.HttpStatusCode.OK)
         {
             var hostnames = ((IEnumerable)hostnameResponse.Response).Cast<dynamic>()
                 .Select(x => new Hostname() { Name = x.computer_name, SensorId = x.id }).OrderBy(x => x.Name).ToList();
             return hostnames;
         }
         else
         {
             throw new ApplicationException(String.Format("Could not get list of hostnames - Http Code {0}", hostnameResponse.StatusCode));
         }
     }
 }
Exemplo n.º 11
0
        public async Task <List <Hostname> > GetHostnames()
        {
            using (CbClient cbClient = new CbClient(this.ServerUri, this.ApiToken, false))
            {
                var hostnameResponse = await cbClient.HttpGetAsDynamicAsync("/api/v1/sensor");

                if (hostnameResponse.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var hostnames = ((IEnumerable)hostnameResponse.Response).Cast <dynamic>()
                                    .Select(x => new Hostname()
                    {
                        Name = x.computer_name, SensorId = x.id
                    }).OrderBy(x => x.Name).ToList();
                    return(hostnames);
                }
                else
                {
                    throw new ApplicationException(String.Format("Could not get list of hostnames - Http Code {0}", hostnameResponse.StatusCode));
                }
            }
        }
Exemplo n.º 12
0
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CbClient.Focus();
            TxtBillAmount.Text = "0.00";
            TxtCommDue.Text    = "0.00";
            TxtDFAmount.Text   = "0.00";
            TxtDrawingFee.Text = "11.00";

            if (decimal.TryParse(TxtDrawingFee.Text.Replace(",", "").Replace(".", "").TrimStart('0'), out decimal result))
            {
                result    /= 10000;
                drawingFee = result;
            }

            TxtBillAmount.SelectionStart = TxtBillAmount.Text.Length;
            TxtDrawingFee.SelectionStart = TxtDrawingFee.Text.Length;

            DtpDate.SelectedDate  = DateTime.Now;
            CbFinalized.IsChecked = false;

            LoadClients();

            GetInvoiceNumber();
        }
Exemplo n.º 13
0
 public async Task<int> GetSensorIdForHost(string hostname)
 {
     using (CbClient cbClient = new CbClient(this.ServerUri, this.ApiToken, false))
     {
         var queryForSensorIdResponse = await cbClient.HttpGetAsDynamicAsync(String.Format("/api/v1/sensor?hostname={0}", hostname));
         if (queryForSensorIdResponse.StatusCode == System.Net.HttpStatusCode.OK)
         {
             var sensor = ((IEnumerable)queryForSensorIdResponse.Response).Cast<dynamic>()
                 .FirstOrDefault();
             if (sensor != null)
             {
                 return sensor.id;
             }
             else
             {
                 return -1;
             }
         }
         else
         {
             throw new ApplicationException(String.Format("Could not find sensor with hostname: '{0}' - Http Code {1}", hostname, queryForSensorIdResponse.StatusCode));
         }
     }
 }
Exemplo n.º 14
0
        public void Constructor1_should_use_no_ssl_handler_if_sslVerify_is_false_and_should_return_true_for_server_certificate_validation()
        {
            string serverUri = "http://serveruri";
            string token = "token";

            var client = new CbClient(serverUri: serverUri, token: token, sslVerify: false);

            client.HttpClient.ShouldNotBeNull();
            var handler = this.GetHandlerFromHttpClient(client.HttpClient) as WebRequestHandler;
            handler.ShouldNotBeNull();
            handler.ServerCertificateValidationCallback.ShouldNotBeNull();
            handler.ServerCertificateValidationCallback(null, null, null, SslPolicyErrors.RemoteCertificateNameMismatch).ShouldBeTrue();
        }
Exemplo n.º 15
0
        public void Constructor3_should_use_http_client_from_argument()
        {
            string serverUri = "http://serveruri";
            string token = "token";
            var httpClient = MockRepository.GenerateMock<HttpClient>();

            var client = new CbClient(serverUri: serverUri, token: token, httpClient: httpClient);

            client.HttpClient.ShouldNotBeNull();
            client.HttpClient.ShouldBeSameAs(httpClient);
        }
Exemplo n.º 16
0
        public void Constructor2_should_use_httpClientMessageHandler_if_passed()
        {
            string serverUri = "http://serveruri";
            string token = "token";
            var httpMessageHandler = MockRepository.GenerateMock<HttpMessageHandler>();

            var client = new CbClient(serverUri: serverUri, token: token, httpClientMessageHandler: httpMessageHandler);

            client.HttpClient.ShouldNotBeNull();
            this.GetHandlerFromHttpClient(client.HttpClient).ShouldBeSameAs(httpMessageHandler);
        }
Exemplo n.º 17
0
        public void Constructor1_should_use_default_handler_if_sslVerify_is_true()
        {
            string serverUri = "http://serveruri";
            string token = "token";

            var client = new CbClient(serverUri: serverUri, token: token, sslVerify: true);

            client.HttpClient.ShouldNotBeNull();
            this.GetHandlerFromHttpClient(client.HttpClient).ShouldBeOfType(typeof(HttpClientHandler));
        }