Skip to content

0x2a-labs/bencode-wasm

Repository files navigation

Torrent Recovery

A tool to recover as much data as possible from partially downloaded or corrupted .torrent files. Runs entirely in the browser via WebAssembly — no data is uploaded anywhere.

Live Demo →

What it recovers

  • Info hash & magnet link (computed from raw bytes, works even with outer corruption)
  • Name, trackers, piece info
  • File list with sizes (multi-file torrents)
  • Metadata: comment, created by, creation date, source, private flag
  • Raw byte scan fallback for severely damaged files (extracts URLs and strings via pattern matching)

How it works

Three-layer recovery strategy:

  1. Fault-tolerant bencode parser — handles truncated strings/ints, missing terminators, leading garbage, and malformed keys without crashing
  2. Raw info dict extraction — scans for the 4:infod byte pattern and walks the bencode structure to compute the SHA-1 info hash, independent of outer file corruption
  3. Regex fallback — when bencode parsing fails entirely, scans raw bytes for tracker URLs and known key/value patterns

Project structure

├── src/lib.rs             # Rust WASM library (bencode parser + recovery logic)
├── Cargo.toml
├── web/index.html         # Website (dev, imports from ../pkg/)
├── docs/                  # GitHub Pages deployment
│   ├── index.html
│   └── pkg/               # Built WASM artifacts
├── torrent_recovery.py    # Original Python CLI script
└── test_recovery.py       # Generates 16 corrupted variants and tests recovery

Building

Requirements: Rust and wasm-pack

# Build the WASM module
wasm-pack build --target web --release

# Copy artifacts for GitHub Pages
cp pkg/torrent_recovery.js docs/pkg/
cp pkg/torrent_recovery_bg.wasm docs/pkg/

# Serve locally
cd docs && python3 -m http.server 8080

Testing

The test suite generates 16 corrupted variants from a reference torrent file and verifies the recovery script handles each without crashing:

python3 test_recovery.py

Test cases include: truncated at 25/50/75%, header-only, random byte corruption, garbage prepended/appended, middle chunk removed, broken terminators, zeroed sections, info-dict-only, tiny fragments, and empty files.

Python CLI

The original Python script works standalone with no dependencies:

python3 torrent_recovery.py damaged_file.torrent

License

MIT

About

Best effort decode corrupted .torrent files

Resources

Stars

Watchers

Forks

Contributors