| 
 | 1 | +//! HTTP Tracker client:  | 
 | 2 | +//!  | 
 | 3 | +//! Examples:  | 
 | 4 | +//!  | 
 | 5 | +//! Announce request:  | 
 | 6 | +//!  | 
 | 7 | +//! ```text  | 
 | 8 | +//! cargo run --bin udp_tracker_client 127.0.0.1:6969 9c38422213e30bff212b30c360d26f9a02136422 | jq  | 
 | 9 | +//! ```  | 
 | 10 | +//!  | 
 | 11 | +//! Announce response:  | 
 | 12 | +//!  | 
 | 13 | +//! ```json  | 
 | 14 | +//! {  | 
 | 15 | +//!   "transaction_id": -888840697  | 
 | 16 | +//!   "announce_interval": 120,  | 
 | 17 | +//!   "leechers": 0,  | 
 | 18 | +//!   "seeders": 1,  | 
 | 19 | +//!   "peers": [  | 
 | 20 | +//!     "123.123.123.123:51289"  | 
 | 21 | +//!   ],  | 
 | 22 | +//! }  | 
 | 23 | +/// ````    | 
1 | 24 | use std::env;  | 
2 | 25 | use std::net::{Ipv4Addr, SocketAddr};  | 
3 | 26 | use std::str::FromStr;  | 
4 | 27 | 
 
  | 
5 | 28 | use aquatic_udp_protocol::common::InfoHash;  | 
 | 29 | +use aquatic_udp_protocol::Response::{AnnounceIpv4, AnnounceIpv6};  | 
6 | 30 | use aquatic_udp_protocol::{  | 
7 | 31 |     AnnounceEvent, AnnounceRequest, ConnectRequest, ConnectionId, NumberOfBytes, NumberOfPeers, PeerId, PeerKey, Port, Response,  | 
8 | 32 |     TransactionId,  | 
9 | 33 | };  | 
10 | 34 | use log::{debug, LevelFilter};  | 
 | 35 | +use serde_json::json;  | 
11 | 36 | use torrust_tracker::shared::bit_torrent::info_hash::InfoHash as TorrustInfoHash;  | 
12 | 37 | use torrust_tracker::shared::bit_torrent::tracker::udp::client::{UdpClient, UdpTrackerClient};  | 
13 | 38 | 
 
  | 
@@ -52,7 +77,31 @@ async fn main() {  | 
52 | 77 |     )  | 
53 | 78 |     .await;  | 
54 | 79 | 
 
  | 
55 |  | -    println!("{response:#?}");  | 
 | 80 | +    match response {  | 
 | 81 | +        AnnounceIpv4(announce) => {  | 
 | 82 | +            let json = json!({  | 
 | 83 | +                "transaction_id": announce.transaction_id.0,  | 
 | 84 | +                "announce_interval": announce.announce_interval.0,  | 
 | 85 | +                "leechers": announce.leechers.0,  | 
 | 86 | +                "seeders": announce.seeders.0,  | 
 | 87 | +                "peers": announce.peers.iter().map(|peer| format!("{}:{}", peer.ip_address, peer.port.0)).collect::<Vec<_>>(),  | 
 | 88 | +            });  | 
 | 89 | +            let pretty_json = serde_json::to_string_pretty(&json).unwrap();  | 
 | 90 | +            println!("{pretty_json}");  | 
 | 91 | +        }  | 
 | 92 | +        AnnounceIpv6(announce) => {  | 
 | 93 | +            let json = json!({  | 
 | 94 | +                "transaction_id": announce.transaction_id.0,  | 
 | 95 | +                "announce_interval": announce.announce_interval.0,  | 
 | 96 | +                "leechers": announce.leechers.0,  | 
 | 97 | +                "seeders": announce.seeders.0,  | 
 | 98 | +                "peers6": announce.peers.iter().map(|peer| format!("{}:{}", peer.ip_address, peer.port.0)).collect::<Vec<_>>(),  | 
 | 99 | +            });  | 
 | 100 | +            let pretty_json = serde_json::to_string_pretty(&json).unwrap();  | 
 | 101 | +            println!("{pretty_json}");  | 
 | 102 | +        }  | 
 | 103 | +        _ => println!("{response:#?}"),  | 
 | 104 | +    }  | 
56 | 105 | }  | 
57 | 106 | 
 
  | 
58 | 107 | fn setup_logging(level: LevelFilter) {  | 
 | 
0 commit comments