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
use std::path::PathBuf;
use anyhow::{anyhow, Result};
use clap::{ArgGroup, Subcommand};
use iroh::EndpointId;
use crate::registry::{Registry, OPEN_RING_NAME};
use crate::util::parse_peer_id;
#[derive(Subcommand)]
pub enum Cmd {
/// Manage rings
#[command(subcommand)]
Ring(RingCmd),
/// Manage blobs (import, list, remove)
#[command(subcommand)]
Blob(BlobCmd),
/// Import a file/directory into the blob store and print a ticket (shortcut for `blob import`)
Import {
/// Path to import (file or directory)
path: PathBuf,
/// Ring to tag the blob with; if omitted the blob won't be served until tagged
#[arg(long, conflicts_with = "open")]
tag: Option<String>,
/// Tag as publicly accessible (anyone can download); shorthand for --tag open
#[arg(long, conflicts_with = "tag")]
open: bool,
},
/// Start the node and share all authorised blobs until Ctrl-C
Share,
/// Download a file from a ringdrop ticket (automatically resumes if interrupted)
Receive {
/// Ticket string (rdrop://...)
ticket: String,
/// Destination path (directory or file path)
#[arg(long, default_value = ".")]
dest: PathBuf,
},
/// Grant access to a file by tagging it with a ring
#[command(group(ArgGroup::new("access").required(true).args(["rings", "open"])))]
Tag {
/// File path or BLAKE3 hash (hex)
target: String,
/// Tag with a named ring (repeat for multiple)
#[arg(long = "ring", conflicts_with = "open")]
rings: Vec<String>,
/// Tag as publicly accessible (anyone can download)
#[arg(long, conflicts_with = "rings")]
open: bool,
},
/// Show which rings a file is tagged with
Tags {
/// File path or BLAKE3 hash (hex)
target: String,
},
/// Print your peer-id (this node public-id) so others can add you to their rings
Id,
}
#[derive(Subcommand)]
pub enum BlobCmd {
/// Import a file/directory into the blob store and print a ticket
Import {
/// Path to import (file or directory)
path: PathBuf,
/// Ring to tag the blob with; if omitted the blob won't be served until tagged
#[arg(long, conflicts_with = "open")]
tag: Option<String>,
/// Tag as publicly accessible (anyone can download); shorthand for --tag open
#[arg(long, conflicts_with = "tag")]
open: bool,
},
/// Remove a blob from the local store and all its ring tags
Remove {
/// File path or BLAKE3 hash (hex)
target: String,
},
/// List all local blobs with their ring tags and share ticket
List,
}
#[derive(Subcommand)]
pub enum RingCmd {
/// Create a new ring with the given name
New {
/// Name for the ring (e.g. "friends", "work-team")
name: String,
},
/// List all rings
List,
/// Add a peer to a ring
Add {
ring: String,
#[arg(value_name = "PEER-ID")]
peer: String,
/// Optional display label for this peer
#[arg(long)]
nickname: Option<String>,
},
/// Remove a peer from a ring
Remove {
ring: String,
#[arg(value_name = "PEER-ID")]
peer: String,
},
/// List members of a ring
Members { ring: String },
}
pub fn run_ring(cmd: RingCmd, registry: Registry, public_id: EndpointId) -> Result<()> {
match cmd {
RingCmd::New { name } => {
registry.create_ring(&name)?;
println!("Ring created: {name}");
println!("Add peers: rdrop ring add {name} <peer-id>");
}
RingCmd::List => {
let rings = registry.list_rings()?;
println!("{} ring(s):", rings.len());
for r in rings {
if r.is_open() {
println!(
" {} — publicly accessible (no membership required)",
r.as_str()
);
} else {
let members = registry.list_members(r.as_str())?;
println!(" {} ({} member(s))", r.as_str(), members.len());
}
}
}
RingCmd::Add {
ring,
peer,
nickname,
} => {
if ring == OPEN_RING_NAME {
println!("The open ring has no membership list — everyone is welcome.");
return Ok(());
}
let peer = parse_peer_id(&peer)?;
if peer == public_id {
return Err(anyhow!("cannot add yourself to a ring"));
}
registry.add_member(&ring, peer, nickname.as_deref())?;
match &nickname {
Some(nick) => println!("Added {peer} ({nick}) to ring {ring}"),
None => println!("Added {peer} to ring {ring}"),
}
}
RingCmd::Remove { ring, peer } => {
if ring == OPEN_RING_NAME {
println!("The open ring has no membership list to remove from.");
return Ok(());
}
let peer = parse_peer_id(&peer)?;
registry.remove_member(&ring, peer)?;
println!("Removed {peer} from ring {ring}");
}
RingCmd::Members { ring } => {
if ring == OPEN_RING_NAME {
println!("The open ring is public — any peer may access blobs tagged with it.");
return Ok(());
}
let members = registry.list_members(&ring)?;
if members.is_empty() {
println!("Ring '{ring}' has no members yet.");
println!("Add peers: rdrop ring add {ring} <peer-id>");
println!("Peers print their PeerId with: rdrop id");
} else {
println!("Ring '{ring}' — {} member(s):", members.len());
for (peer, nick) in members {
match nick {
Some(n) => println!(" {peer} ({n})"),
None => println!(" {peer}"),
}
}
}
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use super::*;
use crate::registry::Registry;
fn setup() -> (Registry, EndpointId, TempDir) {
let dir = TempDir::new().unwrap();
let registry = Registry::open(dir.path().join("test.redb")).unwrap();
let public_id = iroh::SecretKey::generate().public();
(registry, public_id, dir)
}
#[test]
fn ring_add_self_is_rejected() {
let (registry, public_id, _dir) = setup();
registry.create_ring("friends").unwrap();
let err = run_ring(
RingCmd::Add {
ring: "friends".into(),
peer: public_id.to_string(),
nickname: None,
},
registry,
public_id,
)
.unwrap_err();
assert!(err.to_string().contains("yourself"));
}
#[test]
fn ring_add_to_open_ring_does_not_add_member() {
let (registry, public_id, _dir) = setup();
let peer = iroh::SecretKey::generate().public();
run_ring(
RingCmd::Add {
ring: OPEN_RING_NAME.into(),
peer: peer.to_string(),
nickname: None,
},
registry.clone(),
public_id,
)
.unwrap();
assert_eq!(registry.list_members(OPEN_RING_NAME).unwrap().len(), 0);
}
}