Skip to content

Commit 9a127f1

Browse files
committed
save
1 parent 92349d3 commit 9a127f1

File tree

3 files changed

+124
-55
lines changed

3 files changed

+124
-55
lines changed

http_trackers.sh

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# Check if a filename is provided as an argument
4+
if [ $# -eq 0 ]; then
5+
echo "Usage: $0 <filename>"
6+
exit 1
7+
fi
8+
9+
# Store the filename
10+
filename="$1"
11+
12+
# Check if the file exists
13+
if [ ! -f "$filename" ]; then
14+
echo "Error: File '$filename' does not exist."
15+
exit 1
16+
fi
17+
18+
# Read lines from the file and store them in an array
19+
declare -a list
20+
21+
while IFS= read -r line; do
22+
list+=("$line")
23+
done < "$filename"
24+
25+
# Print the list elements (one per line)
26+
for item in "${list[@]}"; do
27+
command="cargo run --bin http_tracker_client scrape $item 9c38422213e30bff212b30c360d26f9a02136422"
28+
$command | jq
29+
done

src/shared/bit_torrent/tracker/udp/client.rs

+66-55
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ use std::net::SocketAddr;
33
use std::sync::Arc;
44
use std::time::Duration;
55

6+
use crate::shared::bit_torrent::tracker::udp::{source_address, MAX_PACKET_SIZE};
7+
use anyhow::anyhow;
8+
use anyhow::{Context, Result};
9+
use core::result::Result::{Ok, Err};
10+
use anyhow::Error as AError;
611
use aquatic_udp_protocol::{ConnectRequest, Request, Response, TransactionId};
712
use log::debug;
813
use tokio::net::UdpSocket;
914
use tokio::time;
1015

11-
use crate::shared::bit_torrent::tracker::udp::{source_address, MAX_PACKET_SIZE};
12-
1316
/// Default timeout for sending and receiving packets. And waiting for sockets
1417
/// to be readable and writable.
1518
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5);
@@ -28,31 +31,24 @@ impl UdpClient {
2831
/// # Panics
2932
///
3033
/// Will panic if the local address can't be bound.
31-
pub async fn bind(local_address: &str) -> Self {
32-
let valid_socket_addr = local_address
33-
.parse::<SocketAddr>()
34-
.unwrap_or_else(|_| panic!("{local_address} is not a valid socket address"));
35-
36-
let socket = UdpSocket::bind(valid_socket_addr).await.unwrap();
37-
38-
Self {
34+
///
35+
pub async fn bind(local_address: &str) -> Result<Self> {
36+
let socket_addr = local_address.parse::<SocketAddr>().map_err(|err| err).context("{local_address} is not a valid socket address")?;
37+
let socket = UdpSocket::bind(socket_addr).await?;
38+
let udp_client = Self {
3939
socket: Arc::new(socket),
4040
timeout: DEFAULT_TIMEOUT,
41-
}
41+
};
42+
Ok(udp_client)
4243
}
4344

4445
/// # Panics
4546
///
4647
/// Will panic if can't connect to the socket.
47-
pub async fn connect(&self, remote_address: &str) {
48-
let valid_socket_addr = remote_address
49-
.parse::<SocketAddr>()
50-
.unwrap_or_else(|_| panic!("{remote_address} is not a valid socket address"));
51-
52-
match self.socket.connect(valid_socket_addr).await {
53-
Ok(()) => debug!("Connected successfully"),
54-
Err(e) => panic!("Failed to connect: {e:?}"),
55-
}
48+
pub async fn connect(&self, remote_address: &str) -> anyhow::Result<()> {
49+
let socket_addr = remote_address.parse::<SocketAddr>().map_err(|err| err).context(format!("{} is not a valid socket address", remote_address))?;
50+
self.socket.connect(socket_addr).await.map_err(|err| err)?;
51+
Ok(())
5652
}
5753

5854
/// # Panics
@@ -61,24 +57,31 @@ impl UdpClient {
6157
///
6258
/// - Can't write to the socket.
6359
/// - Can't send data.
64-
pub async fn send(&self, bytes: &[u8]) -> usize {
60+
pub async fn send(&self, bytes: &[u8]) -> Result<usize, anyhow::Error> {
6561
debug!(target: "UDP client", "sending {bytes:?} ...");
6662

67-
match time::timeout(self.timeout, self.socket.writable()).await {
68-
Ok(writable_result) => match writable_result {
69-
Ok(()) => (),
70-
Err(e) => panic!("{}", format!("IO error waiting for the socket to become readable: {e:?}")),
71-
},
72-
Err(e) => panic!("{}", format!("Timeout waiting for the socket to become readable: {e:?}")),
63+
let _:Result<(), anyhow::Error> = match time::timeout(self.timeout, self.socket.writable()).await {
64+
Ok(writable_result) => {
65+
let writable_result_status : Result<(), anyhow::Error> = match writable_result {
66+
Ok(()) => Ok(()),
67+
Err(e) => Err(anyhow!("IO error waiting for the socket to become readable: {e:?}"))
68+
};
69+
writable_result_status
70+
}
71+
Err(e) => Err(anyhow!("Timeout waiting for the socket to become readable: {e:?}"))
7372
};
7473

75-
match time::timeout(self.timeout, self.socket.send(bytes)).await {
76-
Ok(send_result) => match send_result {
77-
Ok(size) => size,
78-
Err(e) => panic!("{}", format!("IO error during send: {e:?}")),
79-
},
80-
Err(e) => panic!("{}", format!("Send operation timed out: {e:?}")),
81-
}
74+
let send_status:Result<usize, anyhow::Error> = match time::timeout(self.timeout, self.socket.send(bytes)).await {
75+
Ok(send_result) => {
76+
let send_result_status: Result<usize, anyhow::Error> = match send_result {
77+
Ok(size) => Ok(size),
78+
Err(e) => Err(anyhow!("IO error waiting for the socket to become readable: {}", e))
79+
};
80+
send_result_status
81+
}
82+
Err(e) => Err(anyhow!("IO error waiting for the socket to become readable: {}", e))
83+
};
84+
send_status
8285
}
8386

8487
/// # Panics
@@ -87,37 +90,45 @@ impl UdpClient {
8790
///
8891
/// - Can't read from the socket.
8992
/// - Can't receive data.
90-
pub async fn receive(&self, bytes: &mut [u8]) -> usize {
93+
pub async fn receive(&self, bytes: &mut [u8]) -> Result<usize> {
9194
debug!(target: "UDP client", "receiving ...");
9295

93-
match time::timeout(self.timeout, self.socket.readable()).await {
94-
Ok(readable_result) => match readable_result {
95-
Ok(()) => (),
96-
Err(e) => panic!("{}", format!("IO error waiting for the socket to become readable: {e:?}")),
96+
let _ :Result<(), anyhow::Error>= match time::timeout(self.timeout, self.socket.readable()).await {
97+
Ok(readable_result) => {
98+
let readable_result_status = match readable_result {
99+
Ok(()) => Ok(()),
100+
Err(e) => Err(anyhow!("IO error waiting for the socket to become readable: {e:?}")),
101+
};
102+
readable_result_status
97103
},
98-
Err(e) => panic!("{}", format!("Timeout waiting for the socket to become readable: {e:?}")),
104+
Err(e) => Err(anyhow!("Timeout waiting for the socket to become readable: {e:?}")),
99105
};
100106

101-
let size = match time::timeout(self.timeout, self.socket.recv(bytes)).await {
107+
let size: Result<usize, anyhow::Error> = match time::timeout(self.timeout, self.socket.recv(bytes)).await {
102108
Ok(recv_result) => match recv_result {
103-
Ok(size) => size,
104-
Err(e) => panic!("{}", format!("IO error during send: {e:?}")),
109+
Ok(size) => Ok(size),
110+
Err(e) => Err(anyhow!("IO error during send: {e:?}")),
105111
},
106-
Err(e) => panic!("{}", format!("Receive operation timed out: {e:?}")),
112+
Err(e) => Err(anyhow!("Receive operation timed out: {e:?}")),
107113
};
108114

109115
debug!(target: "UDP client", "{size} bytes received {bytes:?}");
110116

111117
size
112118
}
113-
}
119+
114120

115121
/// Creates a new `UdpClient` connected to a Udp server
116-
pub async fn new_udp_client_connected(remote_address: &str) -> UdpClient {
122+
pub async fn new_udp_client_connected(remote_address: &str) -> Result<UdpClient> {
117123
let port = 0; // Let OS choose an unused port.
118-
let client = UdpClient::bind(&source_address(port)).await;
119-
client.connect(remote_address).await;
120-
client
124+
match UdpClient::bind(&source_address(port)).await {
125+
Ok(client) => {
126+
client.connect(remote_address).await;
127+
Ok(client)
128+
}
129+
Err(err) => Err(err),
130+
}
131+
}
121132
}
122133

123134
#[allow(clippy::module_name_repetitions)]
@@ -130,7 +141,7 @@ impl UdpTrackerClient {
130141
/// # Panics
131142
///
132143
/// Will panic if can't write request to bytes.
133-
pub async fn send(&self, request: Request) -> usize {
144+
pub async fn send(&self, request: Request) -> Result<usize> {
134145
debug!(target: "UDP tracker client", "send request {request:?}");
135146

136147
// Write request into a buffer
@@ -145,7 +156,7 @@ impl UdpTrackerClient {
145156
// Return slice which contains written request data
146157
&inner_request_buffer[..position]
147158
}
148-
Err(e) => panic!("could not write request to bytes: {e}."),
159+
Err(e) => Err(anyhow!("could not write request to bytes: {e}.")),
149160
};
150161

151162
self.udp_client.send(request_data).await
@@ -167,8 +178,8 @@ impl UdpTrackerClient {
167178

168179
/// Creates a new `UdpTrackerClient` connected to a Udp Tracker server
169180
pub async fn new_udp_tracker_client_connected(remote_address: &str) -> UdpTrackerClient {
170-
let udp_client = new_udp_client_connected(remote_address).await;
171-
UdpTrackerClient { udp_client }
181+
let udp_client = new_udp_client_connected(remote_address).await?;
182+
UdpTrackerClient { udp_client.unwrap() }
172183
}
173184

174185
/// Helper Function to Check if a UDP Service is Connectable
@@ -193,7 +204,7 @@ pub async fn check(binding: &SocketAddr) -> Result<String, String> {
193204
if matches!(response, Response::Connect(_connect_response)) {
194205
Ok("Connected".to_string())
195206
} else {
196-
Err("Did not Connect".to_string())
207+
Error("Did not Connect".to_string())
197208
}
198209
};
199210

@@ -202,7 +213,7 @@ pub async fn check(binding: &SocketAddr) -> Result<String, String> {
202213

203214
tokio::select! {
204215
() = &mut sleep => {
205-
Err("Timed Out".to_string())
216+
Error("Timed Out".to_string())
206217
}
207218
response = client.receive() => {
208219
process(response)

trackers.txt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
http://tracker.gbitt.info:80
2+
http://tracker.files.fm:6969
3+
http://open.acgnxtracker.com:80
4+
http://bt.okmp3.ru:2710
5+
https://tracker.loligirl.cn:443
6+
https://tracker.tamersunion.org:443
7+
https://trackers.mlsub.net:443
8+
https://tracker.yemekyedim.com:443
9+
http://tracker.bt4g.com:2095
10+
http://tracker.renfei.net:8080
11+
http://bvarf.tracker.sh:2086
12+
https://www.peckservers.com:9443
13+
https://tracker.lilithraws.org:443
14+
https://tracker.gcrreen.xyz:443
15+
https://tracker1.520.jp:443
16+
https://track3r.site:443
17+
https://t.peer-exchange.download:443
18+
http://tracker.mywaifu.best:6969
19+
http://aboutbeautifulgallopinghorsesinthegreenpasture.online:80
20+
https://tr.qfruiti.com:443
21+
http://tr.kxmp.cf:80
22+
https://tracker.cloudit.top:443
23+
https://trackers.ptlsp.com:443
24+
https://shahidrazi.online:443
25+
http://p2p.0g.cx:6969
26+
http://tracker1.itzmx.com:8080
27+
http://open.acgtracker.com:1096
28+
http://t1.aag.moe:17715
29+
https://t1.hloli.org:443

0 commit comments

Comments
 (0)