From 21678caf58466a2d40b2bbb7d3a73d1ced441478 Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Mon, 28 Aug 2023 16:20:20 +0900 Subject: [PATCH 1/2] Add send_device_info --- Cargo.lock | 24 +++++++++++++++++ Cargo.toml | 1 + src/main.rs | 25 ++++++++++++++++++ src/nodex/utils/hub_client.rs | 50 +++++++++++++++++++++++++++++++++-- src/services/hub.rs | 35 ++++++++++++++++++++++++ 5 files changed, 133 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4479c252..e6e7ead7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2372,6 +2372,16 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "mac_address" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4863ee94f19ed315bf3bc00299338d857d4b5bc856af375cc97d237382ad3856" +dependencies = [ + "nix", + "winapi", +] + [[package]] name = "match_cfg" version = "0.1.0" @@ -2441,6 +2451,19 @@ dependencies = [ "getrandom 0.2.7", ] +[[package]] +name = "nix" +version = "0.23.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f3790c00a0150112de0f4cd161e3d7fc4b2d8a5542ffc35f099a2562aecb35c" +dependencies = [ + "bitflags", + "cc", + "cfg-if", + "libc", + "memoffset", +] + [[package]] name = "nodex" version = "1.1.12" @@ -2469,6 +2492,7 @@ dependencies = [ "k256 0.10.4", "libloading", "log", + "mac_address", "qstring", "reqwest", "rstest", diff --git a/Cargo.toml b/Cargo.toml index ac183a8d..f6c21005 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -75,6 +75,7 @@ rumqttc = { version = "0.20.0" } cuid = { version = "1.3.1" } shadow-rs = "0.21.0" dotenv = "0.15.0" +mac_address = "1.1.5" [dev-dependencies] rstest = { version = "0.17.0" } diff --git a/src/main.rs b/src/main.rs index d2a04869..05b4c5c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,8 @@ use crate::config::ServerConfig; use crate::network::Network; use dotenv::dotenv; use handlers::Command; +use mac_address::get_mac_address; +use std::env; mod config; mod controllers; @@ -180,6 +182,7 @@ async fn main() -> std::io::Result<()> { // NOTE: hub initilize hub_initilize(did.did_document.id.clone()).await; + send_device_info(did.did_document.id.clone()).await; let sock_path = runtime_dir.clone().join("nodex.sock"); @@ -323,6 +326,28 @@ async fn hub_initilize(did: String) { }; } +async fn send_device_info(did: String) { + const VERSION: &str = env!("CARGO_PKG_VERSION"); + const OS: &str = env::consts::OS; + let mac_address: String = match get_mac_address() { + Ok(Some(ma)) => ma.to_string(), + Ok(None) => String::from("No MAC address found."), + Err(_) => String::from("No MAC address found."), + }; + + let hub = Hub::new(); + match hub + .send_device_info(did, mac_address, VERSION.to_string(), OS.to_string()) + .await + { + Ok(()) => (), + Err(e) => { + log::error!("{:?}", e); + panic!() + } + }; +} + use env_logger::fmt::Color; use log::Level; diff --git a/src/nodex/utils/hub_client.rs b/src/nodex/utils/hub_client.rs index 4b7685c3..e74e8c04 100644 --- a/src/nodex/utils/hub_client.rs +++ b/src/nodex/utils/hub_client.rs @@ -7,6 +7,9 @@ use reqwest::{ }; use sha2::Sha256; +use crate::services::internal::didcomm_encrypted::DIDCommEncryptedService; +use serde_json::json; + type HmacSha256 = Hmac; pub struct HubClientConfig { @@ -82,8 +85,8 @@ impl HubClient { } } - pub async fn post(&self, _path: &str, body: &str) -> Result { - let url = self.base_url.join(_path); + pub async fn post(&self, path: &str, body: &str) -> Result { + let url = self.base_url.join(path); let headers = self.auth_headers(body.to_string()); if let Err(e) = headers { log::error!("{:?}", e); @@ -105,6 +108,49 @@ impl HubClient { } } + pub async fn send_device_info( + &self, + path: &str, + to_did: &str, + mac_address: &str, + version: &str, + os: &str, + ) -> Result { + let message = json!({ + "mac_address": mac_address, + "version": version, + "os": os, + }); + let payload = match DIDCommEncryptedService::generate(to_did, &json!(message), None).await { + Ok(v) => v, + Err(e) => { + log::error!("{:?}", e); + return Err(NodeXError {}); + } + }; + let payload = match serde_json::to_string(&payload) { + Ok(v) => v, + Err(e) => { + log::error!("{:?}", e); + return Err(NodeXError {}); + } + }; + let url = self.base_url.join(path); + match self + .instance + .post(&url.unwrap().to_string()) + .body(payload.to_string()) + .send() + .await + { + Ok(v) => Ok(v), + Err(e) => { + log::error!("{:?}", e); + Err(NodeXError {}) + } + } + } + #[allow(dead_code)] pub async fn put(&self, _path: &str) -> Result { let url = self.base_url.join(_path); diff --git a/src/services/hub.rs b/src/services/hub.rs index c291677b..137acc1e 100644 --- a/src/services/hub.rs +++ b/src/services/hub.rs @@ -18,6 +18,14 @@ struct RegisterDeviceRequest { project_id: String, } +#[derive(Debug, Serialize, Deserialize)] +struct SendDeviceInfoRequest { + device_did: String, + mac_address: String, + version: String, + os: String, +} + impl Hub { pub fn new() -> Self { let server_config = server_config(); @@ -59,4 +67,31 @@ impl Hub { } } } + + pub async fn send_device_info( + &self, + to_did: String, + mac_address: String, + version: String, + os: String, + ) -> Result<(), NodeXError> { + let res = match self + .http_client + .send_device_info("/v1/device_info", &to_did, &mac_address, &version, &os) + .await + { + Ok(v) => v, + Err(e) => { + log::error!("{:?}", e); + return Err(NodeXError {}); + } + }; + match res.json::().await { + Ok(_) => Ok(()), + Err(e) => { + log::error!("{:?}", e); + Err(NodeXError {}) + } + } + } } From c0b716cc9687f1a849f326ee89c803c84a06507a Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Tue, 29 Aug 2023 22:19:01 +0900 Subject: [PATCH 2/2] Fix match arm --- src/main.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 05b4c5c8..f2fc220a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -331,8 +331,7 @@ async fn send_device_info(did: String) { const OS: &str = env::consts::OS; let mac_address: String = match get_mac_address() { Ok(Some(ma)) => ma.to_string(), - Ok(None) => String::from("No MAC address found."), - Err(_) => String::from("No MAC address found."), + _ => String::from("No MAC address found."), }; let hub = Hub::new();