Userspace TCP/UDP port forwarder for Android — no VpnService, no system
TUN device, no root. It wires one local port to another and can pin each
forward's upstream connection to a specific network (Wi‑Fi, cellular,
ethernet), without any elevated privilege. It can also run a SOCKS5 proxy
whose upstream is pinnable the same way.
Switchboard is a thin Kotlin + Jetpack Compose (Material 3) front end over the
portmux forwarding engine, which it
consumes as prebuilt static libraries from portmux's GitHub releases and
links into a single JNI shared library (libswitchboard.so).
- Platform: Android,
compileSdk/targetSdk37 (Android 17),minSdk23 - ABIs: arm64‑v8a, armeabi‑v7a, x86_64, x86
- License: MIT
- What it's for
- How it works
- Architecture & data flow
- Component reference — every file, its purpose, and its functions
- Egress modes
- SOCKS5 proxy
- Permissions
- Build
- Usage examples
- Verified behavior
- Limitations
A single device often runs several apps that each listen on a fixed local port,
and you want to connect them without root, without a VPN, and without the OS's
one-and-only VpnService TUN slot. Concrete cases:
- MCP server → reverse‑SSH tunnel. An MCP server listens on a fixed port; a reverse‑SSH forward lands on another local port. Switchboard bridges them.
- DAW → VST host app. Route a DAW's network port to a VST host app's port.
- Reach a privileged local service via a high port. Listen on
:8443, forward to a service on:443(connecting out to a privileged port needs no privilege). - Pin a forward to a chosen network. Force a forward's upstream over cellular while Wi‑Fi is the default (or vice‑versa) — e.g. keep one flow on a metered/unmetered link deliberately.
The engine terminates the client's TCP/UDP connection on a local listener, then
opens a separate kernel socket to the target and splices bytes between the two
with backpressure. UDP is handled per source‑address flow with idle eviction.
Everything runs in userspace on a single cooperative coroutine scheduler
(hev-task). There is no packet capture and no TUN — so no VpnService
permission and no interference with a real VPN.
Egress pinning works by calling the NDK's android_setsocknetwork() on the
upstream socket before it connects, binding that connection to a specific
Android Network. For a non‑default network the app first requestNetwork()s
the transport so the framework permits the bind.
┌───────────────────────────── app process ──────────────────────────────┐
│ │
│ Compose UI ──> MainViewModel ──> ForwarderService (foreground) │
│ (routes) (state) │ │
│ │ worker thread │
│ ┌──────────────┴───────────────┐ │
│ │ NetworkRequester.resolveHandle│ (pin routes) │
│ └──────────────┬───────────────┘ │
│ │ JNI │
│ libswitchboard.so │
│ ├─ switchboard-jni.c (bridge + bind_hook) │
│ ├─ libportmux.a (engine) │
│ └─ libhev-task-system.a (coroutines) │
└─────────────────────────────────────────────────────────────────────────┘
Per TCP connection: client ──> :listenPort (portmux)
accept, then connect(target) with egress pin
splice bytes both ways ──> target
Every source file, its purpose, and its public functions.
The C glue between Kotlin and the portmux engine. Loaded as libswitchboard.so.
| Function (JNI) | Kotlin name | Purpose |
|---|---|---|
Java_..._nativeNew |
nativeNew() |
Create a portmux instance; returns an opaque long handle. |
Java_..._nativeAddRoute |
nativeAddRoute(...) |
Register one forwarding route (proto, listen, target, egress kind, egress arg, network handle). Builds a PortmuxEgress and, for CALLBACK egress, allocates a per‑route context carrying the net_handle_t. |
Java_..._nativeAddSocks |
nativeAddSocks(...) |
Register a SOCKS5 proxy listener (listen, egress kind, egress arg, network handle). No fixed target; reuses the same PortmuxEgress construction as nativeAddRoute via portmux_add_socks(). |
Java_..._nativeRun |
nativeRun(handle) |
Blocks: runs the portmux event loop until stopped. Must be on a worker thread. |
Java_..._nativeStop |
nativeStop(handle) |
Signal the loop to shut down (wakes the listener/flow tasks). |
Java_..._nativeDestroy |
nativeDestroy(handle) |
Free the instance. |
bind_hook(fd, ctx) (static) |
— | portmux's CALLBACK egress hook. Runs on the worker thread before connect(). Calls android_setsocknetwork(handle, fd) to pin the upstream socket to a Network. Pure native — no JNI up‑call (an earlier JNI up‑call crashed with an invalid‑transition‑frame abort). |
Imports the two prebuilt static archives (libportmux.a, libhev-task-system.a)
for the current ABI from PORTMUX_LIBS_DIR, compiles switchboard-jni.c, and
links portmux + hevtask + android (for android_setsocknetwork) + log
into libswitchboard.so. Link order matters: portmux depends on hevtask.
Thin wrapper over the JNI methods plus the enum constants mirrored from
portmux.h.
external fun nativeNew(): Longexternal fun nativeAddRoute(handle, proto, listenAddr, listenPort, targetAddr, targetPort, egressKind, egressArg, networkHandle): Intexternal fun nativeAddSocks(handle, listenAddr, listenPort, egressKind, egressArg, networkHandle): Int— register a SOCKS5 proxy listener (no fixed target).external fun nativeRun(handle: Long)— blocks; call off the main thread.external fun nativeStop(handle: Long)external fun nativeDestroy(handle: Long)- Constants:
PROTO_TCP=0,PROTO_UDP=1;EGRESS_NONE=0,EGRESS_SOURCE=1,EGRESS_DEVICE=2,EGRESS_CALLBACK=3. init { System.loadLibrary("switchboard") }
enum Proto { TCP, UDP }enum EgressMode { NONE, SOURCE, DEVICE, NETWORK }— see Egress modes.enum NetTransport { ANY, WIFI, CELLULAR, ETHERNET }— which transport a NETWORK route shouldrequestNetwork().data class Route(id, name, proto, listenAddr, listenPort, targetAddr, targetPort, egress, egressArg, networkHandle, transport, socks)socks: Boolean— when true the route is a SOCKS5 proxy listener (no fixed target;proto/targetAddr/targetPortare ignored).fun summary(): String— one‑line human label, e.g.tcp 127.0.0.1:6001 → 1.1.1.1:80 via cellular, or for SOCKSsocks5 127.0.0.1:1080 via cellular.
Persists routes as a JSON array in SharedPreferences (switchboard.routes /
key routes). No external JSON dependency.
fun load(ctx): List<Route>— parse persisted routes (tolerant of missing fields).fun save(ctx, routes: List<Route>)— serialize and store.
Enumerates currently‑available networks for the egress picker.
data class EgressNetwork(handle: Long, label: String, transport: NetTransport)fun available(ctx): List<EgressNetwork>— walksConnectivityManager's networks and returns a label (Wi‑Fi · internet · active, …), the opaquenetworkHandle, and the transport. Needs no location permission.
Requests specific transports via ConnectivityManager.requestNetwork() and
holds the requests for the forwarder's lifetime, which is what lets the app pin
a socket to a network that is not the active default.
fun resolveHandle(transport: NetTransport, fallbackHandle: Long, timeoutMs=8000): Long— ensure a network oftransportis requested, block until it resolves (or timeout), return its live handle (falling back to the stored handle). ForANY, returnsfallbackHandleunchanged.fun releaseAll()— unregister all held requests; call on stop.
Foreground service that owns the portmux instance and runs it on a dedicated
worker thread. No VpnService.
onStartCommand(...)— loads routes; enters the foreground; on a worker thread creates the portmux instance, resolves each NETWORK route's handle viaNetworkRequester, adds every route (vianativeAddSocksfor SOCKS routes,nativeAddRouteotherwise), and callsnativeRun(blocks).onDestroy()—nativeStop, joins the worker,nativeDestroy, andNetworkRequester.releaseAll().startForegroundSafely(routeCount): Boolean— enters foreground passingFOREGROUND_SERVICE_TYPE_SPECIAL_USEon API 34+ (required at runtime there), wrapped so a refusal stops the service instead of crashing.buildNotification(routeCount): Notification— ongoing, low‑importance,category=service, with a tapPendingIntentto open the app.- companion
start(ctx)/stop(ctx)— start as a foreground service / stop.
onCreate(...)— requestsPOST_NOTIFICATIONSat runtime on API 33+ (without it the FGS notification is silently suppressed), then sets the Compose content.
routes: StateFlow<List<Route>>,running: StateFlow<Boolean>,networks: StateFlow<List<EgressNetwork>>— observable UI state.fun addRoute(r),fun removeRoute(id)— mutate + persist routes.fun refreshNetworks()— re‑enumerate available networks.fun start()/fun stop()— start/stopForwarderService.
Top‑level screen: Start/Stop button + status, the route list (each card shows
Route.summary() and a Delete action), and a FAB to add a route.
Form to create a route: name, a route‑type toggle (Forward / SOCKS5 proxy), TCP/UDP (forwards only), listen address/port, target address/port (forwards only — hidden for SOCKS), and an egress selector (Default / Network / Source IP). For Network it lists available networks to pick (recording both handle and transport). Height‑capped and scrollable.
Each route chooses how its upstream (target) socket leaves the device.
Mirrors PortmuxEgress in the engine.
Mode (EgressMode) |
JNI kind | Needs | Behavior |
|---|---|---|---|
NONE |
0 | — | Default route / loopback. |
SOURCE |
1 | — | bind() the upstream socket to a local source IP (egressArg). |
DEVICE |
2 | root / CAP_NET_RAW |
SO_BINDTODEVICE to an interface name. Not exposed in the UI (needs privilege). |
NETWORK |
3 (CALLBACK) | CHANGE_NETWORK_STATE |
requestNetwork() the chosen transport, then android_setsocknetwork() the upstream socket to it. The no‑root way to pin egress. |
Besides fixed one‑to‑one forwards, a route can instead be a SOCKS5 proxy listener. Unlike a forward it has no fixed target — each client supplies the destination per connection in the SOCKS5 request. The engine implements RFC 1928 no‑auth with both CONNECT (TCP) and UDP ASSOCIATE.
Crucially, the proxy's upstream sockets go through the same per‑route egress
as a normal forward, so a SOCKS5 listener can be pinned to a specific network —
including the no‑root android_setsocknetwork() pin — and every connection the
proxy dials then egresses over that network. Point any SOCKS5‑aware client (a
browser, curl --socks5, an app's proxy setting) at the listener and all of its
traffic leaves over the chosen link.
In the app, pick SOCKS5 proxy as the route type in the Add dialog. The
target fields disappear (there is no fixed target) and only the listen
address/port and the egress selector remain. A SOCKS route renders as, e.g.,
socks5 127.0.0.1:1080 via cellular.
A SOCKS5 proxy dials arbitrary destinations by nature — anything the client asks for. Switchboard is the human‑facing UI with no allow‑list gate, so the egress selector is the control: choose which link the proxy's upstream uses.
Example — SOCKS5 on 127.0.0.1:1080 pinned to cellular:
Route type: SOCKS5 proxy
Listen: 127.0.0.1 : 1080
Egress: Network → (pick the Cellular entry)
Then e.g. curl --socks5-hostname 127.0.0.1:1080 https://ifconfig.me from an
on‑device shell egresses over cellular even while Wi‑Fi is the system default.
Privileged‑port caveat: like any listener, a SOCKS5 proxy cannot bind a port
< 1024from the sandboxed app (the OS gates it). Use≥ 1024; the SOCKS default1080is fine.
Declared in AndroidManifest.xml:
| Permission | Why |
|---|---|
INTERNET |
Open sockets. |
ACCESS_NETWORK_STATE |
Enumerate networks for the egress picker. |
CHANGE_NETWORK_STATE |
requestNetwork() + bind to a non‑default network. |
POST_NOTIFICATIONS |
Show the foreground‑service notification (API 33+, requested at runtime). |
FOREGROUND_SERVICE + FOREGROUND_SERVICE_SPECIAL_USE |
Run the forwarder as a foreground service. |
Notably not required: location, NEARBY_WIFI_DEVICES, or VpnService /
BIND_VPN_SERVICE — Switchboard neither scans Wi‑Fi nor uses a TUN.
Requires the Android SDK (platform 37) and NDK r28. The portmux prebuilt
artifacts are fetched automatically from the pinned release
(portmuxVersion in gradle.properties).
./gradlew :app:assembleDebug
# APK: app/build/outputs/apk/debug/app-debug.apkThe Gradle fetchPortmux task downloads the per‑ABI release tarballs into
app/.portmux/ and CMake links them into libswitchboard.so. A plain clone
builds without any submodule init.
Toolchain: AGP 9.3.2, Gradle 9.7.1, Kotlin 2.4.10, JVM target 21.
In the app: tap +, fill the form, Add, then Start.
1 — MCP server to a reverse‑SSH forward (loopback):
Proto: TCP
Listen: 127.0.0.1 : 39811 (where the MCP client connects)
Target: 127.0.0.1 : 2222 (where ssh -R landed the tunnel)
Egress: Default
2 — DAW to a VST host app (loopback):
Proto: TCP
Listen: 127.0.0.1 : 9000
Target: 127.0.0.1 : 9001
Egress: Default
3 — Forward to a privileged target port:
Proto: TCP
Listen: 127.0.0.1 : 8443
Target: 127.0.0.1 : 443
Egress: Default
4 — Pin a forward to cellular while Wi‑Fi is the default:
Proto: TCP
Listen: 127.0.0.1 : 6001
Target: 1.1.1.1 : 80
Egress: Network → (pick the Cellular entry)
The upstream connection then egresses over cellular even though Wi‑Fi is the system default.
5 — UDP relay with idle eviction:
Proto: UDP
Listen: 127.0.0.1 : 9000
Target: 127.0.0.1 : 9001
Egress: Default
Routes persist as JSON in the app's prefs, so a route can be seeded without the UI (debug builds):
# one TCP loopback route 6001 -> 6002
JSON='[{"id":"t","name":"demo","proto":0,"listenAddr":"127.0.0.1","listenPort":6001,"targetAddr":"127.0.0.1","targetPort":6002,"egress":0,"egressArg":"","networkHandle":0,"transport":0}]'
# write it into shared_prefs/switchboard.routes.xml via run-as, then launch + Start.Verify the forwarder is live and its egress:
adb shell 'toybox netstat -ltn | grep :6001' # listener up
adb shell 'toybox netstat -tn | grep <target>' # upstream src IP = chosen egress
adb logcat -d | grep -E 'bind_hook|NetworkRequester' # pin mechanismConfirmed on a Pixel 10 Pro XL (Android 17 / API 37, arm64‑v8a):
- TCP forwarding — loopback relay, bidirectional, multiple connections.
- UDP forwarding — per‑flow relay, round‑trips, idle eviction.
- Forward to a privileged target port (e.g.
:443) from the unprivileged app. - Foreground service + notification —
isForeground=true,FOREGROUND_SERVICE_TYPE_SPECIAL_USE, ongoingNO_CLEARnotification on theswitchboard.forwardingchannel. - Non‑default network pinning — with Wi‑Fi as the default, a route pinned to
cellular egressed from the cellular source IP (
10.160.120.239) instead of the Wi‑Fi default (192.168.200.24); the identical route under Default egress used Wi‑Fi. Logs showedrequestNetwork()resolving the cellular network andandroid_setsocknetwork()succeeding. - Clean start/stop — the listener opens on Start and closes on Stop; network requests are released.
- Listening on a privileged port (binding
< 1024) is not possible from the sandboxed app — the OS gates it. Forwarding to a privileged port is fine. Binding a privileged listener would require running the portmux CLI with elevation (root / Shizuku), outside this app. - Pinning to a non‑default network can still be denied by a non‑bypassable (lockdown) VPN holding the default route — that's an OS policy, not an app bug.
- Changing a route requires Stop/Start (portmux routes are fixed at
run()). DEVICEegress (SO_BINDTODEVICE) needs root and is not exposed in the UI.