diff -ruN a/features/dns/localdns/client.go b/features/dns/localdns/client.go --- a/features/dns/localdns/client.go +++ b/features/dns/localdns/client.go @@ -3,6 +3,7 @@ import ( "context" "net/netip" + "runtime" "syscall" "time" @@ -33,7 +34,7 @@ func (c *Client) LookupIP(host string, option dns.IPOption) ([]net.IP, uint32, error) { var ips []net.IP var err error - if len(internet.Controllers) > 0 { + if len(internet.Controllers) > 0 && runtime.GOOS != "android" { ips, err = c.r.LookupIP(context.Background(), "ip", host) } else { ips, err = net.LookupIP(host) diff -ruN a/proxy/proxy.go b/proxy/proxy.go --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -737,7 +737,10 @@ readerConn, readCounter, _ := UnwrapRawConn(readerConn) writerConn, _, writeCounter := UnwrapRawConn(writerConn) reader := buf.NewReader(readerConn) - if runtime.GOOS != "linux" && runtime.GOOS != "android" { + // RayRay: no splice on Android, as on iOS. A spliced connection adds its bytes to the stats only when it ends, so + // a long download or a kept-alive connection stays out of the app's traffic numbers and its "the server still + // answers" checks until then. + if runtime.GOOS != "linux" { return readV(ctx, reader, writer, timer, readCounter) } tc, ok := writerConn.(*net.TCPConn) diff -ruN a/proxy/tun/rayray_limits.go b/proxy/tun/rayray_limits.go --- a/proxy/tun/rayray_limits.go +++ b/proxy/tun/rayray_limits.go @@ -71,7 +71,7 @@ } func queueLimit() int { - if runtime.GOOS == "ios" { + if runtime.GOOS == "ios" || runtime.GOOS == "android" { return 128 } return 1024 diff -ruN a/proxy/tun/stack.go b/proxy/tun/stack.go --- a/proxy/tun/stack.go +++ b/proxy/tun/stack.go @@ -115,7 +115,7 @@ if options.Remote { s.window = tcpLargeWindow } - if runtime.GOOS == "ios" { + if runtime.GOOS == "ios" || runtime.GOOS == "android" { s.window = tcpWindowIOS } readLimits() diff -ruN a/transport/internet/finalmask/realm/http.go b/transport/internet/finalmask/realm/http.go --- a/transport/internet/finalmask/realm/http.go +++ b/transport/internet/finalmask/realm/http.go @@ -14,6 +14,8 @@ "net/url" "strings" + xnet "github.com/xtls/xray-core/common/net" + "github.com/xtls/xray-core/transport/internet" "github.com/xtls/xray-core/transport/internet/tls" ) @@ -81,9 +83,24 @@ return fmt.Sprintf("realm server returned %d", e.StatusCode) } +// On Android a socket that skips the registered controllers (VpnService.protect) goes into the app's own tunnel, +// back to this outbound, and loops. +func dialThroughControllers(ctx context.Context, _, addr string) (net.Conn, error) { + dest, err := xnet.ParseDestination("tcp:" + addr) + if err != nil { + return nil, err + } + return internet.DialSystem(ctx, dest, nil) +} + // RayRay: dial is how the client reaches the realm server, nil for Go's own dialer. A client outbound passes its own // dial, so these requests leave the way its packets do, with its socket options. func NewClient(scheme, host, port, token string, tlsConfig *tls.Config, dial func(ctx context.Context, network, addr string) (net.Conn, error)) *Client { + // RayRay (Android): a nil dial means dialThroughControllers here, not Go's own dialer, so every realm client (the + // server's too) gets its own transport without keep-alives and the DefaultClient branch below is never taken. + if dial == nil { + dial = dialThroughControllers + } client := http.DefaultClient if tlsConfig != nil || dial != nil { tr := http.DefaultTransport.(*http.Transport).Clone() diff -ruN a/transport/internet/sockopt_linux.go b/transport/internet/sockopt_linux.go --- a/transport/internet/sockopt_linux.go +++ b/transport/internet/sockopt_linux.go @@ -5,22 +5,42 @@ "runtime" "strconv" "strings" + "sync/atomic" "syscall" "github.com/xtls/xray-core/common/errors" "golang.org/x/sys/unix" ) + +// RayRay (Android): an outbound whose sockopt.interface is BypassInterface has its sockets bound by the registered +// bypass controller (the app binds them to the device's own network, past its tunnel) instead of SO_BINDTODEVICE, +// which needs privileges. Only such sockets are bound: the dialer controllers would bind every socket of the process. +const BypassInterface = "rayray-bypass" + +var bypassController atomic.Pointer[func(fd uintptr) bool] + +// RegisterBypassController sets the controller that binds the sockets of BypassInterface outbounds. +func RegisterBypassController(controller func(fd uintptr) bool) { + bypassController.Store(&controller) +} // applyOutboundSocketOptions applies socket options for outbound connection. // note that unlike other part of Xray, this function needs network with speified network stack(tcp4/tcp6/udp4/udp6) func applyOutboundSocketOptions(network string, address string, fd uintptr, config *SocketConfig) error { + // RayRay: first, so no other option's failure (SO_MARK needs privileges) can leave such a socket unbound. + if config.Interface == BypassInterface { + if controller := bypassController.Load(); controller == nil || !(*controller)(fd) { + return NewBindError(errors.New("failed to bind the socket past the tunnel")) + } + } + if config.Mark != 0 { if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(config.Mark)); err != nil { return errors.New("failed to set SO_MARK").Base(err) } } - if config.Interface != "" { + if config.Interface != "" && config.Interface != BypassInterface { if err := syscall.BindToDevice(int(fd), config.Interface); err != nil { return NewBindError(errors.New("failed to set Interface").Base(err)) }