rustdesk/libs/hbb_common/src/lib.rs

465 lines
14 KiB
Rust
Raw Normal View History

2021-03-29 15:59:14 +08:00
pub mod compress;
pub mod platform;
pub mod protos;
2021-03-29 15:59:14 +08:00
pub use bytes;
use config::Config;
2021-03-29 15:59:14 +08:00
pub use futures;
pub use protobuf;
pub use protos::message as message_proto;
pub use protos::rendezvous as rendezvous_proto;
2021-03-29 15:59:14 +08:00
use std::{
fs::File,
io::{self, BufRead},
2022-12-26 16:41:33 +08:00
net::{IpAddr, Ipv4Addr, SocketAddr, SocketAddrV4},
2021-03-29 15:59:14 +08:00
path::Path,
time::{self, SystemTime, UNIX_EPOCH},
};
pub use tokio;
pub use tokio_util;
2022-01-04 19:49:44 +08:00
pub mod socket_client;
2021-03-29 15:59:14 +08:00
pub mod tcp;
pub mod udp;
pub use env_logger;
pub use log;
pub mod bytes_codec;
#[cfg(feature = "quic")]
pub mod quic;
pub use anyhow::{self, bail};
pub use futures_util;
pub mod config;
pub mod fs;
pub use lazy_static;
2022-05-12 17:35:25 +08:00
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub use mac_address;
2022-01-15 14:08:24 +08:00
pub use rand;
pub use regex;
2021-03-29 15:59:14 +08:00
pub use sodiumoxide;
pub use tokio_socks;
2022-01-05 13:21:14 +08:00
pub use tokio_socks::IntoTargetAddr;
pub use tokio_socks::TargetAddr;
pub mod password_security;
pub use chrono;
pub use directories_next;
2023-03-04 17:26:24 +08:00
pub use libc;
2022-12-21 16:03:15 +08:00
pub mod keyboard;
2023-03-11 01:53:36 +08:00
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub use dlopen;
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub use machine_uid;
pub use sysinfo;
pub use toml;
pub use uuid;
2021-03-29 15:59:14 +08:00
#[cfg(feature = "quic")]
pub type Stream = quic::Connection;
#[cfg(not(feature = "quic"))]
pub type Stream = tcp::FramedStream;
pub type SessionID = uuid::Uuid;
2021-03-29 15:59:14 +08:00
#[inline]
pub async fn sleep(sec: f32) {
2021-06-25 19:42:51 +08:00
tokio::time::sleep(time::Duration::from_secs_f32(sec)).await;
2021-03-29 15:59:14 +08:00
}
#[macro_export]
macro_rules! allow_err {
($e:expr) => {
if let Err(err) = $e {
log::debug!(
"{:?}, {}:{}:{}:{}",
err,
module_path!(),
file!(),
line!(),
column!()
);
} else {
}
};
2022-12-26 16:41:33 +08:00
($e:expr, $($arg:tt)*) => {
if let Err(err) = $e {
log::debug!(
"{:?}, {}, {}:{}:{}:{}",
err,
format_args!($($arg)*),
module_path!(),
file!(),
line!(),
column!()
);
} else {
}
};
2021-03-29 15:59:14 +08:00
}
#[inline]
pub fn timeout<T: std::future::Future>(ms: u64, future: T) -> tokio::time::Timeout<T> {
tokio::time::timeout(std::time::Duration::from_millis(ms), future)
}
pub type ResultType<F, E = anyhow::Error> = anyhow::Result<F, E>;
/// Certain router and firewalls scan the packet and if they
/// find an IP address belonging to their pool that they use to do the NAT mapping/translation, so here we mangle the ip address
pub struct AddrMangle();
2023-01-27 11:42:08 +08:00
#[inline]
pub fn try_into_v4(addr: SocketAddr) -> SocketAddr {
match addr {
SocketAddr::V6(v6) if !addr.ip().is_loopback() => {
if let Some(v4) = v6.ip().to_ipv4() {
SocketAddr::new(IpAddr::V4(v4), addr.port())
} else {
addr
}
}
_ => addr,
}
}
2021-03-29 15:59:14 +08:00
impl AddrMangle {
pub fn encode(addr: SocketAddr) -> Vec<u8> {
2023-01-27 11:42:08 +08:00
// not work with [:1]:<port>
let addr = try_into_v4(addr);
2021-03-29 15:59:14 +08:00
match addr {
SocketAddr::V4(addr_v4) => {
let tm = (SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or(std::time::Duration::ZERO)
2021-03-29 15:59:14 +08:00
.as_micros() as u32) as u128;
2021-06-25 19:42:51 +08:00
let ip = u32::from_le_bytes(addr_v4.ip().octets()) as u128;
2021-03-29 15:59:14 +08:00
let port = addr.port() as u128;
let v = ((ip + tm) << 49) | (tm << 17) | (port + (tm & 0xFFFF));
2021-06-25 19:42:51 +08:00
let bytes = v.to_le_bytes();
2021-03-29 15:59:14 +08:00
let mut n_padding = 0;
for i in bytes.iter().rev() {
if i == &0u8 {
n_padding += 1;
} else {
break;
}
}
bytes[..(16 - n_padding)].to_vec()
}
2022-12-26 16:41:33 +08:00
SocketAddr::V6(addr_v6) => {
let mut x = addr_v6.ip().octets().to_vec();
let port: [u8; 2] = addr_v6.port().to_le_bytes();
x.push(port[0]);
x.push(port[1]);
x
2021-03-29 15:59:14 +08:00
}
}
}
pub fn decode(bytes: &[u8]) -> SocketAddr {
2023-01-27 11:42:08 +08:00
use std::convert::TryInto;
2022-12-26 16:41:33 +08:00
if bytes.len() > 16 {
if bytes.len() != 18 {
2022-12-28 13:52:13 +08:00
return Config::get_any_listen_addr(false);
2022-12-26 16:41:33 +08:00
}
let tmp: [u8; 2] = bytes[16..].try_into().unwrap_or_default();
2022-12-26 16:41:33 +08:00
let port = u16::from_le_bytes(tmp);
let tmp: [u8; 16] = bytes[..16].try_into().unwrap_or_default();
2022-12-26 16:41:33 +08:00
let ip = std::net::Ipv6Addr::from(tmp);
return SocketAddr::new(IpAddr::V6(ip), port);
}
2021-03-29 15:59:14 +08:00
let mut padded = [0u8; 16];
2023-01-27 11:42:08 +08:00
padded[..bytes.len()].copy_from_slice(bytes);
2021-06-25 19:42:51 +08:00
let number = u128::from_le_bytes(padded);
2021-03-29 15:59:14 +08:00
let tm = (number >> 17) & (u32::max_value() as u128);
2021-06-25 19:42:51 +08:00
let ip = (((number >> 49) - tm) as u32).to_le_bytes();
2021-03-29 15:59:14 +08:00
let port = (number & 0xFFFFFF) - (tm & 0xFFFF);
SocketAddr::V4(SocketAddrV4::new(
Ipv4Addr::new(ip[0], ip[1], ip[2], ip[3]),
port as u16,
))
}
}
pub fn get_version_from_url(url: &str) -> String {
let n = url.chars().count();
2023-01-27 11:42:08 +08:00
let a = url.chars().rev().position(|x| x == '-');
2021-03-29 15:59:14 +08:00
if let Some(a) = a {
2023-01-27 11:42:08 +08:00
let b = url.chars().rev().position(|x| x == '.');
2021-03-29 15:59:14 +08:00
if let Some(b) = b {
if a > b {
if url
.chars()
.skip(n - b)
.collect::<String>()
.parse::<i32>()
.is_ok()
{
return url.chars().skip(n - a).collect();
} else {
return url.chars().skip(n - a).take(a - b - 1).collect();
}
} else {
return url.chars().skip(n - a).collect();
}
}
}
"".to_owned()
}
pub fn gen_version() {
2023-01-27 11:42:08 +08:00
println!("cargo:rerun-if-changed=Cargo.toml");
2022-11-17 16:36:07 +08:00
use std::io::prelude::*;
2021-03-29 15:59:14 +08:00
let mut file = File::create("./src/version.rs").unwrap();
2023-01-27 11:42:08 +08:00
for line in read_lines("Cargo.toml").unwrap().flatten() {
let ab: Vec<&str> = line.split('=').map(|x| x.trim()).collect();
if ab.len() == 2 && ab[0] == "version" {
file.write_all(format!("pub const VERSION: &str = {};\n", ab[1]).as_bytes())
.ok();
break;
2021-03-29 15:59:14 +08:00
}
}
2022-11-17 16:36:07 +08:00
// generate build date
let build_date = format!("{}", chrono::Local::now().format("%Y-%m-%d %H:%M"));
2023-01-27 11:42:08 +08:00
file.write_all(
format!("#[allow(dead_code)]\npub const BUILD_DATE: &str = \"{build_date}\";\n").as_bytes(),
2023-01-27 11:42:08 +08:00
)
.ok();
2022-11-17 16:36:07 +08:00
file.sync_all().ok();
2021-03-29 15:59:14 +08:00
}
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
2022-05-12 17:35:25 +08:00
pub fn is_valid_custom_id(id: &str) -> bool {
regex::Regex::new(r"^[a-zA-Z]\w{5,15}$")
.unwrap()
.is_match(id)
}
2021-12-27 00:24:57 +08:00
pub fn get_version_number(v: &str) -> i64 {
let mut n = 0;
2023-01-27 11:42:08 +08:00
for x in v.split('.') {
2021-12-27 00:24:57 +08:00
n = n * 1000 + x.parse::<i64>().unwrap_or(0);
}
n
}
2022-04-26 11:19:45 +08:00
pub fn get_modified_time(path: &std::path::Path) -> SystemTime {
2023-01-27 11:42:08 +08:00
std::fs::metadata(path)
2022-04-26 11:19:45 +08:00
.map(|m| m.modified().unwrap_or(UNIX_EPOCH))
.unwrap_or(UNIX_EPOCH)
}
pub fn get_created_time(path: &std::path::Path) -> SystemTime {
2023-01-27 11:42:08 +08:00
std::fs::metadata(path)
.map(|m| m.created().unwrap_or(UNIX_EPOCH))
.unwrap_or(UNIX_EPOCH)
}
pub fn get_exe_time() -> SystemTime {
std::env::current_exe().map_or(UNIX_EPOCH, |path| {
let m = get_modified_time(&path);
let c = get_created_time(&path);
if m > c {
m
} else {
c
}
})
}
pub fn get_uuid() -> Vec<u8> {
#[cfg(not(any(target_os = "android", target_os = "ios")))]
if let Ok(id) = machine_uid::get() {
return id.into();
}
Config::get_key_pair().1
}
#[inline]
pub fn get_time() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis())
.unwrap_or(0) as _
}
2022-12-28 13:52:13 +08:00
#[inline]
pub fn is_ipv4_str(id: &str) -> bool {
if let Ok(reg) = regex::Regex::new(
r"^(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(:\d+)?$",
) {
reg.is_match(id)
} else {
false
}
}
#[inline]
pub fn is_ipv6_str(id: &str) -> bool {
if let Ok(reg) = regex::Regex::new(
r"^((([a-fA-F0-9]{1,4}:{1,2})+[a-fA-F0-9]{1,4})|(\[([a-fA-F0-9]{1,4}:{1,2})+[a-fA-F0-9]{1,4}\]:\d+))$",
) {
reg.is_match(id)
} else {
false
}
}
#[inline]
pub fn is_ip_str(id: &str) -> bool {
is_ipv4_str(id) || is_ipv6_str(id)
}
#[inline]
pub fn is_domain_port_str(id: &str) -> bool {
// modified regex for RFC1123 hostname. check https://stackoverflow.com/a/106223 for original version for hostname.
// according to [TLD List](https://data.iana.org/TLD/tlds-alpha-by-domain.txt) version 2023011700,
// there is no digits in TLD, and length is 2~63.
if let Ok(reg) = regex::Regex::new(
r"(?i)^([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z][a-z-]{0,61}[a-z]:\d{1,5}$",
) {
reg.is_match(id)
} else {
false
}
}
2023-03-04 17:26:24 +08:00
pub fn init_log(_is_async: bool, _name: &str) -> Option<flexi_logger::LoggerHandle> {
#[cfg(debug_assertions)]
{
use env_logger::*;
init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "info"));
None
}
#[cfg(not(debug_assertions))]
{
// https://docs.rs/flexi_logger/latest/flexi_logger/error_info/index.html#write
// though async logger more efficient, but it also causes more problems, disable it for now
let mut logger_holder: Option<flexi_logger::LoggerHandle> = None;
let mut path = config::Config::log_path();
if !_name.is_empty() {
path.push(_name);
}
use flexi_logger::*;
if let Ok(x) = Logger::try_with_env_or_str("debug") {
logger_holder = x
.log_to_file(FileSpec::default().directory(path))
.write_mode(if _is_async {
WriteMode::Async
} else {
WriteMode::Direct
})
.format(opt_format)
.rotate(
Criterion::Age(Age::Day),
Naming::Timestamps,
Cleanup::KeepLogFiles(6),
)
.start()
.ok();
}
logger_holder
}
}
#[cfg(test)]
2023-01-27 11:42:08 +08:00
mod test {
use super::*;
2023-01-27 11:42:08 +08:00
#[test]
fn test_mangle() {
let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(192, 168, 16, 32), 21116));
assert_eq!(addr, AddrMangle::decode(&AddrMangle::encode(addr)));
let addr = "[2001:db8::1]:8080".parse::<SocketAddr>().unwrap();
assert_eq!(addr, AddrMangle::decode(&AddrMangle::encode(addr)));
let addr = "[2001:db8:ff::1111]:80".parse::<SocketAddr>().unwrap();
assert_eq!(addr, AddrMangle::decode(&AddrMangle::encode(addr)));
}
#[test]
fn test_allow_err() {
allow_err!(Err("test err") as Result<(), &str>);
allow_err!(
Err("test err with msg") as Result<(), &str>,
"prompt {}",
"failed"
);
}
#[test]
fn test_ipv6() {
assert!(is_ipv6_str("1:2:3"));
assert!(is_ipv6_str("[ab:2:3]:12"));
assert!(is_ipv6_str("[ABEF:2a:3]:12"));
assert!(!is_ipv6_str("[ABEG:2a:3]:12"));
assert!(!is_ipv6_str("1[ab:2:3]:12"));
assert!(!is_ipv6_str("1.1.1.1"));
assert!(is_ip_str("1.1.1.1"));
assert!(!is_ipv6_str("1:2:"));
assert!(is_ipv6_str("1:2::0"));
assert!(is_ipv6_str("[1:2::0]:1"));
assert!(!is_ipv6_str("[1:2::0]:"));
assert!(!is_ipv6_str("1:2::0]:1"));
2021-03-29 15:59:14 +08:00
}
2023-04-21 18:37:48 +08:00
#[test]
fn test_ipv4() {
assert!(is_ipv4_str("1.2.3.4"));
2023-05-18 13:06:49 +08:00
assert!(is_ipv4_str("1.2.3.4:90"));
2023-04-21 18:37:48 +08:00
assert!(is_ipv4_str("192.168.0.1"));
assert!(is_ipv4_str("0.0.0.0"));
assert!(is_ipv4_str("255.255.255.255"));
assert!(!is_ipv4_str("256.0.0.0"));
assert!(!is_ipv4_str("256.256.256.256"));
assert!(!is_ipv4_str("1:2:"));
assert!(!is_ipv4_str("192.168.0.256"));
assert!(!is_ipv4_str("192.168.0.1/24"));
assert!(!is_ipv4_str("192.168.0."));
assert!(!is_ipv4_str("192.168..1"));
2023-04-21 18:37:48 +08:00
}
2023-01-18 11:35:13 +08:00
#[test]
fn test_hostname_port() {
assert!(!is_domain_port_str("a:12"));
assert!(!is_domain_port_str("a.b.c:12"));
assert!(is_domain_port_str("test.com:12"));
assert!(is_domain_port_str("test-UPPER.com:12"));
assert!(is_domain_port_str("some-other.domain.com:12"));
assert!(!is_domain_port_str("under_score:12"));
assert!(!is_domain_port_str("a@bc:12"));
assert!(!is_domain_port_str("1.1.1.1:12"));
assert!(!is_domain_port_str("1.2.3:12"));
assert!(!is_domain_port_str("1.2.3.45:12"));
assert!(!is_domain_port_str("a.b.c:123456"));
assert!(!is_domain_port_str("---:12"));
assert!(!is_domain_port_str(".:12"));
// todo: should we also check for these edge cases?
// out-of-range port
assert!(is_domain_port_str("test.com:0"));
assert!(is_domain_port_str("test.com:98989"));
}
2023-01-27 11:42:08 +08:00
#[test]
fn test_mangle2() {
let addr = "[::ffff:127.0.0.1]:8080".parse().unwrap();
let addr_v4 = "127.0.0.1:8080".parse().unwrap();
assert_eq!(AddrMangle::decode(&AddrMangle::encode(addr)), addr_v4);
assert_eq!(
AddrMangle::decode(&AddrMangle::encode("[::127.0.0.1]:8080".parse().unwrap())),
addr_v4
);
assert_eq!(AddrMangle::decode(&AddrMangle::encode(addr_v4)), addr_v4);
let addr_v6 = "[ef::fe]:8080".parse().unwrap();
assert_eq!(AddrMangle::decode(&AddrMangle::encode(addr_v6)), addr_v6);
let addr_v6 = "[::1]:8080".parse().unwrap();
assert_eq!(AddrMangle::decode(&AddrMangle::encode(addr_v6)), addr_v6);
}
2021-03-29 15:59:14 +08:00
}