Skip to content

Commit

Permalink
Add the recent wireguard endpoint to NAT candidates if a peer has an …
Browse files Browse the repository at this point in the history
…endpoint override
  • Loading branch information
bschwind committed Mar 18, 2024
1 parent 1b337de commit d2292e4
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions server/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ pub mod admin;
pub mod user;

/// Inject the collected endpoints from the WG interface into a list of peers.
/// This is essentially what adds NAT holepunching functionality.
/// This is essentially what adds NAT holepunching functionality. If a peer
/// already has an endpoint specified (by calling the override-endpoint) API,
/// the relatively recent wireguard endpoint will be added to the list of NAT
/// candidates, so other peers have a better chance of connecting.
pub fn inject_endpoints(session: &Session, peers: &mut Vec<Peer>) {
for peer in peers {
if peer.contents.endpoint.is_none() {
if let Some(endpoint) = session.context.endpoints.read().get(&peer.public_key) {
peer.contents.endpoint = Some(endpoint.to_owned().into());
let endpoints = session.context.endpoints.read();
let wg_endpoint = endpoints.get(&peer.public_key);

if peer.contents.endpoint.is_some() {
if let Some(wg_endpoint) = wg_endpoint {
// The peer already has an endpoint specified, but it might be stale.
// If there is an endpoint reported from wireguard, we should add it
// to the list of candidates so others can try to connect using it.
peer.contents.candidates.push(wg_endpoint.to_owned().into());
}
} else if let Some(wg_endpoint) = wg_endpoint {
peer.contents.endpoint = Some(wg_endpoint.to_owned().into());
}
}
}

0 comments on commit d2292e4

Please sign in to comment.