← Home

Session

A mobile proxy network for agents, running on a ~1MB Rust core in millions of opt-in devices


Date

2024–25

session was a network i built with an incredible team. we were building infrastructure that lets agents use the web: proxies, harnesses for browser use and computer use, and large scale data pipelines for training data.

the core of it was the proxy network. app partners shipped our sdk inside their apps to users who opted in for rewards, and every phone running it became an exit node. at its peak that was ~10M available active devices, pushing 10 Gbps+ while each phone held the client in under 1.8 MB of memory. the hard part was doing that without impacting battery or giving end users a worse experience.

the shape of it

this is a high-level sketch of the design, simplified from the real implementation.

  • the phone sdk: a rust core, compiled for android and bridged into kotlin over jni.
  • relays: rust servers on tokio. customers point any http or socks client at them.
  • discovery and a device registry: phones ask discovery which relay to dial. every online device lands in redis with its geoip, so a customer can ask for an exit right in the proxy username, e.g. user,country-US,city-NewYork,zip-10001,session-12345,sttl-3600. session pins you to the same phone, sttl says for how long.
one websocket, ping every 30sCONNECT example.com as user,country-USpick a US phone from the registrydial example.com, then dial me backTCP connectTCP connect + headerraw bytes flow through relay and phoneAgentRelayPhone (SDK)WebsiteAgentRelayPhone (SDK)Website

phones never listen

a phone can't accept inbound connections, it sits behind carrier nat. so everything runs backwards. each device keeps exactly one websocket open to its relay and does nothing else; a ping every 30 seconds keeps it alive.

when a customer request comes in, the relay sends the phone a single control message: connect to this target, then dial me back here. the phone dials both at the same time, writes a tiny header on the relay socket (6 magic bytes, the first six of SHA1("SESSION"), then a version byte, the connection id, and whether the target connected), and from there it's a raw tcp pipe. no framing, no json and no websocket on the data path. the websocket only ever carries control messages.

why it barely touched the battery

what drains a phone is the cpu and the radio waking up. the design keeps both quiet when idle and does the minimum when busy:

  • idle is nearly free. one open socket and a ping every 30 seconds. no polling, no background jobs.
  • bytes never touch the jvm. kotlin only starts and stops the client. the rust core owns the sockets and moves data socket to socket.
  • nothing between the sockets. no buffering layers, no re-encoding, TCP_NODELAY on both ends, so a request goes through in one pass instead of waking the device again later.
  • a tiny native library. we rebuilt rust's standard library from source for every release (-Z build-std with optimize_for_size), with opt-level = "z", fat lto, a single codegen unit, panic = "abort" and everything stripped. the whole proxy client is ~1.3 MB on arm64 and under 1 MB on armv7.

zero-copy relays

on the relay side, each connection starts with a normal userspace copy for its first 8 KB, which is cheap for handshakes and small requests. once a connection crosses 8 KB it switches to splice(): data goes socket → kernel pipe → socket without ever being copied into our process. we bumped the pipe size with F_SETPIPE_SZ, and fell back to tokio's copy_bidirectional off linux. we also prototyped the same zero-copy path on the phones themselves.

haproxy sat in front of the relays: proxy traffic was load-balanced, websockets were pinned to the relay a device had registered with, and timeouts were long enough for day-long tunnels.