router, conf: support INBOUND-TAG rule

This commit is contained in:
eric committed 2021-09-16 08:41:29 +08:00
1 parent d5da5035b0
commit 8e7c0b4bb8
4 files changed
+53 -1

No files matched your search

+30
View File
@@ -105,6 +105,32 @@ impl Condition for IpCidrMatcher {
}
}
struct InboundTagMatcher {
values: Vec<String>,
}
impl InboundTagMatcher {
fn new(tags: &mut protobuf::RepeatedField<String>) -> Self {
let mut values = Vec::new();
for t in tags.iter_mut() {
values.push(std::mem::take(t));
}
Self { values }
}
}
impl Condition for InboundTagMatcher {
fn apply(&self, sess: &Session) -> bool {
for v in &self.values {
if v == &sess.inbound_tag {
debug!("[{}] matches inbound tag [{}]", &sess.inbound_tag, v);
return true;
}
}
false
}
}
struct NetworkMatcher {
values: Vec<Network>,
}
@@ -437,6 +463,10 @@ impl Router {
cond_and.add(Box::new(NetworkMatcher::new(&mut rr.networks)));
}
if rr.inbound_tags.len() > 0 {
cond_and.add(Box::new(InboundTagMatcher::new(&mut rr.inbound_tags)));
}
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" | "NETWORK" => {
| "PORT-RANGE" | "NETWORK" | "INBOUND-TAG" => {
rule.filter = Some(params[1].to_string());
}
_ => {}
@@ -1120,6 +1120,9 @@ pub fn to_internal(conf: &mut Config) -> Result<internal::Config> {
"NETWORK" => {
rule.networks.push(ext_filter);
}
"INBOUND-TAG" => {
rule.inbound_tags.push(ext_filter);
}
_ => {}
}
rules.push(rule);
+1
View File
@@ -207,6 +207,7 @@ message Router {
repeated Mmdb mmdbs = 4;
repeated string port_ranges = 5;
repeated string networks = 6;
repeated string inbound_tags = 7;
}
repeated Rule rules = 1;
+18
View File
@@ -4728,6 +4728,7 @@ pub struct Router_Rule {
pub mmdbs: ::protobuf::RepeatedField<Router_Rule_Mmdb>,
pub port_ranges: ::protobuf::RepeatedField<::std::string::String>,
pub networks: ::protobuf::RepeatedField<::std::string::String>,
pub inbound_tags: ::protobuf::RepeatedField<::std::string::String>,
// special fields
pub unknown_fields: ::protobuf::UnknownFields,
pub cached_size: ::protobuf::CachedSize,
@@ -4785,6 +4786,13 @@ impl Router_Rule {
pub fn get_networks(&self) -> &[::std::string::String] {
&self.networks
}
// repeated string inbound_tags = 7;
pub fn get_inbound_tags(&self) -> &[::std::string::String] {
&self.inbound_tags
}
}
impl ::protobuf::Message for Router_Rule {
@@ -4824,6 +4832,9 @@ impl ::protobuf::Message for Router_Rule {
6 => {
::protobuf::rt::read_repeated_string_into(wire_type, is, &mut self.networks)?;
},
7 => {
::protobuf::rt::read_repeated_string_into(wire_type, is, &mut self.inbound_tags)?;
},
_ => {
::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
},
@@ -4856,6 +4867,9 @@ impl ::protobuf::Message for Router_Rule {
for value in &self.networks {
my_size += ::protobuf::rt::string_size(6, &value);
};
for value in &self.inbound_tags {
my_size += ::protobuf::rt::string_size(7, &value);
};
my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
self.cached_size.set(my_size);
my_size
@@ -4884,6 +4898,9 @@ impl ::protobuf::Message for Router_Rule {
for v in &self.networks {
os.write_string(6, &v)?;
};
for v in &self.inbound_tags {
os.write_string(7, &v)?;
};
os.write_unknown_fields(self.get_unknown_fields())?;
::std::result::Result::Ok(())
}
@@ -4932,6 +4949,7 @@ impl ::protobuf::Clear for Router_Rule {
self.mmdbs.clear();
self.port_ranges.clear();
self.networks.clear();
self.inbound_tags.clear();
self.unknown_fields.clear();
}
}