forked from webtorrent/bittorrent-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/megatorrent reference 2716865330907683697 #2
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
Merged
robertpelloni
merged 2 commits into
master
from
feature/megatorrent-reference-2716865330907683697
Dec 15, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [submodule "qbittorrent"] | ||
| path = qbittorrent | ||
| url = https://github.com/robertpelloni/qbittorrent |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Megatorrent Monorepo | ||
|
|
||
| This repository contains the reference implementation for the Megatorrent protocol, a decentralized, mutable successor to BitTorrent. | ||
|
|
||
| ## Structure | ||
|
|
||
| * `docs/`: Architecture and Protocol Specifications. | ||
| * `tracker/`: Node.js implementation of the Tracker and Reference Client (the root of this repo, historically). | ||
| * `qbittorrent/`: C++ Client Fork (Submodule). | ||
|
|
||
| ## Getting Started | ||
|
|
||
| ### Node.js Tracker & Reference Client | ||
| Run the tracker and client tests: | ||
| ```bash | ||
| npm install | ||
| npm test | ||
| ``` | ||
|
|
||
| ### qBittorrent Client | ||
| See `qbittorrent/README.md` for C++ build instructions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
|
|
||
| ## 6. Obfuscated Storage Protocol (Phase 2) | ||
| To achieve plausible deniability and distributed redundancy, data is not stored as "files" but as "Blobs" of high-entropy data. | ||
|
|
||
| ### Concepts | ||
| * **Blob**: A fixed-size (or variable) container stored by a Host. To the Host, it is just random bytes. | ||
| * **Chunk**: A segment of a user's file. | ||
| * **Encryption**: Each Chunk is encrypted with a *unique* random key (ChaCha20-Poly1305) before being placed into a Blob. | ||
| * **Muxing**: A Blob may contain multiple Chunks (potentially from different files) or padding. | ||
|
|
||
| ### Data Structure: The `FileEntry` | ||
| This structure replaces the simple "magnet link" in the Megatorrent Manifest. | ||
|
|
||
| ```json | ||
| { | ||
| "name": "episode1.mkv", | ||
| "mime": "video/x-matroska", | ||
| "size": 104857600, | ||
| "chunks": [ | ||
| { | ||
| "blobId": "<SHA256 Hash of the Blob>", | ||
| "offset": 0, // Byte offset in the Blob | ||
| "length": 1048576, // Length of the encrypted chunk | ||
| "key": "<32-byte Hex Key>", | ||
| "nonce": "<12-byte Hex Nonce>", | ||
| "authTag": "<16-byte Hex Tag>" | ||
| }, | ||
| ... | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| ### Process | ||
| 1. **Ingest**: | ||
| * File is split into N chunks. | ||
| * For each chunk: | ||
| * Generate random Key & Nonce. | ||
| * Encrypt Chunk -> EncryptedChunk. | ||
| * (Simplification for Ref Impl) EncryptedChunk becomes a "Blob" directly (or is wrapped). | ||
| * Blob ID = SHA256(Blob). | ||
| * Result: A list of Blobs (to be uploaded) and a `FileEntry` (to be put in the Manifest). | ||
|
|
||
| 2. **Access**: | ||
| * Subscriber receives Manifest. | ||
| * Parses `FileEntry`. | ||
| * Requests Blob(ID) from the network (simulated via local dir or tracker relay). | ||
| * Extracts bytes at `offset` for `length`. | ||
| * Decrypts using `key` and `nonce`. | ||
| * Reassembles file. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule qbittorrent
added at
5abf45
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,17 +6,20 @@ import minimist from 'minimist' | |||||
| import Client from 'bittorrent-tracker' | ||||||
| import { generateKeypair } from './lib/crypto.js' | ||||||
| import { createManifest, validateManifest } from './lib/manifest.js' | ||||||
| import { ingest, reassemble } from './lib/storage.js' | ||||||
|
|
||||||
| const argv = minimist(process.argv.slice(2), { | ||||||
| alias: { | ||||||
| k: 'keyfile', | ||||||
| t: 'tracker', | ||||||
| i: 'input', | ||||||
| o: 'output' | ||||||
| o: 'output', | ||||||
| d: 'dir' | ||||||
| }, | ||||||
| default: { | ||||||
| keyfile: './identity.json', | ||||||
| tracker: 'ws://localhost:8000' // Default to local WS tracker | ||||||
| tracker: 'ws://localhost:8000', // Default to local WS tracker | ||||||
| dir: './storage' | ||||||
| } | ||||||
| }) | ||||||
|
|
||||||
|
|
@@ -25,12 +28,18 @@ const command = argv._[0] | |||||
| if (!command) { | ||||||
| console.error(`Usage: | ||||||
| gen-key [-k identity.json] | ||||||
| publish [-k identity.json] [-t ws://tracker] -i magnet_list.txt | ||||||
| subscribe [-t ws://tracker] <public_key_hex> | ||||||
| ingest -i <file> [-d ./storage] -> Returns FileEntry JSON | ||||||
| publish [-k identity.json] [-t ws://tracker] -i <file_entry.json> | ||||||
| subscribe [-t ws://tracker] <public_key_hex> [-d ./storage] | ||||||
| `) | ||||||
| process.exit(1) | ||||||
| } | ||||||
|
|
||||||
| // Ensure storage dir exists | ||||||
| if (!fs.existsSync(argv.dir)) { | ||||||
| fs.mkdirSync(argv.dir, { recursive: true }) | ||||||
| } | ||||||
|
|
||||||
| // 1. Generate Key | ||||||
| if (command === 'gen-key') { | ||||||
| const keypair = generateKeypair() | ||||||
|
|
@@ -44,7 +53,27 @@ if (command === 'gen-key') { | |||||
| process.exit(0) | ||||||
| } | ||||||
|
|
||||||
| // 2. Publish | ||||||
| // 2. Ingest | ||||||
| if (command === 'ingest') { | ||||||
| if (!argv.input) { | ||||||
| console.error('Please specify input file with -i') | ||||||
| process.exit(1) | ||||||
| } | ||||||
| const fileBuf = fs.readFileSync(argv.input) | ||||||
| const result = ingest(fileBuf, path.basename(argv.input)) | ||||||
|
|
||||||
| // Save Blobs | ||||||
| result.blobs.forEach(blob => { | ||||||
| fs.writeFileSync(path.join(argv.dir, blob.id), blob.buffer) | ||||||
| }) | ||||||
|
|
||||||
| console.log(`Ingested ${result.blobs.length} blobs to ${argv.dir}`) | ||||||
| console.log('FileEntry JSON (save this to a file to publish it):') | ||||||
| console.log(JSON.stringify(result.fileEntry, null, 2)) | ||||||
| process.exit(0) | ||||||
| } | ||||||
|
|
||||||
| // 3. Publish | ||||||
| if (command === 'publish') { | ||||||
| if (!fs.existsSync(argv.keyfile)) { | ||||||
| console.error('Keyfile not found. Run gen-key first.') | ||||||
|
|
@@ -56,23 +85,32 @@ if (command === 'publish') { | |||||
| secretKey: Buffer.from(keyData.secretKey, 'hex') | ||||||
| } | ||||||
|
|
||||||
| // Read magnet links | ||||||
| // Read Input | ||||||
| if (!argv.input) { | ||||||
| console.error('Please specify input file with -i (list of magnet links)') | ||||||
| console.error('Please specify input file with -i (json file entry or text list)') | ||||||
| process.exit(1) | ||||||
| } | ||||||
|
|
||||||
| const content = fs.readFileSync(argv.input, 'utf-8') | ||||||
| const lines = content.split('\n').map(l => l.trim()).filter(l => l.length > 0) | ||||||
| let items | ||||||
| try { | ||||||
| // Try parsing as JSON (FileEntry) | ||||||
| const json = JSON.parse(content) | ||||||
| // Wrap in our "Items" list. | ||||||
| // In the future, "Items" can be Magnet Links OR FileEntries. | ||||||
| items = [json] | ||||||
| } catch (e) { | ||||||
| // Fallback: Line-separated magnet links | ||||||
| items = content.split('\n').map(l => l.trim()).filter(l => l.length > 0) | ||||||
| } | ||||||
|
|
||||||
| // Create Collections structure (flat for now) | ||||||
| // Create Collections structure | ||||||
| const collections = [{ | ||||||
| title: 'Default Collection', | ||||||
| items: lines | ||||||
| items | ||||||
| }] | ||||||
|
|
||||||
| // Create Manifest | ||||||
| // TODO: Retrieve last sequence from somewhere or store it? | ||||||
| // For now, we use timestamp as sequence to be simple and monotonic | ||||||
| const sequence = Date.now() | ||||||
| const manifest = createManifest(keypair, sequence, collections) | ||||||
|
|
||||||
|
|
@@ -123,7 +161,7 @@ if (command === 'publish') { | |||||
| }, 1000) | ||||||
| } | ||||||
|
|
||||||
| // 3. Subscribe | ||||||
| // 4. Subscribe | ||||||
| if (command === 'subscribe') { | ||||||
| const pubKeyHex = argv._[1] | ||||||
| if (!pubKeyHex) { | ||||||
|
|
@@ -166,7 +204,37 @@ if (command === 'subscribe') { | |||||
| if (valid && data.manifest.publicKey === pubKeyHex) { | ||||||
| console.log('VERIFIED UPDATE from ' + pubKeyHex) | ||||||
| console.log('Sequence:', data.manifest.sequence) | ||||||
| console.log('Items:', data.manifest.collections[0].items) | ||||||
|
|
||||||
| const items = data.manifest.collections[0].items | ||||||
| console.log(`Received ${items.length} items.`) | ||||||
|
|
||||||
| // Auto-Download Logic (Prototype) | ||||||
| items.forEach(async (item, idx) => { | ||||||
| if (typeof item === 'object' && item.chunks) { | ||||||
| console.log(`Item ${idx}: Detected Megatorrent FileEntry: ${item.name}`) | ||||||
| try { | ||||||
| const fileBuf = await reassemble(item, async (blobId) => { | ||||||
| const p = path.join(argv.dir, blobId) | ||||||
| if (fs.existsSync(p)) { | ||||||
| return fs.readFileSync(p) | ||||||
| } | ||||||
| // TODO: Network fetch | ||||||
| console.log(`Blob ${blobId} not found locally.`) | ||||||
|
||||||
| console.log(`Blob ${blobId} not found locally.`) | |
| console.warn(`Warning: Blob ${blobId} not found locally. Network fetch not yet implemented.`) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
forEachwith an async callback does not wait for promises to resolve. The async operations will run concurrently without proper error handling or completion tracking. Consider usingfor...ofloop instead:for (const [idx, item] of items.entries())