From aeb9d8b31721ad5b4bfe042d3779be7d5007dd6c Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 17 Mar 2020 03:49:39 -0700 Subject: [PATCH] Add name and peers to magnet links Add additional data to magnet links in `imdl torrent link`. - Set the `dn` query parameter with `metinfo.info.name` - Add peers to the `x.pe` query parameter: imdl torrent link --peer HOST:PORT type: added --- src/subcommand/torrent/link.rs | 91 ++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 3 deletions(-) diff --git a/src/subcommand/torrent/link.rs b/src/subcommand/torrent/link.rs index 4673f59..ba8ab45 100644 --- a/src/subcommand/torrent/link.rs +++ b/src/subcommand/torrent/link.rs @@ -15,6 +15,13 @@ pub(crate) struct Link { parse(from_os_str) )] input: PathBuf, + #[structopt( + long = "peer", + short = "p", + value_name = "PEER", + help = "Add `PEER` to magnet link." + )] + peers: Vec, } impl Link { @@ -25,6 +32,8 @@ impl Link { let mut link = MagnetLink::with_infohash(infohash); + link.set_name(&metainfo.info.name); + let mut trackers = HashSet::new(); for result in metainfo.trackers() { let tracker = result?; @@ -34,6 +43,10 @@ impl Link { } } + for peer in self.peers { + link.add_peer(peer); + } + outln!(env, "{}", link.to_url())?; Ok(()) @@ -67,7 +80,10 @@ mod tests { let infohash = Sha1Digest::from_data(INFO.as_bytes()); - assert_eq!(env.out(), format!("magnet:?xt=urn:btih:{}\n", infohash),); + assert_eq!( + env.out(), + format!("magnet:?xt=urn:btih:{}&dn=foo\n", infohash), + ); } #[test] @@ -96,7 +112,73 @@ mod tests { assert_eq!( env.out(), format!( - "magnet:?xt=urn:btih:{}&tr=https://foo.com/announce\n", + "magnet:?xt=urn:btih:{}&dn=foo&tr=https://foo.com/announce\n", + infohash + ), + ); + } + + #[test] + fn unique_trackers() { + let mut env = test_env! { + args: [ + "torrent", + "link", + "--input", + "foo.torrent", + ], + tree: { + "foo.torrent": "d\ + 8:announce24:https://foo.com/announce\ + 13:announce-listll24:https://foo.com/announceel24:https://bar.com/announceee\ + 4:infod6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e\ + e", + } + }; + + assert_ok!(env.run()); + + const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; + + let infohash = Sha1Digest::from_data(INFO.as_bytes()); + + assert_eq!( + env.out(), + format!( + "magnet:?xt=urn:btih:{}&dn=foo&tr=https://foo.com/announce&tr=https://bar.com/announce\n", + infohash + ), + ); + } + #[test] + fn with_peer() { + let mut env = test_env! { + args: [ + "torrent", + "link", + "--input", + "foo.torrent", + "--peer", + "foo.com:1337", + ], + tree: { + "foo.torrent": "d\ + 8:announce24:https://foo.com/announce\ + 4:infod6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e\ + e", + } + }; + + assert_ok!(env.run()); + + const INFO: &str = "d6:lengthi0e4:name3:foo12:piece lengthi1e6:pieces0:e"; + + let infohash = Sha1Digest::from_data(INFO.as_bytes()); + + assert_eq!( + env.out(), + format!( + "magnet:?xt=urn:btih:{}&dn=foo&tr=https://foo.com/announce&x.pe=foo.com:1337\n", infohash ), ); @@ -122,7 +204,10 @@ mod tests { let infohash = Sha1Digest::from_data(INFO.as_bytes()); - assert_eq!(env.out(), format!("magnet:?xt=urn:btih:{}\n", infohash),); + assert_eq!( + env.out(), + format!("magnet:?xt=urn:btih:{}&dn=foo\n", infohash), + ); } #[test]