mirror of
https://github.com/YGGverse/aquatic.git
synced 2026-04-02 02:35:31 +00:00
Run rustfmt, clean up aquatic_http_protocol/Cargo.toml
This commit is contained in:
parent
0cc312a78d
commit
d0e716f80b
65 changed files with 1754 additions and 2590 deletions
|
|
@ -1,46 +1,40 @@
|
|||
use std::net::IpAddr;
|
||||
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub enum IpVersion {
|
||||
IPv4,
|
||||
IPv6
|
||||
IPv6,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct AnnounceInterval (pub i32);
|
||||
|
||||
pub struct AnnounceInterval(pub i32);
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct InfoHash (pub [u8; 20]);
|
||||
|
||||
pub struct InfoHash(pub [u8; 20]);
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct ConnectionId (pub i64);
|
||||
pub struct ConnectionId(pub i64);
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct TransactionId (pub i32);
|
||||
|
||||
pub struct TransactionId(pub i32);
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct NumberOfBytes (pub i64);
|
||||
pub struct NumberOfBytes(pub i64);
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct NumberOfPeers (pub i32);
|
||||
pub struct NumberOfPeers(pub i32);
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct NumberOfDownloads (pub i32);
|
||||
|
||||
pub struct NumberOfDownloads(pub i32);
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct Port (pub u16);
|
||||
pub struct Port(pub u16);
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, PartialOrd, Ord)]
|
||||
pub struct PeerId (pub [u8; 20]);
|
||||
pub struct PeerId(pub [u8; 20]);
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub struct PeerKey (pub u32);
|
||||
|
||||
pub struct PeerKey(pub u32);
|
||||
|
||||
#[derive(Hash, PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ResponsePeer {
|
||||
|
|
@ -48,8 +42,6 @@ pub struct ResponsePeer {
|
|||
pub port: Port,
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
impl quickcheck::Arbitrary for IpVersion {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
|
|
@ -61,7 +53,6 @@ impl quickcheck::Arbitrary for IpVersion {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
impl quickcheck::Arbitrary for InfoHash {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
|
|
@ -75,7 +66,6 @@ impl quickcheck::Arbitrary for InfoHash {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
impl quickcheck::Arbitrary for PeerId {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
|
|
@ -89,7 +79,6 @@ impl quickcheck::Arbitrary for PeerId {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
impl quickcheck::Arbitrary for ResponsePeer {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
|
|
@ -98,4 +87,4 @@ impl quickcheck::Arbitrary for ResponsePeer {
|
|||
port: Port(u16::arbitrary(g)),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,4 +4,4 @@ pub mod response;
|
|||
|
||||
pub use self::common::*;
|
||||
pub use self::request::*;
|
||||
pub use self::response::*;
|
||||
pub use self::response::*;
|
||||
|
|
|
|||
|
|
@ -2,23 +2,20 @@ use std::convert::TryInto;
|
|||
use std::io::{self, Cursor, Read, Write};
|
||||
use std::net::Ipv4Addr;
|
||||
|
||||
use byteorder::{ReadBytesExt, WriteBytesExt, NetworkEndian};
|
||||
use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt};
|
||||
|
||||
use super::common::*;
|
||||
|
||||
|
||||
const PROTOCOL_IDENTIFIER: i64 = 4_497_486_125_440;
|
||||
|
||||
|
||||
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
|
||||
pub enum AnnounceEvent {
|
||||
Started,
|
||||
Stopped,
|
||||
Completed,
|
||||
None
|
||||
None,
|
||||
}
|
||||
|
||||
|
||||
impl AnnounceEvent {
|
||||
#[inline]
|
||||
pub fn from_i32(i: i32) -> Self {
|
||||
|
|
@ -26,7 +23,7 @@ impl AnnounceEvent {
|
|||
1 => Self::Completed,
|
||||
2 => Self::Started,
|
||||
3 => Self::Stopped,
|
||||
_ => Self::None
|
||||
_ => Self::None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -36,18 +33,16 @@ impl AnnounceEvent {
|
|||
AnnounceEvent::None => 0,
|
||||
AnnounceEvent::Completed => 1,
|
||||
AnnounceEvent::Started => 2,
|
||||
AnnounceEvent::Stopped => 3
|
||||
AnnounceEvent::Stopped => 3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ConnectRequest {
|
||||
pub transaction_id: TransactionId
|
||||
pub transaction_id: TransactionId,
|
||||
}
|
||||
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct AnnounceRequest {
|
||||
pub connection_id: ConnectionId,
|
||||
|
|
@ -58,21 +53,19 @@ pub struct AnnounceRequest {
|
|||
pub bytes_uploaded: NumberOfBytes,
|
||||
pub bytes_left: NumberOfBytes,
|
||||
pub event: AnnounceEvent,
|
||||
pub ip_address: Option<Ipv4Addr>,
|
||||
pub ip_address: Option<Ipv4Addr>,
|
||||
pub key: PeerKey,
|
||||
pub peers_wanted: NumberOfPeers,
|
||||
pub port: Port
|
||||
pub port: Port,
|
||||
}
|
||||
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ScrapeRequest {
|
||||
pub connection_id: ConnectionId,
|
||||
pub transaction_id: TransactionId,
|
||||
pub info_hashes: Vec<InfoHash>
|
||||
pub info_hashes: Vec<InfoHash>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RequestParseError {
|
||||
pub transaction_id: Option<TransactionId>,
|
||||
|
|
@ -80,20 +73,19 @@ pub struct RequestParseError {
|
|||
pub error: Option<io::Error>,
|
||||
}
|
||||
|
||||
|
||||
impl RequestParseError {
|
||||
pub fn new(err: io::Error, transaction_id: i32) -> Self {
|
||||
Self {
|
||||
transaction_id: Some(TransactionId(transaction_id)),
|
||||
message: None,
|
||||
error: Some(err)
|
||||
error: Some(err),
|
||||
}
|
||||
}
|
||||
pub fn io(err: io::Error) -> Self {
|
||||
Self {
|
||||
transaction_id: None,
|
||||
message: None,
|
||||
error: Some(err)
|
||||
error: Some(err),
|
||||
}
|
||||
}
|
||||
pub fn text(transaction_id: i32, message: &str) -> Self {
|
||||
|
|
@ -105,7 +97,6 @@ impl RequestParseError {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub enum Request {
|
||||
Connect(ConnectRequest),
|
||||
|
|
@ -113,28 +104,24 @@ pub enum Request {
|
|||
Scrape(ScrapeRequest),
|
||||
}
|
||||
|
||||
|
||||
impl From<ConnectRequest> for Request {
|
||||
fn from(r: ConnectRequest) -> Self {
|
||||
Self::Connect(r)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl From<AnnounceRequest> for Request {
|
||||
fn from(r: AnnounceRequest) -> Self {
|
||||
Self::Announce(r)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl From<ScrapeRequest> for Request {
|
||||
fn from(r: ScrapeRequest) -> Self {
|
||||
Self::Scrape(r)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Request {
|
||||
pub fn write(self, bytes: &mut impl Write) -> Result<(), io::Error> {
|
||||
match self {
|
||||
|
|
@ -142,7 +129,7 @@ impl Request {
|
|||
bytes.write_i64::<NetworkEndian>(PROTOCOL_IDENTIFIER)?;
|
||||
bytes.write_i32::<NetworkEndian>(0)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.transaction_id.0)?;
|
||||
},
|
||||
}
|
||||
|
||||
Request::Announce(r) => {
|
||||
bytes.write_i64::<NetworkEndian>(r.connection_id.0)?;
|
||||
|
|
@ -158,15 +145,12 @@ impl Request {
|
|||
|
||||
bytes.write_i32::<NetworkEndian>(r.event.to_i32())?;
|
||||
|
||||
bytes.write_all(&r.ip_address.map_or(
|
||||
[0; 4],
|
||||
|ip| ip.octets()
|
||||
))?;
|
||||
bytes.write_all(&r.ip_address.map_or([0; 4], |ip| ip.octets()))?;
|
||||
|
||||
bytes.write_u32::<NetworkEndian>(r.key.0)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.peers_wanted.0)?;
|
||||
bytes.write_u16::<NetworkEndian>(r.port.0)?;
|
||||
},
|
||||
}
|
||||
|
||||
Request::Scrape(r) => {
|
||||
bytes.write_i64::<NetworkEndian>(r.connection_id.0)?;
|
||||
|
|
@ -182,17 +166,17 @@ impl Request {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn from_bytes(
|
||||
bytes: &[u8],
|
||||
max_scrape_torrents: u8,
|
||||
) -> Result<Self, RequestParseError> {
|
||||
pub fn from_bytes(bytes: &[u8], max_scrape_torrents: u8) -> Result<Self, RequestParseError> {
|
||||
let mut cursor = Cursor::new(bytes);
|
||||
|
||||
let connection_id = cursor.read_i64::<NetworkEndian>()
|
||||
let connection_id = cursor
|
||||
.read_i64::<NetworkEndian>()
|
||||
.map_err(RequestParseError::io)?;
|
||||
let action = cursor.read_i32::<NetworkEndian>()
|
||||
let action = cursor
|
||||
.read_i32::<NetworkEndian>()
|
||||
.map_err(RequestParseError::io)?;
|
||||
let transaction_id = cursor.read_i32::<NetworkEndian>()
|
||||
let transaction_id = cursor
|
||||
.read_i32::<NetworkEndian>()
|
||||
.map_err(RequestParseError::io)?;
|
||||
|
||||
match action {
|
||||
|
|
@ -200,15 +184,16 @@ impl Request {
|
|||
0 => {
|
||||
if connection_id == PROTOCOL_IDENTIFIER {
|
||||
Ok((ConnectRequest {
|
||||
transaction_id: TransactionId(transaction_id)
|
||||
}).into())
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
})
|
||||
.into())
|
||||
} else {
|
||||
Err(RequestParseError::text(
|
||||
transaction_id,
|
||||
"Protocol identifier missing"
|
||||
"Protocol identifier missing",
|
||||
))
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// Announce
|
||||
1 => {
|
||||
|
|
@ -216,28 +201,38 @@ impl Request {
|
|||
let mut peer_id = [0; 20];
|
||||
let mut ip = [0; 4];
|
||||
|
||||
cursor.read_exact(&mut info_hash)
|
||||
cursor
|
||||
.read_exact(&mut info_hash)
|
||||
.map_err(|err| RequestParseError::new(err, transaction_id))?;
|
||||
cursor.read_exact(&mut peer_id)
|
||||
cursor
|
||||
.read_exact(&mut peer_id)
|
||||
.map_err(|err| RequestParseError::new(err, transaction_id))?;
|
||||
|
||||
let bytes_downloaded = cursor.read_i64::<NetworkEndian>()
|
||||
let bytes_downloaded = cursor
|
||||
.read_i64::<NetworkEndian>()
|
||||
.map_err(|err| RequestParseError::new(err, transaction_id))?;
|
||||
let bytes_left = cursor.read_i64::<NetworkEndian>()
|
||||
let bytes_left = cursor
|
||||
.read_i64::<NetworkEndian>()
|
||||
.map_err(|err| RequestParseError::new(err, transaction_id))?;
|
||||
let bytes_uploaded = cursor.read_i64::<NetworkEndian>()
|
||||
let bytes_uploaded = cursor
|
||||
.read_i64::<NetworkEndian>()
|
||||
.map_err(|err| RequestParseError::new(err, transaction_id))?;
|
||||
let event = cursor.read_i32::<NetworkEndian>()
|
||||
let event = cursor
|
||||
.read_i32::<NetworkEndian>()
|
||||
.map_err(|err| RequestParseError::new(err, transaction_id))?;
|
||||
|
||||
cursor.read_exact(&mut ip)
|
||||
cursor
|
||||
.read_exact(&mut ip)
|
||||
.map_err(|err| RequestParseError::new(err, transaction_id))?;
|
||||
|
||||
let key = cursor.read_u32::<NetworkEndian>()
|
||||
let key = cursor
|
||||
.read_u32::<NetworkEndian>()
|
||||
.map_err(|err| RequestParseError::new(err, transaction_id))?;
|
||||
let peers_wanted = cursor.read_i32::<NetworkEndian>()
|
||||
let peers_wanted = cursor
|
||||
.read_i32::<NetworkEndian>()
|
||||
.map_err(|err| RequestParseError::new(err, transaction_id))?;
|
||||
let port = cursor.read_u16::<NetworkEndian>()
|
||||
let port = cursor
|
||||
.read_u16::<NetworkEndian>()
|
||||
.map_err(|err| RequestParseError::new(err, transaction_id))?;
|
||||
|
||||
let opt_ip = if ip == [0; 4] {
|
||||
|
|
@ -258,16 +253,18 @@ impl Request {
|
|||
ip_address: opt_ip,
|
||||
key: PeerKey(key),
|
||||
peers_wanted: NumberOfPeers(peers_wanted),
|
||||
port: Port(port)
|
||||
}).into())
|
||||
},
|
||||
port: Port(port),
|
||||
})
|
||||
.into())
|
||||
}
|
||||
|
||||
// Scrape
|
||||
2 => {
|
||||
let position = cursor.position() as usize;
|
||||
let inner = cursor.into_inner();
|
||||
|
||||
let info_hashes = (&inner[position..]).chunks_exact(20)
|
||||
let info_hashes = (&inner[position..])
|
||||
.chunks_exact(20)
|
||||
.take(max_scrape_torrents as usize)
|
||||
.map(|chunk| InfoHash(chunk.try_into().unwrap()))
|
||||
.collect();
|
||||
|
|
@ -275,16 +272,16 @@ impl Request {
|
|||
Ok((ScrapeRequest {
|
||||
connection_id: ConnectionId(connection_id),
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
info_hashes
|
||||
}).into())
|
||||
info_hashes,
|
||||
})
|
||||
.into())
|
||||
}
|
||||
|
||||
_ => Err(RequestParseError::text(transaction_id, "Invalid action"))
|
||||
_ => Err(RequestParseError::text(transaction_id, "Invalid action")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use quickcheck_macros::quickcheck;
|
||||
|
|
@ -293,7 +290,7 @@ mod tests {
|
|||
|
||||
impl quickcheck::Arbitrary for AnnounceEvent {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
match (bool::arbitrary(g), bool::arbitrary(g)){
|
||||
match (bool::arbitrary(g), bool::arbitrary(g)) {
|
||||
(false, false) => Self::Started,
|
||||
(true, false) => Self::Started,
|
||||
(false, true) => Self::Completed,
|
||||
|
|
@ -321,19 +318,19 @@ mod tests {
|
|||
bytes_uploaded: NumberOfBytes(i64::arbitrary(g)),
|
||||
bytes_left: NumberOfBytes(i64::arbitrary(g)),
|
||||
event: AnnounceEvent::arbitrary(g),
|
||||
ip_address: None,
|
||||
ip_address: None,
|
||||
key: PeerKey(u32::arbitrary(g)),
|
||||
peers_wanted: NumberOfPeers(i32::arbitrary(g)),
|
||||
port: Port(u16::arbitrary(g))
|
||||
port: Port(u16::arbitrary(g)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl quickcheck::Arbitrary for ScrapeRequest {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
let info_hashes = (0..u8::arbitrary(g)).map(|_| {
|
||||
InfoHash::arbitrary(g)
|
||||
}).collect();
|
||||
let info_hashes = (0..u8::arbitrary(g))
|
||||
.map(|_| InfoHash::arbitrary(g))
|
||||
.collect();
|
||||
|
||||
Self {
|
||||
connection_id: ConnectionId(i64::arbitrary(g)),
|
||||
|
|
@ -359,23 +356,17 @@ mod tests {
|
|||
}
|
||||
|
||||
#[quickcheck]
|
||||
fn test_connect_request_convert_identity(
|
||||
request: ConnectRequest
|
||||
) -> bool {
|
||||
fn test_connect_request_convert_identity(request: ConnectRequest) -> bool {
|
||||
same_after_conversion(request.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[quickcheck]
|
||||
fn test_announce_request_convert_identity(
|
||||
request: AnnounceRequest
|
||||
) -> bool {
|
||||
fn test_announce_request_convert_identity(request: AnnounceRequest) -> bool {
|
||||
same_after_conversion(request.into())
|
||||
}
|
||||
}
|
||||
|
||||
#[quickcheck]
|
||||
fn test_scrape_request_convert_identity(
|
||||
request: ScrapeRequest
|
||||
) -> bool {
|
||||
fn test_scrape_request_convert_identity(request: ScrapeRequest) -> bool {
|
||||
same_after_conversion(request.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,51 +1,45 @@
|
|||
use std::convert::TryInto;
|
||||
use std::io::{self, Cursor, Write};
|
||||
use std::net::{IpAddr, Ipv6Addr, Ipv4Addr};
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
|
||||
|
||||
use byteorder::{ReadBytesExt, WriteBytesExt, NetworkEndian};
|
||||
use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt};
|
||||
|
||||
use super::common::*;
|
||||
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
|
||||
pub struct TorrentScrapeStatistics {
|
||||
pub seeders: NumberOfPeers,
|
||||
pub completed: NumberOfDownloads,
|
||||
pub leechers: NumberOfPeers
|
||||
pub leechers: NumberOfPeers,
|
||||
}
|
||||
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ConnectResponse {
|
||||
pub connection_id: ConnectionId,
|
||||
pub transaction_id: TransactionId
|
||||
pub transaction_id: TransactionId,
|
||||
}
|
||||
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct AnnounceResponse {
|
||||
pub transaction_id: TransactionId,
|
||||
pub announce_interval: AnnounceInterval,
|
||||
pub leechers: NumberOfPeers,
|
||||
pub seeders: NumberOfPeers,
|
||||
pub peers: Vec<ResponsePeer>
|
||||
pub peers: Vec<ResponsePeer>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ScrapeResponse {
|
||||
pub transaction_id: TransactionId,
|
||||
pub torrent_stats: Vec<TorrentScrapeStatistics>
|
||||
pub torrent_stats: Vec<TorrentScrapeStatistics>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ErrorResponse {
|
||||
pub transaction_id: TransactionId,
|
||||
pub message: String
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub enum Response {
|
||||
Connect(ConnectResponse),
|
||||
|
|
@ -54,35 +48,30 @@ pub enum Response {
|
|||
Error(ErrorResponse),
|
||||
}
|
||||
|
||||
|
||||
impl From<ConnectResponse> for Response {
|
||||
fn from(r: ConnectResponse) -> Self {
|
||||
Self::Connect(r)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl From<AnnounceResponse> for Response {
|
||||
fn from(r: AnnounceResponse) -> Self {
|
||||
Self::Announce(r)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl From<ScrapeResponse> for Response {
|
||||
fn from(r: ScrapeResponse) -> Self {
|
||||
Self::Scrape(r)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl From<ErrorResponse> for Response {
|
||||
fn from(r: ErrorResponse) -> Self {
|
||||
Self::Error(r)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Response {
|
||||
/// Returning IPv6 peers doesn't really work with UDP. It is not supported
|
||||
/// by https://libtorrent.org/udp_tracker_protocol.html. There is a
|
||||
|
|
@ -91,17 +80,13 @@ impl Response {
|
|||
/// addresses. Clients seem not to support it very well, but due to a lack
|
||||
/// of alternative solutions, it is implemented here.
|
||||
#[inline]
|
||||
pub fn write(
|
||||
self,
|
||||
bytes: &mut impl Write,
|
||||
ip_version: IpVersion
|
||||
) -> Result<(), io::Error> {
|
||||
pub fn write(self, bytes: &mut impl Write, ip_version: IpVersion) -> Result<(), io::Error> {
|
||||
match self {
|
||||
Response::Connect(r) => {
|
||||
bytes.write_i32::<NetworkEndian>(0)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.transaction_id.0)?;
|
||||
bytes.write_i64::<NetworkEndian>(r.connection_id.0)?;
|
||||
},
|
||||
}
|
||||
Response::Announce(r) => {
|
||||
if ip_version == IpVersion::IPv4 {
|
||||
bytes.write_i32::<NetworkEndian>(1)?;
|
||||
|
|
@ -132,7 +117,7 @@ impl Response {
|
|||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
Response::Scrape(r) => {
|
||||
bytes.write_i32::<NetworkEndian>(2)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.transaction_id.0)?;
|
||||
|
|
@ -142,13 +127,13 @@ impl Response {
|
|||
bytes.write_i32::<NetworkEndian>(torrent_stat.completed.0)?;
|
||||
bytes.write_i32::<NetworkEndian>(torrent_stat.leechers.0)?;
|
||||
}
|
||||
},
|
||||
}
|
||||
Response::Error(r) => {
|
||||
bytes.write_i32::<NetworkEndian>(3)?;
|
||||
bytes.write_i32::<NetworkEndian>(r.transaction_id.0)?;
|
||||
|
||||
bytes.write_all(r.message.as_bytes())?;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
@ -168,9 +153,10 @@ impl Response {
|
|||
|
||||
Ok((ConnectResponse {
|
||||
connection_id: ConnectionId(connection_id),
|
||||
transaction_id: TransactionId(transaction_id)
|
||||
}).into())
|
||||
},
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
})
|
||||
.into())
|
||||
}
|
||||
// Announce
|
||||
1 => {
|
||||
let announce_interval = cursor.read_i32::<NetworkEndian>()?;
|
||||
|
|
@ -180,49 +166,57 @@ impl Response {
|
|||
let position = cursor.position() as usize;
|
||||
let inner = cursor.into_inner();
|
||||
|
||||
let peers = inner[position..].chunks_exact(6).map(|chunk| {
|
||||
let ip_bytes: [u8; 4] = (&chunk[..4]).try_into().unwrap();
|
||||
let ip_address = IpAddr::V4(Ipv4Addr::from(ip_bytes));
|
||||
let port = (&chunk[4..]).read_u16::<NetworkEndian>().unwrap();
|
||||
let peers = inner[position..]
|
||||
.chunks_exact(6)
|
||||
.map(|chunk| {
|
||||
let ip_bytes: [u8; 4] = (&chunk[..4]).try_into().unwrap();
|
||||
let ip_address = IpAddr::V4(Ipv4Addr::from(ip_bytes));
|
||||
let port = (&chunk[4..]).read_u16::<NetworkEndian>().unwrap();
|
||||
|
||||
ResponsePeer {
|
||||
ip_address,
|
||||
port: Port(port),
|
||||
}
|
||||
}).collect();
|
||||
ResponsePeer {
|
||||
ip_address,
|
||||
port: Port(port),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok((AnnounceResponse {
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
announce_interval: AnnounceInterval(announce_interval),
|
||||
leechers: NumberOfPeers(leechers),
|
||||
seeders: NumberOfPeers(seeders),
|
||||
peers
|
||||
}).into())
|
||||
},
|
||||
peers,
|
||||
})
|
||||
.into())
|
||||
}
|
||||
// Scrape
|
||||
2 => {
|
||||
let position = cursor.position() as usize;
|
||||
let inner = cursor.into_inner();
|
||||
|
||||
let stats = inner[position..].chunks_exact(12).map(|chunk| {
|
||||
let mut cursor: Cursor<&[u8]> = Cursor::new(&chunk[..]);
|
||||
let stats = inner[position..]
|
||||
.chunks_exact(12)
|
||||
.map(|chunk| {
|
||||
let mut cursor: Cursor<&[u8]> = Cursor::new(&chunk[..]);
|
||||
|
||||
let seeders = cursor.read_i32::<NetworkEndian>().unwrap();
|
||||
let downloads = cursor.read_i32::<NetworkEndian>().unwrap();
|
||||
let leechers = cursor.read_i32::<NetworkEndian>().unwrap();
|
||||
let seeders = cursor.read_i32::<NetworkEndian>().unwrap();
|
||||
let downloads = cursor.read_i32::<NetworkEndian>().unwrap();
|
||||
let leechers = cursor.read_i32::<NetworkEndian>().unwrap();
|
||||
|
||||
TorrentScrapeStatistics {
|
||||
seeders: NumberOfPeers(seeders),
|
||||
completed: NumberOfDownloads(downloads),
|
||||
leechers:NumberOfPeers(leechers)
|
||||
}
|
||||
}).collect();
|
||||
TorrentScrapeStatistics {
|
||||
seeders: NumberOfPeers(seeders),
|
||||
completed: NumberOfDownloads(downloads),
|
||||
leechers: NumberOfPeers(leechers),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok((ScrapeResponse {
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
torrent_stats: stats
|
||||
}).into())
|
||||
},
|
||||
torrent_stats: stats,
|
||||
})
|
||||
.into())
|
||||
}
|
||||
// Error
|
||||
3 => {
|
||||
let position = cursor.position() as usize;
|
||||
|
|
@ -230,9 +224,10 @@ impl Response {
|
|||
|
||||
Ok((ErrorResponse {
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
message: String::from_utf8_lossy(&inner[position..]).into()
|
||||
}).into())
|
||||
},
|
||||
message: String::from_utf8_lossy(&inner[position..]).into(),
|
||||
})
|
||||
.into())
|
||||
}
|
||||
// IPv6 announce
|
||||
4 => {
|
||||
let announce_interval = cursor.read_i32::<NetworkEndian>()?;
|
||||
|
|
@ -242,36 +237,38 @@ impl Response {
|
|||
let position = cursor.position() as usize;
|
||||
let inner = cursor.into_inner();
|
||||
|
||||
let peers = inner[position..].chunks_exact(18).map(|chunk| {
|
||||
let ip_bytes: [u8; 16] = (&chunk[..16]).try_into().unwrap();
|
||||
let ip_address = IpAddr::V6(Ipv6Addr::from(ip_bytes));
|
||||
let port = (&chunk[16..]).read_u16::<NetworkEndian>().unwrap();
|
||||
let peers = inner[position..]
|
||||
.chunks_exact(18)
|
||||
.map(|chunk| {
|
||||
let ip_bytes: [u8; 16] = (&chunk[..16]).try_into().unwrap();
|
||||
let ip_address = IpAddr::V6(Ipv6Addr::from(ip_bytes));
|
||||
let port = (&chunk[16..]).read_u16::<NetworkEndian>().unwrap();
|
||||
|
||||
ResponsePeer {
|
||||
ip_address,
|
||||
port: Port(port),
|
||||
}
|
||||
}).collect();
|
||||
ResponsePeer {
|
||||
ip_address,
|
||||
port: Port(port),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok((AnnounceResponse {
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
announce_interval: AnnounceInterval(announce_interval),
|
||||
leechers: NumberOfPeers(leechers),
|
||||
seeders: NumberOfPeers(seeders),
|
||||
peers
|
||||
}).into())
|
||||
},
|
||||
_ => {
|
||||
Ok((ErrorResponse {
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
message: "Invalid action".to_string()
|
||||
}).into())
|
||||
peers,
|
||||
})
|
||||
.into())
|
||||
}
|
||||
_ => Ok((ErrorResponse {
|
||||
transaction_id: TransactionId(transaction_id),
|
||||
message: "Invalid action".to_string(),
|
||||
})
|
||||
.into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use quickcheck_macros::quickcheck;
|
||||
|
|
@ -287,7 +284,7 @@ mod tests {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl quickcheck::Arbitrary for ConnectResponse {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
Self {
|
||||
|
|
@ -296,13 +293,13 @@ mod tests {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl quickcheck::Arbitrary for AnnounceResponse {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
let peers = (0..u8::arbitrary(g)).map(|_| {
|
||||
ResponsePeer::arbitrary(g)
|
||||
}).collect();
|
||||
|
||||
let peers = (0..u8::arbitrary(g))
|
||||
.map(|_| ResponsePeer::arbitrary(g))
|
||||
.collect();
|
||||
|
||||
Self {
|
||||
transaction_id: TransactionId(i32::arbitrary(g)),
|
||||
announce_interval: AnnounceInterval(i32::arbitrary(g)),
|
||||
|
|
@ -315,9 +312,9 @@ mod tests {
|
|||
|
||||
impl quickcheck::Arbitrary for ScrapeResponse {
|
||||
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
|
||||
let torrent_stats = (0..u8::arbitrary(g)).map(|_| {
|
||||
TorrentScrapeStatistics::arbitrary(g)
|
||||
}).collect();
|
||||
let torrent_stats = (0..u8::arbitrary(g))
|
||||
.map(|_| TorrentScrapeStatistics::arbitrary(g))
|
||||
.collect();
|
||||
|
||||
Self {
|
||||
transaction_id: TransactionId(i32::arbitrary(g)),
|
||||
|
|
@ -326,10 +323,7 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
fn same_after_conversion(
|
||||
response: Response,
|
||||
ip_version: IpVersion
|
||||
) -> bool {
|
||||
fn same_after_conversion(response: Response, ip_version: IpVersion) -> bool {
|
||||
let mut buf = Vec::new();
|
||||
|
||||
response.clone().write(&mut buf, ip_version).unwrap();
|
||||
|
|
@ -345,16 +339,12 @@ mod tests {
|
|||
}
|
||||
|
||||
#[quickcheck]
|
||||
fn test_connect_response_convert_identity(
|
||||
response: ConnectResponse
|
||||
) -> bool {
|
||||
fn test_connect_response_convert_identity(response: ConnectResponse) -> bool {
|
||||
same_after_conversion(response.into(), IpVersion::IPv4)
|
||||
}
|
||||
}
|
||||
|
||||
#[quickcheck]
|
||||
fn test_announce_response_convert_identity(
|
||||
data: (AnnounceResponse, IpVersion)
|
||||
) -> bool {
|
||||
fn test_announce_response_convert_identity(data: (AnnounceResponse, IpVersion)) -> bool {
|
||||
let mut r = data.0;
|
||||
|
||||
if data.1 == IpVersion::IPv4 {
|
||||
|
|
@ -364,12 +354,10 @@ mod tests {
|
|||
}
|
||||
|
||||
same_after_conversion(r.into(), data.1)
|
||||
}
|
||||
}
|
||||
|
||||
#[quickcheck]
|
||||
fn test_scrape_response_convert_identity(
|
||||
response: ScrapeResponse
|
||||
) -> bool {
|
||||
fn test_scrape_response_convert_identity(response: ScrapeResponse) -> bool {
|
||||
same_after_conversion(response.into(), IpVersion::IPv4)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue