Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deps: localy provided bencode implmentation #366

Merged
merged 1 commit into from
Aug 21, 2023

Conversation

da2ce7
Copy link
Contributor

@da2ce7 da2ce7 commented Aug 8, 2023

No description provided.

@codecov
Copy link

codecov bot commented Aug 8, 2023

Codecov Report

Merging #366 (a4ac682) into develop (3a1ac86) will decrease coverage by 1.08%.
Report is 1 commits behind head on develop.
The diff coverage is 80.35%.

@@             Coverage Diff             @@
##           develop     #366      +/-   ##
===========================================
- Coverage    85.02%   83.95%   -1.08%     
===========================================
  Files           89      102      +13     
  Lines         6565     7267     +702     
===========================================
+ Hits          5582     6101     +519     
- Misses         983     1166     +183     
Files Changed Coverage Δ
src/servers/http/v1/responses/announce.rs 80.23% <ø> (ø)
src/servers/http/v1/responses/scrape.rs 100.00% <ø> (ø)
contrib/bencode/src/error.rs 7.84% <19.04%> (ø)
contrib/bencode/src/access/dict.rs 30.00% <30.00%> (ø)
contrib/bencode/src/access/list.rs 51.42% <51.42%> (ø)
contrib/bencode/src/mutable/bencode_mut.rs 66.66% <66.66%> (ø)
contrib/bencode/src/cow.rs 75.00% <75.00%> (ø)
contrib/bencode/src/reference/bencode_ref.rs 85.10% <85.10%> (ø)
contrib/bencode/src/reference/decode_opt.rs 93.75% <93.75%> (ø)
contrib/bencode/src/reference/decode.rs 96.29% <96.29%> (ø)
... and 4 more

... and 2 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@da2ce7 da2ce7 force-pushed the 20230808_contrib_package_bencode branch from 50573fc to fc10b0a Compare August 21, 2023 09:30
@josecelano
Copy link
Member

Hi @da2ce7, I want to fix this bug. I need to calculate the infohash from the torrent file. We are using serde_bencode, but it does not work to calculate the infohash because we cannot include the custom fields. I would like to use a different bencoding crate to calculate the infohash:

Something like this:

use bendy::decoding::{Error as BencodeError, FromBencode, Object};
use sha1::{Digest, Sha1};
use std::fs;
use std::path::Path;

fn calculate_infohash<P: AsRef<Path>>(path: P) -> Result<[u8; 20], BencodeError> {
    // Read and decode the torrent file
    let data = fs::read(path)?;
    let torrent = Object::decode(&data)?;

    // Find the "info" dictionary
    let info = torrent
        .lookup("info")
        .ok_or(BencodeError::Unspecified)?
        .to_owned();

    // Encode the "info" dictionary into bytes
    let info_bytes = info.encode()?;

    // Calculate the SHA-1 hash
    let mut hasher = Sha1::new();
    hasher.update(&info_bytes);
    let result = hasher.finalize();

    // Convert the hash to a 20-byte array and return
    Ok(result.into())
}

fn main() {
    let path = "path_to_torrent_file.torrent";
    match calculate_infohash(path) {
        Ok(hash) => {
            println!("Infohash: {:?}", hash);
        }
        Err(e) => {
            eprintln!("Error: {}", e);
        }
    }
}

I need to use a bencoding crate:

Since we are already using bip_bencode I will probably use it:

use bip_bencode::{Bencode, BencodeRef, BencodeConvert};
use sha1::{Digest, Sha1};
use std::fs;
use std::io::{self, Read};
use std::path::Path;

fn calculate_infohash<P: AsRef<Path>>(path: P) -> io::Result<[u8; 20]> {
    // Read the torrent file
    let mut data = Vec::new();
    let mut file = fs::File::open(path)?;
    file.read_to_end(&mut data)?;

    // Decode the torrent file
    let torrent = BencodeRef::decode(&data).unwrap();

    // Extract the "info" dictionary
    let info = match torrent.dict().unwrap().lookup(b"info") {
        Some(info) => info,
        None => return Err(io::Error::new(io::ErrorKind::Other, "Missing info dictionary")),
    };

    // Re-encode the "info" dictionary into bytes
    let info_bytes = info.encode();

    // Calculate the SHA-1 hash
    let mut hasher = Sha1::new();
    hasher.update(&info_bytes);
    let result = hasher.finalize();

    Ok(result.into())
}

fn main() {
    let path = "path_to_torrent_file.torrent";
    match calculate_infohash(path) {
        Ok(hash) => {
            println!("Infohash: {:?}", hash);
        }
        Err(e) => {
            eprintln!("Error: {}", e);
        }
    }
}

I do not like to parse the torrent bytes twice (with serde_bencode to extract the structs and with bip_encode to extract the info dict and calculate the infohash), but I want to fix the bug asap. After fixing the bug, we can consider using only the bip_encode and construct structs manually instead of using serde_bencode.

@da2ce7 da2ce7 changed the title Copy Bencode Into Local Contrib Folder deps: localy provided bencode implmentation Aug 21, 2023
@da2ce7 da2ce7 force-pushed the 20230808_contrib_package_bencode branch from fc10b0a to a4ac682 Compare August 21, 2023 17:02
@da2ce7
Copy link
Contributor Author

da2ce7 commented Aug 21, 2023

ACK a4ac682

@da2ce7 da2ce7 merged commit 8a51b32 into torrust:develop Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants