From 7ac76da921fad8e0d9655a64daf7d6db203ddfd2 Mon Sep 17 00:00:00 2001 From: Demmie <2e3s19@gmail.com> Date: Sun, 19 Jan 2025 03:43:06 -0500 Subject: [PATCH] Fix client host --- src/main.rs | 8 ++++++-- watchers/src/config.rs | 18 ++++++++++++++++++ watchers/src/report_client.rs | 2 +- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index bf2b944..0d274d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,9 +25,13 @@ async fn main() -> anyhow::Result<(), Box> { let config = config.watchers_config; if config.no_server { - warn!("Not sending to server {}:{}", config.host, config.port); + warn!( + "Not sending to server {}:{}", + config.client_host(), + config.port + ); } else { - info!("Sending to server {}:{}", config.host, config.port); + info!("Sending to server {}:{}", config.client_host(), config.port); } info!( "Idle timeout: {} seconds", diff --git a/watchers/src/config.rs b/watchers/src/config.rs index 29055d8..3fb20c1 100644 --- a/watchers/src/config.rs +++ b/watchers/src/config.rs @@ -2,6 +2,8 @@ pub mod defaults; mod file_config; mod filters; +use std::{net::Ipv4Addr, str::FromStr}; + use self::filters::Filter; use chrono::Duration; pub use file_config::FileConfig; @@ -17,6 +19,18 @@ pub struct Config { pub filters: Vec, } +fn normalize_server_host(server_host: &str) -> String { + let is_zero_first_octet = match Ipv4Addr::from_str(server_host) { + Ok(ip) => ip.octets()[0] == 0, + Err(_) => false, + }; + if is_zero_first_octet { + "127.0.0.1".to_string() + } else { + server_host.to_string() + } +} + impl Config { pub fn match_window_data(&self, app_id: &str, title: &str) -> FilterResult { for filter in &self.filters { @@ -28,4 +42,8 @@ impl Config { FilterResult::Skip } + + pub fn client_host(&self) -> String { + normalize_server_host(&self.host) + } } diff --git a/watchers/src/report_client.rs b/watchers/src/report_client.rs index aa5fb44..f980e1a 100644 --- a/watchers/src/report_client.rs +++ b/watchers/src/report_client.rs @@ -17,7 +17,7 @@ pub struct ReportClient { impl ReportClient { pub async fn new(config: Config) -> anyhow::Result> { - let client = AwClient::new(&config.host, config.port, "awatcher")?; + let client = AwClient::new(&config.client_host(), config.port, "awatcher")?; let hostname = gethostname::gethostname().into_string().unwrap(); let idle_bucket_name = format!("aw-watcher-afk_{hostname}");