Skip to content

Commit 88bb66f

Browse files
committed
update the way IP aliases are serialized and deserialized
1 parent 67e9951 commit 88bb66f

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

src/firewall/items/tcp_connection.rs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use std::str::FromStr;
1212
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
1313
#[serde(rename_all = "snake_case")]
1414
pub enum TcpConnectionField {
15-
SourceIp(IpAlias),
16-
DestinationIp(IpAlias),
15+
SourceIp(Vec<String>),
16+
DestinationIp(Vec<String>),
1717
SourcePort(Vec<u32>),
1818
DestinationPort(Vec<u32>),
1919
Protocol(Vec<String>),
@@ -43,7 +43,7 @@ impl TcpConnectionField {
4343
FirewallRuleDirection::Out => item.destination_ip.as_ref(),
4444
};
4545
if let Some(ip) = ip_opt.and_then(|ip| IpAddr::from_str(ip).ok()) {
46-
Some(FirewallCompareType::Ip((ip, a.to_ips(context).await?)))
46+
Some(FirewallCompareType::Ip((ip, to_ips(a, context).await?)))
4747
} else {
4848
None
4949
}
@@ -54,7 +54,7 @@ impl TcpConnectionField {
5454
FirewallRuleDirection::Out => item.source_ip.as_ref(),
5555
};
5656
if let Some(ip) = ip_opt.and_then(|ip| IpAddr::from_str(ip).ok()) {
57-
Some(FirewallCompareType::Ip((ip, a.to_ips(context).await?)))
57+
Some(FirewallCompareType::Ip((ip, to_ips(a, context).await?)))
5858
} else {
5959
None
6060
}
@@ -105,36 +105,28 @@ impl<'a> PredicateEvaluator for &'a AppGuardTcpConnection {
105105
}
106106
}
107107

108-
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
109-
#[serde(untagged)]
110-
pub enum IpAlias {
111-
Name(String),
112-
Addresses(Vec<String>),
113-
}
108+
async fn to_ips(vec: &Vec<String>, context: &AppContext) -> Option<Vec<IpNetwork>> {
109+
let mut ret_val = Vec::new();
114110

115-
impl IpAlias {
116-
async fn to_ips(&self, context: &AppContext) -> Option<Vec<IpNetwork>> {
117-
match self {
118-
IpAlias::Name(name) => {
119-
let token = context.root_token_provider.get().await.ok()?.jwt.clone();
120-
context
121-
.datastore
122-
.clone()
123-
.get_ip_aliases(token, name)
124-
.await
125-
.ok()
126-
}
127-
IpAlias::Addresses(addresses) => {
128-
let mut ipnetworks = Vec::new();
129-
for address in addresses {
130-
if let Ok(cidr) = IpNetwork::from_str(address) {
131-
ipnetworks.push(cidr);
132-
}
133-
}
134-
Some(ipnetworks)
135-
}
111+
for a in vec {
112+
if let Ok(cidr) = IpNetwork::from_str(a) {
113+
ret_val.push(cidr);
114+
} else {
115+
// alias
116+
let token = context.root_token_provider.get().await.ok()?.jwt.clone();
117+
let Ok(cidrs) = context
118+
.datastore
119+
.clone()
120+
.get_ip_aliases(token, a)
121+
.await
122+
else {
123+
continue;
124+
};
125+
ret_val.extend(cidrs);
136126
}
137127
}
128+
129+
Some(ret_val)
138130
}
139131

140132
// #[cfg(test)]

0 commit comments

Comments
 (0)