Git · dadoum.zone
Repositories
Help
Report an Issue
apple-crates
Code
Commits
Branches
Tags
Search
Tree:
a74924e
Branches
Tags
main
apple-crates
airdrop
examples
send.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
send.rs
Blame
History
Raw
//! Send files to an AirDrop receiver at a known address: //! `cargo run -p airdrop --example send -- [::1]:18771 FILE...` //! The identity is self-signed, or the Apple one in `$AIRDROP_KEYS` (a //! directory with certificate.pem, key.pem and validation_record.cms). use std::path::{Path, PathBuf}; use airdrop::connect::Endpoint; use airdrop::identity::Identity; use airdrop::sender::{Client, Outgoing, Sender}; fn main() { let mut args = std::env::args().skip(1); let target = args.next().expect("HOST:PORT"); let files: Vec<Outgoing> = args .map(|p| Outgoing::new(Path::new(&p)).expect("a regular file")) .collect(); let identity = match std::env::var_os("AIRDROP_KEYS").map(PathBuf::from) { Some(dir) => Identity::from_pem( &std::fs::read(dir.join("certificate.pem")).unwrap(), b"", &std::fs::read(dir.join("key.pem")).unwrap(), std::fs::read(dir.join("validation_record.cms")).ok(), ) .unwrap(), None => Identity::self_signed("airdrop example sender").unwrap(), }; println!( "identity: {}", if identity.is_apple() { "Apple" } else { "self-signed" } ); let sender = Sender::new( &identity, "airdrop example", "Linux1,1", "aabbccddeeff", "lo", ) .unwrap(); let (host, port) = target.rsplit_once(':').unwrap(); let host = host.trim_matches(|c| c == '[' || c == ']'); let endpoint = Endpoint::resolve(host, port.parse().unwrap(), "lo").unwrap(); let mut client = Client::new(sender, endpoint); println!("discover: {:?}", client.discover().unwrap()); client.begin_transfer().unwrap(); println!("ask: {:?}", client.ask(&files).unwrap()); println!("upload: {:?}", client.upload(&files, |_| true).unwrap()); }