ringdrop 0.4.0

P2P streamed file transfer with ring-based access control, built on iroh and bao protocols
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! `rdrop` — ringdrop CLI
//!
//! # Usage
//!
//! ```text
//! # Print your peer-id so others can add you to their rings
//! rdrop id
//!
//! # Manage rings
//! rdrop ring new friends               # create a ring named "friends"
//! rdrop ring list                      # list all rings
//! rdrop ring add friends <peer-id>     # add a peer to a ring
//! rdrop ring members friends
//!
//! # Import a file and get a ticket (shortcut)
//! rdrop import file.txt                   # untagged — warns until tagged
//! rdrop import file.txt --open            # publicly accessible
//! rdrop import file.txt --tag friends     # restrict to a ring
//!
//! # Manage blobs
//! rdrop blob import file.txt --open
//! rdrop blob list
//! rdrop blob remove file.txt
//! rdrop blob remove <hash>
//!
//! # Re-tag a blob at any time
//! rdrop tag file.txt --ring friends
//! rdrop tag <hash> --open
//!
//! # Start serving all authorised blobs
//! rdrop share
//!
//! # Receive — resumes automatically if interrupted
//! rdrop receive rdrop://ABCDEF... [--dest ./downloads]
//! ```

mod command;

use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use clap::Parser;
use indicatif::{ProgressBar, ProgressStyle};
use iroh_blobs::{BlobFormat, Hash};
use tracing_subscriber::{fmt, EnvFilter};

use crate::config::Config;
use crate::core::Node;
use crate::registry::{Registry, OPEN_RING_NAME};
use crate::ticket::ShareTicket;
use crate::util::{default_data_dir, parse_hash};
use command::{BlobCmd, Cmd};

#[derive(Parser)]
#[command(
    name = "rdrop",
    about = "P2P streamed file transfer with ring-based access control.\n\
             Built on iroh and bao protocols.",
    version
)]
struct Cli {
    /// Directory for blob store + registry (default: ~/.ringdrop)
    #[arg(long, env = "RINGDROP_DATA_DIR")]
    data_dir: Option<PathBuf>,

    #[command(subcommand)]
    command: Cmd,
}

/// Returns the final output path, or an error if it already exists and `force_overwrite` is false.
///
/// When `dest` is an existing directory the output lands at `dest/<name>` (or `dest/<hash>` when
/// the ticket carries no name). When `dest` is not an existing directory it is used as-is.
fn check_dest(
    dest: &Path,
    name: Option<&str>,
    hash_hex: &str,
    force_overwrite: bool,
) -> Result<PathBuf> {
    let expected = if dest.is_dir() {
        dest.join(name.unwrap_or(hash_hex))
    } else {
        dest.to_path_buf()
    };
    if expected.exists() && !force_overwrite {
        anyhow::bail!(
            "destination '{}' already exists; \
             use --dest to choose a different location or --force-overwrite to replace it",
            expected.display()
        );
    }
    Ok(expected)
}

async fn import_path(node: &Node, path: &std::path::Path) -> Result<(Hash, BlobFormat)> {
    if path.is_dir() {
        node.import_directory(path).await
    } else {
        node.import_file(path).await
    }
}

async fn resolve_target(target: &str, data_dir: &std::path::Path) -> Result<(Hash, Registry)> {
    tokio::fs::create_dir_all(data_dir).await?;
    let path = PathBuf::from(target);
    if path.exists() {
        let cfg = Config::load_or_create(data_dir).context("loading config")?;
        let node = Node::start(data_dir, cfg).await?;
        let (hash, _) = import_path(&node, &path).await?;
        let registry = node.registry.clone();
        node.shutdown().await?;
        Ok((hash, registry))
    } else {
        let hash = parse_hash(target)?;
        let registry = Registry::open(data_dir.join("registry.redb"))?;
        Ok((hash, registry))
    }
}

async fn run_import(node: &Node, path: PathBuf, tag: Option<String>, open: bool) -> Result<()> {
    let (hash, format) = import_path(node, &path).await?;

    let tag = if open {
        Some(OPEN_RING_NAME.to_owned())
    } else {
        tag
    };

    if let Some(ref ring) = tag {
        node.registry.tag_file(hash, ring)?;
        if ring == OPEN_RING_NAME {
            println!("Tagged as open (publicly accessible)");
        } else {
            println!("Tagged with ring '{ring}'");
        }
    } else {
        let rings = node.registry.file_rings(hash)?;
        if rings.is_empty() {
            println!("Warning: not tagged — this blob won't be served to any peer.");
            println!("Tag it with:");
            println!("  rdrop tag {hash} --ring <ring-name>");
            println!("  rdrop tag {hash} --open");
        } else {
            println!("Already tagged:");
            for r in &rings {
                if r.is_open() {
                    println!("  {} (open — publicly accessible)", r.as_str());
                } else {
                    println!("  {}", r.as_str());
                }
            }
        }
    }

    let display_name = path.file_name().map(|n| n.to_string_lossy().into_owned());
    let ticket = node.make_ticket(hash, format, display_name);
    let ticket_str = ticket.to_uri()?;

    println!("\nTicket:");
    println!("  {ticket_str}\n");
    println!("Run `rdrop share` to start accepting connections.");
    println!("Peers receive with:");
    println!("  rdrop receive {ticket_str}");

    Ok(())
}

