From cc92afcdec91328afb1dbf4915c3603ab1146b78 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Tue, 5 Dec 2023 22:19:58 +0000 Subject: [PATCH] Prevent self-connections just in case --- crates/librqbit/src/peer_connection.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/crates/librqbit/src/peer_connection.rs b/crates/librqbit/src/peer_connection.rs index 33c3f2a..5605efe 100644 --- a/crates/librqbit/src/peer_connection.rs +++ b/crates/librqbit/src/peer_connection.rs @@ -3,7 +3,7 @@ use std::{ time::{Duration, Instant}, }; -use anyhow::Context; +use anyhow::{bail, Context}; use buffers::{ByteBuf, ByteString}; use clone_to_owned::CloneToOwned; use librqbit_core::{id20::Id20, lengths::ChunkInfo, peer_id::try_decode_peer_id}; @@ -142,6 +142,10 @@ impl PeerConnection { anyhow::bail!("wrong info hash"); } + if handshake.peer_id == self.peer_id.0 { + bail!("looks like we are connecting to ourselves"); + } + trace!( "incoming connection: id={:?}", try_decode_peer_id(Id20(handshake.peer_id)) @@ -217,6 +221,10 @@ impl PeerConnection { anyhow::bail!("info hash does not match"); } + if h.peer_id == self.peer_id.0 { + bail!("looks like we are connecting to ourselves"); + } + self.handler.on_handshake(h)?; if read_so_far > size {