Git · dadoum.zone
Repositories
Help
Report an Issue
apple-crates
Code
Commits
Branches
Tags
Search
Tree:
a74924e
Branches
Tags
main
apple-crates
airdrop
examples
receive.rs
Refactor and unify a lot of things for another project and implement airdrop support
Dadoum
commited
a74924e
at 2026-10-25 22:15:58
receive.rs
Blame
History
Raw
//! A tiny AirDrop receiver on a TCP port, for trying the protocol against //! another implementation on loopback: //! `cargo run -p airdrop --example receive -- 18771 /tmp/inbox [contacts]` //! Auto-accepts every Ask; with `contacts` it applies Contacts Only against the //! identifiers listed on stdin (one per line). use std::net::TcpListener; use std::sync::Arc; use airdrop::contacts::{ContactBook, Visibility}; use airdrop::identity::Identity; use airdrop::receiver::*; use airdrop::tls::{ClientAuth, server_config}; struct Print { visibility: Visibility, book: ContactBook, } impl Handler for Print { fn visibility(&self) -> Visibility { self.visibility } fn contacts(&self) -> ContactBook { self.book.clone() } fn name(&self) -> (String, String) { ("airdrop example".into(), "Linux1,1".into()) } fn ask(&self, ask: &AskRequest) -> Decision { println!( "ask from {:?}: {} file(s), {} link(s), account proven: {}, known contact: {}", ask.sender_name, ask.files.len(), ask.links.len(), ask.account.is_some(), ask.known_contact ); Decision::Accept } fn stored(&self, s: &Stored) { println!("stored {:?} ({} bytes)", s.names, s.bytes); } fn failed(&self, id: &str, why: &str) { println!("transfer {id} failed: {why}"); } } fn main() { env_logger_init(); let mut args = std::env::args().skip(1); let port: u16 = args.next().and_then(|p| p.parse().ok()).expect("PORT"); let dest = args.next().expect("DEST"); let contacts_only = args.next().as_deref() == Some("contacts"); let book = if contacts_only { let mut entries = Vec::new(); for line in std::io::stdin().lines() { entries.push(line.unwrap()); } ContactBook::from_entries(entries.iter().map(String::as_str), None) } else { ContactBook::new() }; let identity = Identity::self_signed("airdrop example").unwrap(); let tls = server_config( &identity, ClientAuth::Request { hint_subjects: vec![], }, ) .unwrap(); let mut settings = Settings::new(dest); settings.min_free_bytes = 0; let handler = Arc::new(Print { visibility: if contacts_only { Visibility::Contacts } else { Visibility::Everyone }, book, }); let listener = TcpListener::bind(("::1", port)).unwrap(); println!("listening on [::1]:{port}"); Arc::new(Receiver::new(settings, tls, handler)).serve(listener); } fn env_logger_init() {}