From d9c2b0f698df3e11a978892481fa10f82663a14f Mon Sep 17 00:00:00 2001 From: perp Date: Sat, 25 May 2024 00:56:59 +0100 Subject: [PATCH] Added hosts endpoint group --- Cargo.lock | 4 ++-- README.md | 3 --- construct-models/Cargo.toml | 2 +- construct/Cargo.toml | 2 +- construct/src/hosts/aggregate.rs | 31 +++++++++++++++++++++++++ construct/src/hosts/certificates.rs | 32 ++++++++++++++++++++++++++ construct/src/hosts/comments.rs | 19 ++++++++++++++++ construct/src/hosts/diff.rs | 30 +++++++++++++++++++++++++ construct/src/hosts/mod.rs | 27 ++++++++++++++++++++++ construct/src/hosts/names.rs | 20 +++++++++++++++++ construct/src/hosts/search.rs | 35 +++++++++++++++++++++++++++++ construct/src/lib.rs | 1 + 12 files changed, 199 insertions(+), 7 deletions(-) create mode 100644 construct/src/hosts/aggregate.rs create mode 100644 construct/src/hosts/certificates.rs create mode 100644 construct/src/hosts/comments.rs create mode 100644 construct/src/hosts/diff.rs create mode 100644 construct/src/hosts/mod.rs create mode 100644 construct/src/hosts/names.rs create mode 100644 construct/src/hosts/search.rs diff --git a/Cargo.lock b/Cargo.lock index 6658ffd..61ec058 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -99,7 +99,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "construct" -version = "1.6.0" +version = "1.13.0" dependencies = [ "async-trait", "construct-models", @@ -109,7 +109,7 @@ dependencies = [ [[package]] name = "construct-models" -version = "1.6.0" +version = "1.13.0" dependencies = [ "reqwest", "serde", diff --git a/README.md b/README.md index 743ff6f..b8f72a9 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,5 @@ Construct is a Censys API wrapper made in Rust cargo run --example basic ``` -## Todo -- [ ] Add authentication - ## Disclaimer ###### Developers are not responsible for any misuse diff --git a/construct-models/Cargo.toml b/construct-models/Cargo.toml index 1408f86..ab596a0 100644 --- a/construct-models/Cargo.toml +++ b/construct-models/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "construct-models" -version = "1.6.0" +version = "1.13.0" edition = "2021" authors.workspace = true license.workspace = true diff --git a/construct/Cargo.toml b/construct/Cargo.toml index c4cf17a..50851bb 100644 --- a/construct/Cargo.toml +++ b/construct/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "construct" -version = "1.6.0" +version = "1.13.0" edition = "2021" authors.workspace = true license.workspace = true diff --git a/construct/src/hosts/aggregate.rs b/construct/src/hosts/aggregate.rs new file mode 100644 index 0000000..f39b1bf --- /dev/null +++ b/construct/src/hosts/aggregate.rs @@ -0,0 +1,31 @@ +use construct_models::hosts::Aggregate; + +use crate::prelude::*; + +impl Construct { + /// Returns aggregation of hosts that match the given query string + pub async fn fetch_hosts_aggregate( + &self, + query: &str, + field: &str, + num_buckets: i16, + virtual_hosts: &str, + ) -> Result { + Ok(self + .client + .get(format!("{}/v2/hosts/aggregate", self.base_url)) + .basic_auth(&self.api_id, Some(&self.api_secret)) + .query(&[ + ("q", query), + ("field", field), + ("num_buckets", &num_buckets.to_string()), + ("virtual_hosts", virtual_hosts), + ]) + .send() + .await? + .process_error() + .await? + .json() + .await?) + } +} diff --git a/construct/src/hosts/certificates.rs b/construct/src/hosts/certificates.rs new file mode 100644 index 0000000..241ed22 --- /dev/null +++ b/construct/src/hosts/certificates.rs @@ -0,0 +1,32 @@ +use construct_models::hosts::Certificates; + +use crate::prelude::*; + +impl Construct { + /// Returns a list of observations containing certificates which were ever presented on this host. + pub async fn fetch_hosts_certificates( + &self, + ip: &str, + per_page: i16, + start_time: &str, + end_time: &str, + cursor: &str, + ) -> Result { + Ok(self + .client + .get(format!("{}/v2/hosts/{}/certificates", self.base_url, ip)) + .basic_auth(&self.api_id, Some(&self.api_secret)) + .query(&[ + ("start_time", start_time), + ("end_time", end_time), + ("cursor", cursor), + ("per_page", &per_page.to_string()), + ]) + .send() + .await? + .process_error() + .await? + .json() + .await?) + } +} diff --git a/construct/src/hosts/comments.rs b/construct/src/hosts/comments.rs new file mode 100644 index 0000000..cec293d --- /dev/null +++ b/construct/src/hosts/comments.rs @@ -0,0 +1,19 @@ +use construct_models::hosts::Comments; + +use crate::prelude::*; + +impl Construct { + /// Returns a list of comments on the given host. + pub async fn fetch_hosts_comments(&self, ip: &str) -> Result { + Ok(self + .client + .get(format!("{}/v2/hosts/{}/comments", self.base_url, ip)) + .basic_auth(&self.api_id, Some(&self.api_secret)) + .send() + .await? + .process_error() + .await? + .json() + .await?) + } +} diff --git a/construct/src/hosts/diff.rs b/construct/src/hosts/diff.rs new file mode 100644 index 0000000..aa17ef5 --- /dev/null +++ b/construct/src/hosts/diff.rs @@ -0,0 +1,30 @@ +use construct_models::hosts::Diff; + +use crate::prelude::*; + +impl Construct { + /// Returns a diff of a host against different points in time or against a different host altogether. + pub async fn fetch_hosts_diff( + &self, + ip: &str, + ip_b: &str, + at_time: &str, + at_time_b: &str, + ) -> Result { + Ok(self + .client + .get(format!("{}/v2/hosts/{}/diff", self.base_url, ip)) + .basic_auth(&self.api_id, Some(&self.api_secret)) + .query(&[ + ("ip_b", ip_b), + ("at_time", at_time), + ("at_time_b", at_time_b), + ]) + .send() + .await? + .process_error() + .await? + .json() + .await?) + } +} diff --git a/construct/src/hosts/mod.rs b/construct/src/hosts/mod.rs new file mode 100644 index 0000000..d773b36 --- /dev/null +++ b/construct/src/hosts/mod.rs @@ -0,0 +1,27 @@ +use construct_models::hosts::Host; + +use crate::prelude::*; + +pub(crate) mod aggregate; +pub(crate) mod certificates; +pub(crate) mod comments; +pub(crate) mod diff; +pub(crate) mod names; +pub(crate) mod search; + +impl Construct { + /// Returns host information for the specified IP address + pub async fn fetch_hosts(&self, ip: &str, at_time: &str) -> Result { + Ok(self + .client + .get(format!("{}/v2/hosts/{}", self.base_url, ip)) + .basic_auth(&self.api_id, Some(&self.api_secret)) + .query(&[("at_time", at_time)]) + .send() + .await? + .process_error() + .await? + .json() + .await?) + } +} diff --git a/construct/src/hosts/names.rs b/construct/src/hosts/names.rs new file mode 100644 index 0000000..c55ca52 --- /dev/null +++ b/construct/src/hosts/names.rs @@ -0,0 +1,20 @@ +use construct_models::hosts::Names; + +use crate::prelude::*; + +impl Construct { + /// Returns host names for the specified IP address + pub async fn fetch_hosts_names(&self, ip: &str, per_page: i16, cursor: &str) -> Result { + Ok(self + .client + .get(format!("{}/v2/hosts/{}/names", self.base_url, ip)) + .basic_auth(&self.api_id, Some(&self.api_secret)) + .query(&[("cursor", cursor), ("per_page", &per_page.to_string())]) + .send() + .await? + .process_error() + .await? + .json() + .await?) + } +} diff --git a/construct/src/hosts/search.rs b/construct/src/hosts/search.rs new file mode 100644 index 0000000..ac26486 --- /dev/null +++ b/construct/src/hosts/search.rs @@ -0,0 +1,35 @@ +use construct_models::hosts::Search; + +use crate::prelude::*; + +impl Construct { + /// Returns previews of hosts matching a specified search query + pub async fn search_hosts_( + &self, + query: &str, + per_page: i16, + virtual_hosts: &str, + sort: &str, + cursor: &str, + fields: &str, + ) -> Result { + Ok(self + .client + .get(format!("{}/v2/hosts/search", self.base_url)) + .basic_auth(&self.api_id, Some(&self.api_secret)) + .query(&[ + ("q", query), + ("per_page", &per_page.to_string()), + ("virtual_hosts", virtual_hosts), + ("sort", sort), + ("cursor", cursor), + ("fields", fields), + ]) + .send() + .await? + .process_error() + .await? + .json() + .await?) + } +} diff --git a/construct/src/lib.rs b/construct/src/lib.rs index ea4e3b3..bf53a96 100644 --- a/construct/src/lib.rs +++ b/construct/src/lib.rs @@ -3,6 +3,7 @@ use construct_models::Error; mod account; mod data; +mod hosts; mod metadata; pub(crate) mod prelude {