Prevent self-connections just in case

This commit is contained in:
Igor Katson 2023-12-05 22:19:58 +00:00
parent 5e238419f4
commit cc92afcdec
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5

View file

@ -3,7 +3,7 @@ use std::{
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use anyhow::Context; use anyhow::{bail, Context};
use buffers::{ByteBuf, ByteString}; use buffers::{ByteBuf, ByteString};
use clone_to_owned::CloneToOwned; use clone_to_owned::CloneToOwned;
use librqbit_core::{id20::Id20, lengths::ChunkInfo, peer_id::try_decode_peer_id}; use librqbit_core::{id20::Id20, lengths::ChunkInfo, peer_id::try_decode_peer_id};
@ -142,6 +142,10 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {
anyhow::bail!("wrong info hash"); anyhow::bail!("wrong info hash");
} }
if handshake.peer_id == self.peer_id.0 {
bail!("looks like we are connecting to ourselves");
}
trace!( trace!(
"incoming connection: id={:?}", "incoming connection: id={:?}",
try_decode_peer_id(Id20(handshake.peer_id)) try_decode_peer_id(Id20(handshake.peer_id))
@ -217,6 +221,10 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {
anyhow::bail!("info hash does not match"); 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)?; self.handler.on_handshake(h)?;
if read_so_far > size { if read_so_far > size {