Skip to content

[DO NOT MERGE] feat(devtunnel): debug-only reverse relay framework - #588

Draft
edusperoni wants to merge 2 commits into
mainfrom
feat/dev-tunnel-framework
Draft

edusperoni wants to merge 2 commits into
mainfrom
feat/dev-tunnel-framework

Conversation

@edusperoni

@edusperoni edusperoni commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

⛔ DO NOT MERGE

This PR is parked and kept open only as a reference. It is not planned to land.

Why. The relay exists so an app on a USB-connected device can reach a server on the developer's Mac without Wi-Fi or the Local Network permission prompt. While it was being built, device testing showed that iOS already provides that path on iOS 17 and later: an ordinary app can open a connection to the Mac's address on the CoreDevice tunnel (the link devicectl sets up over the cable), with no permission prompt and no native code in the app.

  • Verified on an iPhone 11 Pro (iOS 26.0.1) and an iPad Pro 12.9 (iOS 27.2.1) with plain sockets, Network.framework and URLSession, from an app that had never been asked for Local Network access.
  • A real Vue dev app booted from http://[<tunnel address>]:5173 on both devices: static module graph, relative and root-relative dynamic import(), HMR and the dev server's WebSocket all work, with no App Transport Security exception.
  • The runtime needs no change for it: the module loader already handles IPv6-literal origins.
  • Where the tunnel is not available, the fallbacks also need no relay: Wi-Fi with the Local Network prompt, or IPv4 over the cable's second network link once that prompt is allowed.

So the tooling side can do this with what the OS provides, and roughly 4,000 lines of relay, protocol and packaging would be maintained for a case that is mostly gone.

When this could come back. Only if that route turns out to be unusable in practice and prompt-free USB is still wanted, for example:

  • the CoreDevice tunnel is IPv6 only, so a Mac with IPv6 blocked (some VPNs or policies) cannot use it;
  • devices below iOS 17 have no such tunnel.

State of the code, if it is ever picked up again.

  • Stage 1 only: framework, packaging, relay core, host test harness and release-exclusion check. There is no JavaScript API, and nothing was validated on a physical device.
  • The latest Test (ASan) run on this branch failed after about 1 h 24 min and was not investigated; that has to be understood before any merge.
  • The branch is behind main and would need a rebase.

The original description follows unchanged.


This is stage 1 of the dev tunnel: the debug-only framework, its packaging, the relay core, a host-side test harness and a release-exclusion check. The JavaScript API (stage 2) and validation on a physical device are not in this PR.

What

A new debug-only framework, NativeScriptDevTunnel.framework, that lets dev tooling on the Mac reach an app on a USB-connected device and lets the app reach servers on the Mac through it:

  • Relay core (NativeScriptDevTunnel/src/nsdt_relay.{h,c}): plain POSIX C plus CommonCrypto. One tunnel port per app process for the Mac-side tool (pool and control connections), one app-side port per named tunnel, a pairing thread and two splice threads per pair, all off the JS thread.
  • Framework entry point (NativeScriptDevTunnel/NativeScriptDevTunnel.c): implements a small versioned C SPI and registers for the <bundleId>:NativeScript.Debug.TunnelRequest Darwin notification on a queue of its own. It exports one symbol (NativeScriptDevTunnelConnect), has no Objective-C classes and no public headers, so the metadata generator finds nothing in it.
  • Runtime side: NativeScript/runtime/DevTunnelSPI.h (C linkage, version/size on both tables, no V8 types) and a connector, NativeScript/runtime/DevTunnel.{h,mm}, which resolves the hook once with dlsym(RTLD_DEFAULT, …) and exposes DevTunnelAvailable(), Request(tunnels, tunnelPort?, token?) and Info() with plain C++ structs. The runtime calls it once at startup in debug so the framework can register its notification; nothing else calls Request yet. The runtime keeps the hook symbol name and two error messages; no relay code.
  • Packaging, mirroring the leak probe's touch points: the Xcode target and shared scheme, build_dev_tunnel.sh, build_all_ios.sh / build_all_vision.sh, build_spm_artifacts.sh (zip + NS_CHECKSUM_DEVTUNNEL_{IOS,VISIONOS}), build_npm_ios.sh / build_npm_vision.sh, scripts/generate-spm-manifest.mjs, both local SwiftPM templates, NS_DEBUG_FRAMEWORKS in the iOS and visionOS app templates, and the README.
  • Docs: docs/dev-tunnel.md (ports, notification, discovery log line, wire protocol v1, token semantics, release exclusion) and NativeScriptDevTunnel/README.md (harness).
  • Tests: a host harness (node NativeScriptDevTunnel/tests/run-host-tests.mjs) and two in-app specs (TestRunner/app/tests/DevTunnelTests.js) through test-only fixtures (TestFixtures/TNSDevTunnel.{h,mm}).
  • Release check: scripts/check-dev-tunnel-exclusion.sh <App.app>.