pub async fn run() -> Result<()> {
    let cli = Cli::parse();

    let default_filter = if matches!(cli.command, Cmd::Share) {
        "ringdrop=info"
    } else {
        "warn"
    };

    fmt()
        .with_env_filter(
            EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(default_filter)),
        )
        .with_target(false)
        .compact()
        .init();

    let data_dir = cli.data_dir.unwrap_or_else(default_data_dir);

    match cli.command {
        Cmd::Ring(ring_cmd) => {
            tokio::fs::create_dir_all(&data_dir).await?;
            let cfg = Config::load_or_create(&data_dir).context("loading config")?;
            let public_id = cfg.public_id();
            let registry = Registry::open(data_dir.join("registry.redb"))?;
            command::run_ring(ring_cmd, registry, public_id)?;
        }

        Cmd::Blob(blob_cmd) => match blob_cmd {
            BlobCmd::Import { path, tag, open } => {
                let cfg = Config::load_or_create(&data_dir).context("loading config")?;
                let node = Node::start(&data_dir, cfg).await?;
                run_import(&node, path, tag, open).await?;
                node.shutdown().await?;
            }

            BlobCmd::Remove { target } => {
                let cfg = Config::load_or_create(&data_dir).context("loading config")?;
                let node = Node::start(&data_dir, cfg).await?;
                let path = PathBuf::from(&target);
                let hash = if path.exists() {
                    let (hash, _) = import_path(&node, &path).await?;
                    hash
                } else {
                    parse_hash(&target)?
                };
                node.registry.remove_file_tags(hash)?;
                node.delete_blob(hash).await?;
                println!("Removed {hash}");
                println!("Disk space will be reclaimed on the next `rdrop share` run.");
                node.shutdown().await?;
            }

            BlobCmd::List => {
                let cfg = Config::load_or_create(&data_dir).context("loading config")?;
                let node = Node::start(&data_dir, cfg).await?;
                let blobs = node.list_blobs().await?;
                if blobs.is_empty() {
                    println!("No blobs in local store.");
                } else {
                    println!("{} Blobs:", blobs.len());
                    for (hash, format, name) in blobs {
                        let rings = node.registry.file_rings(hash)?;
                        let ticket = node.make_ticket(hash, format, Some(name.clone()));
                        let ticket_str = ticket.to_uri()?;
                        println!("\n  {hash}  ({name})");
                        if rings.is_empty() {
                            println!("    no rings:  (inaccessible for all peers)");
                        } else {
                            let names: Vec<_> =
                                rings.iter().map(|r| r.as_str().to_owned()).collect();
                            println!("    rings:  {}", names.join(", "));
                        }
                        println!("    ticket: {ticket_str}");
                    }
                    println!("\nNote that the ticket link may change between sessions, but the blob is always uniquely identified and addressed by the protocol.");
                }
                node.shutdown().await?;
            }
        },

        Cmd::Import { path, tag, open } => {
            let cfg = Config::load_or_create(&data_dir).context("loading config")?;
            let node = Node::start(&data_dir, cfg).await?;
            run_import(&node, path, tag, open).await?;
            node.shutdown().await?;
        }

        Cmd::Share => {
            let cfg = Config::load_or_create(&data_dir).context("loading config")?;
            let public_id = cfg.public_id();
            let node = Node::start(&data_dir, cfg).await?;
            println!("Node online. Peer ID: {public_id}");
            println!("Sharing all authorised blobs — Ctrl-C to stop.");
            tokio::signal::ctrl_c().await?;
            node.shutdown().await?;
        }

        Cmd::Receive {
            ticket,
            dest,
            force_overwrite,
        } => {
            let ticket = ShareTicket::from_uri(&ticket)?;

            // Check for destination conflict before starting any network activity.
            let hash_hex = ticket.hash().to_string();
            if let Err(e) = check_dest(&dest, ticket.name.as_deref(), &hash_hex, force_overwrite) {
                eprintln!("error: {e}");
                std::process::exit(1);
            }

            let cfg = Config::load_or_create(&data_dir).context("loading config")?;
            let public_id = cfg.public_id();
            let node = Node::start(&data_dir, cfg).await?;

            println!(
                "Fetching {} from {}{}",
                ticket.hash(),
                ticket.peer_id(),
                ticket
                    .name
                    .as_deref()
                    .map(|n| format!(" ({n})"))
                    .unwrap_or_default()
            );
            println!("Destination: {}", dest.display());
            println!("(If interrupted, re-run this command to resume from where it stopped.)");

            let pb = ProgressBar::new(0);
            pb.set_style(
                ProgressStyle::default_bar()
                    .template(
                        "{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] \
                         {bytes}/{total_bytes} ({bytes_per_sec}, {eta})",
                    )
                    .unwrap()
                    .progress_chars("#>-"),
            );
            let on_progress = {
                let pb = pb.clone();
                move |bytes: u64, total: u64| {
                    pb.set_length(total);
                    pb.set_position(bytes);
                }
            };

            match node
                .download_with_progress(&ticket, &dest, on_progress)
                .await
            {
                Ok(()) => {
                    pb.finish_and_clear();
                    println!("Transfer complete.");
                }
                Err(e) => {
                    pb.finish_and_clear();
                    eprintln!("Transfer failed: {e:#}");
                    if e.to_string().contains("access denied") {
                        eprintln!("\nYour peer-id: {public_id}");
                        eprintln!("Ask the file owner to run:");
                        eprintln!("  rdrop ring add <ring-name> {public_id}");
                    }
                    std::process::exit(1);
                }
            }

            node.shutdown().await?;
        }

        Cmd::Tag {
            target,
            rings,
            open,
        } => {
            let (hash, registry) = resolve_target(&target, &data_dir).await?;

            for ring in &rings {
                registry.tag_file(hash, ring)?;
                println!("Tagged {hash} with ring '{ring}'");
            }
            if open {
                registry.tag_file(hash, OPEN_RING_NAME)?;
                println!("Tagged {hash} as open (publicly accessible)");
            }
        }

        Cmd::Tags { target } => {
            let (hash, registry) = resolve_target(&target, &data_dir).await?;

            let rings = registry.file_rings(hash)?;
            if rings.is_empty() {
                println!("{hash}: no rings (access denied to all peers)");
            } else {
                println!("{}: {} rings:", hash, rings.len());
                for ring in &rings {
                    if ring.is_open() {
                        println!("  {}  (open — publicly accessible)", ring.as_str());
                    } else {
                        println!("  {}", ring.as_str());
                    }
                }
            }
        }

        Cmd::Id => {
            tokio::fs::create_dir_all(&data_dir).await?;
            let cfg = Config::load_or_create(&data_dir).context("loading config")?;
            println!("{}", cfg.public_id());
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn dest_does_not_exist_is_accepted() {
        let dir = TempDir::new().unwrap();
        let dest = dir.path().join("output.txt");
        assert!(check_dest(&dest, Some("output.txt"), "deadbeef", false).is_ok());
    }

    #[test]
    fn existing_dest_without_force_overwrite_is_rejected() {
        let dir = TempDir::new().unwrap();
        let dest = dir.path().join("output.txt");
        std::fs::write(&dest, b"old").unwrap();
        let err = check_dest(&dest, Some("output.txt"), "deadbeef", false).unwrap_err();
        assert!(err.to_string().contains("already exists"));
        assert!(err.to_string().contains("--force-overwrite"));
    }

    #[test]
    fn existing_dest_with_force_overwrite_is_accepted() {
        let dir = TempDir::new().unwrap();
        let dest = dir.path().join("output.txt");
        std::fs::write(&dest, b"old").unwrap();
        assert!(check_dest(&dest, Some("output.txt"), "deadbeef", true).is_ok());
    }

    #[test]
    fn dest_is_dir_and_named_file_does_not_exist_is_accepted() {
        let dir = TempDir::new().unwrap();
        let result = check_dest(dir.path(), Some("fox.txt"), "deadbeef", false).unwrap();
        assert_eq!(result, dir.path().join("fox.txt"));
    }

    #[test]
    fn dest_is_dir_and_named_file_exists_without_force_is_rejected() {
        let dir = TempDir::new().unwrap();
        std::fs::write(dir.path().join("fox.txt"), b"old").unwrap();
        let err = check_dest(dir.path(), Some("fox.txt"), "deadbeef", false).unwrap_err();
        assert!(err.to_string().contains("already exists"));
    }

    #[test]
    fn dest_is_dir_and_named_file_exists_with_force_is_accepted() {
        let dir = TempDir::new().unwrap();
        std::fs::write(dir.path().join("fox.txt"), b"old").unwrap();
        assert!(check_dest(dir.path(), Some("fox.txt"), "deadbeef", true).is_ok());
    }

    #[test]
    fn dest_is_dir_and_no_ticket_name_falls_back_to_hash() {
        let dir = TempDir::new().unwrap();
        let hash_hex = "abc123";
        std::fs::write(dir.path().join(hash_hex), b"old").unwrap();
        let err = check_dest(dir.path(), None, hash_hex, false).unwrap_err();
        assert!(err.to_string().contains("already exists"));
    }
}