Sending files with nobody in the middle
How BiteDrop gets two browsers talking directly, using free MQTT brokers for the handshake and nothing for the files.
Every few days I need to move a file from my phone to my laptop, or from my laptop to someone sitting next to me. The usual options all send it through somebody’s server first. I wanted something that just opens a pipe between two browsers, with no account, no app, and no upload.
So I built BiteDrop. It’s a single static page. You open it on both devices, one side gets a four-digit code, the other side types it in, and files go straight across.
The interesting part is that “straight across” still needs a little help to get started.
The handshake problem
Browsers can talk to each other directly with WebRTC, but they can’t find each other on their own. Before a connection exists, each side has to send the other a description of itself: what it supports, and the network addresses it might be reachable on. That blob is usually one or two kilobytes. Every WebRTC app needs some way to carry it, and that’s normally a server the app’s author runs.
I didn’t want to run a server. The page is a handful of static files on Cloudflare Pages, and I wanted it to stay that way.
It turns out there’s already a lot of free infrastructure for passing small messages around: public MQTT brokers. HiveMQ, EMQX and Mosquitto all run open brokers you can reach over a WebSocket from a browser. You subscribe to a topic, publish to it, and anyone else on that topic gets the message. That’s exactly the shape of a handshake.
Not trusting the brokers
Public brokers are public. Anyone can subscribe to any topic, so the handshake can’t go over them in the clear, and I didn’t want someone else to be able to inject a fake one either.
The short code solves both. It’s four digits, short enough to read out across a room. Both devices run it through PBKDF2 with 200,000 iterations of SHA-256 and get 48 bytes back. The first 32 become an AES-GCM key. The rest becomes the topic name.
1
2
3
4
5
6
7
const bits = await crypto.subtle.deriveBits(
{ name: 'PBKDF2', salt: enc.encode('bitedrop/v1'), iterations: 200000, hash: 'SHA-256' },
base,
384,
);
const key = await crypto.subtle.importKey('raw', bits.slice(0, 32), 'AES-GCM', false, ['encrypt', 'decrypt']);
const topic = 'bitedrop/v1/' + hex(bits.slice(32));
So the broker sees a random-looking topic and some ciphertext. It can’t read the handshake, and it can’t forge one, because it doesn’t have the key. Four digits is a deliberate trade-off, though: there are only 10,000 codes, so someone actively trying could guess one while you wait. The code keeps out casual snooping, not a determined attacker. It also only lives for a moment, since it’s single-use: the first device to join gets the connection and the code stops listening.
Free brokers go down, so BiteDrop connects to all three and publishes every message to each of them. The receiving side gets duplicates and ignores them. Anything that fails to decrypt is dropped without comment, which also takes care of whatever else happens to be on the topic.
Once the handshake is done, the brokers are out of the picture. The files travel over the WebRTC connection, which is encrypted with DTLS by the browser.
Moving the bytes
The file transfer itself is simpler than I expected, with two details that mattered.
The first is message size. A WebRTC data channel has a maximum message size that varies by browser, so BiteDrop reads it from the connection and sends 64 KB chunks when it can, falling back to 16 KB when the browser doesn’t say.
The second is backpressure. If you call send() in a tight loop, the browser happily buffers the whole file in memory and the progress bar lies to you. The fix is to watch bufferedAmount and wait whenever it goes above 8 MB:
1
2
3
4
for (let i = 0; i < block.byteLength; i += this.chunk) {
if (ch.bufferedAmount > HIGH_WATER) await this.drained();
ch.send(block.slice(i, i + this.chunk));
}
The sender’s progress bar doesn’t count what it has sent. It counts what the other side says it has received: the receiver sends an acknowledgement every 512 KB. When the bar reaches the end, the file is actually there.
One bug I didn’t see coming: if you drag in a Word document that’s still open, Word can autosave halfway through the transfer and the read fails. Files up to 256 MB now get copied into memory the moment you drop them, so later edits to the original can’t break the send.
What it can’t do
BiteDrop doesn’t use relay servers, and that’s a real limitation. Some networks don’t allow a direct connection, mainly mobile carriers that put many phones behind one shared address. On those, the connection just fails. Two devices on the same Wi-Fi almost always connect.
Received files are held in memory until you save them, so multi-gigabyte files can be too much for a phone. And both pages have to stay open until the transfer finishes.
I’m fine with those trade-offs. The point was a tool where I can say exactly who sees what: Cloudflare sees that you loaded a page, the brokers see a couple of kilobytes they can’t read, a STUN server sees your public IP address, and your files go to the other device and nowhere else.
You can use it at bd.almanie.dev. It’s plain HTML, CSS and JavaScript with no build step, and the code is on GitHub if you want to read it or run your own copy.