Why

Dev tooling wants an app on a physical device to use http://127.0.0.1:<port> and have the traffic reach a dev server or WebSocket on the Mac over USB, without Wi-Fi and without the Local Network permission. On iOS only the Mac can open connections to the device (usbmux), so every listening socket has to be in the app: the tool keeps idle connections to the app's tunnel port, and the app's own clients connect to loopback ports the relay pairs with them.

Design

  • Debug only, attached through an SPI. The framework is in NS_DEBUG_FRAMEWORKS, so only debug configurations embed it, and it is weak-linked everywhere. The runtime finds it with dlsym (the leak probe's pattern); both sides refuse another SPI version. The framework refuses to open anything unless the host reports RuntimeConfig.IsDebug, even if someone ships it in a release build.
  • Nothing opens until asked. Either the app requests tunnels (name + preferred port each; idempotent: an existing name keeps its port), or the tool posts the TunnelRequest notification and then asks over a control connection. The relay is process-wide and outlives runtime restarts.
  • Ports. Loopback only, 127.0.0.1 and ::1 on the same port number for each listener (localhost is ::1 on a device). A preferred port that is taken on either family moves both up together, at most 32 attempts; nothing binding is a failed request plus a public log line. The tunnel port defaults to 18283, distinct from the inspector's 18183-and-up.
  • No shadowing, stable ports across relaunches. SO_REUSEADDR would let a loopback bind sit on top of another process's wildcard listener and take its loopback traffic; without it, a relaunched app loses its ports to TIME_WAIT for a while. The relay binds without it first and only retries with it after a wildcard probe shows no listener covers the port. The host harness covers both cases.
  • Discovery by log. Whenever the set of bound ports changes, a public os_log line: NativeScript dev tunnel listening for <bundleId>: tunnel port <T>; <name>=<port> ... (- when there are no tunnels yet). Each TunnelRequest notification writes it again, like the inspector's AttachRequest answer.
  • Optional token. When the app supplies one, both sides prove they know it with HMAC-SHA256 over fresh nonces (CSPRNG, constant-time comparison). The first token stays; a different one is refused. If a token arrives after unauthenticated connections were admitted (tool-opened tunnel port, then an app request with a token), those idle pool and control connections are closed and never used. Without a token there is no authentication, as with the inspector today.
  • Robustness. Idle pool connections that become readable are closed, never paired; app connections wait at most 10 s for a pool connection (then a log line); half-close passes through; SO_NOSIGPIPE and TCP_NODELAY on every connection; the handshake runs off the accept thread; listeners that start failing (defunct sockets) are rebound on the same port.

Protocol

ASCII lines on the tunnel port, at most 256 bytes including the \n:

  1. Host → relay: NSDT/1 HELLO <pool|control> <hostNonceHex> within 2 s of connecting.
  2. Relay → host: NSDT/1 READY <bundleId> <pid> <tunnelPort> <name>=<port>,…|- <relayNonceHex> <proof|->, where proof = HMAC-SHA256(token, host nonce bytes); or NSDT/1 DENY <reason> and close.
  3. With a token, host → relay within 2 s: NSDT/1 AUTH <HMAC-SHA256(token, relay nonce bytes)>; a wrong or missing answer gets NSDT/1 DENY auth.
  4. Pool connections stay silent until paired; the relay then writes NSDT/1 PAIR <name> <appPort> <connId> before any app byte and splices raw bytes both ways. The header names the tunnel, never a host or port to dial.
  5. Control connections may send NSDT/1 OPEN <name> <preferredPort> (→ NSDT/1 OPENED <name> <port> or NSDT/1 DENY <reason>) and NSDT/1 LIST (→ a READY line with - for nonce and proof) until they close.

A tunnel whose addition would push READY past 256 bytes is refused rather than truncated. Full details in docs/dev-tunnel.md.

Release exclusion

  • The app templates list the framework in NS_DEBUG_FRAMEWORKS; release configurations do not embed it, and nsld.sh weak-links it so dyld skips it.
  • scripts/check-dev-tunnel-exclusion.sh <App.app> fails when an app embeds the framework, links it strongly, or carries a relay string (NSDT/1, the log line text, the notification name) or relay symbol in any Mach-O file; it reports the weak load command as a note and lists what the runtime binary keeps.
  • On a release simulator build of a blank app created from the packaged template (local npm tarball from ./build_all_ios.sh), the check passes: the framework is absent, the app executable keeps only the weak load command, and the runtime binary keeps the strings NativeScriptDevTunnelConnect, "the dev tunnel framework is not embedded in this app" and "the dev tunnel works in debug builds only", plus the connector's own exported symbols. That release app launches, and posting the TunnelRequest notification to it opens nothing.

How it was verified

  • Host harness, node NativeScriptDevTunnel/tests/run-host-tests.mjs: 29/29 passing on six consecutive runs, and 30/30 with --sanitize (relay built with AddressSanitizer and UBSan, plus a case asserting no sanitizer report). It ports the original 11 cases (pool started before the relay; app connection queued before any pool connection; 20 keep-alive requests over one connection; 100 parallel 256 KiB requests with matching hashes; pool replenishment; WebSocket echo over 127.0.0.1 and localhost; HTTP over localhost and [::1]; closed idle connections never paired; relay restart, now also asserting the same ports come back; half-close; the 10 s pending timeout) and adds: HELLO timeout; DENY for a wrong AUTH and a missing AUTH; the relay's proof checked by the host (a wrong token never reaches AUTH); no-token mode end to end; an unknown tunnel name on the host side; two tunnels interleaved over one pool; a tunnel opened at run time over a control connection, with traffic, and LIST reflecting it; two relays ("two apps") asking for the same preferred ports, the second moving up; a port taken on one loopback family moving both up (and the abandoned family released); a server that speaks first; wildcard and dual-stack listeners not shadowed; the TunnelRequest notification (posted with notifyutil) opening the tunnel port when nothing was open before; a token configured after unauthenticated pool and control connections dropping them; the 256-byte READY cap; DENY for another version, an unknown role, a bad nonce and an overlong line; a release host opening nothing; idempotent requests and a refused second token.
  • Builds: TestRunner Release for the iOS Simulator (arm64); the framework built by build_dev_tunnel.sh for iOS device, iOS Simulator (arm64 + x86_64) and Mac Catalyst, each slice exporting only _NativeScriptDevTunnelConnect, and for visionOS device and simulator (--no-catalyst --no-iphone --no-sim).
  • Test suite (./run_tests.sh --temp-sim, iOS Simulator): 2159 test cases (main: 2157, plus the two new specs), 0 failures, 11 skipped, on two runs. The specs request two tunnels through the runtime's connector, carry bytes end to end through each (pool side and app client played inside the test process), read the info back and check the reported ports, and observe the tunnel port opening after posting the TunnelRequest notification.
  • Packaging: ./build_all_ios.sh produced the npm tarball with framework/internal/local-spm/NativeScriptDevTunnel.xcframework.zip next to the leak probe's, the local Package.swift listing it, NS_DEBUG_FRAMEWORKS including it, and NS_CHECKSUM_DEVTUNNEL_IOS in checksums-ios.env. generate-spm-manifest.mjs was run with the iOS checksums and with dummy visionOS checksums (--strict) to check both manifest shapes.
  • Real app template: a blank app created with the NativeScript CLI against that tarball. Debug simulator build: the framework is embedded and weak-linked; on a simulator nothing listened until the TunnelRequest notification, which opened 127.0.0.1 and ::1 on 18283 and logged the public line; a control connection from the Mac opened a tunnel, LIST reflected it, and an HTTP request to the app-side port reached a server on the Mac through a pool. Release build: see "Release exclusion".
  • Not verified: anything on a physical device (usbmux reaching the loopback-only tunnel port, posting the notification to a device, listeners across app suspension and the defunct-listener rebind path, which has no automated test, two dev apps on one device); the Mac Catalyst test lane (the new specs run there too, since the test app embeds the framework for Catalyst and is a debug build; the lane was not run locally); visionOS beyond compiling the framework (build_all_vision.sh and build_npm_vision.sh were not run); an app-side client inside a template app (covered only by the in-app specs in the test app); CI wiring of the host harness (no step runs it yet).

Not in this PR

  • Stage 2, the JavaScript API: a builtin to request tunnels by name and preferred port with an optional token, and to read the open tunnels back; later, events and registering app-side servers for the forward direction.
  • The Mac-side tool (usbmux transport, pool, notification posting), which lives outside this repository.
  • Device validation: usbmux to the loopback tunnel port, the notification on a device, behavior across suspension, two dev apps at once.
  • CI: running the host harness in a workflow.

Adds NativeScriptDevTunnel.framework, which lets a tool on the Mac reach an
app on a USB-connected device and the app reach servers on the Mac: the app
listens on loopback for the tool's pool and control connections (one tunnel
port per process, default 18283) and on one port per named tunnel for its own
clients, and pairs each app connection with an idle pool connection, splicing
bytes off the JS thread. Wire protocol v1 (HELLO / READY / AUTH / PAIR, OPEN
and LIST on control connections) with an optional HMAC-SHA256 token is
documented in docs/dev-tunnel.md.

