2025-06-14 11:42:45 +08:00
|
|
|
use dashmap::DashMap;
|
2024-04-25 23:25:37 +08:00
|
|
|
use std::sync::{Arc, Mutex};
|
2023-09-23 01:53:45 +00:00
|
|
|
use tokio::task::JoinSet;
|
|
|
|
|
|
|
|
|
|
use crate::common::global_ctx::ArcGlobalCtx;
|
|
|
|
|
|
|
|
|
|
pub mod icmp_proxy;
|
2024-06-09 22:59:50 +08:00
|
|
|
pub mod ip_reassembler;
|
2023-09-23 01:53:45 +00:00
|
|
|
pub mod tcp_proxy;
|
2024-06-10 10:27:24 +08:00
|
|
|
#[cfg(feature = "smoltcp")]
|
|
|
|
|
pub mod tokio_smoltcp;
|
2024-03-01 21:37:45 +08:00
|
|
|
pub mod udp_proxy;
|
2024-08-14 23:26:15 +08:00
|
|
|
|
|
|
|
|
#[cfg(feature = "socks5")]
|
|
|
|
|
pub mod fast_socks5;
|
|
|
|
|
#[cfg(feature = "socks5")]
|
|
|
|
|
pub mod socks5;
|
|
|
|
|
|
2025-01-26 00:41:15 +08:00
|
|
|
pub mod kcp_proxy;
|
|
|
|
|
|
2025-06-15 19:43:45 +08:00
|
|
|
pub mod quic_proxy;
|
|
|
|
|
|
2023-09-23 01:53:45 +00:00
|
|
|
#[derive(Debug)]
|
2025-01-26 00:41:15 +08:00
|
|
|
pub(crate) struct CidrSet {
|
2023-09-23 01:53:45 +00:00
|
|
|
global_ctx: ArcGlobalCtx,
|
2025-06-14 11:42:45 +08:00
|
|
|
cidr_set: Arc<Mutex<Vec<cidr::Ipv4Cidr>>>,
|
2023-09-23 01:53:45 +00:00
|
|
|
tasks: JoinSet<()>,
|
2025-06-14 11:42:45 +08:00
|
|
|
|
|
|
|
|
mapped_to_real: Arc<DashMap<cidr::Ipv4Cidr, cidr::Ipv4Cidr>>,
|
2023-09-23 01:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl CidrSet {
|
|
|
|
|
pub fn new(global_ctx: ArcGlobalCtx) -> Self {
|
|
|
|
|
let mut ret = Self {
|
|
|
|
|
global_ctx,
|
2024-04-25 23:25:37 +08:00
|
|
|
cidr_set: Arc::new(Mutex::new(vec![])),
|
2023-09-23 01:53:45 +00:00
|
|
|
tasks: JoinSet::new(),
|
2025-06-14 11:42:45 +08:00
|
|
|
|
|
|
|
|
mapped_to_real: Arc::new(DashMap::new()),
|
2023-09-23 01:53:45 +00:00
|
|
|
};
|
|
|
|
|
ret.run_cidr_updater();
|
|
|
|
|
ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn run_cidr_updater(&mut self) {
|
|
|
|
|
let global_ctx = self.global_ctx.clone();
|
|
|
|
|
let cidr_set = self.cidr_set.clone();
|
2025-06-14 11:42:45 +08:00
|
|
|
let mapped_to_real = self.mapped_to_real.clone();
|
2023-09-23 01:53:45 +00:00
|
|
|
self.tasks.spawn(async move {
|
|
|
|
|
let mut last_cidrs = vec![];
|
|
|
|
|
loop {
|
2025-06-14 11:42:45 +08:00
|
|
|
let cidrs = global_ctx.config.get_proxy_cidrs();
|
2023-09-23 01:53:45 +00:00
|
|
|
if cidrs != last_cidrs {
|
|
|
|
|
last_cidrs = cidrs.clone();
|
2025-06-14 11:42:45 +08:00
|
|
|
mapped_to_real.clear();
|
2024-04-25 23:25:37 +08:00
|
|
|
cidr_set.lock().unwrap().clear();
|
2023-09-23 01:53:45 +00:00
|
|
|
for cidr in cidrs.iter() {
|
2025-06-14 11:42:45 +08:00
|
|
|
let real_cidr = cidr.cidr;
|
|
|
|
|
let mapped = cidr.mapped_cidr.unwrap_or(real_cidr.clone());
|
|
|
|
|
cidr_set.lock().unwrap().push(mapped.clone());
|
|
|
|
|
|
|
|
|
|
if mapped != real_cidr {
|
|
|
|
|
mapped_to_real.insert(mapped.clone(), real_cidr.clone());
|
|
|
|
|
}
|
2023-09-23 01:53:45 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-14 11:42:45 +08:00
|
|
|
pub fn contains_v4(&self, ipv4: std::net::Ipv4Addr, real_ip: &mut std::net::Ipv4Addr) -> bool {
|
|
|
|
|
let ip = ipv4.into();
|
2024-04-25 23:25:37 +08:00
|
|
|
let s = self.cidr_set.lock().unwrap();
|
|
|
|
|
for cidr in s.iter() {
|
|
|
|
|
if cidr.contains(&ip) {
|
2025-06-14 11:42:45 +08:00
|
|
|
if let Some(real_cidr) = self.mapped_to_real.get(&cidr).map(|v| v.value().clone()) {
|
|
|
|
|
let origin_network_bits = real_cidr.first().address().to_bits();
|
|
|
|
|
let network_mask = cidr.mask().to_bits();
|
|
|
|
|
|
|
|
|
|
let mut converted_ip = ipv4.to_bits();
|
|
|
|
|
converted_ip &= !network_mask;
|
|
|
|
|
converted_ip |= origin_network_bits;
|
|
|
|
|
|
|
|
|
|
*real_ip = std::net::Ipv4Addr::from(converted_ip);
|
|
|
|
|
} else {
|
|
|
|
|
*real_ip = ipv4;
|
|
|
|
|
}
|
2024-04-25 23:25:37 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
false
|
2023-09-23 01:53:45 +00:00
|
|
|
}
|
2024-03-01 21:37:45 +08:00
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
2024-04-25 23:25:37 +08:00
|
|
|
self.cidr_set.lock().unwrap().is_empty()
|
2024-03-01 21:37:45 +08:00
|
|
|
}
|
2023-09-23 01:53:45 +00:00
|
|
|
}
|