Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions cgo_bridge/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import "C"

import (
"os"
"unsafe"

libXray "github.com/xtls/libxray"
Expand All @@ -23,3 +24,22 @@ func CGoInvoke(requestJSON *C.char) *C.char {
func CGoFree(value *C.char) {
C.free(unsafe.Pointer(value))
}

// CGoSetEnv sets an environment variable inside the Go runtime.
//
// The Go runtime snapshots environ when the c-archive image initializes, so a
// libc setenv() performed later by the host application is never visible to
// os.LookupEnv. That makes xray-core's env-based knobs unreachable from a
// non-Go host — most importantly platform.TunFdKey ("xray.tun.fd"), which
// proxy/tun/tun_darwin.go reads to adopt an existing tun descriptor.
//
// On iOS the descriptor only exists after NEPacketTunnelProvider.startTunnel,
// long after the snapshot, so the "iOS: use provided fd from NetworkExtension"
// branch 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
// "app/proxyman/inbound: failed to start proxy > operation not permitted".
//
//export CGoSetEnv
func CGoSetEnv(name *C.char, value *C.char) {
_ = os.Setenv(C.GoString(name), C.GoString(value))
}