-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathmain.rs
More file actions
895 lines (835 loc) · 29.5 KB
/
main.rs
File metadata and controls
895 lines (835 loc) · 29.5 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
//! Command line arguments.
use std::{
collections::BTreeMap,
fmt::{Display, Formatter},
net::{SocketAddrV4, SocketAddrV6},
path::{Component, Path, PathBuf},
str::FromStr,
sync::Arc,
time::Duration,
};
use anyhow::Context;
use clap::{
error::{ContextKind, ErrorKind},
CommandFactory, Parser, Subcommand,
};
use console::style;
use data_encoding::HEXLOWER;
use futures_buffered::BufferedStreamExt;
use futures_lite::{future::Boxed, StreamExt};
use indicatif::{
HumanBytes, HumanDuration, MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle,
};
use iroh::{
discovery::{dns::DnsDiscovery, pkarr::PkarrPublisher},
Endpoint, NodeAddr, RelayMap, RelayMode, RelayUrl, SecretKey,
};
use iroh_blobs::{
format::collection::Collection,
get::{
db::DownloadProgress,
fsm::{AtBlobHeaderNextError, DecodeError},
request::get_hash_seq_and_sizes,
},
net_protocol::Blobs,
provider::{self, CustomEventSender},
store::{ExportMode, ImportMode, ImportProgress},
ticket::BlobTicket,
util::local_pool::LocalPool,
BlobFormat, Hash, HashAndFormat, TempTag,
};
use rand::Rng;
use serde::{Deserialize, Serialize};
use walkdir::WalkDir;
/// Send a file or directory between two machines, using blake3 verified streaming.
///
/// For all subcommands, you can specify a secret key using the IROH_SECRET
/// environment variable. If you don't, a random one will be generated.
///
/// You can also specify a port for the magicsocket. If you don't, a random one
/// will be chosen.
#[derive(Parser, Debug)]
#[command(version, about)]
pub struct Args {
#[clap(subcommand)]
pub command: Commands,
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum Format {
#[default]
Hex,
Cid,
}
impl FromStr for Format {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
"hex" => Ok(Format::Hex),
"cid" => Ok(Format::Cid),
_ => Err(anyhow::anyhow!("invalid format")),
}
}
}
impl Display for Format {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Format::Hex => write!(f, "hex"),
Format::Cid => write!(f, "cid"),
}
}
}
fn print_hash(hash: &Hash, format: Format) -> String {
match format {
Format::Hex => hash.to_hex().to_string(),
Format::Cid => hash.to_string(),
}
}
#[derive(Subcommand, Debug)]
pub enum Commands {
/// Send a file or directory.
Send(SendArgs),
/// Receive a file or directory.
Receive(ReceiveArgs),
}
#[derive(Parser, Debug)]
pub struct CommonArgs {
/// The IPv4 address that magicsocket will listen on.
///
/// If None, defaults to a random free port, but it can be useful to specify a fixed
/// port, e.g. to configure a firewall rule.
#[clap(long, default_value = None)]
pub magic_ipv4_addr: Option<SocketAddrV4>,
/// The IPv6 address that magicsocket will listen on.
///
/// If None, defaults to a random free port, but it can be useful to specify a fixed
/// port, e.g. to configure a firewall rule.
#[clap(long, default_value = None)]
pub magic_ipv6_addr: Option<SocketAddrV6>,
#[clap(long, default_value_t = Format::Hex)]
pub format: Format,
#[clap(short = 'v', long, action = clap::ArgAction::Count)]
pub verbose: u8,
/// The relay URL to use as a home relay,
///
/// Can be set to "disable" to disable relay servers and "default"
/// to configure default servers.
#[clap(long, default_value_t = RelayModeOption::Default)]
pub relay: RelayModeOption,
}
/// Available command line options for configuring relays.
#[derive(Clone, Debug)]
pub enum RelayModeOption {
/// Disables relays altogether.
Disabled,
/// Uses the default relay servers.
Default,
/// Uses a single, custom relay server by URL.
Custom(RelayUrl),
}
impl FromStr for RelayModeOption {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"disabled" => Ok(Self::Disabled),
"default" => Ok(Self::Default),
_ => Ok(Self::Custom(RelayUrl::from_str(s)?)),
}
}
}
impl Display for RelayModeOption {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::Disabled => f.write_str("disabled"),
Self::Default => f.write_str("default"),
Self::Custom(url) => url.fmt(f),
}
}
}
impl From<RelayModeOption> for RelayMode {
fn from(value: RelayModeOption) -> Self {
match value {
RelayModeOption::Disabled => RelayMode::Disabled,
RelayModeOption::Default => RelayMode::Default,
RelayModeOption::Custom(url) => RelayMode::Custom(RelayMap::from_url(url)),
}
}
}
#[derive(Parser, Debug)]
pub struct SendArgs {
/// Path to the file or directory to send.
///
/// The last component of the path will be used as the name of the data
/// being shared.
pub path: PathBuf,
/// What type of ticket to use.
///
/// Use "id" for the shortest type only including the node ID,
/// "addresses" to only add IP addresses without a relay url,
/// "relay" to only add a relay address, and leave the option out
/// to use the biggest type of ticket that includes both relay and
/// address information.
///
/// Generally, the more information the higher the likelyhood of
/// a successful connection, but also the bigger a ticket to connect.
///
/// This is most useful for debugging which methods of connection
/// establishment work well.
#[clap(long, default_value_t = AddrInfoOptions::RelayAndAddresses)]
pub ticket_type: AddrInfoOptions,
#[clap(flatten)]
pub common: CommonArgs,
}
#[derive(Parser, Debug)]
pub struct ReceiveArgs {
/// The ticket to use to connect to the sender.
pub ticket: BlobTicket,
#[clap(flatten)]
pub common: CommonArgs,
}
/// Options to configure what is included in a [`NodeAddr`]
#[derive(
Copy,
Clone,
PartialEq,
Eq,
Default,
Debug,
derive_more::Display,
derive_more::FromStr,
Serialize,
Deserialize,
)]
pub enum AddrInfoOptions {
/// Only the Node ID is added.
///
/// This usually means that iroh-dns discovery is used to find address information.
#[default]
Id,
/// Includes the Node ID and both the relay URL, and the direct addresses.
RelayAndAddresses,
/// Includes the Node ID and the relay URL.
Relay,
/// Includes the Node ID and the direct addresses.
Addresses,
}
fn apply_options(addr: &mut NodeAddr, opts: AddrInfoOptions) {
match opts {
AddrInfoOptions::Id => {
addr.direct_addresses.clear();
addr.relay_url = None;
}
AddrInfoOptions::RelayAndAddresses => {
// nothing to do
}
AddrInfoOptions::Relay => {
addr.direct_addresses.clear();
}
AddrInfoOptions::Addresses => {
addr.relay_url = None;
}
}
}
/// Get the secret key or generate a new one.
///
/// Print the secret key to stderr if it was generated, so the user can save it.
fn get_or_create_secret(print: bool) -> anyhow::Result<SecretKey> {
match std::env::var("IROH_SECRET") {
Ok(secret) => SecretKey::from_str(&secret).context("invalid secret"),
Err(_) => {
let key = SecretKey::generate(rand::rngs::OsRng);
if print {
eprintln!("using secret key {}", key);
}
Ok(key)
}
}
}
fn validate_path_component(component: &str) -> anyhow::Result<()> {
anyhow::ensure!(
!component.contains('/'),
"path components must not contain the only correct path separator, /"
);
Ok(())
}
/// This function converts an already canonicalized path to a string.
///
/// If `must_be_relative` is true, the function will fail if any component of the path is
/// `Component::RootDir`
///
/// This function will also fail if the path is non canonical, i.e. contains
/// `..` or `.`, or if the path components contain any windows or unix path
/// separators.
pub fn canonicalized_path_to_string(
path: impl AsRef<Path>,
must_be_relative: bool,
) -> anyhow::Result<String> {
let mut path_str = String::new();
let parts = path
.as_ref()
.components()
.filter_map(|c| match c {
Component::Normal(x) => {
let c = match x.to_str() {
Some(c) => c,
None => return Some(Err(anyhow::anyhow!("invalid character in path"))),
};
if !c.contains('/') && !c.contains('\\') {
Some(Ok(c))
} else {
Some(Err(anyhow::anyhow!("invalid path component {:?}", c)))
}
}
Component::RootDir => {
if must_be_relative {
Some(Err(anyhow::anyhow!("invalid path component {:?}", c)))
} else {
path_str.push('/');
None
}
}
_ => Some(Err(anyhow::anyhow!("invalid path component {:?}", c))),
})
.collect::<anyhow::Result<Vec<_>>>()?;
let parts = parts.join("/");
path_str.push_str(&parts);
Ok(path_str)
}
pub async fn show_ingest_progress(
recv: async_channel::Receiver<ImportProgress>,
) -> anyhow::Result<()> {
let mp = MultiProgress::new();
mp.set_draw_target(ProgressDrawTarget::stderr());
let op = mp.add(ProgressBar::hidden());
op.set_style(
ProgressStyle::default_spinner().template("{spinner:.green} [{elapsed_precise}] {msg}")?,
);
// op.set_message(format!("{} Ingesting ...\n", style("[1/2]").bold().dim()));
// op.set_length(total_files);
let mut names = BTreeMap::new();
let mut sizes = BTreeMap::new();
let mut pbs = BTreeMap::new();
loop {
let event = recv.recv().await;
match event {
Ok(ImportProgress::Found { id, name }) => {
names.insert(id, name);
}
Ok(ImportProgress::Size { id, size }) => {
sizes.insert(id, size);
let total_size = sizes.values().sum::<u64>();
op.set_message(format!(
"{} Ingesting {} files, {}\n",
style("[1/2]").bold().dim(),
sizes.len(),
HumanBytes(total_size)
));
let name = names.get(&id).cloned().unwrap_or_default();
let pb = mp.add(ProgressBar::hidden());
pb.set_style(ProgressStyle::with_template(
"{msg}{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes}",
)?.progress_chars("#>-"));
pb.set_message(format!("{} {}", style("[2/2]").bold().dim(), name));
pb.set_length(size);
pbs.insert(id, pb);
}
Ok(ImportProgress::OutboardProgress { id, offset }) => {
if let Some(pb) = pbs.get(&id) {
pb.set_position(offset);
}
}
Ok(ImportProgress::OutboardDone { id, .. }) => {
// you are not guaranteed to get any OutboardProgress
if let Some(pb) = pbs.remove(&id) {
pb.finish_and_clear();
}
}
Ok(ImportProgress::CopyProgress { .. }) => {
// we are not copying anything
}
Err(e) => {
op.set_message(format!("Error receiving progress: {e}"));
break;
}
}
}
op.finish_and_clear();
Ok(())
}
/// Import from a file or directory into the database.
///
/// The returned tag always refers to a collection. If the input is a file, this
/// is a collection with a single blob, named like the file.
///
/// If the input is a directory, the collection contains all the files in the
/// directory.
async fn import(
path: PathBuf,
db: impl iroh_blobs::store::Store,
) -> anyhow::Result<(TempTag, u64, Collection)> {
let path = path.canonicalize()?;
anyhow::ensure!(path.exists(), "path {} does not exist", path.display());
let root = path.parent().context("context get parent")?;
// walkdir also works for files, so we don't need to special case them
let files = WalkDir::new(path.clone()).into_iter();
// flatten the directory structure into a list of (name, path) pairs.
// ignore symlinks.
let data_sources: Vec<(String, PathBuf)> = files
.map(|entry| {
let entry = entry?;
if !entry.file_type().is_file() {
// Skip symlinks. Directories are handled by WalkDir.
return Ok(None);
}
let path = entry.into_path();
let relative = path.strip_prefix(root)?;
let name = canonicalized_path_to_string(relative, true)?;
anyhow::Ok(Some((name, path)))
})
.filter_map(Result::transpose)
.collect::<anyhow::Result<Vec<_>>>()?;
let (send, recv) = async_channel::bounded(32);
let progress = iroh_blobs::util::progress::AsyncChannelProgressSender::new(send);
let show_progress = tokio::spawn(show_ingest_progress(recv));
// import all the files, using num_cpus workers, return names and temp tags
let mut names_and_tags = futures_lite::stream::iter(data_sources)
.map(|(name, path)| {
let db = db.clone();
let progress = progress.clone();
async move {
let (temp_tag, file_size) = db
.import_file(path, ImportMode::TryReference, BlobFormat::Raw, progress)
.await?;
anyhow::Ok((name, temp_tag, file_size))
}
})
.buffered_unordered(num_cpus::get())
.collect::<Vec<_>>()
.await
.into_iter()
.collect::<anyhow::Result<Vec<_>>>()?;
drop(progress);
names_and_tags.sort_by(|(a, _, _), (b, _, _)| a.cmp(b));
// total size of all files
let size = names_and_tags.iter().map(|(_, _, size)| *size).sum::<u64>();
// collect the (name, hash) tuples into a collection
// we must also keep the tags around so the data does not get gced.
let (collection, tags) = names_and_tags
.into_iter()
.map(|(name, tag, _)| ((name, *tag.hash()), tag))
.unzip::<_, _, Collection, Vec<_>>();
let temp_tag = collection.clone().store(&db).await?;
// now that the collection is stored, we can drop the tags
// data is protected by the collection
drop(tags);
show_progress.await??;
Ok((temp_tag, size, collection))
}
fn get_export_path(root: &Path, name: &str) -> anyhow::Result<PathBuf> {
let parts = name.split('/');
let mut path = root.to_path_buf();
for part in parts {
validate_path_component(part)?;
path.push(part);
}
Ok(path)
}
async fn export(db: impl iroh_blobs::store::Store, collection: Collection) -> anyhow::Result<()> {
let root = std::env::current_dir()?;
for (name, hash) in collection.iter() {
let target = get_export_path(&root, name)?;
if target.exists() {
eprintln!(
"target {} already exists. Export stopped.",
target.display()
);
eprintln!("You can remove the file or directory and try again. The download will not be repeated.");
anyhow::bail!("target {} already exists", target.display());
}
db.export(
*hash,
target,
ExportMode::TryReference,
Box::new(move |_position| Ok(())),
)
.await?;
}
Ok(())
}
#[derive(Debug, Clone)]
struct SendStatus {
/// the multiprogress bar
mp: MultiProgress,
}
impl SendStatus {
fn new() -> Self {
let mp = MultiProgress::new();
mp.set_draw_target(ProgressDrawTarget::stderr());
Self { mp }
}
fn new_client(&self) -> ClientStatus {
let current = self.mp.add(ProgressBar::hidden());
current.set_style(
ProgressStyle::default_spinner()
.template("{spinner:.green} [{elapsed_precise}] {msg}")
.unwrap(),
);
current.enable_steady_tick(Duration::from_millis(100));
current.set_message("waiting for requests");
ClientStatus {
current: current.into(),
}
}
}
#[derive(Debug, Clone)]
struct ClientStatus {
current: Arc<ProgressBar>,
}
impl Drop for ClientStatus {
fn drop(&mut self) {
if Arc::strong_count(&self.current) == 1 {
self.current.finish_and_clear();
}
}
}
impl CustomEventSender for ClientStatus {
fn send(&self, event: iroh_blobs::provider::Event) -> Boxed<()> {
self.try_send(event);
Box::pin(std::future::ready(()))
}
fn try_send(&self, event: provider::Event) {
tracing::info!("{:?}", event);
let msg = match event {
provider::Event::ClientConnected { connection_id } => {
Some(format!("{} got connection", connection_id))
}
provider::Event::TransferBlobCompleted {
connection_id,
hash,
index,
size,
..
} => Some(format!(
"{} transfer blob completed {} {} {}",
connection_id,
hash,
index,
HumanBytes(size)
)),
provider::Event::TransferCompleted {
connection_id,
stats,
..
} => Some(format!(
"{} transfer completed {} {}",
connection_id,
stats.send.write_bytes.size,
HumanDuration(stats.send.write_bytes.stats.duration)
)),
provider::Event::TransferAborted { connection_id, .. } => {
Some(format!("{} transfer completed", connection_id))
}
_ => None,
};
if let Some(msg) = msg {
self.current.set_message(msg);
}
}
}
async fn send(args: SendArgs) -> anyhow::Result<()> {
let secret_key = get_or_create_secret(args.common.verbose > 0)?;
// create a magicsocket endpoint
let mut builder = Endpoint::builder()
.alpns(vec![iroh_blobs::protocol::ALPN.to_vec()])
.secret_key(secret_key)
.relay_mode(args.common.relay.into());
if args.ticket_type == AddrInfoOptions::Id {
builder =
builder.add_discovery(|secret_key| Some(PkarrPublisher::n0_dns(secret_key.clone())));
}
if let Some(addr) = args.common.magic_ipv4_addr {
builder = builder.bind_addr_v4(addr);
}
if let Some(addr) = args.common.magic_ipv6_addr {
builder = builder.bind_addr_v6(addr);
}
// use a flat store - todo: use a partial in mem store instead
let suffix = rand::thread_rng().gen::<[u8; 16]>();
let cwd = std::env::current_dir()?;
let blobs_data_dir = cwd.join(format!(".sendme-send-{}", HEXLOWER.encode(&suffix)));
if blobs_data_dir.exists() {
println!(
"can not share twice from the same directory: {}",
cwd.display(),
);
std::process::exit(1);
}
tokio::fs::create_dir_all(&blobs_data_dir).await?;
let endpoint = builder.bind().await?;
let lp = LocalPool::default();
let ps = SendStatus::new();
let blobs = Blobs::persistent(&blobs_data_dir)
.await?
.events(ps.new_client().into())
.build(lp.handle(), &endpoint);
let router = iroh::protocol::Router::builder(endpoint)
.accept(iroh_blobs::ALPN, blobs.clone())
.spawn()
.await?;
let path = args.path;
let (temp_tag, size, collection) = import(path.clone(), blobs.store().clone()).await?;
let hash = *temp_tag.hash();
// wait for the endpoint to figure out its address before making a ticket
let _ = router.endpoint().home_relay().initialized().await?;
// make a ticket
let mut addr = router.endpoint().node_addr().await?;
apply_options(&mut addr, args.ticket_type);
let ticket = BlobTicket::new(addr, hash, BlobFormat::HashSeq)?;
let entry_type = if path.is_file() { "file" } else { "directory" };
println!(
"imported {} {}, {}, hash {}",
entry_type,
path.display(),
HumanBytes(size),
print_hash(&hash, args.common.format)
);
if args.common.verbose > 0 {
for (name, hash) in collection.iter() {
println!(" {} {name}", print_hash(hash, args.common.format));
}
}
println!("to get this data, use");
println!("sendme receive {}", ticket);
drop(temp_tag);
// Wait for exit
tokio::signal::ctrl_c().await?;
println!("shutting down");
tokio::time::timeout(Duration::from_secs(2), router.shutdown()).await??;
tokio::fs::remove_dir_all(blobs_data_dir).await?;
Ok(())
}
fn make_download_progress() -> ProgressBar {
let pb = ProgressBar::hidden();
pb.enable_steady_tick(std::time::Duration::from_millis(100));
pb.set_style(
ProgressStyle::with_template(
"{msg}{spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} {binary_bytes_per_sec}",
)
.unwrap()
.progress_chars("#>-"),
);
pb
}
pub async fn show_download_progress(
recv: async_channel::Receiver<DownloadProgress>,
total_size: u64,
) -> anyhow::Result<()> {
let mp = MultiProgress::new();
mp.set_draw_target(ProgressDrawTarget::stderr());
let op = mp.add(make_download_progress());
op.set_message(format!("{} Connecting ...\n", style("[1/3]").bold().dim()));
let mut total_done = 0;
let mut sizes = BTreeMap::new();
loop {
let x = recv.recv().await;
match x {
Ok(DownloadProgress::Connected) => {
op.set_message(format!("{} Requesting ...\n", style("[2/3]").bold().dim()));
}
Ok(DownloadProgress::FoundHashSeq { children, .. }) => {
op.set_message(format!(
"{} Downloading {} blob(s)\n",
style("[3/3]").bold().dim(),
children + 1,
));
op.set_length(total_size);
op.reset();
}
Ok(DownloadProgress::Found { id, size, .. }) => {
sizes.insert(id, size);
}
Ok(DownloadProgress::Progress { offset, .. }) => {
op.set_position(total_done + offset);
}
Ok(DownloadProgress::Done { id }) => {
total_done += sizes.remove(&id).unwrap_or_default();
}
Ok(DownloadProgress::AllDone(stats)) => {
op.finish_and_clear();
eprintln!(
"Transferred {} in {}, {}/s",
HumanBytes(stats.bytes_read),
HumanDuration(stats.elapsed),
HumanBytes((stats.bytes_read as f64 / stats.elapsed.as_secs_f64()) as u64)
);
break;
}
Ok(DownloadProgress::Abort(e)) => {
anyhow::bail!("download aborted: {e:?}");
}
Err(e) => {
anyhow::bail!("error reading progress: {e:?}");
}
_ => {}
}
}
Ok(())
}
fn show_get_error(e: anyhow::Error) -> anyhow::Error {
if let Some(err) = e.downcast_ref::<DecodeError>() {
match err {
DecodeError::NotFound => {
eprintln!("{}", style("send side no longer has a file").yellow())
}
DecodeError::LeafNotFound(_) | DecodeError::ParentNotFound(_) => eprintln!(
"{}",
style("send side no longer has part of a file").yellow()
),
DecodeError::Io(err) => eprintln!(
"{}",
style(format!("generic network error: {}", err)).yellow()
),
DecodeError::Read(err) => eprintln!(
"{}",
style(format!("error reading data from quinn: {}", err)).yellow()
),
DecodeError::LeafHashMismatch(_) | DecodeError::ParentHashMismatch(_) => {
eprintln!("{}", style("send side sent wrong data").red())
}
};
} else if let Some(header_error) = e.downcast_ref::<AtBlobHeaderNextError>() {
// TODO(iroh-bytes): get_to_db should have a concrete error type so you don't have to guess
match header_error {
AtBlobHeaderNextError::Io(err) => eprintln!(
"{}",
style(format!("generic network error: {}", err)).yellow()
),
AtBlobHeaderNextError::Read(err) => eprintln!(
"{}",
style(format!("error reading data from quinn: {}", err)).yellow()
),
AtBlobHeaderNextError::NotFound => {
eprintln!("{}", style("send side no longer has a file").yellow())
}
};
} else {
eprintln!(
"{}",
style(format!("generic error: {:?}", e.root_cause())).red()
);
}
e
}
async fn receive(args: ReceiveArgs) -> anyhow::Result<()> {
let ticket = args.ticket;
let addr = ticket.node_addr().clone();
let secret_key = get_or_create_secret(args.common.verbose > 0)?;
let mut builder = Endpoint::builder()
.alpns(vec![])
.secret_key(secret_key)
.relay_mode(args.common.relay.into());
if ticket.node_addr().relay_url.is_none() && ticket.node_addr().direct_addresses.is_empty() {
builder = builder.add_discovery(|_| Some(DnsDiscovery::n0_dns()));
}
if let Some(addr) = args.common.magic_ipv4_addr {
builder = builder.bind_addr_v4(addr);
}
if let Some(addr) = args.common.magic_ipv6_addr {
builder = builder.bind_addr_v6(addr);
}
let endpoint = builder.bind().await?;
let dir_name = format!(".sendme-get-{}", ticket.hash().to_hex());
let iroh_data_dir = std::env::current_dir()?.join(dir_name);
let db = iroh_blobs::store::fs::Store::load(&iroh_data_dir).await?;
let mp = MultiProgress::new();
let connect_progress = mp.add(ProgressBar::hidden());
connect_progress.set_draw_target(ProgressDrawTarget::stderr());
connect_progress.set_style(ProgressStyle::default_spinner());
connect_progress.set_message(format!("connecting to {}", addr.node_id));
let connection = endpoint.connect(addr, iroh_blobs::protocol::ALPN).await?;
let hash_and_format = HashAndFormat {
hash: ticket.hash(),
format: ticket.format(),
};
connect_progress.finish_and_clear();
let (send, recv) = async_channel::bounded(32);
let progress = iroh_blobs::util::progress::AsyncChannelProgressSender::new(send);
let (_hash_seq, sizes) =
get_hash_seq_and_sizes(&connection, &hash_and_format.hash, 1024 * 1024 * 32)
.await
.map_err(show_get_error)?;
let total_size = sizes.iter().sum::<u64>();
let total_files = sizes.len().saturating_sub(1);
let payload_size = sizes.iter().skip(1).sum::<u64>();
eprintln!(
"getting collection {} {} files, {}",
print_hash(&ticket.hash(), args.common.format),
total_files,
HumanBytes(payload_size)
);
// print the details of the collection only in verbose mode
if args.common.verbose > 0 {
eprintln!(
"getting {} blobs in total, {}",
sizes.len(),
HumanBytes(total_size)
);
}
let _task = tokio::spawn(show_download_progress(recv, total_size));
let get_conn = || async move { Ok(connection) };
let stats = iroh_blobs::get::db::get_to_db(&db, get_conn, &hash_and_format, progress)
.await
.map_err(|e| show_get_error(anyhow::anyhow!(e)))?;
let collection = Collection::load_db(&db, &hash_and_format.hash).await?;
if args.common.verbose > 0 {
for (name, hash) in collection.iter() {
println!(" {} {name}", print_hash(hash, args.common.format));
}
}
if let Some((name, _)) = collection.iter().next() {
if let Some(first) = name.split('/').next() {
println!("downloading to: {};", first);
}
}
export(db, collection).await?;
tokio::fs::remove_dir_all(iroh_data_dir).await?;
if args.common.verbose > 0 {
println!(
"downloaded {} files, {}. took {} ({}/s)",
total_files,
HumanBytes(payload_size),
HumanDuration(stats.elapsed),
HumanBytes((stats.bytes_read as f64 / stats.elapsed.as_secs_f64()) as u64),
);
}
Ok(())
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let args = match Args::try_parse() {
Ok(args) => args,
Err(cause) => {
if let Some(text) = cause.get(ContextKind::InvalidSubcommand) {
eprintln!("{} \"{}\"\n", ErrorKind::InvalidSubcommand, text);
eprintln!("Available subcommands are");
for cmd in Args::command().get_subcommands() {
eprintln!(" {}", style(cmd.get_name()).bold());
}
std::process::exit(1);
} else {
cause.exit();
}
}
};
let res = match args.command {
Commands::Send(args) => send(args).await,
Commands::Receive(args) => receive(args).await,
};
if let Err(e) = &res {
eprintln!("{e}");
}
match res {
Ok(()) => std::process::exit(0),
Err(_) => std::process::exit(1),
}
}