forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.rs
214 lines (180 loc) · 7.22 KB
/
checker.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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,
};
use log::debug;
use thiserror::Error;
use torrust_tracker_primitives::info_hash::InfoHash as TorrustInfoHash;
use crate::shared::bit_torrent::tracker::udp::client::{UdpClient, UdpTrackerClient};
#[derive(Error, Debug)]
pub enum ClientError {
#[error("Local socket address is not bound yet. Try binding before connecting.")]
NotBound,
#[error("Not connected to remote tracker UDP socket. Try connecting before making requests.")]
NotConnected,
#[error("Unexpected response while connecting the the remote server.")]
UnexpectedConnectionResponse,
}
/// A UDP Tracker client to make test requests (checks).
#[derive(Debug, Default)]
pub struct Client {
/// Local UDP socket. It could be 0 to assign a free port.
local_binding_address: Option<SocketAddr>,
/// Local UDP socket after binding. It's equals to binding address if a
/// non- zero port was used.
local_bound_address: Option<SocketAddr>,
/// Remote UDP tracker socket
remote_socket: Option<SocketAddr>,
/// The client used to make UDP requests to the tracker.
udp_tracker_client: Option<UdpTrackerClient>,
}
impl Client {
/// Binds to the local socket and connects to the remote one.
///
/// # Errors
///
/// Will return an error if
///
/// - It can't bound to the local socket address.
/// - It can't make a connection request successfully to the remote UDP server.
pub async fn bind_and_connect(&mut self, local_port: u16, remote_socket_addr: &SocketAddr) -> anyhow::Result<SocketAddr> {
let bound_to = self.bind(local_port).await?;
self.connect(remote_socket_addr).await?;
Ok(bound_to)
}
/// Binds local client socket.
///
/// # Errors
///
/// Will return an error if it can't bound to the local address.
async fn bind(&mut self, local_port: u16) -> anyhow::Result<SocketAddr> {
let local_bind_to = format!("0.0.0.0:{local_port}");
let binding_address = local_bind_to.parse().context("binding local address")?;
debug!("Binding to: {local_bind_to}");
let udp_client = UdpClient::bind(&local_bind_to).await?;
let bound_to = udp_client.socket.local_addr().context("bound local address")?;
debug!("Bound to: {bound_to}");
self.local_binding_address = Some(binding_address);
self.local_bound_address = Some(bound_to);
self.udp_tracker_client = Some(UdpTrackerClient { udp_client });
Ok(bound_to)
}
/// Connects to the remote server socket.
///
/// # Errors
///
/// Will return and error if it can't make a connection request successfully
/// to the remote UDP server.
async fn connect(&mut self, tracker_socket_addr: &SocketAddr) -> anyhow::Result<()> {
debug!("Connecting to tracker: udp://{tracker_socket_addr}");
match &self.udp_tracker_client {
Some(client) => {
client.udp_client.connect(&tracker_socket_addr.to_string()).await?;
self.remote_socket = Some(*tracker_socket_addr);
Ok(())
}
None => Err(ClientError::NotBound.into()),
}
}
/// Sends a connection request to the UDP Tracker server.
///
/// # Errors
///
/// Will return and error if
///
/// - It can't connect to the remote UDP socket.
/// - It can't make a connection request successfully to the remote UDP
/// server (after successfully connecting to the remote UDP socket).
///
/// # Panics
///
/// Will panic if it receives an unexpected response.
pub async fn send_connection_request(&self, transaction_id: TransactionId) -> anyhow::Result<ConnectionId> {
debug!("Sending connection request with transaction id: {transaction_id:#?}");
let connect_request = ConnectRequest { transaction_id };
match &self.udp_tracker_client {
Some(client) => {
client.send(connect_request.into()).await?;
let response = client.receive().await?;
debug!("connection request response:\n{response:#?}");
match response {
Response::Connect(connect_response) => Ok(connect_response.connection_id),
_ => Err(ClientError::UnexpectedConnectionResponse.into()),
}
}
None => Err(ClientError::NotConnected.into()),
}
}
/// Sends an announce request to the UDP Tracker server.
///
/// # Errors
///
/// Will return and error if the client is not connected. You have to connect
/// before calling this function.
pub async fn send_announce_request(
&self,
connection_id: ConnectionId,
transaction_id: TransactionId,
info_hash: TorrustInfoHash,
client_port: Port,
) -> anyhow::Result<Response> {
debug!("Sending announce request with transaction id: {transaction_id:#?}");
let announce_request = AnnounceRequest {
connection_id,
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),
port: client_port,
};
match &self.udp_tracker_client {
Some(client) => {
client.send(announce_request.into()).await?;
let response = client.receive().await?;
debug!("announce request response:\n{response:#?}");
Ok(response)
}
None => Err(ClientError::NotConnected.into()),
}
}
/// Sends a scrape request to the UDP Tracker server.
///
/// # Errors
///
/// Will return and error if the client is not connected. You have to connect
/// before calling this function.
pub async fn send_scrape_request(
&self,
connection_id: ConnectionId,
transaction_id: TransactionId,
info_hashes: Vec<TorrustInfoHash>,
) -> anyhow::Result<Response> {
debug!("Sending scrape request with transaction id: {transaction_id:#?}");
let scrape_request = ScrapeRequest {
connection_id,
transaction_id,
info_hashes: info_hashes
.iter()
.map(|torrust_info_hash| InfoHash(torrust_info_hash.bytes()))
.collect(),
};
match &self.udp_tracker_client {
Some(client) => {
client.send(scrape_request.into()).await?;
let response = client.receive().await?;
debug!("scrape request response:\n{response:#?}");
Ok(response)
}
None => Err(ClientError::NotConnected.into()),
}
}
}