Skip to content

Commit e249dad

Browse files
committed
fix: acl check inbound trunk only
1 parent 211c0d8 commit e249dad

3 files changed

Lines changed: 76 additions & 15 deletions

File tree

src/proxy/acl.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl AclModule {
257257
if let Some(server) = &self.inner.server {
258258
let trunks = server.data_context.trunks_snapshot();
259259
for (name, trunk) in trunks.iter() {
260-
if trunk.matches_inbound_ip(addr).await {
260+
if trunk.matches_inbound_source_ip(addr).await {
261261
return Some(TrunkContext {
262262
id: trunk.id,
263263
name: name.clone(),
@@ -277,7 +277,7 @@ impl AclModule {
277277
.collect();
278278

279279
for (name, trunk) in trunks {
280-
if trunk.matches_inbound_ip(addr).await {
280+
if trunk.matches_inbound_source_ip(addr).await {
281281
return Some(TrunkContext {
282282
id: trunk.id,
283283
name,
@@ -498,6 +498,7 @@ fn parse_rules(rules: Vec<String>) -> Vec<AclRule> {
498498
#[cfg(test)]
499499
mod tests {
500500
use super::*;
501+
use crate::proxy::routing::TrunkDirection;
501502
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
502503

503504
fn create_test_config(rules: Vec<String>) -> Arc<ProxyConfig> {
@@ -524,6 +525,60 @@ mod tests {
524525
})
525526
}
526527

528+
#[tokio::test]
529+
async fn trunk_context_ignores_outbound_dest_same_ip() {
530+
let source_ip = IpAddr::V4(Ipv4Addr::new(43, 198, 217, 33));
531+
let mut config = ProxyConfig::default();
532+
533+
config.trunks.insert(
534+
"outbound-same-ip".to_string(),
535+
TrunkConfig {
536+
id: Some(147),
537+
direction: Some(TrunkDirection::Outbound),
538+
dest: "43.198.217.33:5396".to_string(),
539+
..Default::default()
540+
},
541+
);
542+
config.trunks.insert(
543+
"inbound-source".to_string(),
544+
TrunkConfig {
545+
id: Some(144),
546+
direction: Some(TrunkDirection::Inbound),
547+
inbound_hosts: vec!["43.198.217.33".to_string()],
548+
..Default::default()
549+
},
550+
);
551+
552+
let acl = AclModule::new(Arc::new(config));
553+
let ctx = acl
554+
.is_from_trunk_context(&source_ip)
555+
.await
556+
.expect("inbound trunk should match");
557+
558+
assert_eq!(ctx.id, Some(144));
559+
assert_eq!(ctx.name, "inbound-source");
560+
}
561+
562+
#[tokio::test]
563+
async fn trunk_context_does_not_match_inbound_dest_only() {
564+
let source_ip = IpAddr::V4(Ipv4Addr::new(43, 198, 217, 33));
565+
let mut config = ProxyConfig::default();
566+
567+
config.trunks.insert(
568+
"inbound-dest-only".to_string(),
569+
TrunkConfig {
570+
id: Some(144),
571+
direction: Some(TrunkDirection::Inbound),
572+
dest: "43.198.217.33:5060".to_string(),
573+
..Default::default()
574+
},
575+
);
576+
577+
let acl = AclModule::new(Arc::new(config));
578+
579+
assert!(acl.is_from_trunk_context(&source_ip).await.is_none());
580+
}
581+
527582
#[test]
528583
fn test_parse_network() {
529584
let (net, prefix) = parse_network("192.168.1.0/24").unwrap();

src/proxy/data.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ use tracing::{info, warn};
1616

1717
use crate::{
1818
addons::queue::services::utils as queue_utils,
19-
call::DialDirection,
2019
config::{ProxyConfig, RecordingPolicy},
2120
models::{routing, sip_trunk},
2221
proxy::routing::matcher::RouteResourceLookup,
2322
proxy::routing::{
2423
CacPolicy, CallIdMode, ConfigOrigin, DestConfig, MatchConditions, MediaMode, RewriteRules,
25-
RouteAction, RouteQueueConfig, RouteRule, TrunkConfig, VideoPolicy, candidate_matches_ip,
24+
RouteAction, RouteQueueConfig, RouteRule, TrunkConfig, VideoPolicy,
2625
},
2726
proxy::trunk_registrar::TrunkRegistrar,
2827
};
@@ -214,17 +213,8 @@ impl ProxyDataContext {
214213
let trunks = self.trunks_snapshot();
215214
let mut matches = Vec::new();
216215
for (name, trunk) in trunks.iter() {
217-
if let Some(trunk_direction) = trunk.direction
218-
&& !trunk_direction.allows(&DialDirection::Inbound)
219-
{
220-
continue;
221-
}
222-
223-
for host in &trunk.inbound_hosts {
224-
if candidate_matches_ip(host, addr).await {
225-
matches.push(name.clone());
226-
break;
227-
}
216+
if trunk.matches_inbound_source_ip(addr).await {
217+
matches.push(name.clone());
228218
}
229219
}
230220
matches

src/proxy/routing/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,22 @@ impl Default for TrunkConfig {
294294
}
295295

296296
impl TrunkConfig {
297+
pub async fn matches_inbound_source_ip(&self, addr: &IpAddr) -> bool {
298+
if let Some(trunk_direction) = self.direction
299+
&& !trunk_direction.allows(&DialDirection::Inbound)
300+
{
301+
return false;
302+
}
303+
304+
for host in &self.inbound_hosts {
305+
if candidate_matches(host, addr).await {
306+
return true;
307+
}
308+
}
309+
310+
false
311+
}
312+
297313
pub async fn matches_inbound_ip(&self, addr: &IpAddr) -> bool {
298314
for host in &self.inbound_hosts {
299315
if candidate_matches(host, addr).await {

0 commit comments

Comments
 (0)