cgo_bridge: export CGoSetEnv so hosts can reach xray-core env knobs - #149
Open
plutoisnotaplanet wants to merge 1 commit into
Open
cgo_bridge: export CGoSetEnv so hosts can reach xray-core env knobs#149plutoisnotaplanet wants to merge 1 commit into
plutoisnotaplanet wants to merge 1 commit into
Conversation
The Go runtime snapshots environ when the c-archive image initializes, so a
setenv() performed later by a non-Go host is never visible to os.LookupEnv,
which common/platform.EnvFlag relies on.
That makes platform.TunFdKey ("xray.tun.fd") unreachable on iOS: the tun
descriptor only exists after NEPacketTunnelProvider.startTunnel, long after
the snapshot, so the "iOS: use provided fd from NetworkExtension" branch in
proxy/tun/tun_darwin.go can never be taken. NewTun falls through to creating
its own utun via an AF_SYSTEM socket, which the app sandbox denies, and the
inbound fails with "failed to start proxy > operation not permitted".
Collaborator
|
Thanks for contribution. |
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.
Problem
The Go runtime snapshots
environwhen a c-archive image initializes. Asetenv()performed later by the host application is therefore never visibleto
os.LookupEnv, which is whatcommon/platform.EnvFlaguses.That makes every env-based knob in xray-core unreachable from a non-Go host.
The one that matters in practice is
platform.TunFdKey(xray.tun.fd), readby
proxy/tun/tun_darwin.goin the branch commented "iOS: use provided fd fromNetworkExtension".
On iOS the tun descriptor only exists after
NEPacketTunnelProvider.startTunnelruns — long after the snapshot — so aSwift host cannot publish it.
NewTunthen falls through to the macOS path andtries to create its own utun via an
AF_SYSTEMsocket, which the app sandboxdenies:
Reproduced on iPhone 16 Pro / iOS 26.6 with libXray v26.7.28, and isolated with
a minimal spike: a C program calls
setenv("xray.tun.fd", "5", 1)and then anexported Go function —
getenvreturns5,os.LookupEnvreturnsfound=false.Change
One exported function in
cgo_bridge, callingos.Setenvinside the Goruntime. No behavior change for existing callers; nothing else is touched.
With it, the iOS branch of
NewTunfinally becomes reachable: our client nowbrings the tunnel up on device with the same core version that previously
failed at the inbound. The packet path needed no further work — the 4-byte utun
header is handled in
DarwinTun.ReadPacket/WritePacket, which are shared withthe macOS path.
Alternative considered
Passing the descriptor through
TunConfig(afdfield) would be moreexplicit and would not rely on process env at all. That is a larger change to
xray-core's config surface; happy to prepare it instead if maintainers prefer.