FIx error handling for netlink banned in Android

This commit is contained in:
世界 2023-12-12 13:50:07 +08:00
parent 62f2d98190
commit 69c3b72eec
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -6,6 +6,7 @@ import (
"sync" "sync"
"github.com/sagernet/netlink" "github.com/sagernet/netlink"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger" "github.com/sagernet/sing/common/logger"
"github.com/sagernet/sing/common/x/list" "github.com/sagernet/sing/common/x/list"
@ -22,6 +23,12 @@ type networkUpdateMonitor struct {
logger logger.Logger logger logger.Logger
} }
var ErrNetlinkBanned = E.New(
"netlink socket in Android is banned by Google, " +
"use the root or system (ADB) user to run sing-box, " +
"or switch to the sing-box Adnroid graphical interface client",
)
func NewNetworkUpdateMonitor(logger logger.Logger) (NetworkUpdateMonitor, error) { func NewNetworkUpdateMonitor(logger logger.Logger) (NetworkUpdateMonitor, error) {
monitor := &networkUpdateMonitor{ monitor := &networkUpdateMonitor{
routeUpdate: make(chan netlink.RouteUpdate, 2), routeUpdate: make(chan netlink.RouteUpdate, 2),
@ -33,14 +40,14 @@ func NewNetworkUpdateMonitor(logger logger.Logger) (NetworkUpdateMonitor, error)
if runtime.GOOS == "android" { if runtime.GOOS == "android" {
netlinkSocket, err := unix.Socket(unix.AF_NETLINK, unix.SOCK_DGRAM, unix.NETLINK_ROUTE) netlinkSocket, err := unix.Socket(unix.AF_NETLINK, unix.SOCK_DGRAM, unix.NETLINK_ROUTE)
if err != nil { if err != nil {
return nil, os.ErrInvalid return nil, ErrNetlinkBanned
} }
err = unix.Bind(netlinkSocket, &unix.SockaddrNetlink{ err = unix.Bind(netlinkSocket, &unix.SockaddrNetlink{
Family: unix.AF_NETLINK, Family: unix.AF_NETLINK,
}) })
unix.Close(netlinkSocket) unix.Close(netlinkSocket)
if err != nil { if err != nil {
return nil, os.ErrInvalid return nil, ErrNetlinkBanned
} }
} }
return monitor, nil return monitor, nil