rightkit-send 0.2.0

Nearby file and text sharing over the open LocalSend v2 protocol, with Pulse extensions: TLS identity pinning, per-device trust, and macOS Bonjour discovery. Product-neutral; the host supplies identity, paths and policy.
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
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
//! Sending: `prepare-upload` to a peer, then each accepted file as an upload.

use super::error::{Error, peer_io};
use super::net::{self, Wire};
use super::proto::{
    self, DeviceInfo, FileMeta, PrepareUploadRequest, PrepareUploadResponse, PulseExtra,
};
use std::collections::BTreeMap;
use std::fs::File;
use std::io::{self, Read, Write};
use std::net::{IpAddr, TcpStream};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

/// Where to send: a device found on the network.
#[derive(Clone, Debug)]
pub struct Peer {
    pub ip: IpAddr,
    pub port: u16,
    pub https: bool,
    pub fingerprint: String,
    pub alias: String,
}

/// What the user chose to send.
#[derive(Clone, Debug)]
pub enum SendItem {
    Path(PathBuf),
    Text(String),
}

#[derive(Clone, Debug)]
enum Source {
    File(PathBuf),
    Text(String),
}

#[derive(Clone, Debug)]
pub struct Entry {
    pub id: String,
    /// Name as the receiver sees it; folders keep their structure ("Dir/a.txt").
    pub name: String,
    pub size: u64,
    pub mime: String,
    source: Source,
}

