<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Bo Sun]]></title><description><![CDATA[Bo Sun]]></description><link>https://woolmate.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 04 Dec 2026 21:26:16 GMT</lastBuildDate><atom:link href="https://woolmate.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Local-only file sharing on Android: NSD, an embedded HTTP server, and TLS 1.3]]></title><description><![CDATA[Sometimes the simplest way to move a photo or a file to the person sitting next to you is surprisingly complicated. A chat app uploads it to a server on…]]></description><link>https://woolmate.hashnode.dev/local-only-file-sharing-on-android-nsd-an-embedded-http-server-and-tls-1-3</link><guid isPermaLink="true">https://woolmate.hashnode.dev/local-only-file-sharing-on-android-nsd-an-embedded-http-server-and-tls-1-3</guid><category><![CDATA[Android]]></category><category><![CDATA[Kotlin]]></category><category><![CDATA[showdev]]></category><category><![CDATA[networking]]></category><dc:creator><![CDATA[Bo Sun]]></dc:creator><pubDate>Tue, 01 Dec 2026 15:16:34 GMT</pubDate><content:encoded><![CDATA[<p>Sometimes the simplest way to move a photo or a file to the person sitting next to you is surprisingly complicated. A chat app uploads it to a server on another continent, then downloads it back. Email has size limits. And if there is no signal at all, most of these just stop working.</p>
<p>I wanted something boring and reliable: pick a file on one phone, hand it to a nearby phone over the Wi-Fi that is already in the room, and never let the bytes leave the local network. That turned into SendHop. This is a tour of how the pieces fit, and the parts that were harder than they looked.</p>
<h2>The constraints I designed around</h2>
<ul>
<li>Files only move over a local IP network: the same Wi-Fi, or a phone hotspot when there is no router.</li>
<li>The receiver should not be forced to install anything.</li>
<li>Nothing gets uploaded to a cloud, and there is no third-party script in the receiving page.</li>
<li>A batch can hold up to 20 items and 4 GiB, so large video files have to work, including interrupted downloads.</li>
</ul>
<h2>1. Finding each other on the network</h2>
<p>Before any bytes move, the phones have to find each other. I used Android's <code>NsdManager</code>, the platform wrapper around DNS-SD/mDNS. A device advertises a service of type <code>_sendhop._tcp.</code>, and the TXT records carry the small amount of metadata a peer needs before connecting:</p>
<ul>
<li>a protocol version,</li>
<li>a capabilities flag,</li>
<li>a short session prefix,</li>
<li>an ephemeral presence id.</li>
</ul>
<p>The important habit here is that seeing a service advertised is not the same as having a usable peer. A phone can sleep, change networks, or leave between the mDNS announcement and your connection. So reachability is treated as a verified, short-lived "presence" fact rather than a cached "this device exists" entry. History never proves a device is online right now.</p>
<h2>2. Two transports, depending on who is receiving</h2>
<p>There are two quite different receivers, and they use different transports.</p>
<h3>Native app to native app: TLS 1.3 with pinned identity</h3>
<p>When both sides run the app, the connection is TLS 1.3, and the certificate is not blindly trusted from a public CA. Each contact binds a long-term public key, and connections are validated against pinned identity/certificate fingerprints. That gives you authentication — you are talking to the contact you think — and encryption on top of a network that might be a coffee-shop hotspot.</p>
<p>Control messages and the receive/invite codes use a single CBOR/COSE format rather than ad-hoc JSON: one signed envelope, explicit versions, and unknown formats are rejected instead of best-effort parsed.</p>
<h3>A receiver with just a browser: a tiny HTTP server on the phone</h3>
<p>Forcing the other person to install an app to receive one file is a bad trade. So the sender can also spin up a short-lived guest web host. Under the hood it is a plain <code>java.net.ServerSocket</code> bound to the local address, serving a small page and the selected files over ordinary HTTP. The guest opens a link, or scans a QR code, in any browser, confirms, and downloads. There is no install, no account, and no third-party script.</p>
<p>The host is deliberately short-lived and token-gated: a guest token has an expiry, and the server only answers for the current session rather than exposing the whole filesystem.</p>
<h2>3. The hard part: big files and broken connections</h2>
<p>A 4 GiB transfer over a phone hotspot will, in real life, get interrupted. Browsers already know how to resume, so instead of inventing a resume protocol I implemented standard HTTP Range requests on the guest host.</p>
<p>The server keeps track of which byte ranges have actually been delivered for each item and merges them as chunks arrive, so a paused or reconnected download can ask for the missing slice with a <code>Range</code> header and continue instead of starting over. The bookkeeping that merges overlapping and adjacent ranges turned out to be fiddlier than the socket itself — off-by-one errors at chunk boundaries are easy, and the merge logic has to cap how many ranges it will track.</p>
<h2>4. Keeping the data on-device</h2>
<p>Because the product promise is "nothing leaves the local network," the boundary matters as much as the features:</p>
<ul>
<li>File content, identity, the QR codes, and tokens never go into analytics or ad SDK payloads.</li>
<li>The guest web page has no third-party scripts at all.</li>
<li>Selected items are held as references in memory for the active transfer; the send flow does not persist a long-lived index of the user's files.</li>
</ul>
<h2>How it is structured</h2>
<p>The Android app is a multi-module Gradle project in Kotlin with Jetpack Compose and a version catalog, split into thin <code>app_cn</code> and <code>app_global</code> shells over shared modules. The networking lives behind clear seams: <code>lib_discovery</code> for presence, <code>lib_channel_wifi</code> for NSD, the guest web host and the native transport, <code>lib_security</code> and <code>lib_protocol</code> for the CBOR/COSE envelopes and identity, and <code>lib_transport</code> for the transfer state machines and frame streams. Keeping the Wi-Fi channel, the protocol, and the transfer state machine separate is what let me add the browser receiver without tangling it into the native TLS path.</p>
<h2>Try it</h2>
<p>SendHop is live on Google Play. The landing page with the store links is here:</p>
<p><a href="https://link.woolmate.cn/apps/sendhop?region=global&amp;clientPlatform=android&amp;locale=en-US&amp;ref=ihY0U1RFJv0P5RgPSffYZ4gQWCZAckcx">https://link.woolmate.cn/apps/sendhop?region=global&amp;clientPlatform=android&amp;locale=en-US&amp;ref=ihY0U1RFJv0P5RgPSffYZ4gQWCZAckcx</a></p>
<p>The quickest way to test it is to connect two phones to the same Wi-Fi — or have one open a hotspot — and send a photo from the app to the other phone's browser.</p>
<hr />
<p>A note on AI: I used an AI coding agent to help write parts of the code. The architecture, product decisions, and review were mine, and I am describing the implementation as it actually exists rather than as a generic "how a file-sharing app works."</p>
]]></content:encoded></item></channel></rss>