From 6bdc5a7c6d9d986115e4e7a5214234c10ce123b2 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sun, 14 Sep 2025 21:41:15 +0800 Subject: feat: client initialization fetches basic infos refactor: connector manager -> client manager, similar concept, different mentality note: if custom ip fetcher fails then it defaults to ifconfig.me --- src/client/client.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/client/client.rs (limited to 'src/client/client.rs') diff --git a/src/client/client.rs b/src/client/client.rs new file mode 100644 index 0000000..2a5f595 --- /dev/null +++ b/src/client/client.rs @@ -0,0 +1,74 @@ +use gethostname::gethostname; +use std::net::IpAddr; +use whoami::{DesktopEnv, Language}; + +use crate::connector::Connection; + +pub struct Client { + pub connector: C, + ip: Option, + hostname: String, + device_name: String, + os: &'static str, + cpu_arch: String, + distro: String, + desktop_env: DesktopEnv, + username: String, + realname: String, + langs: Option>, + env: Vec<(String, String)>, +} + +impl std::fmt::Display for Client { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!( + f, + "IP: {:?}\n\ + Hostname: {}\n\ + Device Name: {}\n\ + OS: {}\n\ + CPU Architecture: {}\n\ + Distro: {}\n\ + Desktop Environment: {:?}\n\ + Username: {}\n\ + Real Name: {}\n\ + Languages: {:?}\n\ + Environment Variables: {:?}\n", + self.ip, + self.hostname, + self.device_name, + self.os, + self.cpu_arch, + self.distro, + self.desktop_env, + self.username, + self.realname, + self.langs, + self.env + ) + } +} + +impl Client { + pub fn new(connector: C) -> Self { + let c = Self { + ip: connector.get_ip(), + connector, + hostname: gethostname().into_string().unwrap_or_default(), + device_name: whoami::devicename(), + os: std::env::consts::OS, + cpu_arch: std::env::consts::ARCH.to_string(), + distro: whoami::distro(), + desktop_env: whoami::desktop_env(), + username: whoami::username(), + realname: whoami::realname(), + langs: match whoami::langs() { + Ok(it) => Some(it.collect()), + Err(_) => None, + }, + env: std::env::vars().collect(), + }; + c.connector.init(&c); + c + } +} -- cgit v1.2.3