The relay is plain POSIX C. Tunnels open on request from the runtime or when
the tool posts <bundleId>:NativeScript.Debug.TunnelRequest; ports are bound on
127.0.0.1 and ::1 together, moving upward when taken, without shadowing
another process's wildcard listener; a public os_log line names the bundle id
and every bound port.

The runtime reaches the framework through a versioned C SPI resolved with
dlsym (DevTunnelSPI.h, DevTunnel.{h,mm}) and attaches it once at startup in
debug; it has no JS API yet. The framework is packaged like the leak probe
(xcframework, SwiftPM targets, npm local package) and listed in
NS_DEBUG_FRAMEWORKS, so release builds do not embed it;
scripts/check-dev-tunnel-exclusion.sh checks a built app for it.

A host harness (node NativeScriptDevTunnel/tests/run-host-tests.mjs) drives
the relay built for macOS through 29 cases; two TestRunner specs exercise the
connector and the notification in the test app.
@coderabbitai

coderabbitai Bot commented Nov 20, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@NathanWalker NathanWalker added this to the 9.2 milestone Nov 21, 2026
@edusperoni edusperoni changed the title feat(devtunnel): debug-only reverse relay framework [DO NOT MERGE] feat(devtunnel): debug-only reverse relay framework Nov 22, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants