Skip to content

Commit

Permalink
feat: wip: path manager
Browse files Browse the repository at this point in the history
  • Loading branch information
junkurihara committed Oct 27, 2023
1 parent 771b3ef commit e427200
Showing 1 changed file with 76 additions and 7 deletions.
83 changes: 76 additions & 7 deletions dap-lib/src/doh_client/path_manage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ impl DoHPath {
}
}
}

/// check if the path is looped
pub fn is_looped(&self) -> bool {
let mut seen = vec![self.target.authority.clone()];
for relay in &self.relays {
if seen.contains(&relay.authority) {
return true;
}
seen.push(relay.authority.clone());
}
false
}
}

/// represents the health of a path
Expand Down Expand Up @@ -207,7 +219,7 @@ impl DoHPathManager {
});

// build path object
let maybe_looped_path = targets.map(|target| {
let maybe_looped_paths = targets.map(|target| {
relay_paths
.clone()
.map(|relay_path| {
Expand All @@ -226,14 +238,27 @@ impl DoHPathManager {
.collect::<Vec<_>>()
});

let v = maybe_looped_path.clone().collect::<Vec<_>>();

// TODO: TODO: TODO: remove loop paths: add check loop function in DoHPath
// remove looped paths
let loop_free_paths = maybe_looped_paths
.map(|per_target| {
let loop_free = per_target.iter().map(|per_next_hop| {
per_next_hop
.iter()
.filter(|path| !path.is_looped())
.cloned()
.collect::<Vec<_>>()
});
loop_free
.filter(|per_next_hop| !per_next_hop.is_empty())
.collect::<Vec<_>>()
})
.filter(|per_target| !per_target.is_empty())
.collect::<Vec<_>>();

Ok(Self {
paths: vec![],
target_randomization: true,
nexthop_randomization: true,
paths: loop_free_paths,
target_randomization: globals.proxy_config.target_config.target_randomization,
nexthop_randomization: nexthop_relay_config.odoh_relay_randomization,
})
}
}
Expand Down Expand Up @@ -279,4 +304,48 @@ mod tests {

assert_eq!(decoded, "https://relay1.dns.google/proxy?targethost=dns.google&targetpath=/dns-query&relayhost[1]=relay2.dns.google&relaypath[1]=/proxy&relayhost[2]=relay3.dns.google&relaypath[2]=/proxy");
}

#[tokio::test]
async fn is_looped_works() {
let target = Arc::new(DoHTarget {
authority: "dns.google".to_string(),
path: "/dns-query".to_string(),
scheme: Scheme::Https,
});
let relay1 = Arc::new(DoHRelay {
authority: "relay1.dns.google".to_string(),
path: "/proxy".to_string(),
scheme: Scheme::Https,
can_be_next_hop: true,
});
let relay2 = Arc::new(DoHRelay {
authority: "relay2.dns.google".to_string(),
path: "/proxy".to_string(),
scheme: Scheme::Https,
can_be_next_hop: false,
});
let relay3 = Arc::new(DoHRelay {
authority: "relay3.dns.google".to_string(),
path: "/proxy".to_string(),
scheme: Scheme::Https,
can_be_next_hop: false,
});
let mut path = DoHPath {
target,
relays: vec![relay1, relay2, relay3],
is_healthy: IsHealthy::new(),
doh_type: DoHType::Oblivious,
};
assert!(!path.is_looped());

let relay4 = Arc::new(DoHRelay {
authority: "relay1.dns.google".to_string(),
path: "/proxy".to_string(),
scheme: Scheme::Https,
can_be_next_hop: true,
});

path.relays.push(relay4);
assert!(path.is_looped());
}
}

0 comments on commit e427200

Please sign in to comment.