Fix client host

This commit is contained in:
Demmie 2025-01-19 03:43:06 -05:00
parent 03e94e2f26
commit 7ac76da921
No known key found for this signature in database
GPG Key ID: B06DAA3D432C6E9A
3 changed files with 25 additions and 3 deletions

View File

@ -25,9 +25,13 @@ async fn main() -> anyhow::Result<(), Box<dyn Error>> {
let config = config.watchers_config; let config = config.watchers_config;
if config.no_server { if config.no_server {
warn!("Not sending to server {}:{}", config.host, config.port); warn!(
"Not sending to server {}:{}",
config.client_host(),
config.port
);
} else { } else {
info!("Sending to server {}:{}", config.host, config.port); info!("Sending to server {}:{}", config.client_host(), config.port);
} }
info!( info!(
"Idle timeout: {} seconds", "Idle timeout: {} seconds",

View File

@ -2,6 +2,8 @@ pub mod defaults;
mod file_config; mod file_config;
mod filters; mod filters;
use std::{net::Ipv4Addr, str::FromStr};
use self::filters::Filter; use self::filters::Filter;
use chrono::Duration; use chrono::Duration;
pub use file_config::FileConfig; pub use file_config::FileConfig;
@ -17,6 +19,18 @@ pub struct Config {
pub filters: Vec<Filter>, pub filters: Vec<Filter>,
} }
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 { impl Config {
pub fn match_window_data(&self, app_id: &str, title: &str) -> FilterResult { pub fn match_window_data(&self, app_id: &str, title: &str) -> FilterResult {
for filter in &self.filters { for filter in &self.filters {
@ -28,4 +42,8 @@ impl Config {
FilterResult::Skip FilterResult::Skip
} }
pub fn client_host(&self) -> String {
normalize_server_host(&self.host)
}
} }

View File

@ -17,7 +17,7 @@ pub struct ReportClient {
impl ReportClient { impl ReportClient {
pub async fn new(config: Config) -> anyhow::Result<Self, Box<dyn Error>> { pub async fn new(config: Config) -> anyhow::Result<Self, Box<dyn Error>> {
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 hostname = gethostname::gethostname().into_string().unwrap();
let idle_bucket_name = format!("aw-watcher-afk_{hostname}"); let idle_bucket_name = format!("aw-watcher-afk_{hostname}");