From 6e2376e3d008caa49c5823e3bcda53269c804f8c Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 23 Apr 2026 10:57:32 +0800 Subject: [PATCH] added get_hostname for login and token-login --- Cargo.lock | 30 ++++++++++++----- Cargo.toml | 1 + src/bin/punchnet/api/mod.rs | 10 +++++- src/utils/mod.rs | 66 +++++++++++-------------------------- 4 files changed, 50 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 62e970b..f296ed6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -129,7 +129,7 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -140,7 +140,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -884,7 +884,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -1259,6 +1259,17 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "hostname" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "617aaa3557aef3810a6369d0a99fac8a080891b68bd9f9812a1eeda0c0730cbd" +dependencies = [ + "cfg-if", + "libc", + "windows-link", +] + [[package]] name = "http" version = "1.4.0" @@ -1869,7 +1880,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -2251,6 +2262,7 @@ dependencies = [ "futures-util", "hex", "hmac", + "hostname", "ipnet", "libc", "local-ip-address", @@ -2614,7 +2626,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2682,7 +2694,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2965,7 +2977,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" dependencies = [ "libc", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -3329,7 +3341,7 @@ dependencies = [ "getrandom 0.4.2", "once_cell", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -4029,7 +4041,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.48.0", + "windows-sys 0.61.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 842c306..dd23f45 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,7 @@ rustls-native-certs = "0.8.3" simple-dns = "0.11.2" default-net = "0.22.0" socket2 = "0.6.3" +hostname = "0.4.2" # rolling-file = { path = "../rolling-file" } [target.'cfg(unix)'.dependencies] diff --git a/src/bin/punchnet/api/mod.rs b/src/bin/punchnet/api/mod.rs index c3eabb1..3de82da 100644 --- a/src/bin/punchnet/api/mod.rs +++ b/src/bin/punchnet/api/mod.rs @@ -1,5 +1,5 @@ use hmac::{Hmac, Mac as HamcMac}; -use punchnet::TokenLogin; +use punchnet::{TokenLogin, get_hostname}; use reqwest::Client; use sdlan_sn_rs::utils::{Mac, Result, SDLanError}; use serde::{Deserialize, Serialize}; @@ -25,6 +25,7 @@ struct TokenLoginData<'a> { mac: &'a str, system: &'a str, version: &'a str, + hostname: &'a str, } fn do_calculate(data: &[u8]) -> Result{ @@ -58,6 +59,7 @@ struct UserPassLoginData<'a> { mac: &'a str, system: &'a str, version: &'a str, + hostname: &'a str, } impl HMacCalculator for UserPassLoginData<'_> { @@ -158,6 +160,8 @@ pub async fn login_with_user_pass( mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); + let hostname = get_hostname(); + let post_data = UserPassLoginData { client_id, username, @@ -165,6 +169,7 @@ pub async fn login_with_user_pass( mac: &mac, system, version, + hostname: &hostname, }; post_with_data(&format!("{}/auth/login", url_prefix), post_data).await @@ -182,12 +187,15 @@ pub async fn login_with_token( mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] ); + let hostname = get_hostname(); + let post_data = TokenLoginData { client_id, token, mac: &mac, system, version, + hostname: &hostname, }; post_with_data(&format!("{}/auth/token", url_prefix), post_data).await diff --git a/src/utils/mod.rs b/src/utils/mod.rs index ced2824..9b4cbdd 100755 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -6,6 +6,7 @@ mod file_configuration; mod dns; use std::{fs::OpenOptions, io::Write, net::Ipv4Addr, path::Path}; +use tracing::error; pub use encrypter::*; pub use command::*; @@ -57,6 +58,24 @@ pub fn get_access_token() -> Option { None } +pub fn get_hostname() -> String { + match hostname::get() { + Ok(os_string) => { + match os_string.into_string() { + Ok(s) => s, + Err(e) => { + error!("failed to convert hostname to string"); + "".to_owned() + } + } + } + Err(e) => { + error!("failed to get hostname"); + "".to_owned() + } + } +} + pub fn set_access_token(cache_info: &CachedLoginInfo) -> Result<()> { let path = format!("{}/.access_token", get_base_dir()); let data = serde_json::to_string(cache_info).unwrap(); @@ -115,50 +134,3 @@ pub fn generate_mac_address() -> Mac { mac } - -#[cfg(windows)] -pub mod mod_hostname { - use std::{ffi::OsString, os::windows::ffi::OsStringExt}; - - use winapi::um::winbase::GetComputerNameW; - - pub fn get_hostname() -> Option { - unsafe { - let mut buffer: Vec = vec![0; 64]; - let mut buffer_size = 64; - if GetComputerNameW(buffer.as_mut_ptr(), &mut buffer_size) != 0 { - let hostname = OsString::from_wide(&buffer[..buffer_size as usize]); - return hostname.into_string().ok(); - } - None - } - } -} - -#[cfg(unix)] -pub mod mod_hostname { - use libc::{size_t, c_int, c_char}; - - extern "C" { - fn gethostname(name: *mut c_char, size: size_t) -> c_int; - } - - pub fn get_hostname() -> Option { - let mut buffer = vec![0u8; 255]; - - unsafe { - if gethostname(buffer.as_mut_ptr() as *mut c_char, 255) == 0 { - let len = buffer.iter().position(|&b| b == 0).unwrap_or(255); - buffer.truncate(len); - return String::from_utf8(buffer).ok(); - } - None - } - } -} - - -#[cfg(windows)] -pub mod hostname { - -} \ No newline at end of file