134 lines
3.1 KiB
Rust
Executable File
134 lines
3.1 KiB
Rust
Executable File
mod command;
|
|
|
|
use std::{fs::OpenOptions, io::Write, path::Path};
|
|
|
|
pub use command::*;
|
|
|
|
mod socks;
|
|
use rand::Rng;
|
|
use sdlan_sn_rs::utils::{Mac, Result, SDLanError, save_to_file};
|
|
pub use socks::*;
|
|
|
|
use crate::get_base_dir;
|
|
|
|
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 get_access_token() -> Option<String> {
|
|
let path = format!("{}/.access_token", get_base_dir());
|
|
if let Ok(content) = std::fs::read(&path) {
|
|
let result = String::from_utf8_lossy(&content);
|
|
return Some(result.to_string());
|
|
}
|
|
None
|
|
}
|
|
|
|
pub fn set_access_token(tk: &str) -> Result<()> {
|
|
let path = format!("{}/.access_token", get_base_dir());
|
|
std::fs::write(path, tk)?;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn create_or_load_mac() -> Mac {
|
|
let path = format!("{}/.mac", get_base_dir());
|
|
if let Ok(content) = std::fs::read(&path) {
|
|
if content.len() == 6 {
|
|
let mut mac = [0; 6];
|
|
mac.copy_from_slice(&content);
|
|
return mac;
|
|
}
|
|
}
|
|
let mac = generate_mac_address();
|
|
let _ = save_to_file_binary(&path, &mac);
|
|
mac
|
|
}
|
|
|
|
|
|
pub fn save_to_file_binary(idfile: &str, content: &[u8]) -> Result<()> {
|
|
if idfile.len() == 0 {
|
|
return Err(SDLanError::IOError("file is empty".to_owned()));
|
|
}
|
|
|
|
let filepath = Path::new(idfile);
|
|
OpenOptions::new()
|
|
.create(true)
|
|
.write(true)
|
|
.truncate(true)
|
|
.open(filepath)?
|
|
.write_all(content)?;
|
|
Ok(())
|
|
}
|
|
|
|
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 {
|
|
|
|
} |