Repository navigation
[DO NOT MERGE] feat(devtunnel): debug-only reverse relay framework - #588
Draft
edusperoni wants to merge 2 commits into
Draft
edusperoni wants to merge 2 commits into
edusperoni wants to merge 2 commits into
Conversation
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.
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
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. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
⛔ 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
devicectlsets up over the cable), with no permission prompt and no native code in the app.http://[<tunnel address>]:5173on both devices: static module graph, relative and root-relative dynamicimport(), HMR and the dev server's WebSocket all work, with no App Transport Security exception.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:
State of the code, if it is ever picked up again.
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.mainand 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: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.NativeScriptDevTunnel/NativeScriptDevTunnel.c): implements a small versioned C SPI and registers for the<bundleId>:NativeScript.Debug.TunnelRequestDarwin 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.NativeScript/runtime/DevTunnelSPI.h(C linkage,version/sizeon both tables, no V8 types) and a connector,NativeScript/runtime/DevTunnel.{h,mm}, which resolves the hook once withdlsym(RTLD_DEFAULT, …)and exposesDevTunnelAvailable(),Request(tunnels, tunnelPort?, token?)andInfo()with plain C++ structs. The runtime calls it once at startup in debug so the framework can register its notification; nothing else callsRequestyet. The runtime keeps the hook symbol name and two error messages; no relay code.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_FRAMEWORKSin the iOS and visionOS app templates, and the README.docs/dev-tunnel.md(ports, notification, discovery log line, wire protocol v1, token semantics, release exclusion) andNativeScriptDevTunnel/README.md(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}).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
NS_DEBUG_FRAMEWORKS, so only debug configurations embed it, and it is weak-linked everywhere. The runtime finds it withdlsym(the leak probe's pattern); both sides refuse another SPI version. The framework refuses to open anything unless the host reportsRuntimeConfig.IsDebug, even if someone ships it in a release build.127.0.0.1and::1on the same port number for each listener (localhostis::1on 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.SO_REUSEADDRwould 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 toTIME_WAITfor 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.os_logline: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.SO_NOSIGPIPEandTCP_NODELAYon 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:NSDT/1 HELLO <pool|control> <hostNonceHex>within 2 s of connecting.NSDT/1 READY <bundleId> <pid> <tunnelPort> <name>=<port>,…|- <relayNonceHex> <proof|->, where proof = HMAC-SHA256(token, host nonce bytes); orNSDT/1 DENY <reason>and close.NSDT/1 AUTH <HMAC-SHA256(token, relay nonce bytes)>; a wrong or missing answer getsNSDT/1 DENY auth.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.NSDT/1 OPEN <name> <preferredPort>(→NSDT/1 OPENED <name> <port>orNSDT/1 DENY <reason>) andNSDT/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
NS_DEBUG_FRAMEWORKS; release configurations do not embed it, andnsld.shweak-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../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 stringsNativeScriptDevTunnelConnect, "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
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 withnotifyutil) 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.TestRunnerRelease for the iOS Simulator (arm64); the framework built bybuild_dev_tunnel.shfor 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)../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../build_all_ios.shproduced the npm tarball withframework/internal/local-spm/NativeScriptDevTunnel.xcframework.zipnext to the leak probe's, the localPackage.swiftlisting it,NS_DEBUG_FRAMEWORKSincluding it, andNS_CHECKSUM_DEVTUNNEL_IOSinchecksums-ios.env.generate-spm-manifest.mjswas run with the iOS checksums and with dummy visionOS checksums (--strict) to check both manifest shapes.build_all_vision.shandbuild_npm_vision.shwere 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