From 0d98b9caa67eb1a6c4a065b46b036b3a598e649c Mon Sep 17 00:00:00 2001 From: hails Date: Sat, 25 May 2019 14:51:10 -0300 Subject: [PATCH] chore: refactor name usage --- src/main.rs | 8 ++++---- src/{torrent_info.rs => torrent.rs} | 10 +++++----- src/{announce.rs => tracker.rs} | 31 +++++++++++++---------------- 3 files changed, 23 insertions(+), 26 deletions(-) rename src/{torrent_info.rs => torrent.rs} (84%) rename src/{announce.rs => tracker.rs} (87%) diff --git a/src/main.rs b/src/main.rs index c0e18ef..dee1c1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,17 +5,17 @@ use std::env; use std::fs::File; use std::io::BufReader; -mod announce; -mod torrent_info; +mod torrent; +mod tracker; fn main() -> Result<(), Error> { let args: Vec = env::args().collect(); let file = File::open(&args[1])?; - let torrent = torrent_info::parse(&mut BufReader::new(file))?; + let torrent = torrent::parse(&mut BufReader::new(file))?; - let res = announce::announce(announce::generate_announce(&torrent)?, &torrent.announce)?; + let res = tracker::announce(tracker::generate_announce(&torrent)?, &torrent.announce)?; println!( "Announced torrent << {} >> on `{}`", diff --git a/src/torrent_info.rs b/src/torrent.rs similarity index 84% rename from src/torrent_info.rs rename to src/torrent.rs index 805bedb..23b37aa 100644 --- a/src/torrent_info.rs +++ b/src/torrent.rs @@ -9,26 +9,26 @@ use serde_bytes::ByteBuf; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Torrent { pub announce: String, - // #[serde(rename = "announce-list")] + #[serde(rename = "announce-list")] pub announce_list: Option>, #[serde(rename = "creation date")] pub creation_date: i32, - pub info: TorrentInfo, + pub info: Info, } #[derive(Serialize, Deserialize, Debug, Hash, Eq, PartialEq, Clone)] -pub struct TorrentInfo { +pub struct Info { pub name: String, pub pieces: ByteBuf, #[serde(rename = "piece length")] pub piece_length: u32, pub length: Option, pub md5sum: Option, - pub files: Option>, + pub files: Option>, } #[derive(Serialize, Deserialize, Debug, Hash, Eq, PartialEq, Clone)] -pub struct TorrentInfoFile { +pub struct File { pub length: u32, pub md5sum: Option, pub path: String, diff --git a/src/announce.rs b/src/tracker.rs similarity index 87% rename from src/announce.rs rename to src/tracker.rs index 52b5fbd..dee8049 100644 --- a/src/announce.rs +++ b/src/tracker.rs @@ -9,7 +9,7 @@ use sha1::Sha1; use rand::Rng; -use crate::torrent_info::Torrent; +use crate::torrent::Torrent; use reqwest; @@ -18,7 +18,7 @@ use percent_encoding::percent_encode_byte; use itertools::Itertools; #[derive(Serialize, Debug, PartialEq)] -pub struct TrackerAnnounce { +pub struct Announce { #[serde(skip_serializing)] pub info_hash: String, #[serde(skip_serializing)] @@ -32,7 +32,7 @@ pub struct TrackerAnnounce { } #[derive(Serialize, Deserialize, Debug, Clone)] -pub struct TrackerResponse { +pub struct Response { #[serde(rename = "failure reason")] failure_reason: Option, pub complete: Option, @@ -40,20 +40,17 @@ pub struct TrackerResponse { pub interval: Option, #[serde(rename = "peers")] peers_bin: Option, - pub peers: Option>, + pub peers: Option>, } #[derive(Serialize, Deserialize, Debug, Clone)] -pub struct TrackerResponsePeer { +pub struct Peer { pub ip: String, pub port: u16, } -pub fn announce( - announce_info: TrackerAnnounce, - tracker_url: &str, -) -> Result { - let announce_info = TrackerAnnounce { +pub fn announce(announce_info: Announce, tracker_url: &str) -> Result { + let announce_info = Announce { info_hash: announce_info .info_hash_bytes .iter() @@ -73,7 +70,7 @@ pub fn announce( let mut buf: Vec = vec![]; response.copy_to(&mut buf)?; - let mut tracker_response: TrackerResponse = de::from_bytes(&buf)?; + let mut tracker_response: Response = de::from_bytes(&buf)?; match &tracker_response.failure_reason { Some(failure) => panic!("{:?}", failure), @@ -88,14 +85,14 @@ pub fn announce( Ok(tracker_response) } -fn parse_peers(peers: &ByteBuf) -> Vec { +fn parse_peers(peers: &ByteBuf) -> Vec { let mut parsed_peers = vec![]; for mut chunk in &peers.into_iter().chunks(6) { let ip: String = format!("{}", chunk.by_ref().take(4).format(".")); let port: Vec<_> = chunk.take(2).collect(); - parsed_peers.push(TrackerResponsePeer { + parsed_peers.push(Peer { ip, port: u16::from(*port[0]) << 8 | u16::from(*port[1]), }) @@ -104,14 +101,14 @@ fn parse_peers(peers: &ByteBuf) -> Vec { parsed_peers } -pub fn generate_announce(torrent: &Torrent) -> Result { +pub fn generate_announce(torrent: &Torrent) -> Result { let torrent_info = serde_bencode::to_bytes(&torrent.info)?; let info_hash = Sha1::from(&torrent_info).digest(); let peer_id = format!("-RS0001-{}", random_numbers()); assert!(peer_id.len() == 20, "peer_id should have 20 bytes"); - Ok(TrackerAnnounce { + Ok(Announce { info_hash: "".to_owned(), info_hash_bytes: info_hash.bytes(), peer_id, @@ -146,7 +143,7 @@ mod tests { #[test] fn generate_announce_correctly() { - use crate::torrent_info::TorrentInfo; + use crate::torrent::Info; let peer_id_start = "-RS0001-"; @@ -154,7 +151,7 @@ mod tests { announce: "http://nyaa.tracker.wf:7777/announce".to_string(), announce_list: None, creation_date: 1276147560, - info: TorrentInfo { + info: Info { name: "[CrunchyRip]_heroman_heroman_pv.ass".to_string(), pieces: ByteBuf::from(vec![ 179, 44, 185, 20, 5, 96, 4, 178, 51, 254, 139, 204, 87, 213, 125, 68, 213, 108,