From f0be07df1546fa89ed7276df020074c1b0d53d98 Mon Sep 17 00:00:00 2001 From: cn-kali-team Date: Wed, 22 Nov 2023 23:50:32 +0800 Subject: [PATCH] fix format --- nvd-api/src/lib.rs | 43 ++++++++++++++----------------- nvd-api/src/v2/api.rs | 10 +++++++ nvd-api/src/v2/mod.rs | 3 +-- nvd-api/src/v2/vulnerabilities.rs | 20 +++++++------- 4 files changed, 41 insertions(+), 35 deletions(-) create mode 100644 nvd-api/src/v2/api.rs diff --git a/nvd-api/src/lib.rs b/nvd-api/src/lib.rs index 9f938e0..98702ad 100644 --- a/nvd-api/src/lib.rs +++ b/nvd-api/src/lib.rs @@ -1,9 +1,11 @@ -use reqwest::{ClientBuilder, RequestBuilder}; use crate::error::Error; +use reqwest::{ClientBuilder, RequestBuilder}; +use serde_json::Value::Object; -mod v2; mod error; - +mod v2; +const BASE_URL: &'static str = "https://services.nvd.nist.gov/rest/json/"; +const VERSION: f32 = 2.0; #[derive(Debug, Clone)] pub struct NVDApi { base_path: String, @@ -11,22 +13,18 @@ pub struct NVDApi { } impl NVDApi { - pub fn new(api_token: String) -> Result { + pub fn new(api_token: Option) -> Result { let mut headers = reqwest::header::HeaderMap::new(); - headers.insert( - "Notion-Version", - reqwest::header::HeaderValue::from_static(NOTION_API_VERSION), - ); let mut auth_value = reqwest::header::HeaderValue::from_str(&format!("Bearer {api_token}")) - .map_err(|source| Error::InvalidApiToken { source })?; + .map_err(|source| Error::InvalidApiToken { source })?; auth_value.set_sensitive(true); headers.insert(reqwest::header::AUTHORIZATION, auth_value); let api_client = ClientBuilder::new() - .default_headers(headers) - .build() - .map_err(|source| Error::ErrorBuildingClient { source })?; + .default_headers(headers) + .build() + .map_err(|source| Error::ErrorBuildingClient { source })?; Ok(NVDApi { - base_path: "https://api.notion.com/v1".to_owned(), + base_path: BASE_URL.to_owned(), client: api_client, }) } @@ -36,18 +34,17 @@ impl NVDApi { pub async fn request(&self, request: RequestBuilder) -> Result { let request = request.build()?; let json = self - .client - .execute(request) - .await - .map_err(|source| Error::RequestFailed { source })? - .text() - .await - .map_err(|source| Error::ResponseIoError { source })?; - let result = - serde_json::from_str(&json).map_err(|source| Error::JsonParseError { source })?; + .client + .execute(request) + .await + .map_err(|source| Error::RequestFailed { source })? + .text() + .await + .map_err(|source| Error::ResponseIoError { source })?; + let result = serde_json::from_str(&json).map_err(|source| Error::JsonParseError { source })?; match result { Object::Error { error } => Err(Error::ApiError { error }), response => Ok(response), } } -} \ No newline at end of file +} diff --git a/nvd-api/src/v2/api.rs b/nvd-api/src/v2/api.rs new file mode 100644 index 0000000..49a5656 --- /dev/null +++ b/nvd-api/src/v2/api.rs @@ -0,0 +1,10 @@ +use crate::v2::vulnerabilities::Vulnerabilities; +use crate::{Error, NVDApi, Object}; +const ROUTER: &str = "cve"; + +impl NVDApi { + pub async fn vulnerabilities(&self, query: Vulnerabilities) -> Result { + let u = format!("{}/{}", self.base_path, ROUTER); + self.request(self.client.get(u).query(&query)).await + } +} diff --git a/nvd-api/src/v2/mod.rs b/nvd-api/src/v2/mod.rs index 3339120..acd2a8f 100644 --- a/nvd-api/src/v2/mod.rs +++ b/nvd-api/src/v2/mod.rs @@ -1,4 +1,3 @@ mod vulnerabilities; +mod api; -const BASE_URL: &'static str = "https://services.nvd.nist.gov/rest/json/"; -const VERSION: f32 = 2.0; \ No newline at end of file diff --git a/nvd-api/src/v2/vulnerabilities.rs b/nvd-api/src/v2/vulnerabilities.rs index 6acf772..bde0bfd 100644 --- a/nvd-api/src/v2/vulnerabilities.rs +++ b/nvd-api/src/v2/vulnerabilities.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; - +// https://nvd.nist.gov/developers/vulnerabilities #[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)] -struct CVE { +pub struct Vulnerabilities { cpe_name: Option, cve_id: Option, cvss_v2_metrics: Option, @@ -29,7 +29,7 @@ struct CVE { } #[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)] -struct VirtualMatch { +pub struct VirtualMatch { virtual_match_string: String, #[serde(flatten)] version_start: Option, @@ -38,43 +38,43 @@ struct VirtualMatch { } #[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)] -struct VersionStart { +pub struct VersionStart { version_start: String, version_start_type: String, } #[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)] -struct VersionEnd { +pub struct VersionEnd { version_end: String, version_end_type: String, } #[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)] -enum VersionType { +pub enum VersionType { Including, Excluding, } #[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)] -struct LimitOffset { +pub struct LimitOffset { results_per_page: Option, start_index: Option, } #[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)] -struct PubDate { +pub struct PubDate { pub_start_date: String, pub_end_date: String, } #[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)] -struct Keyword { +pub struct Keyword { keyword_exact_match: bool, keyword_search: String, } #[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Eq)] -struct LastModDate { +pub struct LastModDate { last_mod_start_date: String, last_mod_end_date: String, } \ No newline at end of file