Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
hungfnt committed May 2, 2024
1 parent 92349d3 commit 9a127f1
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 55 deletions.
29 changes: 29 additions & 0 deletions http_trackers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# Check if a filename is provided as an argument
if [ $# -eq 0 ]; then
echo "Usage: $0 <filename>"
exit 1
fi

# Store the filename
filename="$1"

# Check if the file exists
if [ ! -f "$filename" ]; then
echo "Error: File '$filename' does not exist."
exit 1
fi

# Read lines from the file and store them in an array
declare -a list

while IFS= read -r line; do
list+=("$line")
done < "$filename"

# Print the list elements (one per line)
for item in "${list[@]}"; do
command="cargo run --bin http_tracker_client scrape $item 9c38422213e30bff212b30c360d26f9a02136422"
$command | jq
done
121 changes: 66 additions & 55 deletions src/shared/bit_torrent/tracker/udp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;

use crate::shared::bit_torrent::tracker::udp::{source_address, MAX_PACKET_SIZE};
use anyhow::anyhow;
use anyhow::{Context, Result};
use core::result::Result::{Ok, Err};
use anyhow::Error as AError;
use aquatic_udp_protocol::{ConnectRequest, Request, Response, TransactionId};
use log::debug;
use tokio::net::UdpSocket;
use tokio::time;

use crate::shared::bit_torrent::tracker::udp::{source_address, MAX_PACKET_SIZE};

/// Default timeout for sending and receiving packets. And waiting for sockets
/// to be readable and writable.
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(5);
Expand All @@ -28,31 +31,24 @@ impl UdpClient {
/// # Panics
///
/// Will panic if the local address can't be bound.
pub async fn bind(local_address: &str) -> Self {
let valid_socket_addr = local_address
.parse::<SocketAddr>()
.unwrap_or_else(|_| panic!("{local_address} is not a valid socket address"));

let socket = UdpSocket::bind(valid_socket_addr).await.unwrap();

Self {
///
pub async fn bind(local_address: &str) -> Result<Self> {
let socket_addr = local_address.parse::<SocketAddr>().map_err(|err| err).context("{local_address} is not a valid socket address")?;
let socket = UdpSocket::bind(socket_addr).await?;
let udp_client = Self {
socket: Arc::new(socket),
timeout: DEFAULT_TIMEOUT,
}
};
Ok(udp_client)
}

/// # Panics
///
/// Will panic if can't connect to the socket.
pub async fn connect(&self, remote_address: &str) {
let valid_socket_addr = remote_address
.parse::<SocketAddr>()
.unwrap_or_else(|_| panic!("{remote_address} is not a valid socket address"));

match self.socket.connect(valid_socket_addr).await {
Ok(()) => debug!("Connected successfully"),
Err(e) => panic!("Failed to connect: {e:?}"),
}
pub async fn connect(&self, remote_address: &str) -> anyhow::Result<()> {
let socket_addr = remote_address.parse::<SocketAddr>().map_err(|err| err).context(format!("{} is not a valid socket address", remote_address))?;
self.socket.connect(socket_addr).await.map_err(|err| err)?;
Ok(())
}

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

match time::timeout(self.timeout, self.socket.writable()).await {
Ok(writable_result) => match writable_result {
Ok(()) => (),
Err(e) => panic!("{}", format!("IO error waiting for the socket to become readable: {e:?}")),
},
Err(e) => panic!("{}", format!("Timeout waiting for the socket to become readable: {e:?}")),
let _:Result<(), anyhow::Error> = match time::timeout(self.timeout, self.socket.writable()).await {
Ok(writable_result) => {
let writable_result_status : Result<(), anyhow::Error> = match writable_result {
Ok(()) => Ok(()),
Err(e) => Err(anyhow!("IO error waiting for the socket to become readable: {e:?}"))
};
writable_result_status
}
Err(e) => Err(anyhow!("Timeout waiting for the socket to become readable: {e:?}"))
};

match time::timeout(self.timeout, self.socket.send(bytes)).await {
Ok(send_result) => match send_result {
Ok(size) => size,
Err(e) => panic!("{}", format!("IO error during send: {e:?}")),
},
Err(e) => panic!("{}", format!("Send operation timed out: {e:?}")),
}
let send_status:Result<usize, anyhow::Error> = match time::timeout(self.timeout, self.socket.send(bytes)).await {
Ok(send_result) => {
let send_result_status: Result<usize, anyhow::Error> = match send_result {
Ok(size) => Ok(size),
Err(e) => Err(anyhow!("IO error waiting for the socket to become readable: {}", e))
};
send_result_status
}
Err(e) => Err(anyhow!("IO error waiting for the socket to become readable: {}", e))
};
send_status
}

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

match time::timeout(self.timeout, self.socket.readable()).await {
Ok(readable_result) => match readable_result {
Ok(()) => (),
Err(e) => panic!("{}", format!("IO error waiting for the socket to become readable: {e:?}")),
let _ :Result<(), anyhow::Error>= match time::timeout(self.timeout, self.socket.readable()).await {
Ok(readable_result) => {
let readable_result_status = match readable_result {
Ok(()) => Ok(()),
Err(e) => Err(anyhow!("IO error waiting for the socket to become readable: {e:?}")),
};
readable_result_status
},
Err(e) => panic!("{}", format!("Timeout waiting for the socket to become readable: {e:?}")),
Err(e) => Err(anyhow!("Timeout waiting for the socket to become readable: {e:?}")),
};

let size = match time::timeout(self.timeout, self.socket.recv(bytes)).await {
let size: Result<usize, anyhow::Error> = match time::timeout(self.timeout, self.socket.recv(bytes)).await {
Ok(recv_result) => match recv_result {
Ok(size) => size,
Err(e) => panic!("{}", format!("IO error during send: {e:?}")),
Ok(size) => Ok(size),
Err(e) => Err(anyhow!("IO error during send: {e:?}")),
},
Err(e) => panic!("{}", format!("Receive operation timed out: {e:?}")),
Err(e) => Err(anyhow!("Receive operation timed out: {e:?}")),
};

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

size
}
}


/// Creates a new `UdpClient` connected to a Udp server
pub async fn new_udp_client_connected(remote_address: &str) -> UdpClient {
pub async fn new_udp_client_connected(remote_address: &str) -> Result<UdpClient> {
let port = 0; // Let OS choose an unused port.
let client = UdpClient::bind(&source_address(port)).await;
client.connect(remote_address).await;
client
match UdpClient::bind(&source_address(port)).await {
Ok(client) => {
client.connect(remote_address).await;
Ok(client)
}
Err(err) => Err(err),
}
}
}

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

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

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

/// Creates a new `UdpTrackerClient` connected to a Udp Tracker server
pub async fn new_udp_tracker_client_connected(remote_address: &str) -> UdpTrackerClient {
let udp_client = new_udp_client_connected(remote_address).await;
UdpTrackerClient { udp_client }
let udp_client = new_udp_client_connected(remote_address).await?;
UdpTrackerClient { udp_client.unwrap() }
}

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

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

tokio::select! {
() = &mut sleep => {
Err("Timed Out".to_string())
Error("Timed Out".to_string())
}
response = client.receive() => {
process(response)
Expand Down
29 changes: 29 additions & 0 deletions trackers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
http://tracker.gbitt.info:80
http://tracker.files.fm:6969
http://open.acgnxtracker.com:80
http://bt.okmp3.ru:2710
https://tracker.loligirl.cn:443
https://tracker.tamersunion.org:443
https://trackers.mlsub.net:443
https://tracker.yemekyedim.com:443
http://tracker.bt4g.com:2095
http://tracker.renfei.net:8080
http://bvarf.tracker.sh:2086
https://www.peckservers.com:9443
https://tracker.lilithraws.org:443
https://tracker.gcrreen.xyz:443
https://tracker1.520.jp:443
https://track3r.site:443
https://t.peer-exchange.download:443
http://tracker.mywaifu.best:6969
http://aboutbeautifulgallopinghorsesinthegreenpasture.online:80
https://tr.qfruiti.com:443
http://tr.kxmp.cf:80
https://tracker.cloudit.top:443
https://trackers.ptlsp.com:443
https://shahidrazi.online:443
http://p2p.0g.cx:6969
http://tracker1.itzmx.com:8080
http://open.acgtracker.com:1096
http://t1.aag.moe:17715
https://t1.hloli.org:443

0 comments on commit 9a127f1

Please sign in to comment.