tun_win里面添加了touch_packet

This commit is contained in:
alex 2026-07-04 16:28:07 +08:00
parent ea85aa1b76
commit 2e823188ba
3 changed files with 64 additions and 17 deletions

View File

@ -1,5 +1,5 @@
{ {
// "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu", // "rust-analyzer.cargo.target": "x86_64-pc-windows-gnu",
"rust-analyzer.cargo.target": "x86_64-unknown-linux-gnu", // "rust-analyzer.cargo.target": "x86_64-unknown-linux-gnu",
// "rust-analyzer.cargo.features": ["tun"] // "rust-analyzer.cargo.features": ["tun"]
} }

View File

@ -495,7 +495,17 @@ impl TunTapPacketHandler for Iface {
if let Some(transport) = sliced_packet.transport { if let Some(transport) = sliced_packet.transport {
match transport { match transport {
TransportSlice::Tcp(tcp) => { TransportSlice::Tcp(tcp) => {
// just fall back use crate::FiveTuple;
use std::net::IpAddr;
let out_five_tuple = FiveTuple {
src_ip: IpAddr::V4(ipv4.header().source_addr()),
dst_ip: IpAddr::V4(ipv4.header().destination_addr()),
src_port: tcp.source_port(),
dst_port: tcp.destination_port(),
proto: etherparse::IpNumber::TCP.0,
};
eee.rule_cache.touch_packet(out_five_tuple);
} }
TransportSlice::Udp(udp) => { TransportSlice::Udp(udp) => {
if dstip == DNS_IP { if dstip == DNS_IP {
@ -512,6 +522,18 @@ impl TunTapPacketHandler for Iface {
.await; .await;
// edge.udp_sock_for_dns.send_to() // edge.udp_sock_for_dns.send_to()
return Ok(()); return Ok(());
} else {
use crate::FiveTuple;
use std::net::IpAddr;
let out_five_tuple = FiveTuple {
src_ip: IpAddr::V4(ipv4.header().source_addr()),
dst_ip: IpAddr::V4(ipv4.header().destination_addr()),
src_port: udp.source_port(),
dst_port: udp.destination_port(),
proto: etherparse::IpNumber::UDP.0,
};
eee.rule_cache.touch_packet(out_five_tuple);
} }
} }
_other => {} _other => {}

View File

@ -1,4 +1,11 @@
use std::{net::IpAddr, sync::{Arc, atomic::{AtomicU64, Ordering}}, time::{Duration, SystemTime, UNIX_EPOCH}}; use std::{
net::IpAddr,
sync::{
atomic::{AtomicU64, Ordering},
Arc,
},
time::{Duration, SystemTime, UNIX_EPOCH},
};
use ahash::RandomState; use ahash::RandomState;
use dashmap::{DashMap, DashSet}; use dashmap::{DashMap, DashSet};
@ -56,7 +63,12 @@ impl SessionTable {
info.last_active.store(now_secs(), Ordering::Relaxed); info.last_active.store(now_secs(), Ordering::Relaxed);
return; return;
} }
self.table.insert(key, SessionInfo { last_active: AtomicU64::new(now_secs()) }); self.table.insert(
key,
SessionInfo {
last_active: AtomicU64::new(now_secs()),
},
);
} }
pub fn process_packet(&self, key: &FiveTuple) -> bool { pub fn process_packet(&self, key: &FiveTuple) -> bool {
@ -87,8 +99,6 @@ type Proto = u8;
type RuleInfo = (Port, Proto); type RuleInfo = (Port, Proto);
#[derive(Debug)] #[derive(Debug)]
pub struct RuleFromServer { pub struct RuleFromServer {
pub proto: Proto, pub proto: Proto,
@ -121,7 +131,10 @@ impl RuleCache {
} }
pub fn set_identity_cache(&self, identity: IdentityID, infos: Vec<RuleFromServer>) { pub fn set_identity_cache(&self, identity: IdentityID, infos: Vec<RuleFromServer>) {
debug!("setting identity cache for identity={}, infos: {:?}", identity, infos); debug!(
"setting identity cache for identity={}, infos: {:?}",
identity, infos
);
let now = now_secs(); let now = now_secs();
@ -130,7 +143,8 @@ impl RuleCache {
// let mut protomap = HashSet::new(); // let mut protomap = HashSet::new();
now_sets.insert((info.port, info.proto)); now_sets.insert((info.port, info.proto));
} }
self.rule_info.insert(identity, (AtomicU64::new(now) , now_sets)); self.rule_info
.insert(identity, (AtomicU64::new(now), now_sets));
} }
pub fn touch_packet(&self, info: FiveTuple) { pub fn touch_packet(&self, info: FiveTuple) {
@ -138,7 +152,12 @@ impl RuleCache {
self.session_table.add_session_info(info); self.session_table.add_session_info(info);
} }
pub fn is_identity_ok(&self, allow_routing: bool, identity: IdentityID, info: FiveTuple) -> (bool, ShouldRenew) { pub fn is_identity_ok(
&self,
allow_routing: bool,
identity: IdentityID,
info: FiveTuple,
) -> (bool, ShouldRenew) {
// return (true, false); // return (true, false);
if allow_routing { if allow_routing {
return (true, false); return (true, false);
@ -162,7 +181,13 @@ impl RuleCache {
} }
} }
self.rule_info.insert(identity, (AtomicU64::new(now), DashSet::with_hasher(RandomState::new()))); self.rule_info.insert(
identity,
(
AtomicU64::new(now),
DashSet::with_hasher(RandomState::new()),
),
);
(false, true) (false, true)
} }