Files
Easytier_lkddi/easytier/src/gateway/mod.rs
T

62 lines
1.6 KiB
Rust
Raw Normal View History

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;
pub mod tcp_proxy;
2024-03-01 21:37:45 +08:00
pub mod udp_proxy;
2023-09-23 01:53:45 +00:00
#[derive(Debug)]
struct CidrSet {
global_ctx: ArcGlobalCtx,
2024-04-25 23:25:37 +08:00
cidr_set: Arc<Mutex<Vec<cidr::IpCidr>>>,
2023-09-23 01:53:45 +00:00
tasks: JoinSet<()>,
}
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(),
};
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();
self.tasks.spawn(async move {
let mut last_cidrs = vec![];
loop {
let cidrs = global_ctx.get_proxy_cidrs();
if cidrs != last_cidrs {
last_cidrs = cidrs.clone();
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() {
2024-04-25 23:25:37 +08:00
cidr_set.lock().unwrap().push(cidr.clone());
2023-09-23 01:53:45 +00:00
}
}
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
});
}
pub fn contains_v4(&self, ip: std::net::Ipv4Addr) -> bool {
let ip = ip.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) {
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
}