Skip to content

Commit

Permalink
Deal with dead circuits in SOCKS5 handler
Browse files Browse the repository at this point in the history
  • Loading branch information
egbertbouman committed Jan 29, 2024
1 parent 200c63a commit b40ae4f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ build-backend = "maturin"

[project]
name = "ipv8-rust-tunnels"
description = "IPv8 tunnel performance enhancements"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Rust",
Expand Down
41 changes: 30 additions & 11 deletions src/socks5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,9 @@ async fn process_socks5_packet(

let pkt = &buf[header.serialized_len()..].to_vec();
let address = &header.address;
let circuit_id = match addr_to_cid.get(address) {
Some(cid) => *cid,
None => {
let options = get_options(&associated_socket, &circuits);
let cid = match options.choose(&mut rand::thread_rng()) {
Some(cid) => *cid,
None => return None,
};
addr_to_cid.insert(address.clone(), cid);
cid
}
let Some(circuit_id) = select_circuit(address, associated_socket, circuits, addr_to_cid) else {
warn!("No circuits available, dropping packet");
return None;
};

match circuits.lock().unwrap().get_mut(&circuit_id) {
Expand All @@ -115,6 +107,33 @@ async fn process_socks5_packet(
}
}

fn select_circuit(
address: &Address,
socket: &Arc<UdpSocket>,
circuits: &Arc<Mutex<HashMap<u32, Circuit>>>,
addr_to_cid: &mut HashMap<Address, u32>,
) -> Option<u32> {
// Remove dead circuits from addr_to_cid
if let Some(cid) = addr_to_cid.get(address) {
if !circuits.lock().unwrap().contains_key(cid) {
debug!("Not sending packet for {} over dead circuit {}, removing link", address, cid);
addr_to_cid.remove(address);
}
}

match addr_to_cid.get(address) {
Some(cid) => Some(*cid),
None => {
let options = get_options(&socket, &circuits);
let Some(&cid) = options.choose(&mut rand::thread_rng()) else {
return None;
};
addr_to_cid.insert(address.clone(), cid);
Some(cid)
}
}
}

fn get_options(socket: &Arc<UdpSocket>, circuits: &Arc<Mutex<HashMap<u32, Circuit>>>) -> Vec<u32> {
let guard = circuits.lock().unwrap();
guard
Expand Down

0 comments on commit b40ae4f

Please sign in to comment.