Refactor register node
add IP information retrieval and update dependencies
This commit is contained in:
		
							parent
							
								
									17400339fc
								
							
						
					
					
						commit
						2b67cff7c1
					
				
							
								
								
									
										18
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										18
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							| @ -308,6 +308,8 @@ dependencies = [ | |||||||
|  "prost-types", |  "prost-types", | ||||||
|  "rand", |  "rand", | ||||||
|  "reqwest", |  "reqwest", | ||||||
|  |  "serde", | ||||||
|  |  "serde_json", | ||||||
|  "tar", |  "tar", | ||||||
|  "tokio", |  "tokio", | ||||||
|  "tokio-stream", |  "tokio-stream", | ||||||
| @ -464,6 +466,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||||||
| checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" | checksum = "2dff15bf788c671c1934e366d07e30c1814a8ef514e1af724a602e8a2fbe1b10" | ||||||
| dependencies = [ | dependencies = [ | ||||||
|  "futures-core", |  "futures-core", | ||||||
|  |  "futures-sink", | ||||||
| ] | ] | ||||||
| 
 | 
 | ||||||
| [[package]] | [[package]] | ||||||
| @ -472,6 +475,12 @@ version = "0.3.31" | |||||||
| source = "registry+https://github.com/rust-lang/crates.io-index" | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
| checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" | checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e" | ||||||
| 
 | 
 | ||||||
|  | [[package]] | ||||||
|  | name = "futures-io" | ||||||
|  | version = "0.3.31" | ||||||
|  | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
|  | checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6" | ||||||
|  | 
 | ||||||
| [[package]] | [[package]] | ||||||
| name = "futures-sink" | name = "futures-sink" | ||||||
| version = "0.3.31" | version = "0.3.31" | ||||||
| @ -491,9 +500,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||||||
| checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" | checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81" | ||||||
| dependencies = [ | dependencies = [ | ||||||
|  "futures-core", |  "futures-core", | ||||||
|  |  "futures-io", | ||||||
|  |  "futures-sink", | ||||||
|  "futures-task", |  "futures-task", | ||||||
|  |  "memchr", | ||||||
|  "pin-project-lite", |  "pin-project-lite", | ||||||
|  "pin-utils", |  "pin-utils", | ||||||
|  |  "slab", | ||||||
| ] | ] | ||||||
| 
 | 
 | ||||||
| [[package]] | [[package]] | ||||||
| @ -1301,6 +1314,7 @@ dependencies = [ | |||||||
|  "base64", |  "base64", | ||||||
|  "bytes", |  "bytes", | ||||||
|  "encoding_rs", |  "encoding_rs", | ||||||
|  |  "futures-channel", | ||||||
|  "futures-core", |  "futures-core", | ||||||
|  "futures-util", |  "futures-util", | ||||||
|  "h2", |  "h2", | ||||||
| @ -1475,9 +1489,9 @@ dependencies = [ | |||||||
| 
 | 
 | ||||||
| [[package]] | [[package]] | ||||||
| name = "serde_json" | name = "serde_json" | ||||||
| version = "1.0.137" | version = "1.0.138" | ||||||
| source = "registry+https://github.com/rust-lang/crates.io-index" | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||||
| checksum = "930cfb6e6abf99298aaad7d29abbef7a9999a9a8806a40088f55f0dcec03146b" | checksum = "d434192e7da787e94a6ea7e9670b26a036d0ca41e0b7efb2676dd32bae872949" | ||||||
| dependencies = [ | dependencies = [ | ||||||
|  "itoa", |  "itoa", | ||||||
|  "memchr", |  "memchr", | ||||||
|  | |||||||
| @ -10,7 +10,7 @@ prost-types = "0.13.4" | |||||||
| tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread", "fs"] } | tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread", "fs"] } | ||||||
| tonic = "0.12.3" | tonic = "0.12.3" | ||||||
| chrono = "0.4.39" | chrono = "0.4.39" | ||||||
| reqwest = "0.12.12" | reqwest = { version = "0.12.12", features = ["blocking"] } | ||||||
| flate2 = "1.0.35" | flate2 = "1.0.35" | ||||||
| tar = "0.4.43" | tar = "0.4.43" | ||||||
| anyhow = "1.0.95" | 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 = { git = "ssh://git@gitea.detee.cloud/noormohammedb/detee-shared" } | ||||||
| # detee-shared = { path = "../detee-shared" } | # detee-shared = { path = "../detee-shared" } | ||||||
| log = "0.4.25" | log = "0.4.25" | ||||||
|  | serde = "1.0.217" | ||||||
|  | serde_json = "1.0.138" | ||||||
| 
 | 
 | ||||||
| [build-dependencies] | [build-dependencies] | ||||||
| tonic-build = "0.12.3" | tonic-build = "0.12.3" | ||||||
|  | |||||||
							
								
								
									
										14
									
								
								src/grpc.rs
									
									
									
									
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										14
									
								
								src/grpc.rs
									
									
									
									
									
								
							| @ -11,6 +11,9 @@ use tokio_stream::wrappers::ReceiverStream; | |||||||
| use tokio_stream::StreamExt; | use tokio_stream::StreamExt; | ||||||
| use tonic::transport::Channel; | use tonic::transport::Channel; | ||||||
| 
 | 
 | ||||||
|  | use crate::utils::IP_INFO; | ||||||
|  | use crate::{ADMIN_PUBKEY, NODE_PUBKEY}; | ||||||
|  | 
 | ||||||
| pub struct ConnectionData { | pub struct ConnectionData { | ||||||
|     pub brain_url: String, |     pub brain_url: String, | ||||||
|     pub brain_msg_tx: Sender<BrainMessage>, |     pub brain_msg_tx: Sender<BrainMessage>, | ||||||
| @ -24,7 +27,12 @@ pub async fn register_node(config: &crate::Config) -> Result<Vec<ContainerContra | |||||||
|     log::debug!("registering node with brain"); |     log::debug!("registering node with brain"); | ||||||
| 
 | 
 | ||||||
|     let req = RegisterNodeReq { |     let req = RegisterNodeReq { | ||||||
|         ..Default::default() |         node_pubkey: NODE_PUBKEY.to_string(), | ||||||
|  |         owner_pubkey: ADMIN_PUBKEY.to_string(), | ||||||
|  |         main_ip: IP_INFO.ip.clone(), | ||||||
|  |         city: IP_INFO.city.clone(), | ||||||
|  |         region: IP_INFO.region.clone(), | ||||||
|  |         country: IP_INFO.country.clone(), | ||||||
|     }; |     }; | ||||||
| 
 | 
 | ||||||
|     let mut container_contracts = vec![]; |     let mut container_contracts = vec![]; | ||||||
| @ -69,7 +77,7 @@ pub async fn receive_messages( | |||||||
|     mut client: BrainSgxDaemonClient<Channel>, |     mut client: BrainSgxDaemonClient<Channel>, | ||||||
|     tx: Sender<BrainMessage>, |     tx: Sender<BrainMessage>, | ||||||
| ) -> Result<()> { | ) -> Result<()> { | ||||||
|     let pubkey = "0xd0837609aedd53854651210327db90f5c2626188a00e940bbc9eea2c7e6838b7".to_owned(); |     let pubkey = NODE_PUBKEY.to_string(); | ||||||
| 
 | 
 | ||||||
|     log::debug!("starting to listen for messages from brain"); |     log::debug!("starting to listen for messages from brain"); | ||||||
|     let mut grpc_stream = client.brain_messages(Pubkey { pubkey }).await?.into_inner(); |     let mut grpc_stream = client.brain_messages(Pubkey { pubkey }).await?.into_inner(); | ||||||
| @ -94,7 +102,7 @@ pub async fn send_messages( | |||||||
|     rx: Receiver<DaemonMessage>, |     rx: Receiver<DaemonMessage>, | ||||||
|     tx: Sender<DaemonMessage>, |     tx: Sender<DaemonMessage>, | ||||||
| ) -> Result<()> { | ) -> Result<()> { | ||||||
|     let pubkey = "0xd0837609aedd53854651210327db90f5c2626188a00e940bbc9eea2c7e6838b7".to_owned(); |     let pubkey = NODE_PUBKEY.to_string(); | ||||||
| 
 | 
 | ||||||
|     let rx_stream = ReceiverStream::new(rx); |     let rx_stream = ReceiverStream::new(rx); | ||||||
|     tx.send(DaemonMessage { |     tx.send(DaemonMessage { | ||||||
|  | |||||||
| @ -21,6 +21,9 @@ use tokio::sync::mpsc::Sender; | |||||||
| use tokio::time::sleep; | use tokio::time::sleep; | ||||||
| use utils::handle_package; | use utils::handle_package; | ||||||
| 
 | 
 | ||||||
|  | const NODE_PUBKEY: &str = "0xd0837609aedd53854651210327db90f5c2626188a00e940bbc9eea2c7e6838b7"; | ||||||
|  | const ADMIN_PUBKEY: &str = "0x28a3a71197250b0fa4dd0f86288e07ec9cc78ce3338e21e2ebef84dd7780e3eb"; | ||||||
|  | 
 | ||||||
| #[derive(Debug)] | #[derive(Debug)] | ||||||
| pub struct Config { | pub struct Config { | ||||||
|     pub brain_url: String, |     pub brain_url: String, | ||||||
|  | |||||||
							
								
								
									
										16
									
								
								src/utils.rs
									
									
									
									
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										16
									
								
								src/utils.rs
									
									
									
									
									
								
							| @ -5,11 +5,14 @@ use rand::Rng; | |||||||
| use reqwest::Client; | use reqwest::Client; | ||||||
| use std::io::BufReader; | use std::io::BufReader; | ||||||
| use std::path::Path; | use std::path::Path; | ||||||
|  | use std::sync::LazyLock; | ||||||
| use tar::Archive; | use tar::Archive; | ||||||
| use tokio::io::AsyncWriteExt; | use tokio::io::AsyncWriteExt; | ||||||
| use tokio::net::TcpListener; | use tokio::net::TcpListener; | ||||||
| use tokio::{fs, fs::File}; | use tokio::{fs, fs::File}; | ||||||
| 
 | 
 | ||||||
|  | pub static IP_INFO: LazyLock<IPInfo> = LazyLock::new(|| get_ip_info().unwrap()); | ||||||
|  | 
 | ||||||
| pub async fn handle_package(package_url: String) -> Result<String> { | pub async fn handle_package(package_url: String) -> Result<String> { | ||||||
|     let dir_path = Path::new("./enclave_archives"); |     let dir_path = Path::new("./enclave_archives"); | ||||||
|     fs::create_dir_all(dir_path).await?; |     fs::create_dir_all(dir_path).await?; | ||||||
| @ -80,3 +83,16 @@ async fn is_port_available(port: u16) -> bool { | |||||||
|         .await |         .await | ||||||
|         .is_ok() |         .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<IPInfo> { | ||||||
|  |     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)?) | ||||||
|  | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user