impl Entry {
    /// Opens the entry's content for reading: the file from disk, or the message text.
    ///
    /// Each call returns a fresh reader positioned at the start. A file that cannot be opened
    /// (moved, deleted, permission) returns the I/O error; nothing is cached, so a changed file is
    /// read as it is now.
    pub fn open_reader(&self) -> io::Result<Box<dyn Read + Send>> {
        match &self.source {
            Source::Text(text) => Ok(Box::new(io::Cursor::new(text.clone().into_bytes()))),
            Source::File(path) => Ok(Box::new(File::open(path)?)),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Phase {
    /// Waiting for the other device to accept.
    Waiting,
    Sending,
    /// A message (text only) is on the other device's screen. No longer
    /// reported early: a message waits for the other side's answer like a file.
    Delivered,
}

#[derive(Clone, Debug)]
pub struct Progress {
    pub phase: Phase,
    pub done: u64,
    pub total: u64,
    pub files_done: usize,
    pub files_total: usize,
    pub current: Option<String>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Outcome {
    Done,
    Declined,
    Cancelled,
}

const MAX_ENTRIES: usize = 20_000;

/// The files behind the user's choice. Folders are walked (symbolic links
/// inside them are skipped); a chosen path that is a link is followed.
pub fn expand(items: &[SendItem]) -> Result<Vec<Entry>, Error> {
    let mut entries: Vec<Entry> = Vec::new();
    for item in items {
        match item {
            SendItem::Text(text) => {
                if text.is_empty() {
                    continue;
                }
                entries.push(Entry {
                    id: format!("f{}", entries.len()),
                    name: format!("{}.txt", proto::random_hex(4)),
                    size: text.len() as u64,
                    mime: "text/plain".to_string(),
                    source: Source::Text(text.clone()),
                });
            }
            SendItem::Path(path) => {
                let metadata = std::fs::metadata(path).map_err(|e| Error::Unreadable {
                    path: path.display().to_string(),
                    detail: e.to_string(),
                })?;
                let name = path
                    .file_name()
                    .map(|n| n.to_string_lossy().into_owned())
                    .unwrap_or_else(|| "file".to_string());
                if metadata.is_dir() {
                    walk(path, &name, &mut entries)?;
                } else if metadata.is_file() {
                    push_file(&mut entries, path, name, metadata.len());
                } else {
                    return Err(Error::NotFileOrFolder {
                        path: path.display().to_string(),
                    });
                }
            }
        }
        if entries.len() > MAX_ENTRIES {
            return Err(Error::TooManyFiles { max: MAX_ENTRIES });
        }
    }
    if entries.is_empty() {
        return Err(Error::NothingToSend);
    }
    Ok(entries)
}

fn push_file(entries: &mut Vec<Entry>, path: &Path, name: String, size: u64) {
    let mime = proto::mime_for(&name).to_string();
    entries.push(Entry {
        id: format!("f{}", entries.len()),
        name,
        size,
        mime,
        source: Source::File(path.to_path_buf()),
    });
}

fn walk(directory: &Path, prefix: &str, entries: &mut Vec<Entry>) -> Result<(), Error> {
    let mut stack = vec![(directory.to_path_buf(), prefix.to_string())];
    while let Some((dir, relative)) = stack.pop() {
        let listing = std::fs::read_dir(&dir).map_err(|e| Error::Unreadable {
            path: dir.display().to_string(),
            detail: e.to_string(),
        })?;
        for child in listing.flatten() {
            let Ok(kind) = child.file_type() else {
                continue;
            };
            let name = format!("{relative}/{}", child.file_name().to_string_lossy());
            if kind.is_symlink() {
                continue;
            } else if kind.is_dir() {
                stack.push((child.path(), name));
            } else if kind.is_file() {
                let size = child.metadata().map(|m| m.len()).unwrap_or(0);
                push_file(entries, &child.path(), name, size);
            }
            if entries.len() > MAX_ENTRIES {
                return Err(Error::TooManyFiles { max: MAX_ENTRIES });
            }
        }
    }
    Ok(())
}

fn exchange(
    peer: &Peer,
    target: &str,
    json: Option<&[u8]>,
    timeout: Duration,
    register: &dyn Fn(Option<TcpStream>),
    sent: &mut dyn FnMut(),
) -> Result<net::Reply, Error> {
    let mut wire = net::connect(peer.ip, peer.port, peer.https, &peer.fingerprint, timeout)
        .map_err(|e| {
            peer_io(&peer.alias, &e, |peer, detail| Error::PeerUnreachable {
                peer,
                detail,
            })
        })?;
    register(wire.tcp().try_clone().ok());
    let payload = json.unwrap_or(&[]);
    let host = format!("{}:{}", peer.ip, peer.port);
    let result = net::call(
        &mut wire,
        "POST",
        &host,
        target,
        json.map(|_| "application/json"),
        payload.len() as u64,
        &mut |w| {
            w.write_all(payload)?;
            w.flush()?;
            sent();
            Ok(())
        },
    );
    wire.finish();
    register(None);
    result.map_err(|e| {
        peer_io(&peer.alias, &e, |peer, detail| Error::PeerLost {
            peer,
            detail,
        })
    })
}

fn query(session: &str, file: &str, token: &str) -> String {
    format!(
        "{}/upload?sessionId={}&fileId={}&token={}",
        proto::API,
        proto::url_encode(session),
        proto::url_encode(file),
        proto::url_encode(token)
    )
}

/// Ask the peer to accept `entries`, and send them once it does. Blocks.
/// `register` is told each connection's socket so another thread can break a
/// wait by shutting it down; `on` hears progress after every chunk.
pub fn deliver(
    me: &DeviceInfo,
    peer: &Peer,
    entries: &[Entry],
    clipboard: bool,
    cancel: &AtomicBool,
    register: &dyn Fn(Option<TcpStream>),
    on: &mut dyn FnMut(Progress),
) -> Result<Outcome, Error> {
    // Sends go only over a verified TLS connection to a pinned identity.
    if !peer.https {
        return Err(Error::PeerPlaintext {
            peer: peer.alias.clone(),
        });
    }
    if !net::valid_pin(&peer.fingerprint) {
        return Err(Error::InvalidIdentity {
            peer: peer.alias.clone(),
        });
    }
    let mut progress = Progress {
        phase: Phase::Waiting,
        done: 0,
        total: entries.iter().map(|e| e.size).sum(),
        files_done: 0,
        files_total: entries.len(),
        current: None,
    };
    on(progress.clone());

    let mut files = BTreeMap::new();
    for entry in entries {
        files.insert(
            entry.id.clone(),
            FileMeta {
                id: entry.id.clone(),
                file_name: entry.name.clone(),
                size: entry.size,
                file_type: entry.mime.clone(),
                sha256: None,
                preview: match &entry.source {
                    Source::Text(text) => Some(text.clone()),
                    Source::File(_) => None,
                },
                metadata: None,
            },
        );
    }
    let mut info = me.clone();
    info.announce = None;
    let pulse = clipboard.then_some(PulseExtra { clipboard: true });
    let body = serde_json::to_vec(&PrepareUploadRequest { info, files, pulse }).map_err(|e| {
        Error::Internal {
            detail: e.to_string(),
        }
    })?;
    let prepare = format!("{}/prepare-upload", proto::API);
    // The other person may take a while to say yes. A message counts as sent
    // only once they have accepted it (or allow this device), like a file.
    let reply = exchange(
        peer,
        &prepare,
        Some(&body),
        Duration::from_secs(190),
        register,
        &mut || {},
    );
    if cancel.load(Ordering::Relaxed) {
        return Ok(Outcome::Cancelled);
    }
    let reply = reply?;
    let session = match reply.status {
        200 => serde_json::from_slice::<PrepareUploadResponse>(&reply.body).map_err(|_| {
            Error::PeerReply {
                peer: peer.alias.clone(),
            }
        })?,
        204 => return Ok(Outcome::Done),
        403 => return Ok(Outcome::Declined),
        401 => {
            return Err(Error::PeerNeedsPin {
                peer: peer.alias.clone(),
            });
        }
        409 => {
            return Err(Error::PeerBusy {
                peer: peer.alias.clone(),
            });
        }
        413 => {
            return Err(Error::PeerTooLarge {
                peer: peer.alias.clone(),
            });
        }
        429 => {
            return Err(Error::PeerRateLimited {
                peer: peer.alias.clone(),
            });
        }
        other => {
            return Err(Error::PeerAnswer {
                peer: peer.alias.clone(),
                status: other,
            });
        }
    };
    // A partial accept sends only the files it wants.
    progress.files_total = entries
        .iter()
        .filter(|e| session.files.contains_key(&e.id))
        .count();
    progress.total = entries
        .iter()
        .filter(|e| session.files.contains_key(&e.id))
        .map(|e| e.size)
        .sum();
    progress.phase = Phase::Sending;
    on(progress.clone());

    for entry in entries {
        let Some(token) = session.files.get(&entry.id) else {
            continue;
        };
        if cancel.load(Ordering::Relaxed) {
            send_cancel(peer, &session.session_id);
            return Ok(Outcome::Cancelled);
        }
        progress.current = Some(entry.name.clone());
        on(progress.clone());
        let target = query(&session.session_id, &entry.id, token);
        let result = upload_one(peer, &target, entry, cancel, register, &mut progress, on);
        match result {
            Ok(()) => {
                progress.files_done += 1;
                on(progress.clone());
            }
            Err(error) => {
                if cancel.load(Ordering::Relaxed) {
                    send_cancel(peer, &session.session_id);
                    return Ok(Outcome::Cancelled);
                }
                return Err(error);
            }
        }
    }
    Ok(Outcome::Done)
}

fn upload_one(
    peer: &Peer,
    target: &str,
    entry: &Entry,
    cancel: &AtomicBool,
    register: &dyn Fn(Option<TcpStream>),
    progress: &mut Progress,
    on: &mut dyn FnMut(Progress),
) -> Result<(), Error> {
    let mut wire = net::connect(
        peer.ip,
        peer.port,
        peer.https,
        &peer.fingerprint,
        Duration::from_secs(120),
    )
    .map_err(|e| {
        peer_io(&peer.alias, &e, |peer, detail| Error::PeerUnreachable {
            peer,
            detail,
        })
    })?;
    register(wire.tcp().try_clone().ok());
    let host = format!("{}:{}", peer.ip, peer.port);
    let result = net::call(
        &mut wire,
        "POST",
        &host,
        target,
        Some("application/octet-stream"),
        entry.size,
        &mut |w: &mut Wire| match &entry.source {
            Source::Text(text) => {
                w.write_all(text.as_bytes())?;
                progress.done += text.len() as u64;
                on(progress.clone());
                Ok(())
            }
            Source::File(path) => {
                let mut file = File::open(path)?;
                let mut buffer = vec![0u8; 64 * 1024];
                let mut remaining = entry.size;
                while remaining > 0 {
                    if cancel.load(Ordering::Relaxed) {
                        return Err(io::Error::new(io::ErrorKind::Interrupted, "cancelled"));
                    }
                    let want = remaining.min(buffer.len() as u64) as usize;
                    let n = file.read(&mut buffer[..want])?;
                    if n == 0 {
                        return Err(io::Error::new(
                            io::ErrorKind::UnexpectedEof,
                            "the file got shorter",
                        ));
                    }
                    w.write_all(&buffer[..n])?;
                    remaining -= n as u64;
                    progress.done += n as u64;
                    on(progress.clone());
                }
                Ok(())
            }
        },
    );
    wire.finish();
    register(None);
    let reply = result.map_err(|e| {
        peer_io(&peer.alias, &e, |_, detail| Error::FileStopped {
            file: entry.name.clone(),
            detail,
        })
    })?;
    match reply.status {
        200 | 204 => Ok(()),
        403 => Err(Error::FileRefused {
            peer: peer.alias.clone(),
            file: entry.name.clone(),
        }),
        422 => Err(Error::FileDamaged {
            peer: peer.alias.clone(),
            file: entry.name.clone(),
        }),
        other => Err(Error::PeerAnswer {
            peer: peer.alias.clone(),
            status: other,
        }),
    }
}

/// Tell the peer the session is over. Best effort.
fn send_cancel(peer: &Peer, session: &str) {
    let target = format!(
        "{}/cancel?sessionId={}",
        proto::API,
        proto::url_encode(session)
    );
    let _ = net::request_json(
        peer.ip,
        peer.port,
        peer.https,
        &peer.fingerprint,
        "POST",
        &target,
        None,
        Duration::from_secs(5),
    );
}

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

    fn me() -> DeviceInfo {
        DeviceInfo {
            alias: "Me".into(),
            version: "2.0".into(),
            device_model: None,
            device_type: None,
            fingerprint: "f".repeat(64),
            port: 53317,
            protocol: "https".into(),
            download: false,
            announce: None,
        }
    }

    #[test]
    fn nothing_is_sent_over_plain_http() {
        let peer = Peer {
            ip: "127.0.0.1".parse().unwrap(),
            port: 9,
            https: false,
            fingerprint: "a".repeat(64),
            alias: "Plain".into(),
        };
        let entries = expand(&[SendItem::Text("hi".into())]).unwrap();
        let cancel = AtomicBool::new(false);
        let result = deliver(&me(), &peer, &entries, false, &cancel, &|_| {}, &mut |_| {});
        assert_eq!(
            result,
            Err(Error::PeerPlaintext {
                peer: "Plain".into()
            })
        );
    }

    #[test]
    fn a_peer_without_a_valid_fingerprint_is_refused_before_connecting() {
        let peer = Peer {
            ip: "127.0.0.1".parse().unwrap(),
            port: 9,
            https: true,
            fingerprint: "not-hex".into(),
            alias: "Odd".into(),
        };
        let entries = expand(&[SendItem::Text("hi".into())]).unwrap();
        let cancel = AtomicBool::new(false);
        let result = deliver(&me(), &peer, &entries, false, &cancel, &|_| {}, &mut |_| {});
        assert_eq!(result, Err(Error::InvalidIdentity { peer: "Odd".into() }));
    }

    #[test]
    fn empty_selections_and_missing_paths_are_typed() {
        assert!(matches!(expand(&[]), Err(Error::NothingToSend)));
        assert!(matches!(
            expand(&[SendItem::Path("/definitely/not/here/rightkit".into())]),
            Err(Error::Unreadable { .. })
        ));
    }

    #[test]
    fn open_reader_reads_text_and_files_and_reports_missing_files() {
        let entries = expand(&[SendItem::Text("hello".into())]).unwrap();
        let mut body = String::new();
        entries[0]
            .open_reader()
            .unwrap()
            .read_to_string(&mut body)
            .unwrap();
        assert_eq!(body, "hello");

        let dir = std::env::temp_dir().join(format!("rk-send-reader-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let path = dir.join("a.txt");
        std::fs::write(&path, b"file body").unwrap();
        let entries = expand(&[SendItem::Path(path.clone())]).unwrap();
        let mut bytes = Vec::new();
        entries[0]
            .open_reader()
            .unwrap()
            .read_to_end(&mut bytes)
            .unwrap();
        assert_eq!(bytes, b"file body");

        std::fs::remove_file(&path).unwrap();
        assert!(entries[0].open_reader().is_err());
        let _ = std::fs::remove_dir_all(&dir);
    }
}