Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 63 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ version = "3.0.0-alpha.12-develop"

[dependencies]
anyhow = "1"
aquatic_udp_protocol = "0.8"
aquatic_udp_protocol = "0"
async-trait = "0"
axum = { version = "0", features = ["macros"] }
axum-client-ip = "0"
Expand Down Expand Up @@ -76,6 +76,7 @@ trace = "0"
tracing = "0"
url = "2"
uuid = { version = "1", features = ["v4"] }
zerocopy = "0.7.33"

[package.metadata.cargo-machete]
ignored = ["serde_bytes", "crossbeam-skiplist", "dashmap", "parking_lot"]
Expand Down
3 changes: 2 additions & 1 deletion cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@
"Xtorrent",
"Xunlei",
"xxxxxxxxxxxxxxxxxxxxd",
"yyyyyyyyyyyyyyyyyyyyd"
"yyyyyyyyyyyyyyyyyyyyd",
"zerocopy"
],
"enableFiletypes": [
"dockerfile",
Expand Down
4 changes: 2 additions & 2 deletions src/console/clients/checker/checks/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub async fn run(udp_trackers: &Vec<SocketAddr>, check_results: &mut Vec<CheckRe

debug!("UDP tracker: {:?}", udp_tracker);

let transaction_id = TransactionId(RANDOM_TRANSACTION_ID);
let transaction_id = TransactionId::new(RANDOM_TRANSACTION_ID);

let mut client = checker::Client::default();

Expand Down Expand Up @@ -58,7 +58,7 @@ pub async fn run(udp_trackers: &Vec<SocketAddr>, check_results: &mut Vec<CheckRe
debug!("Send announce request");

if (client
.send_announce_request(connection_id, transaction_id, info_hash, Port(bound_to.port()))
.send_announce_request(connection_id, transaction_id, info_hash, Port(bound_to.port().into()))
.await)
.is_ok()
{
Expand Down
6 changes: 3 additions & 3 deletions src/console/clients/udp/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn setup_logging(level: LevelFilter) {
}

async fn handle_announce(tracker_socket_addr: &SocketAddr, info_hash: &TorrustInfoHash) -> anyhow::Result<Response> {
let transaction_id = TransactionId(RANDOM_TRANSACTION_ID);
let transaction_id = TransactionId::new(RANDOM_TRANSACTION_ID);

let mut client = checker::Client::default();

Expand All @@ -151,12 +151,12 @@ async fn handle_announce(tracker_socket_addr: &SocketAddr, info_hash: &TorrustIn
let connection_id = client.send_connection_request(transaction_id).await?;

client
.send_announce_request(connection_id, transaction_id, *info_hash, Port(bound_to.port()))
.send_announce_request(connection_id, transaction_id, *info_hash, Port(bound_to.port().into()))
.await
}

async fn handle_scrape(tracker_socket_addr: &SocketAddr, info_hashes: &[TorrustInfoHash]) -> anyhow::Result<Response> {
let transaction_id = TransactionId(RANDOM_TRANSACTION_ID);
let transaction_id = TransactionId::new(RANDOM_TRANSACTION_ID);

let mut client = checker::Client::default();

Expand Down
19 changes: 10 additions & 9 deletions src/console/clients/udp/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::net::{Ipv4Addr, SocketAddr};
use anyhow::Context;
use aquatic_udp_protocol::common::InfoHash;
use aquatic_udp_protocol::{
AnnounceEvent, AnnounceRequest, ConnectRequest, ConnectionId, NumberOfBytes, NumberOfPeers, PeerId, PeerKey, Port, Response,
ScrapeRequest, TransactionId,
AnnounceActionPlaceholder, AnnounceEvent, AnnounceRequest, ConnectRequest, ConnectionId, NumberOfBytes, NumberOfPeers,
PeerId, PeerKey, Port, Response, ScrapeRequest, TransactionId,
};
use log::debug;
use thiserror::Error;
Expand Down Expand Up @@ -148,16 +148,17 @@ impl Client {

let announce_request = AnnounceRequest {
connection_id,
action_placeholder: AnnounceActionPlaceholder::default(),
transaction_id,
info_hash: InfoHash(info_hash.bytes()),
peer_id: PeerId(*b"-qB00000000000000001"),
bytes_downloaded: NumberOfBytes(0i64),
bytes_uploaded: NumberOfBytes(0i64),
bytes_left: NumberOfBytes(0i64),
event: AnnounceEvent::Started,
ip_address: Some(Ipv4Addr::new(0, 0, 0, 0)),
key: PeerKey(0u32),
peers_wanted: NumberOfPeers(1i32),
bytes_downloaded: NumberOfBytes(0i64.into()),
bytes_uploaded: NumberOfBytes(0i64.into()),
bytes_left: NumberOfBytes(0i64.into()),
event: AnnounceEvent::Started.into(),
ip_address: Ipv4Addr::new(0, 0, 0, 0).into(),
key: PeerKey::new(0i32),
peers_wanted: NumberOfPeers(1i32.into()),
port: client_port,
};

Expand Down
38 changes: 19 additions & 19 deletions src/console/clients/udp/responses.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Aquatic responses are not serializable. These are the serializable wrappers.
use std::net::{Ipv4Addr, Ipv6Addr};

use aquatic_udp_protocol::{AnnounceResponse, ScrapeResponse};
use aquatic_udp_protocol::{AnnounceResponse, Ipv4AddrBytes, Ipv6AddrBytes, ScrapeResponse};
use serde::Serialize;

#[derive(Serialize)]
Expand All @@ -13,33 +13,33 @@ pub struct AnnounceResponseDto {
peers: Vec<String>,
}

impl From<AnnounceResponse<Ipv4Addr>> for AnnounceResponseDto {
fn from(announce: AnnounceResponse<Ipv4Addr>) -> Self {
impl From<AnnounceResponse<Ipv4AddrBytes>> for AnnounceResponseDto {
fn from(announce: AnnounceResponse<Ipv4AddrBytes>) -> Self {
Self {
transaction_id: announce.transaction_id.0,
announce_interval: announce.announce_interval.0,
leechers: announce.leechers.0,
seeders: announce.seeders.0,
transaction_id: announce.fixed.transaction_id.0.into(),
announce_interval: announce.fixed.announce_interval.0.into(),
leechers: announce.fixed.leechers.0.into(),
seeders: announce.fixed.seeders.0.into(),
peers: announce
.peers
.iter()
.map(|peer| format!("{}:{}", peer.ip_address, peer.port.0))
.map(|peer| format!("{}:{}", Ipv4Addr::from(peer.ip_address), peer.port.0))
.collect::<Vec<_>>(),
}
}
}

impl From<AnnounceResponse<Ipv6Addr>> for AnnounceResponseDto {
fn from(announce: AnnounceResponse<Ipv6Addr>) -> Self {
impl From<AnnounceResponse<Ipv6AddrBytes>> for AnnounceResponseDto {
fn from(announce: AnnounceResponse<Ipv6AddrBytes>) -> Self {
Self {
transaction_id: announce.transaction_id.0,
announce_interval: announce.announce_interval.0,
leechers: announce.leechers.0,
seeders: announce.seeders.0,
transaction_id: announce.fixed.transaction_id.0.into(),
announce_interval: announce.fixed.announce_interval.0.into(),
leechers: announce.fixed.leechers.0.into(),
seeders: announce.fixed.seeders.0.into(),
peers: announce
.peers
.iter()
.map(|peer| format!("{}:{}", peer.ip_address, peer.port.0))
.map(|peer| format!("{}:{}", Ipv6Addr::from(peer.ip_address), peer.port.0))
.collect::<Vec<_>>(),
}
}
Expand All @@ -54,14 +54,14 @@ pub struct ScrapeResponseDto {
impl From<ScrapeResponse> for ScrapeResponseDto {
fn from(scrape: ScrapeResponse) -> Self {
Self {
transaction_id: scrape.transaction_id.0,
transaction_id: scrape.transaction_id.0.into(),
torrent_stats: scrape
.torrent_stats
.iter()
.map(|torrent_scrape_statistics| TorrentStats {
seeders: torrent_scrape_statistics.seeders.0,
completed: torrent_scrape_statistics.completed.0,
leechers: torrent_scrape_statistics.leechers.0,
seeders: torrent_scrape_statistics.seeders.0.into(),
completed: torrent_scrape_statistics.completed.0.into(),
leechers: torrent_scrape_statistics.leechers.0.into(),
})
.collect::<Vec<_>>(),
}
Expand Down
8 changes: 6 additions & 2 deletions src/servers/udp/connection_cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ use std::panic::Location;

use aquatic_udp_protocol::ConnectionId;
use torrust_tracker_clock::time_extent::{Extent, TimeExtent};
use zerocopy::network_endian::I64;
use zerocopy::AsBytes;

use super::error::Error;

Expand All @@ -83,13 +85,15 @@ pub const COOKIE_LIFETIME: TimeExtent = TimeExtent::from_sec(2, &60);
/// Converts a connection ID into a connection cookie.
#[must_use]
pub fn from_connection_id(connection_id: &ConnectionId) -> Cookie {
connection_id.0.to_le_bytes()
let mut cookie = [0u8; 8];
connection_id.write_to(&mut cookie);
cookie
}

/// Converts a connection cookie into a connection ID.
#[must_use]
pub fn into_connection_id(connection_cookie: &Cookie) -> ConnectionId {
ConnectionId(i64::from_le_bytes(*connection_cookie))
ConnectionId(I64::new(i64::from_be_bytes(*connection_cookie)))
}

/// Generates a new connection cookie.
Expand Down
Loading