2021-03-29 15:59:14 +08:00
|
|
|
pub mod compress;
|
2022-07-21 01:44:27 +08:00
|
|
|
pub mod platform;
|
2022-07-30 02:01:40 +08:00
|
|
|
pub mod protos;
|
2021-03-29 15:59:14 +08:00
|
|
|
pub use bytes;
|
2022-06-20 10:41:46 +08:00
|
|
|
use config::Config;
|
2021-03-29 15:59:14 +08:00
|
|
|
pub use futures;
|
|
|
|
pub use protobuf;
|
2022-07-30 02:01:40 +08:00
|
|
|
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;
|
2022-06-20 10:41:46 +08:00
|
|
|
pub use lazy_static;
|
2022-05-12 17:35:25 +08:00
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
2022-03-12 19:51:55 +08:00
|
|
|
pub use mac_address;
|
2022-01-15 14:08:24 +08:00
|
|
|
pub use rand;
|
2022-01-05 23:50:13 +08:00
|
|
|
pub use regex;
|
2021-03-29 15:59:14 +08:00
|
|
|
pub use sodiumoxide;
|
2022-01-02 22:55:33 +08:00
|
|
|
pub use tokio_socks;
|
2022-01-05 13:21:14 +08:00
|
|
|
pub use tokio_socks::IntoTargetAddr;
|
|
|
|
pub use tokio_socks::TargetAddr;
|
2022-06-20 10:41:46 +08:00
|
|
|
pub mod password_security;
|
2022-09-15 17:31:28 +08:00
|
|
|
pub use chrono;
|
|
|
|
pub use directories_next;
|
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;
|
|
|
|
|
|
|
|
#[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
|
|
|
|
2022-12-10 19:46:41 +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();
|
|
|
|
|
|
|
|
impl AddrMangle {
|
|
|
|
pub fn encode(addr: SocketAddr) -> Vec<u8> {
|
|
|
|
match addr {
|
|
|
|
SocketAddr::V4(addr_v4) => {
|
|
|
|
let tm = (SystemTime::now()
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.unwrap()
|
|
|
|
.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 {
|
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
|
|
|
}
|
|
|
|
#[allow(invalid_value)]
|
|
|
|
let mut tmp: [u8; 2] = unsafe { std::mem::MaybeUninit::uninit().assume_init() };
|
|
|
|
tmp.copy_from_slice(&bytes[16..]);
|
|
|
|
let port = u16::from_le_bytes(tmp);
|
|
|
|
#[allow(invalid_value)]
|
|
|
|
let mut tmp: [u8; 16] = unsafe { std::mem::MaybeUninit::uninit().assume_init() };
|
|
|
|
tmp.copy_from_slice(&bytes[..16]);
|
|
|
|
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];
|
|
|
|
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();
|
|
|
|
let a = url
|
|
|
|
.chars()
|
|
|
|
.rev()
|
|
|
|
.enumerate()
|
|
|
|
.filter(|(_, x)| x == &'-')
|
|
|
|
.next()
|
|
|
|
.map(|(i, _)| i);
|
|
|
|
if let Some(a) = a {
|
|
|
|
let b = url
|
|
|
|
.chars()
|
|
|
|
.rev()
|
|
|
|
.enumerate()
|
|
|
|
.filter(|(_, x)| x == &'.')
|
|
|
|
.next()
|
|
|
|
.map(|(i, _)| i);
|
|
|
|
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() {
|
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();
|
|
|
|
for line in read_lines("Cargo.toml").unwrap() {
|
|
|
|
if let Ok(line) = line {
|
|
|
|
let ab: Vec<&str> = line.split("=").map(|x| x.trim()).collect();
|
|
|
|
if ab.len() == 2 && ab[0] == "version" {
|
2022-11-17 16:36:07 +08:00
|
|
|
file.write_all(format!("pub const VERSION: &str = {};\n", ab[1]).as_bytes())
|
2021-03-29 15:59:14 +08:00
|
|
|
.ok();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-17 16:36:07 +08:00
|
|
|
// generate build date
|
|
|
|
let build_date = format!("{}", chrono::Local::now().format("%Y-%m-%d %H:%M"));
|
|
|
|
file.write_all(format!("pub const BUILD_DATE: &str = \"{}\";", build_date).as_bytes())
|
|
|
|
.ok();
|
|
|
|
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;
|
|
|
|
for x in v.split(".") {
|
|
|
|
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 {
|
|
|
|
std::fs::metadata(&path)
|
|
|
|
.map(|m| m.modified().unwrap_or(UNIX_EPOCH))
|
|
|
|
.unwrap_or(UNIX_EPOCH)
|
|
|
|
}
|
|
|
|
|
2022-07-30 07:09:31 +08:00
|
|
|
pub fn get_created_time(path: &std::path::Path) -> SystemTime {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-30 02:01:40 +08:00
|
|
|
pub fn get_uuid() -> Vec<u8> {
|
2022-06-20 10:41:46 +08:00
|
|
|
#[cfg(not(any(target_os = "android", target_os = "ios")))]
|
|
|
|
if let Ok(id) = machine_uid::get() {
|
2022-07-30 02:01:40 +08:00
|
|
|
return id.into();
|
2022-06-20 10:41:46 +08:00
|
|
|
}
|
2022-07-30 02:01:40 +08:00
|
|
|
Config::get_key_pair().1
|
2022-06-20 10:41:46 +08:00
|
|
|
}
|
|
|
|
|
2022-09-11 21:46:53 +08:00
|
|
|
#[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 _
|
|
|
|
}
|
|
|
|
|
2021-03-29 15:59:14 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
#[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)));
|
2022-12-26 16:41:33 +08:00
|
|
|
|
|
|
|
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)));
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
2022-12-10 19:46:41 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_allow_err() {
|
|
|
|
allow_err!(Err("test err") as Result<(), &str>);
|
2022-12-26 16:41:33 +08:00
|
|
|
allow_err!(
|
|
|
|
Err("test err with msg") as Result<(), &str>,
|
|
|
|
"prompt {}",
|
|
|
|
"failed"
|
|
|
|
);
|
2022-12-10 19:46:41 +08:00
|
|
|
}
|
2021-03-29 15:59:14 +08:00
|
|
|
}
|
2022-12-28 13:52:13 +08:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_ipv4_str(id: &str) -> bool {
|
|
|
|
regex::Regex::new(r"^\d+\.\d+\.\d+\.\d+(:\d+)?$")
|
|
|
|
.unwrap()
|
|
|
|
.is_match(id)
|
|
|
|
}
|