From 2b67cff7c13b17ac875e110f1544c3ab61b6711b Mon Sep 17 00:00:00 2001 From: Noor Date: Fri, 31 Jan 2025 13:02:07 +0000 Subject: [PATCH] Refactor register node add IP information retrieval and update dependencies --- Cargo.lock | 18 ++++++++++++++++-- Cargo.toml | 4 +++- src/grpc.rs | 14 +++++++++++--- src/main.rs | 3 +++ src/utils.rs | 16 ++++++++++++++++ 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9c59d46..b1c76e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -308,6 +308,8 @@ dependencies = [ "prost-types", "rand", "reqwest", + "serde", + "serde_json", "tar", "tokio", "tokio-stream", @@ -464,6 +466,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -472,6 +475,12 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" +[[package]] +name = "futures-io" +version = "0.3.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" + [[package]] name = "futures-sink" version = "0.3.31" @@ -491,9 +500,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" dependencies = [ "futures-core", + "futures-io", + "futures-sink", "futures-task", + "memchr", "pin-project-lite", "pin-utils", + "slab", ] [[package]] @@ -1301,6 +1314,7 @@ dependencies = [ "base64", "bytes", "encoding_rs", + "futures-channel", "futures-core", "futures-util", "h2", @@ -1475,9 +1489,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.137" +version = "1.0.138" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "930cfb6e6abf99298aaad7d29abbef7a9999a9a8806a40088f55f0dcec03146b" +checksum = "d434192e7da787e94a6ea7e9670b26a036d0ca41e0b7efb2676dd32bae872949" dependencies = [ "itoa", "memchr", diff --git a/Cargo.toml b/Cargo.toml index a940bcf..7855dd1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ prost-types = "0.13.4" tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread", "fs"] } tonic = "0.12.3" chrono = "0.4.39" -reqwest = "0.12.12" +reqwest = { version = "0.12.12", features = ["blocking"] } flate2 = "1.0.35" tar = "0.4.43" anyhow = "1.0.95" @@ -20,6 +20,8 @@ tokio-stream = "0.1.17" detee-shared = { git = "ssh://git@gitea.detee.cloud/noormohammedb/detee-shared" } # detee-shared = { path = "../detee-shared" } log = "0.4.25" +serde = "1.0.217" +serde_json = "1.0.138" [build-dependencies] tonic-build = "0.12.3" diff --git a/src/grpc.rs b/src/grpc.rs index 07325f2..11b8b46 100644 --- a/src/grpc.rs +++ b/src/grpc.rs @@ -11,6 +11,9 @@ use tokio_stream::wrappers::ReceiverStream; use tokio_stream::StreamExt; use tonic::transport::Channel; +use crate::utils::IP_INFO; +use crate::{ADMIN_PUBKEY, NODE_PUBKEY}; + pub struct ConnectionData { pub brain_url: String, pub brain_msg_tx: Sender, @@ -24,7 +27,12 @@ pub async fn register_node(config: &crate::Config) -> Result, tx: Sender, ) -> Result<()> { - let pubkey = "0xd0837609aedd53854651210327db90f5c2626188a00e940bbc9eea2c7e6838b7".to_owned(); + let pubkey = NODE_PUBKEY.to_string(); log::debug!("starting to listen for messages from brain"); let mut grpc_stream = client.brain_messages(Pubkey { pubkey }).await?.into_inner(); @@ -94,7 +102,7 @@ pub async fn send_messages( rx: Receiver, tx: Sender, ) -> Result<()> { - let pubkey = "0xd0837609aedd53854651210327db90f5c2626188a00e940bbc9eea2c7e6838b7".to_owned(); + let pubkey = NODE_PUBKEY.to_string(); let rx_stream = ReceiverStream::new(rx); tx.send(DaemonMessage { diff --git a/src/main.rs b/src/main.rs index cc2e231..3b0c7ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,9 @@ use tokio::sync::mpsc::Sender; use tokio::time::sleep; use utils::handle_package; +const NODE_PUBKEY: &str = "0xd0837609aedd53854651210327db90f5c2626188a00e940bbc9eea2c7e6838b7"; +const ADMIN_PUBKEY: &str = "0x28a3a71197250b0fa4dd0f86288e07ec9cc78ce3338e21e2ebef84dd7780e3eb"; + #[derive(Debug)] pub struct Config { pub brain_url: String, diff --git a/src/utils.rs b/src/utils.rs index c1513ae..a466b9d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,11 +5,14 @@ use rand::Rng; use reqwest::Client; use std::io::BufReader; use std::path::Path; +use std::sync::LazyLock; use tar::Archive; use tokio::io::AsyncWriteExt; use tokio::net::TcpListener; use tokio::{fs, fs::File}; +pub static IP_INFO: LazyLock = LazyLock::new(|| get_ip_info().unwrap()); + pub async fn handle_package(package_url: String) -> Result { let dir_path = Path::new("./enclave_archives"); fs::create_dir_all(dir_path).await?; @@ -80,3 +83,16 @@ async fn is_port_available(port: u16) -> bool { .await .is_ok() } +#[derive(serde::Deserialize, Clone)] +pub struct IPInfo { + pub country: String, + pub region: String, + pub city: String, + pub ip: String, +} + +fn get_ip_info() -> anyhow::Result { + let body = reqwest::blocking::get("https://ipinfo.io/".to_string())?.text()?; + log::info!("Got the following data from ipinfo.io: {body}"); + Ok(serde_json::de::from_str(&body)?) +}