Skip to content

Commit

Permalink
Merge pull request #227 from ikatson/dev
Browse files Browse the repository at this point in the history
Various tweaks to peer protocol information
  • Loading branch information
ikatson authored Aug 29, 2024
2 parents d7f3d88 + 1d48a51 commit af00713
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
6 changes: 5 additions & 1 deletion crates/librqbit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,14 @@ pub use librqbit_core::torrent_metainfo::*;
mod tests;

/// The cargo version of librqbit.
pub fn version() -> &'static str {
pub const fn version() -> &'static str {
env!("CARGO_PKG_VERSION")
}

pub const fn client_name_and_version() -> &'static str {
concat!("rqbit ", env!("CARGO_PKG_VERSION"))
}

pub fn try_increase_nofile_limit() -> anyhow::Result<u64> {
Ok(rlimit::increase_nofile_limit(1024 * 1024)?)
}
3 changes: 2 additions & 1 deletion crates/librqbit/src/peer_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {
.context("error reading handshake")?;
let h_supports_extended = h.supports_extended();
trace!(
peer_id=?h.peer_id,
peer_id=?Id20::new(h.peer_id),
decoded_id=?try_decode_peer_id(Id20::new(h.peer_id)),
"connected",
);
Expand Down Expand Up @@ -247,6 +247,7 @@ impl<H: PeerConnectionHandler> PeerConnection<H> {

if supports_extended {
let mut my_extended = ExtendedHandshake::new();
my_extended.v = Some(ByteBuf(crate::client_name_and_version().as_bytes()));
self.handler
.update_my_extended_handshake(&mut my_extended)?;
let my_extended = Message::Extended(ExtendedMessage::Handshake(my_extended));
Expand Down
8 changes: 7 additions & 1 deletion crates/librqbit_core/src/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ pub enum AzureusStyleKind {
Deluge,
LibTorrent,
Transmission,
QBittorrent,
UTorrent,
RQBit,
Other([char; 2]),
}

Expand All @@ -20,6 +23,9 @@ impl AzureusStyleKind {
b"DE" => AzureusStyleKind::Deluge,
b"lt" | b"LT" => AzureusStyleKind::LibTorrent,
b"TR" => AzureusStyleKind::Transmission,
b"qB" => AzureusStyleKind::QBittorrent,
b"UT" => AzureusStyleKind::UTorrent,
b"rQ" => AzureusStyleKind::RQBit,
_ => AzureusStyleKind::Other([b1 as char, b2 as char]),
}
}
Expand Down Expand Up @@ -53,7 +59,7 @@ pub fn generate_peer_id() -> Id20 {
let u = uuid::Uuid::new_v4();
peer_id[4..20].copy_from_slice(&u.as_bytes()[..]);

peer_id[..8].copy_from_slice(b"-rQ0001-");
peer_id[..8].copy_from_slice(b"-rQ7000-");

Id20::new(peer_id)
}
50 changes: 28 additions & 22 deletions crates/peer_binary_protocol/src/extended/ut_pex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::net::{IpAddr, SocketAddr};
use byteorder::{ByteOrder, BE};
use bytes::Bytes;
use clone_to_owned::CloneToOwned;
use itertools::{EitherOrBoth, Itertools};
use serde::{Deserialize, Serialize};

pub struct PexPeerInfo {
Expand All @@ -23,16 +22,20 @@ impl core::fmt::Debug for PexPeerInfo {

#[derive(Serialize, Default, Deserialize)]
pub struct UtPex<B> {
added: B,
#[serde(skip_serializing_if = "Option::is_none")]
added: Option<B>,
#[serde(rename = "added.f")]
#[serde(skip_serializing_if = "Option::is_none")]
added_f: Option<B>,
added6: B,
#[serde(skip_serializing_if = "Option::is_none")]
added6: Option<B>,
#[serde(rename = "added6.f")]
#[serde(skip_serializing_if = "Option::is_none")]
added6_f: Option<B>,
dropped: B,
dropped6: B,
#[serde(skip_serializing_if = "Option::is_none")]
dropped: Option<B>,
#[serde(skip_serializing_if = "Option::is_none")]
dropped6: Option<B>,
}

impl<B> core::fmt::Debug for UtPex<B>
Expand Down Expand Up @@ -79,28 +82,31 @@ where
{
fn added_peers_inner<'a>(
&'a self,
buf: &'a B,
buf: &'a Option<B>,
flags: &'a Option<B>,
ip_len: usize,
) -> impl Iterator<Item = PexPeerInfo> + Clone + 'a {
let addrs = buf.as_ref().chunks_exact(ip_len + 2).map(move |c| {
let ip = match ip_len {
4 => IpAddr::from(TryInto::<[u8; 4]>::try_into(&c[..4]).unwrap()),
16 => IpAddr::from(TryInto::<[u8; 16]>::try_into(&c[..16]).unwrap()),
_ => unreachable!(),
};
let port = BE::read_u16(&c[ip_len..]);
SocketAddr::new(ip, port)
});
let flags = flags
const PORT_LEN: usize = 2;
const DEFAULT_FLAGS: u8 = 0;
let addrs = buf
.as_ref()
.map(|b| b.as_ref().iter().copied())
.into_iter()
.flatten();
addrs.zip_longest(flags).filter_map(|eob| match eob {
EitherOrBoth::Both(addr, flags) => Some(PexPeerInfo { flags, addr }),
EitherOrBoth::Left(addr) => Some(PexPeerInfo { flags: 0, addr }),
EitherOrBoth::Right(_) => None,
.flat_map(move |it| it.as_ref().chunks_exact(ip_len + PORT_LEN))
.map(move |c| {
let ip = match ip_len {
4 => IpAddr::from(TryInto::<[u8; 4]>::try_into(&c[..4]).unwrap()),
16 => IpAddr::from(TryInto::<[u8; 16]>::try_into(&c[..16]).unwrap()),
_ => unreachable!(),
};
let port = BE::read_u16(&c[ip_len..]);
SocketAddr::new(ip, port)
});
addrs.enumerate().map(move |(id, addr)| PexPeerInfo {
addr,
flags: flags
.as_ref()
.and_then(|f| f.as_ref().get(id).copied())
.unwrap_or(DEFAULT_FLAGS),
})
}

Expand Down

0 comments on commit af00713

Please sign in to comment.