85 lines
1.9 KiB
Rust
Executable File

mod command;
pub use command::*;
mod socks;
use rand::Rng;
use sdlan_sn_rs::utils::Mac;
pub use socks::*;
mod pid_recorder;
// pub const CRC_HASH: crc::Crc<u32> = crc::Crc::<u32>::new(&crc::CRC_32_XFER);
#[allow(unused)]
pub fn caculate_crc(data: &[u8]) -> u32 {
let res = crc32fast::hash(data);
res
}
pub fn mac_to_string(mac: &Mac) -> String {
format!(
"[{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}]",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
)
}
pub fn generate_mac_address() -> Mac {
let mut rng = rand::thread_rng();
let mut mac = [0; 6];
for i in 0..6 {
let number: u8 = rng.gen();
mac[i] = number;
}
mac[0] &= !0x01;
mac[0] |= 0x02;
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 {
}