sinhsac.github.io / photosync

PhotoSync

Offline-first, peer-to-peer photo & video library sync engine written in Rust with a native Android JNI client — zero cloud, zero accounts, mutual TLS 1.3 security, and 4 MB resumable transfers over LAN or Wi-Fi hotspot.

Rust 1.88+ Mutual TLS 1.3 Android API 24+ (Kotlin + JNI) Bundled SQLite WAL MIT License

Core Architecture & Security Guarantees

  • Direct LAN & Hotspot Discovery: Uses per-interface UDP multicast announcements (224.0.0.187:53411), TCP callbacks, /24 subnet-scan fallback, or explicit IP:port addressing without internet access.
  • Mutual TLS 1.3 + 6-Digit Code-Bound Pairing: Self-signed peer certificates (rcgen + rustls/ring) enforce SHA-256 fingerprint pinning inside the TLS 1.3 handshake. Single-use 6-digit pairing codes are bound via HMAC-SHA256 to both peer fingerprints and session nonces (5-minute expiry, 3-attempt limit). Subsequent syncs reconnect automatically via pinned fingerprints with no code re-entry.
  • Two-Tier Content-Addressed Deduplication:
    • Tier 1 (quick_hash): Fast 192 KB sampled hash (SHA-256(size || head 64 KB || middle 64 KB || tail 64 KB)) for rapid scanning and manifest filtering.
    • Tier 2 (Full SHA-256): Streaming authoritative hash computed during transfer and cached in SQLite (sent_log / received_asset) so hash collisions never skip unique files and repeat syncs skip duplicates at zero wire cost.
  • 4 MB Chunked, Resumable Transfers: Every 4 MB chunk carries per-chunk SHA-256 verification. Receiver-authoritative offsets are flushed at chunk boundaries; interrupted multi-GB videos truncate to the last verified boundary and resume cleanly on reconnect.
  • Native Android MediaStore Integration: Paginated _ID-watermark scanning avoids OOM on large galleries, writes directly into IS_PENDING = 1 staging entries (Pictures/PhotoSync, Movies/PhotoSync), and publishes atomically with preserved DATE_TAKEN and DATE_MODIFIED timestamps only after full-file verification.

Workspace Structure

Crate / Folder Responsibility
crates/core (photosync-core)Platform-agnostic sync engine, SQLite WAL catalog (schema/001_initial.sql), chunking, HMAC pairing, protocol framing.
crates/net (photosync-net)Tokio async runtime, mutual TLS 1.3, UDP multicast & subnet discovery, heartbeats, transfer orchestrator.
crates/android (photosync-android)Rust cdylib (libphotosync_android.so) JNI bridge implementing LibrarySource and LibrarySink over Android MediaStore.
crates/ffi (photosync-ffi)Minimal C FFI bring-up library (cdylib + staticlib).
crates/dev (psdev)Desktop CLI & fault-injection test harness (MITM, bit-flips, cut/resume, collision tests, LAN serve/send).
android/Kotlin bring-up app (MainActivity.kt, Native.kt, PhotoStore.kt) with integrated Gradle cargoNdkBuild.

Prerequisites & Host Build

Install Rust 1.88.0+ (pinned in rust-toolchain.toml) and build the desktop/core workspace:

git clone https://github.com/sinhsac/photosync.git
cd photosync
cargo build
Host vs. Android Crate

crates/android is excluded from default-members in Cargo.toml because it depends on Android JNI and std::os::fd. Running cargo build compiles photosync-core, photosync-net, photosync-ffi, and photosync-dev cleanly on Linux, macOS, and Windows.

Syncing Two Devices Over LAN (psdev)

Run the receiver on one machine and the sender on another machine on the same Wi-Fi or hotspot:

# 1. On the RECEIVING machine (prints a 6-digit pairing code & announces via UDP multicast)
cargo run -p photosync-dev -- serve ./received_photos

# 2. On the SENDING machine (auto-discovers receiver on LAN)
cargo run -p photosync-dev -- send ./source_photos 482913

# Or pass an explicit receiver IP:port if multicast is blocked
cargo run -p photosync-dev -- send ./source_photos 482913 192.168.1.19:53411

Running the Fault-Injection & Verification Suite

psdev includes end-to-end tests covering SQLite invariants, real mTLS 1.3 handshakes, MITM rejection, mid-file disconnects, corrupted chunks, and quick-hash collisions:

# Verify SQLite linking, schema migrations & candidate anti-join
cargo run -p photosync-dev -- selfcheck

# Verify 6-digit HMAC pairing, MITM resistance, expiry & 3-attempt budget
cargo run -p photosync-dev -- pairing

# Real TCP + mTLS 1.3 handshakes (honest, MITM relay, fingerprint mismatch)
cargo run -p photosync-dev -- handshake

# Inspect local network interfaces visible to discovery
cargo run -p photosync-dev -- netinfo

# Generate a 45 MB fixture and test clean, cut/resume, bit-flip & misaligned chunks
cargo run -p photosync-dev -- fixture devdata/sample.bin 45
cargo run -p photosync-dev -- transfer devdata/sample.bin all

# Test quick-key collision safety (two distinct files with identical sampled bytes)
cargo run -p photosync-dev -- collide devdata/src
cargo run -p photosync-dev -- scan devdata/src
cargo run -p photosync-dev -- status

# End-to-end local sync, codeless resume, and multicast discovery sync
cargo run -p photosync-dev -- sync devdata/src devdata/dst
cargo run -p photosync-dev -- resume-session devdata/src devdata/dst
cargo run -p photosync-dev -- discover devdata/src devdata/dst

Building the Android App & JNI Library

Prerequisites: JDK 17+, Android SDK (API 34), Android NDK, and cargo-ndk:

# Install cargo-ndk and arm64 Android target
cargo install cargo-ndk
rustup target add aarch64-linux-android

# Build the Android APK (Gradle automatically runs cargoNdkBuild with --platform 24)
cd android
./gradlew assembleDebug

# Optional: build for multiple ABIs (e.g., physical device + x86_64 emulator)
./gradlew assembleDebug -Ppsync.abis=arm64-v8a,x86_64

Manual JNI / C FFI Compilation

# Android JNI library only (--platform 24 required for getifaddrs in Bionic)
cargo ndk -t arm64-v8a --platform 24 -o android/app/src/main/jniLibs build -p photosync-android --release

# C FFI bring-up library for multiple Android ABIs
cargo ndk -t arm64-v8a -t armeabi-v7a -t x86_64 -o jniLibs build -p photosync-ffi --release

Technical Specifications