added get_hostname for login and token-login

This commit is contained in:
alex 2026-04-23 10:57:32 +08:00
parent 2101745c80
commit 6e2376e3d0
4 changed files with 50 additions and 57 deletions

30
Cargo.lock generated
View File

@ -129,7 +129,7 @@ version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [ dependencies = [
"windows-sys 0.60.2", "windows-sys 0.61.2",
] ]
[[package]] [[package]]
@ -140,7 +140,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [ dependencies = [
"anstyle", "anstyle",
"once_cell_polyfill", "once_cell_polyfill",
"windows-sys 0.60.2", "windows-sys 0.61.2",
] ]
[[package]] [[package]]
@ -884,7 +884,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys 0.52.0", "windows-sys 0.61.2",
] ]
[[package]] [[package]]
@ -1259,6 +1259,17 @@ dependencies = [
"windows-sys 0.61.2", "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]] [[package]]
name = "http" name = "http"
version = "1.4.0" version = "1.4.0"
@ -1869,7 +1880,7 @@ version = "0.50.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
dependencies = [ dependencies = [
"windows-sys 0.59.0", "windows-sys 0.61.2",
] ]
[[package]] [[package]]
@ -2251,6 +2262,7 @@ dependencies = [
"futures-util", "futures-util",
"hex", "hex",
"hmac", "hmac",
"hostname",
"ipnet", "ipnet",
"libc", "libc",
"local-ip-address", "local-ip-address",
@ -2614,7 +2626,7 @@ dependencies = [
"errno", "errno",
"libc", "libc",
"linux-raw-sys", "linux-raw-sys",
"windows-sys 0.52.0", "windows-sys 0.61.2",
] ]
[[package]] [[package]]
@ -2682,7 +2694,7 @@ dependencies = [
"security-framework", "security-framework",
"security-framework-sys", "security-framework-sys",
"webpki-root-certs", "webpki-root-certs",
"windows-sys 0.52.0", "windows-sys 0.61.2",
] ]
[[package]] [[package]]
@ -2965,7 +2977,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys 0.60.2", "windows-sys 0.61.2",
] ]
[[package]] [[package]]
@ -3329,7 +3341,7 @@ dependencies = [
"getrandom 0.4.2", "getrandom 0.4.2",
"once_cell", "once_cell",
"rustix", "rustix",
"windows-sys 0.52.0", "windows-sys 0.61.2",
] ]
[[package]] [[package]]
@ -4029,7 +4041,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.61.2",
] ]
[[package]] [[package]]

View File

@ -46,6 +46,7 @@ rustls-native-certs = "0.8.3"
simple-dns = "0.11.2" simple-dns = "0.11.2"
default-net = "0.22.0" default-net = "0.22.0"
socket2 = "0.6.3" socket2 = "0.6.3"
hostname = "0.4.2"
# rolling-file = { path = "../rolling-file" } # rolling-file = { path = "../rolling-file" }
[target.'cfg(unix)'.dependencies] [target.'cfg(unix)'.dependencies]

View File

@ -1,5 +1,5 @@
use hmac::{Hmac, Mac as HamcMac}; use hmac::{Hmac, Mac as HamcMac};
use punchnet::TokenLogin; use punchnet::{TokenLogin, get_hostname};
use reqwest::Client; use reqwest::Client;
use sdlan_sn_rs::utils::{Mac, Result, SDLanError}; use sdlan_sn_rs::utils::{Mac, Result, SDLanError};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -25,6 +25,7 @@ struct TokenLoginData<'a> {
mac: &'a str, mac: &'a str,
system: &'a str, system: &'a str,
version: &'a str, version: &'a str,
hostname: &'a str,
} }
fn do_calculate(data: &[u8]) -> Result<String>{ fn do_calculate(data: &[u8]) -> Result<String>{
@ -58,6 +59,7 @@ struct UserPassLoginData<'a> {
mac: &'a str, mac: &'a str,
system: &'a str, system: &'a str,
version: &'a str, version: &'a str,
hostname: &'a str,
} }
impl HMacCalculator for UserPassLoginData<'_> { 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] mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
); );
let hostname = get_hostname();
let post_data = UserPassLoginData { let post_data = UserPassLoginData {
client_id, client_id,
username, username,
@ -165,6 +169,7 @@ pub async fn login_with_user_pass(
mac: &mac, mac: &mac,
system, system,
version, version,
hostname: &hostname,
}; };
post_with_data(&format!("{}/auth/login", url_prefix), post_data).await 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] mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
); );
let hostname = get_hostname();
let post_data = TokenLoginData { let post_data = TokenLoginData {
client_id, client_id,
token, token,
mac: &mac, mac: &mac,
system, system,
version, version,
hostname: &hostname,
}; };
post_with_data(&format!("{}/auth/token", url_prefix), post_data).await post_with_data(&format!("{}/auth/token", url_prefix), post_data).await

View File

@ -6,6 +6,7 @@ mod file_configuration;
mod dns; mod dns;
use std::{fs::OpenOptions, io::Write, net::Ipv4Addr, path::Path}; use std::{fs::OpenOptions, io::Write, net::Ipv4Addr, path::Path};
use tracing::error;
pub use encrypter::*; pub use encrypter::*;
pub use command::*; pub use command::*;
@ -57,6 +58,24 @@ pub fn get_access_token() -> Option<CachedLoginInfo> {
None 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<()> { pub fn set_access_token(cache_info: &CachedLoginInfo) -> Result<()> {
let path = format!("{}/.access_token", get_base_dir()); let path = format!("{}/.access_token", get_base_dir());
let data = serde_json::to_string(cache_info).unwrap(); let data = serde_json::to_string(cache_info).unwrap();
@ -115,50 +134,3 @@ pub fn generate_mac_address() -> Mac {
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<String> {
unsafe {
let mut buffer: Vec<u16> = 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<String> {
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 {
}