diff --git a/libv2ray_android.go b/libv2ray_android.go index 76ff8e6b..89c973ee 100644 --- a/libv2ray_android.go +++ b/libv2ray_android.go @@ -1,7 +1,9 @@ package libv2ray import ( + "errors" "fmt" + "sync/atomic" corenet "github.com/xtls/xray-core/common/net" ) @@ -21,17 +23,31 @@ type ProcessFinder interface { FindProcessByConnection(network, srcIP string, srcPort int, destIP string, destPort int) int } +type processFinderRegistration struct { + finder ProcessFinder +} + +var androidProcessFinder atomic.Pointer[processFinderRegistration] + // RegisterProcessFinder registers an Android process finder with Xray-core, // enabling per-app routing based on UID. Must be called before starting the // core for process-based routing rules to work. -// Pass nil to unregister a previously registered finder. +// Pass nil to unregister the finder for subsequent lookups. func (x *CoreController) RegisterProcessFinder(finder ProcessFinder) { if finder == nil { - corenet.RegisterAndroidProcessFinder(nil) + androidProcessFinder.Store(nil) return } + androidProcessFinder.Store(&processFinderRegistration{finder: finder}) +} +func init() { + // Xray reads its callback without synchronization. Publish it before use. corenet.RegisterAndroidProcessFinder(func(network, srcIP string, srcPort uint16, destIP string, destPort uint16) (uid int, name string, path string, err error) { + registration := androidProcessFinder.Load() + if registration == nil { + return 0, "", "", errors.New("android process lookup must be registered before use") + } // getConnectionOwnerUid only works for established connections, // so if dest is missing, it likely means the connection is not fully established yet. // In that case, we can return an error to indicate that the process cannot be determined at this time. @@ -44,10 +60,7 @@ func (x *CoreController) RegisterProcessFinder(finder ProcessFinder) { uid, name, path, err = 0, "", "", fmt.Errorf("processFinder panic: %v", r) } }() - uid = finder.FindProcessByConnection(network, srcIP, int(srcPort), destIP, int(destPort)) - // if uid < 0 { - // return 0, "", "", fmt.Errorf("processFinder, not found for %s %s:%d -> %s:%d", network, srcIP, srcPort, destIP, destPort) - // } + uid = registration.finder.FindProcessByConnection(network, srcIP, int(srcPort), destIP, int(destPort)) return uid, fmt.Sprintf("%d", uid), "", nil }) } diff --git a/libv2ray_assets.go b/libv2ray_assets.go new file mode 100644 index 00000000..a5084d7c --- /dev/null +++ b/libv2ray_assets.go @@ -0,0 +1,27 @@ +package libv2ray + +import ( + "errors" + "io" + "os" + "path/filepath" + "sync/atomic" + + corefilesystem "github.com/xtls/xray-core/common/platform/filesystem" + mobasset "golang.org/x/mobile/asset" +) + +var mobileAssetsEnabled atomic.Bool + +func init() { + // Xray reads this hook without synchronization. Install it before use. + readFile := corefilesystem.NewFileReader + corefilesystem.NewFileReader = func(path string) (io.ReadCloser, error) { + reader, err := readFile(path) + if mobileAssetsEnabled.Load() && errors.Is(err, os.ErrNotExist) { + _, file := filepath.Split(path) + return mobasset.Open(file) + } + return reader, err + } +} diff --git a/libv2ray_main.go b/libv2ray_main.go index 68af7eff..601b783d 100644 --- a/libv2ray_main.go +++ b/libv2ray_main.go @@ -2,23 +2,19 @@ package libv2ray import ( "fmt" - "io" "log" "os" - "path/filepath" "strconv" "strings" "sync" coreapplog "github.com/xtls/xray-core/app/log" corecommlog "github.com/xtls/xray-core/common/log" - corefilesystem "github.com/xtls/xray-core/common/platform/filesystem" core "github.com/xtls/xray-core/core" corestats "github.com/xtls/xray-core/features/stats" coreserial "github.com/xtls/xray-core/infra/conf/serial" _ "github.com/xtls/xray-core/main/distro/all" browser_dialer "github.com/xtls/xray-core/transport/internet/browser_dialer" - mobasset "golang.org/x/mobile/asset" ) // Constants for environment variables @@ -75,14 +71,7 @@ func InitCoreEnv(envPath string, key string) { setEnvVariable(xudpBaseKey, key) } - // Custom file reader with path validation - corefilesystem.NewFileReader = func(path string) (io.ReadCloser, error) { - if _, err := os.Stat(path); os.IsNotExist(err) { - _, file := filepath.Split(path) - return mobasset.Open(file) - } - return os.Open(path) - } + mobileAssetsEnabled.Store(true) } // NewCoreController initializes and returns a new CoreController instance