forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [torrust#625] a new UDP tracker client
You can use it with: ```console cargo run --bin udp_tracker_client 144.126.245.19:6969 9c38422213e30bff212b30c360d26f9a02136422 ``` and the output should be something like: ``` AnnounceIpv4( AnnounceResponse { transaction_id: TransactionId( -888840697, ), announce_interval: AnnounceInterval( 300, ), leechers: NumberOfPeers( 0, ), seeders: NumberOfPeers( 1, ), peers: [], }, ) ```
- Loading branch information
1 parent
203ce96
commit 6934338
Showing
3 changed files
with
141 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use std::env; | ||
use std::net::Ipv4Addr; | ||
|
||
use aquatic_udp_protocol::common::InfoHash; | ||
use aquatic_udp_protocol::{ | ||
AnnounceEvent, AnnounceRequest, ConnectRequest, ConnectionId, NumberOfBytes, NumberOfPeers, PeerId, PeerKey, Port, Response, | ||
TransactionId, | ||
}; | ||
use log::{debug, LevelFilter}; | ||
use torrust_tracker::shared::bit_torrent::tracker::udp::client::{UdpClient, UdpTrackerClient}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
setup_logging(LevelFilter::Info); | ||
|
||
let args: Vec<String> = env::args().collect(); | ||
|
||
if args.len() != 3 { | ||
eprintln!("Error: invalid number of arguments!"); | ||
eprintln!("Usage: cargo run --bin udp_tracker_client <UDP_TRACKER_SOCKET_ADDRESS> <INFO_HASH>"); | ||
eprintln!("Example: cargo run --bin udp_tracker_client 144.126.245.19:6969 9c38422213e30bff212b30c360d26f9a02136422"); | ||
std::process::exit(1); | ||
} | ||
|
||
let port = 0; | ||
let transaction_id = -888_840_697; | ||
|
||
let remote_socket_addr = &args[1]; | ||
|
||
let bind_to = format!("0.0.0.0:{port}"); | ||
|
||
debug!("Binding to: {bind_to}"); | ||
|
||
let udp_client = UdpClient::bind(&bind_to).await; | ||
|
||
let bound_to = udp_client.socket.local_addr().unwrap(); | ||
|
||
debug!("Bound to: {bound_to}"); | ||
|
||
debug!("Connecting to remote: udp://{remote_socket_addr}"); | ||
|
||
udp_client.connect(remote_socket_addr).await; | ||
|
||
let udp_tracker_client = UdpTrackerClient { udp_client }; | ||
|
||
let connection_id = send_connection_request(TransactionId(transaction_id), &udp_tracker_client).await; | ||
|
||
// Send announce request | ||
|
||
let announce_request = AnnounceRequest { | ||
connection_id: ConnectionId(connection_id.0), | ||
transaction_id: TransactionId(transaction_id), | ||
info_hash: InfoHash([0u8; 20]), | ||
peer_id: PeerId([255u8; 20]), | ||
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: Port(bound_to.port()), | ||
}; | ||
|
||
debug!("Sending announce request with connection id: {connection_id:#?}"); | ||
|
||
udp_tracker_client.send(announce_request.into()).await; | ||
|
||
let response = udp_tracker_client.receive().await; | ||
|
||
println!("{response:#?}"); | ||
} | ||
|
||
async fn send_connection_request(transaction_id: TransactionId, client: &UdpTrackerClient) -> ConnectionId { | ||
debug!("Sending connection request with transaction id: {transaction_id:#?}"); | ||
|
||
let connect_request = ConnectRequest { transaction_id }; | ||
|
||
client.send(connect_request.into()).await; | ||
|
||
let response = client.receive().await; | ||
|
||
debug!("response:\n{response:#?}"); | ||
|
||
match response { | ||
Response::Connect(connect_response) => connect_response.connection_id, | ||
_ => panic!("error connecting to udp server. Unexpected response"), | ||
} | ||
} | ||
|
||
fn setup_logging(level: LevelFilter) { | ||
if let Err(_err) = fern::Dispatch::new() | ||
.format(|out, message, record| { | ||
out.finish(format_args!( | ||
"{} [{}][{}] {}", | ||
chrono::Local::now().format("%+"), | ||
record.target(), | ||
record.level(), | ||
message | ||
)); | ||
}) | ||
.level(level) | ||
.chain(std::io::stdout()) | ||
.apply() | ||
{ | ||
panic!("Failed to initialize logging.") | ||
} | ||
|
||
debug!("logging initialized."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters