Skip to content

Commit

Permalink
chore: refactor name usage
Browse files Browse the repository at this point in the history
  • Loading branch information
hails committed May 25, 2019
1 parent 28389eb commit 0d98b9c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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 `{}`",
Expand Down
10 changes: 5 additions & 5 deletions src/torrent_info.rs → src/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>>,
#[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<u32>,
pub md5sum: Option<String>,
pub files: Option<Vec<TorrentInfoFile>>,
pub files: Option<Vec<File>>,
}

#[derive(Serialize, Deserialize, Debug, Hash, Eq, PartialEq, Clone)]
pub struct TorrentInfoFile {
pub struct File {
pub length: u32,
pub md5sum: Option<String>,
pub path: String,
Expand Down
31 changes: 14 additions & 17 deletions src/announce.rs → src/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use sha1::Sha1;

use rand::Rng;

use crate::torrent_info::Torrent;
use crate::torrent::Torrent;

use reqwest;

Expand All @@ -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)]
Expand All @@ -32,28 +32,25 @@ pub struct TrackerAnnounce {
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct TrackerResponse {
pub struct Response {
#[serde(rename = "failure reason")]
failure_reason: Option<String>,
pub complete: Option<u32>,
pub incomplete: Option<u32>,
pub interval: Option<u32>,
#[serde(rename = "peers")]
peers_bin: Option<ByteBuf>,
pub peers: Option<Vec<TrackerResponsePeer>>,
pub peers: Option<Vec<Peer>>,
}

#[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<TrackerResponse, Error> {
let announce_info = TrackerAnnounce {
pub fn announce(announce_info: Announce, tracker_url: &str) -> Result<Response, Error> {
let announce_info = Announce {
info_hash: announce_info
.info_hash_bytes
.iter()
Expand All @@ -73,7 +70,7 @@ pub fn announce(
let mut buf: Vec<u8> = 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),
Expand All @@ -88,14 +85,14 @@ pub fn announce(
Ok(tracker_response)
}

fn parse_peers(peers: &ByteBuf) -> Vec<TrackerResponsePeer> {
fn parse_peers(peers: &ByteBuf) -> Vec<Peer> {
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]),
})
Expand All @@ -104,14 +101,14 @@ fn parse_peers(peers: &ByteBuf) -> Vec<TrackerResponsePeer> {
parsed_peers
}

pub fn generate_announce(torrent: &Torrent) -> Result<TrackerAnnounce, Error> {
pub fn generate_announce(torrent: &Torrent) -> Result<Announce, Error> {
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,
Expand Down Expand Up @@ -146,15 +143,15 @@ mod tests {

#[test]
fn generate_announce_correctly() {
use crate::torrent_info::TorrentInfo;
use crate::torrent::Info;

let peer_id_start = "-RS0001-";

let torrent = Torrent {
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,
Expand Down

0 comments on commit 0d98b9c

Please sign in to comment.