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
use std::{str::FromStr, time::Instant};
use mainline::{Dht, Id};
use clap::Parser;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
/// info_hash to annouce a peer on
infohash: String,
}
fn main() {
let cli = Cli::parse();
match Id::from_str(cli.infohash.as_str()) {
Ok(infohash) => {
let dht = Dht::default();
let start = Instant::now();
println!("\nAnnouncing peer on an infohash: {} ...\n", cli.infohash);
let metadata = dht
.announce_peer(infohash, Some(6991))
.expect("announce_peer fialed");
println!(
"Announced peer in {:?} seconds",
start.elapsed().as_secs_f32()
);
let stored_at = metadata.stored_at();
println!("Stored at: {:?} nodes", stored_at.len());
for node in stored_at {
println!(" {:?}", node);
}
// You can now reannounce to the same closest nodes
// skipping the the lookup step.
//
// This time we choose to not sepcify the port, effectively
// making the port implicit to be detected by the storing node
// from the source address of the announce_peer request
//
// Uncomment the following lines to try it out:
// println!(
// "Announcing again to {:?} closest_nodes ...",
// metadata.closest_nodes().len()
// );
//
// let again = Instant::now();
// match dht.announce_peer_to(infohash, metadata.closest_nodes(), None) {
// Ok(metadata) => {
// println!(
// "Announced again to {:?} nodes in {:?} seconds",
// metadata.stored_at().len(),
// again.elapsed().as_secs()
// );
// }
// Err(err) => {
// println!("Error: {:?}", err);
// }
// }
}
Err(err) => {
println!("Error: {:?}", err)
}
};
}