Skip to content
Open
Show file tree
Hide file tree
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
25 changes: 19 additions & 6 deletions libv2ray_android.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package libv2ray

import (
"errors"
"fmt"
"sync/atomic"

corenet "github.com/xtls/xray-core/common/net"
)
Expand All @@ -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.
Expand All @@ -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
})
}
27 changes: 27 additions & 0 deletions libv2ray_assets.go
Original file line number Diff line number Diff line change
@@ -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
}
}
13 changes: 1 addition & 12 deletions libv2ray_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down