From 9aa02821dbed7afd34007c86a63ac14f47fef469 Mon Sep 17 00:00:00 2001 From: Gaius Date: Fri, 2 Jun 2023 19:11:55 +0800 Subject: [PATCH] feat: support to get local ip with local-ip-address (#37) Signed-off-by: Gaius --- Cargo.toml | 1 + src/bin/dfdaemon/main.rs | 2 +- src/config/dfdaemon.rs | 17 ++++++++++++++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 62f38604..14aada23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,3 +42,4 @@ prometheus = "0.13.3" warp = "0.3.5" tokio = { version = "1.28.1", features = ["full"] } hostname = "^0.3" +local-ip-address = "0.5.3" diff --git a/src/bin/dfdaemon/main.rs b/src/bin/dfdaemon/main.rs index c9a3e5cc..ddc33daf 100644 --- a/src/bin/dfdaemon/main.rs +++ b/src/bin/dfdaemon/main.rs @@ -67,7 +67,7 @@ async fn main() { let _guards = init_tracing(NAME, &args.log_dir, args.log_level, None); // Load config. - let config = Config::load(&args.config).unwrap(); + let config = Config::load(&args.config).unwrap().convert().unwrap(); let metrics = Metrics::new(config.network.enable_ipv6); metrics.serve().await; diff --git a/src/config/dfdaemon.rs b/src/config/dfdaemon.rs index 919c8544..bb02e4b6 100644 --- a/src/config/dfdaemon.rs +++ b/src/config/dfdaemon.rs @@ -18,9 +18,10 @@ use crate::config::{ default_cache_dir, default_config_dir, default_data_dir, default_lock_dir, default_log_dir, default_plugin_dir, default_root_dir, }; +use local_ip_address::{local_ip, local_ipv6}; use serde::Deserialize; use std::fs; -use std::net::{Ipv4Addr, SocketAddr}; +use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use std::path::PathBuf; use std::time::Duration; use tracing::info; @@ -120,9 +121,9 @@ pub struct Host { // hostname is the hostname of the host. pub hostname: String, - // TODO Implement default values of ipv4 and ipv6. + // ip is the advertise ip of the host. - // pub ip: IpAddr, + pub ip: IpAddr, } // Host implements default value for Host. @@ -132,6 +133,7 @@ impl Default for Host { idc: None, location: None, hostname: hostname::get().unwrap().to_string_lossy().to_string(), + ip: local_ip().unwrap(), } } } @@ -429,4 +431,13 @@ impl Config { Ok(Self::default()) } } + + // convert converts the configuration. + pub fn convert(&mut self) -> Result { + if self.network.enable_ipv6 { + self.host.ip = local_ipv6().unwrap() + } + + Ok(self.clone()) + } }