conf: support NETWORK rule

This commit is contained in:
eric committed 2021-09-16 05:51:13 +08:00
1 parent 135df3d44e
commit 028f294982
6 files changed
+64 -8

No files matched your search

+2 -2
View File
@@ -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 {
+35 -1
View File
@@ -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<Network>,
}
impl NetworkMatcher {
fn new(networks: &mut protobuf::RepeatedField<String>) -> 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<dyn Condition>,
}
@@ -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;
+4 -1
View File
@@ -590,7 +590,7 @@ pub fn from_lines(lines: Vec<io::Result<String>>) -> Result<Config> {
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<internal::Config> {
"PORT-RANGE" => {
rule.port_ranges.push(ext_filter);
}
"NETWORK" => {
rule.networks.push(ext_filter);
}
_ => {}
}
rules.push(rule);
+2 -2
View File
@@ -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 {
+1
View File
@@ -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;
+20 -2
View File
@@ -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<Router_Rule_Mmdb>,
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();
}
}