Skip to content

Commit

Permalink
Refactor NetGraph and ExitRegistry, improve method signatures, and up…
Browse files Browse the repository at this point in the history
…date usage across modules
  • Loading branch information
nullchinchilla committed Oct 31, 2024
1 parent 4f7e57a commit 5398478
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 26 deletions.
12 changes: 6 additions & 6 deletions libraries/earendil_topology/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ impl RelayGraph {
self.exits.add_exit(relay_fp, exit_info);
}

pub fn get_exit(&self, relay_fp: &RelayFingerprint) -> Option<&ExitInfo> {
self.exits.get_exit(relay_fp)
pub fn get_exit(&self, relay_fp: RelayFingerprint) -> Option<ExitInfo> {
self.exits.get_exit(&relay_fp).cloned()
}

pub fn get_random_exit_for_port(&self, port: u16) -> Option<(&RelayFingerprint, &ExitInfo)> {
pub fn get_random_exit_for_port(&self, port: u16) -> Option<(RelayFingerprint, ExitInfo)> {
self.exits.get_random_exit_for_port(port)
}

Expand Down Expand Up @@ -434,7 +434,7 @@ pub enum IdentityError {
}

#[derive(Default, Serialize, Deserialize, Clone)]
pub struct ExitRegistry {
struct ExitRegistry {
port_to_exits: BTreeMap<u16, Vec<RelayFingerprint>>,
exit_configs: HashMap<RelayFingerprint, ExitInfo>,
}
Expand All @@ -461,15 +461,15 @@ impl ExitRegistry {
self.exit_configs.get(relay_fp)
}

pub fn get_random_exit_for_port(&self, port: u16) -> Option<(&RelayFingerprint, &ExitInfo)> {
pub fn get_random_exit_for_port(&self, port: u16) -> Option<(RelayFingerprint, ExitInfo)> {
self.port_to_exits.get(&port).and_then(|exits| {
exits
.iter()
.choose(&mut thread_rng())
.and_then(|fingerprint| {
self.exit_configs
.get(fingerprint)
.map(|exit_info| (fingerprint, exit_info))
.map(|exit_info| (*fingerprint, exit_info.clone()))
})
})
}
Expand Down
16 changes: 8 additions & 8 deletions src/link_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl LinkNode {
Some(val) => Ok(val.0.public().fingerprint()),
None => Ok(*self
.graph
.usable_relay_neighbors()
.connected_relays()
.choose(&mut rand::thread_rng())
.context("no relay neighbors to act as a SURB destination")?),
}
Expand All @@ -188,14 +188,14 @@ impl LinkNode {
self.recv_incoming.recv().await.unwrap()
}

/// Gets the current relay graph.
pub fn relay_graph(&self) -> RelayGraph {
self.graph.read_graph(|g| g.clone())
}
// /// Gets the current NetGraph.
// pub fn relay_graph(&self) -> RelayGraph {
// self.graph.read_graph(|g| g.clone())
// }

/// Gets all our currently connected neighbors.
pub fn all_neighs(&self) -> Vec<NeighborId> {
todo!()
/// Gets the current NetGraph.
pub fn netgraph(&self) -> NetGraph {
self.graph.clone()
}

/// Sends a chat message to a neighbor.
Expand Down
2 changes: 1 addition & 1 deletion src/link_node/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn gossip_once(
graph: NetGraph,
switch: WeakHandle<SwitchProcess>,
) -> anyhow::Result<()> {
let relays = graph.usable_relay_neighbors();
let relays = graph.connected_relays();
let neighbor_fp = relays
.choose(&mut rand::thread_rng())
.cloned()
Expand Down
19 changes: 15 additions & 4 deletions src/link_node/netgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use earendil_packet::PeelInstruction;
use earendil_topology::RelayGraph;
use parking_lot::RwLock;

use super::NeighborId;
use super::{types::ClientId, NeighborId};

/// A graph of the network. Similar to RelayGraph, but concurrency-enabled and augmented with information about where *we* are in the graph and what live neighbors we have.
#[derive(Clone)]
Expand Down Expand Up @@ -37,8 +37,19 @@ impl NetGraph {
}
}

/// Obtains a list of *usable* neighbor relays, which must be both live and within the relay graph.
pub fn usable_relay_neighbors(&self) -> Vec<RelayFingerprint> {
/// Obtains a list of connected clients.
pub fn connected_clients(&self) -> Vec<ClientId> {
self.live_neighbors
.iter()
.filter_map(|n| match n.key() {
NeighborId::Client(id) => Some(*id),
_ => None,
})
.collect()
}

/// Obtains a list of connected relays, which must be both live and within the relay graph.
pub fn connected_relays(&self) -> Vec<RelayFingerprint> {
let rg = self.relay_graph.read();
self.live_neighbors
.iter()
Expand Down Expand Up @@ -71,7 +82,7 @@ impl NetGraph {

/// Obtain the closest neighbor to the given relay destination.
pub fn closest_neigh_to(&self, dest: RelayFingerprint) -> Option<RelayFingerprint> {
let usable_relays = self.usable_relay_neighbors();
let usable_relays = self.connected_relays();
let graph = self.relay_graph.read();
usable_relays.into_iter().min_by_key(|relay| {
graph
Expand Down
10 changes: 6 additions & 4 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,15 +422,17 @@ async fn socks5_once(
.await?;
}
Socks5Fallback::SimpleProxy { exit_nodes } => {
let relay_graph = v2h.link_node().relay_graph();
let graph = v2h.link_node().netgraph();

let mut rng = StdRng::from_entropy();
let remote_ep: HavenEndpoint = exit_nodes
.choose(&mut rng)
.and_then(|remote_relay_fp| relay_graph.get_exit(remote_relay_fp))
.and_then(|remote_relay_fp| {
graph.read_graph(|graph| graph.get_exit(*remote_relay_fp))
})
.or_else(|| {
relay_graph
.get_random_exit_for_port(port)
graph
.read_graph(|graph| graph.get_random_exit_for_port(port))
.map(|(_, exit_info)| exit_info)
})
.map(|exit_info| exit_info.haven_endpoint)
Expand Down
8 changes: 7 additions & 1 deletion src/node/control_protocol_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,13 @@ impl ControlProtocol for ControlProtocolImpl {

// ---------------- chat-related functionality -----------------
async fn list_neighbors(&self) -> Vec<NeighborId> {
self.ctx.v2h.link_node().all_neighs()
let graph = self.ctx.v2h.link_node().netgraph();
graph
.connected_clients()
.into_iter()
.map(NeighborId::Client)
.chain(graph.connected_relays().into_iter().map(NeighborId::Relay))
.collect()
}

async fn list_chats(&self) -> Result<HashMap<String, (Option<ChatEntry>, u32)>, ChatError> {
Expand Down
7 changes: 5 additions & 2 deletions src/v2h_node/dht.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ pub async fn dht_get(
}

fn dht_key_to_fps(ctx: &V2hNodeCtx, key: &str) -> Vec<RelayFingerprint> {
let mut all_nodes: Vec<RelayFingerprint> =
ctx.n2r.link_node().relay_graph().all_nodes().collect();
let mut all_nodes: Vec<RelayFingerprint> = ctx
.n2r
.link_node()
.netgraph()
.read_graph(|g| g.all_nodes().collect());
all_nodes.sort_unstable_by_key(|fp| *blake3::hash(&(key, fp).stdcode()).as_bytes());
all_nodes
}

0 comments on commit 5398478

Please sign in to comment.