Skip to content

Commit

Permalink
fix: [torrust#226] user torrust-serde-bencode
Browse files Browse the repository at this point in the history
Using the crate `torrust-serde-bencode` instead og the upstream repo
fixed the problem with some torrents deserialization.

There was a problem with list (nodes) containing other list (tuples)
with different value types.

```json
{
   "info": {
      "length": 8,
      "name": "minimal.txt",
      "piece length": 16384,
      "pieces": "<hex>30 9A 84 51 4A F1 09 D0 8C 34 42 11 26 67 7F 84 B3 23 4D 78</hex>"
   },
   "nodes": [
      [
         "188.163.121.224",
         56711
      ],
      [
         "162.250.131.26",
         13386
      ]
   ]
}
```
  • Loading branch information
josecelano committed Sep 26, 2023
1 parent 0d7d72f commit c750cbb
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 26 deletions.
21 changes: 10 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ derive_more = "0.99"
serde = { version = "1.0", features = ["rc"] }
serde_derive = "1"
serde_json = "1.0"
serde_bencode = "0.2.3"
torrust-serde-bencode = { git = "https://github.com/torrust/torrust-serde-bencode.git", rev = "fd7bdce7c605204e0aa3245d6e4d6d9b4d6a2da5" }
serde_bytes = "0.11"
urlencoding = "2.1"
argon2 = "0.5"
Expand Down
4 changes: 2 additions & 2 deletions src/bin/parse_torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::env;
use std::fs::File;
use std::io::{self, Read};

use serde_bencode::de::from_bytes;
use serde_bencode::value::Value as BValue;
use torrust_index_backend::utils::parse_torrent;
use torrust_serde_bencode::de::from_bytes;
use torrust_serde_bencode::value::Value as BValue;

fn main() -> io::Result<()> {
let args: Vec<String> = env::args().collect();
Expand Down
7 changes: 2 additions & 5 deletions src/models/torrent_file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use serde_bencode::ser;
use serde_bytes::ByteBuf;
use sha1::{Digest, Sha1};
use torrust_serde_bencode::ser;

use super::info_hash::InfoHash;
use crate::utils::hex::{from_bytes, into_bytes};
Expand All @@ -12,7 +12,7 @@ pub struct Torrent {
#[serde(default)]
pub announce: Option<String>,
#[serde(default)]
pub nodes: Option<Vec<TorrentNode>>,
pub nodes: Option<Vec<(String, i64)>>,
#[serde(default)]
pub encoding: Option<String>,
#[serde(default)]
Expand All @@ -30,9 +30,6 @@ pub struct Torrent {
pub created_by: Option<String>,
}

#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
pub struct TorrentNode(String, i64);

#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
pub struct TorrentInfoDictionary {
pub name: String,
Expand Down
11 changes: 6 additions & 5 deletions src/utils/parse_torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use std::error;

use derive_more::{Display, Error};
use serde::{self, Deserialize, Serialize};
use serde_bencode::value::Value;
use serde_bencode::{de, Error};
use sha1::{Digest, Sha1};
use torrust_serde_bencode::value::Value;
use torrust_serde_bencode::{de, Error};

use crate::models::info_hash::InfoHash;
use crate::models::torrent_file::Torrent;
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn decode_torrent(bytes: &[u8]) -> Result<Torrent, Box<dyn error::Error>> {
///
/// This function will return an error if unable to bencode torrent.
pub fn encode_torrent(torrent: &Torrent) -> Result<Vec<u8>, Error> {
match serde_bencode::to_bytes(torrent) {
match torrust_serde_bencode::to_bytes(torrent) {
Ok(bencode_bytes) => Ok(bencode_bytes),
Err(e) => {
eprintln!("{e:?}");
Expand All @@ -100,10 +100,11 @@ struct ParsedInfoDictFromMetainfoFile {
pub fn calculate_info_hash(bytes: &[u8]) -> Result<InfoHash, DecodeTorrentFileError> {
// Extract the info dictionary
let metainfo: ParsedInfoDictFromMetainfoFile =
serde_bencode::from_bytes(bytes).map_err(|_| DecodeTorrentFileError::InvalidInfoDictionary)?;
torrust_serde_bencode::from_bytes(bytes).map_err(|_| DecodeTorrentFileError::InvalidInfoDictionary)?;

// Bencode the info dictionary
let info_dict_bytes = serde_bencode::to_bytes(&metainfo.info).map_err(|_| DecodeTorrentFileError::CannotBencodeInfoDict)?;
let info_dict_bytes =
torrust_serde_bencode::to_bytes(&metainfo.info).map_err(|_| DecodeTorrentFileError::CannotBencodeInfoDict)?;

// Calculate the SHA-1 hash of the bencoded info dictionary
let mut hasher = Sha1::new();
Expand Down
4 changes: 2 additions & 2 deletions tests/common/contexts/torrent/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ impl TestTorrentWithCustomInfoField {
}
}

pub fn encode(torrent: &Self) -> Result<Vec<u8>, serde_bencode::Error> {
match serde_bencode::to_bytes(torrent) {
pub fn encode(torrent: &Self) -> Result<Vec<u8>, torrust_serde_bencode::Error> {
match torrust_serde_bencode::to_bytes(torrent) {
Ok(bencode_bytes) => Ok(bencode_bytes),
Err(e) => {
eprintln!("{e:?}");
Expand Down

0 comments on commit c750cbb

Please sign in to comment.