diff --git a/leaf/src/app/outbound/selector_cache.rs b/leaf/src/app/outbound/selector_cache.rs index 8844b17..fe28ee3 100644 --- a/leaf/src/app/outbound/selector_cache.rs +++ b/leaf/src/app/outbound/selector_cache.rs @@ -1,4 +1,4 @@ -// This file is generated by rust-protobuf 2.24.1. Do not edit +// This file is generated by rust-protobuf 2.25.0. Do not edit // @generated // https://github.com/rust-lang/rust-clippy/issues/702 @@ -21,7 +21,7 @@ /// Generated files are compatible only with the same version /// of protobuf runtime. -// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_24_1; +// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_25_0; #[derive(PartialEq,Clone,Default,Debug)] pub struct SelectorCache { diff --git a/leaf/src/app/router.rs b/leaf/src/app/router.rs index c9ca85c..389ff6c 100644 --- a/leaf/src/app/router.rs +++ b/leaf/src/app/router.rs @@ -11,7 +11,7 @@ use memmap::Mmap; use crate::app::SyncDnsClient; use crate::config::{self, Router_Rule}; -use crate::session::{Session, SocksAddr}; +use crate::session::{Network, Session, SocksAddr}; pub trait Condition: Send + Sync + Unpin { fn apply(&self, sess: &Session) -> bool; @@ -105,6 +105,36 @@ impl Condition for IpCidrMatcher { } } +struct NetworkMatcher { + values: Vec, +} + +impl NetworkMatcher { + fn new(networks: &mut protobuf::RepeatedField) -> Self { + let mut values = Vec::new(); + for net in networks.iter_mut() { + match std::mem::take(net).to_uppercase().as_str() { + "TCP" => values.push(Network::Tcp), + "UDP" => values.push(Network::Udp), + _ => (), + } + } + Self { values } + } +} + +impl Condition for NetworkMatcher { + fn apply(&self, sess: &Session) -> bool { + for v in &self.values { + if v == &sess.network { + debug!("[{}] matches network [{}]", &sess.network, v); + return true; + } + } + false + } +} + struct PortMatcher { condition: Box, } @@ -403,6 +433,10 @@ impl Router { cond_and.add(Box::new(PortMatcher::new(&rr.port_ranges))); } + if rr.networks.len() > 0 { + cond_and.add(Box::new(NetworkMatcher::new(&mut rr.networks))); + } + if cond_and.is_empty() { warn!("empty rule at target {}", rr.target_tag); continue; diff --git a/leaf/src/config/conf/config.rs b/leaf/src/config/conf/config.rs index 952b777..a1dbe34 100644 --- a/leaf/src/config/conf/config.rs +++ b/leaf/src/config/conf/config.rs @@ -590,7 +590,7 @@ pub fn from_lines(lines: Vec>) -> Result { match rule.type_field.as_str() { "IP-CIDR" | "DOMAIN" | "DOMAIN-SUFFIX" | "DOMAIN-KEYWORD" | "GEOIP" | "EXTERNAL" - | "PORT-RANGE" => { + | "PORT-RANGE" | "NETWORK" => { rule.filter = Some(params[1].to_string()); } _ => {} @@ -1105,6 +1105,9 @@ pub fn to_internal(conf: &mut Config) -> Result { "PORT-RANGE" => { rule.port_ranges.push(ext_filter); } + "NETWORK" => { + rule.networks.push(ext_filter); + } _ => {} } rules.push(rule); diff --git a/leaf/src/config/geosite.rs b/leaf/src/config/geosite.rs index 0eaf7c1..7d7e9d7 100644 --- a/leaf/src/config/geosite.rs +++ b/leaf/src/config/geosite.rs @@ -1,4 +1,4 @@ -// This file is generated by rust-protobuf 2.24.1. Do not edit +// This file is generated by rust-protobuf 2.25.0. Do not edit // @generated // https://github.com/rust-lang/rust-clippy/issues/702 @@ -21,7 +21,7 @@ /// Generated files are compatible only with the same version /// of protobuf runtime. -// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_24_1; +// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_25_0; #[derive(PartialEq,Clone,Default,Debug)] pub struct Domain { diff --git a/leaf/src/config/internal/config.proto b/leaf/src/config/internal/config.proto index 6e0af58..4a07e15 100644 --- a/leaf/src/config/internal/config.proto +++ b/leaf/src/config/internal/config.proto @@ -206,6 +206,7 @@ message Router { repeated string ip_cidrs = 3; repeated Mmdb mmdbs = 4; repeated string port_ranges = 5; + repeated string networks = 6; } repeated Rule rules = 1; diff --git a/leaf/src/config/internal/config.rs b/leaf/src/config/internal/config.rs index 7130ad2..9aa87d1 100644 --- a/leaf/src/config/internal/config.rs +++ b/leaf/src/config/internal/config.rs @@ -1,4 +1,4 @@ -// This file is generated by rust-protobuf 2.24.1. Do not edit +// This file is generated by rust-protobuf 2.25.0. Do not edit // @generated // https://github.com/rust-lang/rust-clippy/issues/702 @@ -21,7 +21,7 @@ /// Generated files are compatible only with the same version /// of protobuf runtime. -// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_24_1; +// const _PROTOBUF_VERSION_CHECK: () = ::protobuf::VERSION_2_25_0; #[derive(PartialEq,Clone,Default,Debug)] pub struct Api { @@ -4727,6 +4727,7 @@ pub struct Router_Rule { pub ip_cidrs: ::protobuf::RepeatedField<::std::string::String>, pub mmdbs: ::protobuf::RepeatedField, pub port_ranges: ::protobuf::RepeatedField<::std::string::String>, + pub networks: ::protobuf::RepeatedField<::std::string::String>, // special fields pub unknown_fields: ::protobuf::UnknownFields, pub cached_size: ::protobuf::CachedSize, @@ -4777,6 +4778,13 @@ impl Router_Rule { pub fn get_port_ranges(&self) -> &[::std::string::String] { &self.port_ranges } + + // repeated string networks = 6; + + + pub fn get_networks(&self) -> &[::std::string::String] { + &self.networks + } } impl ::protobuf::Message for Router_Rule { @@ -4813,6 +4821,9 @@ impl ::protobuf::Message for Router_Rule { 5 => { ::protobuf::rt::read_repeated_string_into(wire_type, is, &mut self.port_ranges)?; }, + 6 => { + ::protobuf::rt::read_repeated_string_into(wire_type, is, &mut self.networks)?; + }, _ => { ::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?; }, @@ -4842,6 +4853,9 @@ impl ::protobuf::Message for Router_Rule { for value in &self.port_ranges { my_size += ::protobuf::rt::string_size(5, &value); }; + for value in &self.networks { + my_size += ::protobuf::rt::string_size(6, &value); + }; my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields()); self.cached_size.set(my_size); my_size @@ -4867,6 +4881,9 @@ impl ::protobuf::Message for Router_Rule { for v in &self.port_ranges { os.write_string(5, &v)?; }; + for v in &self.networks { + os.write_string(6, &v)?; + }; os.write_unknown_fields(self.get_unknown_fields())?; ::std::result::Result::Ok(()) } @@ -4914,6 +4931,7 @@ impl ::protobuf::Clear for Router_Rule { self.ip_cidrs.clear(); self.mmdbs.clear(); self.port_ranges.clear(); + self.networks.clear(); self.unknown_fields.clear(); } }