mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
Merge branch 'SagerNet:dev-next' into dev-next
This commit is contained in:
commit
a61fde1ab0
@ -51,5 +51,4 @@ type NetworkInterface struct {
|
|||||||
DNSServers []string
|
DNSServers []string
|
||||||
Expensive bool
|
Expensive bool
|
||||||
Constrained bool
|
Constrained bool
|
||||||
RawNetwork any
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +0,0 @@
|
|||||||
//go:build android && debug
|
|
||||||
|
|
||||||
package constant
|
|
||||||
|
|
||||||
// TODO: remove after fixed
|
|
||||||
// https://github.com/golang/go/issues/68760
|
|
||||||
|
|
||||||
const FixAndroidStack = true
|
|
@ -1,5 +0,0 @@
|
|||||||
//go:build !(android && debug)
|
|
||||||
|
|
||||||
package constant
|
|
||||||
|
|
||||||
const FixAndroidStack = false
|
|
@ -7,7 +7,6 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
C "github.com/sagernet/sing-box/constant"
|
|
||||||
"github.com/sagernet/sing/common"
|
"github.com/sagernet/sing/common"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
)
|
)
|
||||||
@ -114,7 +113,7 @@ func (c *CommandClient) Connect() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if C.FixAndroidStack {
|
if sFixAndroidStack {
|
||||||
go func() {
|
go func() {
|
||||||
c.handler.Connected()
|
c.handler.Connected()
|
||||||
c.handler.InitializeClashMode(newIterator(modeList), currentMode)
|
c.handler.InitializeClashMode(newIterator(modeList), currentMode)
|
||||||
|
@ -82,10 +82,6 @@ func (s *platformInterfaceStub) Interfaces() ([]adapter.NetworkInterface, error)
|
|||||||
return nil, os.ErrInvalid
|
return nil, os.ErrInvalid
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *platformInterfaceStub) SetUnderlyingNetworks(networks []adapter.NetworkInterface) error {
|
|
||||||
return os.ErrInvalid
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *platformInterfaceStub) UnderNetworkExtension() bool {
|
func (s *platformInterfaceStub) UnderNetworkExtension() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package libbox
|
package libbox
|
||||||
|
|
||||||
import (
|
import (
|
||||||
C "github.com/sagernet/sing-box/constant"
|
|
||||||
"github.com/sagernet/sing-tun"
|
"github.com/sagernet/sing-tun"
|
||||||
"github.com/sagernet/sing/common/control"
|
"github.com/sagernet/sing/common/control"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
@ -56,7 +55,7 @@ func (m *platformDefaultInterfaceMonitor) UnregisterCallback(element *list.Eleme
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *platformDefaultInterfaceMonitor) UpdateDefaultInterface(interfaceName string, interfaceIndex32 int32, isExpensive bool, isConstrained bool) {
|
func (m *platformDefaultInterfaceMonitor) UpdateDefaultInterface(interfaceName string, interfaceIndex32 int32, isExpensive bool, isConstrained bool) {
|
||||||
if C.FixAndroidStack {
|
if sFixAndroidStack {
|
||||||
go m.updateDefaultInterface(interfaceName, interfaceIndex32, isExpensive, isConstrained)
|
go m.updateDefaultInterface(interfaceName, interfaceIndex32, isExpensive, isConstrained)
|
||||||
} else {
|
} else {
|
||||||
m.updateDefaultInterface(interfaceName, interfaceIndex32, isExpensive, isConstrained)
|
m.updateDefaultInterface(interfaceName, interfaceIndex32, isExpensive, isConstrained)
|
||||||
|
@ -18,7 +18,6 @@ type PlatformInterface interface {
|
|||||||
StartDefaultInterfaceMonitor(listener InterfaceUpdateListener) error
|
StartDefaultInterfaceMonitor(listener InterfaceUpdateListener) error
|
||||||
CloseDefaultInterfaceMonitor(listener InterfaceUpdateListener) error
|
CloseDefaultInterfaceMonitor(listener InterfaceUpdateListener) error
|
||||||
GetInterfaces() (NetworkInterfaceIterator, error)
|
GetInterfaces() (NetworkInterfaceIterator, error)
|
||||||
SetUnderlyingNetworks(networks RawNetworkIterator) error
|
|
||||||
UnderNetworkExtension() bool
|
UnderNetworkExtension() bool
|
||||||
IncludeAllNetworks() bool
|
IncludeAllNetworks() bool
|
||||||
ReadWIFIState() *WIFIState
|
ReadWIFIState() *WIFIState
|
||||||
@ -52,8 +51,6 @@ type NetworkInterface struct {
|
|||||||
Type int32
|
Type int32
|
||||||
DNSServer StringIterator
|
DNSServer StringIterator
|
||||||
Metered bool
|
Metered bool
|
||||||
|
|
||||||
RawNetwork RawNetwork
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type WIFIState struct {
|
type WIFIState struct {
|
||||||
@ -70,11 +67,6 @@ type NetworkInterfaceIterator interface {
|
|||||||
HasNext() bool
|
HasNext() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type RawNetworkIterator interface {
|
|
||||||
Next() RawNetwork
|
|
||||||
HasNext() bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type Notification struct {
|
type Notification struct {
|
||||||
Identifier string
|
Identifier string
|
||||||
TypeName string
|
TypeName string
|
||||||
|
@ -16,7 +16,6 @@ type Interface interface {
|
|||||||
UpdateRouteOptions(options *tun.Options, platformOptions option.TunPlatformOptions) error
|
UpdateRouteOptions(options *tun.Options, platformOptions option.TunPlatformOptions) error
|
||||||
CreateDefaultInterfaceMonitor(logger logger.Logger) tun.DefaultInterfaceMonitor
|
CreateDefaultInterfaceMonitor(logger logger.Logger) tun.DefaultInterfaceMonitor
|
||||||
Interfaces() ([]adapter.NetworkInterface, error)
|
Interfaces() ([]adapter.NetworkInterface, error)
|
||||||
SetUnderlyingNetworks(networks []adapter.NetworkInterface) error
|
|
||||||
UnderNetworkExtension() bool
|
UnderNetworkExtension() bool
|
||||||
IncludeAllNetworks() bool
|
IncludeAllNetworks() bool
|
||||||
ClearDNSCache()
|
ClearDNSCache()
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
package libbox
|
|
||||||
|
|
||||||
type RawNetwork interface{}
|
|
@ -1,7 +0,0 @@
|
|||||||
//go:build !android
|
|
||||||
|
|
||||||
package libbox
|
|
||||||
|
|
||||||
type RawNetwork interface {
|
|
||||||
stub()
|
|
||||||
}
|
|
@ -81,7 +81,7 @@ func NewService(configContent string, platformInterface PlatformInterface) (*Box
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *BoxService) Start() error {
|
func (s *BoxService) Start() error {
|
||||||
if C.FixAndroidStack {
|
if sFixAndroidStack {
|
||||||
var err error
|
var err error
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
@ -220,18 +220,11 @@ func (w *platformInterfaceWrapper) Interfaces() ([]adapter.NetworkInterface, err
|
|||||||
DNSServers: iteratorToArray[string](netInterface.DNSServer),
|
DNSServers: iteratorToArray[string](netInterface.DNSServer),
|
||||||
Expensive: netInterface.Metered || isDefault && w.isExpensive,
|
Expensive: netInterface.Metered || isDefault && w.isExpensive,
|
||||||
Constrained: isDefault && w.isConstrained,
|
Constrained: isDefault && w.isConstrained,
|
||||||
RawNetwork: netInterface.RawNetwork,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return interfaces, nil
|
return interfaces, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *platformInterfaceWrapper) SetUnderlyingNetworks(networks []adapter.NetworkInterface) error {
|
|
||||||
return w.iif.SetUnderlyingNetworks(newIterator(common.Map(networks, func(it adapter.NetworkInterface) RawNetwork {
|
|
||||||
return it.RawNetwork.(RawNetwork)
|
|
||||||
})))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *platformInterfaceWrapper) UnderNetworkExtension() bool {
|
func (w *platformInterfaceWrapper) UnderNetworkExtension() bool {
|
||||||
return w.iif.UnderNetworkExtension()
|
return w.iif.UnderNetworkExtension()
|
||||||
}
|
}
|
||||||
|
@ -14,43 +14,55 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
sBasePath string
|
sBasePath string
|
||||||
sWorkingPath string
|
sWorkingPath string
|
||||||
sTempPath string
|
sTempPath string
|
||||||
sUserID int
|
sUserID int
|
||||||
sGroupID int
|
sGroupID int
|
||||||
sTVOS bool
|
sTVOS bool
|
||||||
|
sFixAndroidStack bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
debug.SetPanicOnFault(true)
|
debug.SetPanicOnFault(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Setup(basePath string, workingPath string, tempPath string, isTVOS bool) {
|
type SetupOptions struct {
|
||||||
sBasePath = basePath
|
BasePath string
|
||||||
sWorkingPath = workingPath
|
WorkingPath string
|
||||||
sTempPath = tempPath
|
TempPath string
|
||||||
sUserID = os.Getuid()
|
Username string
|
||||||
sGroupID = os.Getgid()
|
IsTVOS bool
|
||||||
sTVOS = isTVOS
|
FixAndroidStack bool
|
||||||
os.MkdirAll(sWorkingPath, 0o777)
|
|
||||||
os.MkdirAll(sTempPath, 0o777)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetupWithUsername(basePath string, workingPath string, tempPath string, username string) error {
|
func Setup(options *SetupOptions) error {
|
||||||
sBasePath = basePath
|
sBasePath = options.BasePath
|
||||||
sWorkingPath = workingPath
|
sWorkingPath = options.WorkingPath
|
||||||
sTempPath = tempPath
|
sTempPath = options.TempPath
|
||||||
sUser, err := user.Lookup(username)
|
if options.Username != "" {
|
||||||
if err != nil {
|
sUser, err := user.Lookup(options.Username)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
sUserID, _ = strconv.Atoi(sUser.Uid)
|
||||||
|
sGroupID, _ = strconv.Atoi(sUser.Gid)
|
||||||
|
} else {
|
||||||
|
sUserID = os.Getuid()
|
||||||
|
sGroupID = os.Getgid()
|
||||||
}
|
}
|
||||||
sUserID, _ = strconv.Atoi(sUser.Uid)
|
sTVOS = options.IsTVOS
|
||||||
sGroupID, _ = strconv.Atoi(sUser.Gid)
|
|
||||||
|
// TODO: remove after fixed
|
||||||
|
// https://github.com/golang/go/issues/68760
|
||||||
|
sFixAndroidStack = options.FixAndroidStack
|
||||||
|
|
||||||
os.MkdirAll(sWorkingPath, 0o777)
|
os.MkdirAll(sWorkingPath, 0o777)
|
||||||
os.MkdirAll(sTempPath, 0o777)
|
os.MkdirAll(sTempPath, 0o777)
|
||||||
os.Chown(sWorkingPath, sUserID, sGroupID)
|
if options.Username != "" {
|
||||||
os.Chown(sTempPath, sUserID, sGroupID)
|
os.Chown(sWorkingPath, sUserID, sGroupID)
|
||||||
|
os.Chown(sTempPath, sUserID, sGroupID)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,9 +240,6 @@ func (r *NetworkManager) UpdateInterfaces() error {
|
|||||||
newInterfaces := common.Filter(interfaces, func(it adapter.NetworkInterface) bool {
|
newInterfaces := common.Filter(interfaces, func(it adapter.NetworkInterface) bool {
|
||||||
return it.Flags&net.FlagUp != 0
|
return it.Flags&net.FlagUp != 0
|
||||||
})
|
})
|
||||||
for _, networkInterface := range newInterfaces {
|
|
||||||
networkInterface.RawNetwork = nil
|
|
||||||
}
|
|
||||||
r.networkInterfaces.Store(newInterfaces)
|
r.networkInterfaces.Store(newInterfaces)
|
||||||
if len(newInterfaces) > 0 && !slices.EqualFunc(oldInterfaces, newInterfaces, func(oldInterface adapter.NetworkInterface, newInterface adapter.NetworkInterface) bool {
|
if len(newInterfaces) > 0 && !slices.EqualFunc(oldInterfaces, newInterfaces, func(oldInterface adapter.NetworkInterface, newInterface adapter.NetworkInterface) bool {
|
||||||
return oldInterface.Interface.Index == newInterface.Interface.Index &&
|
return oldInterface.Interface.Index == newInterface.Interface.Index &&
|
||||||
@ -263,15 +260,6 @@ func (r *NetworkManager) UpdateInterfaces() error {
|
|||||||
}
|
}
|
||||||
return F.ToString(it.Name, " (", strings.Join(options, ", "), ")")
|
return F.ToString(it.Name, " (", strings.Join(options, ", "), ")")
|
||||||
}), ", "))
|
}), ", "))
|
||||||
if C.IsAndroid {
|
|
||||||
err = r.platformInterface.SetUnderlyingNetworks(newInterfaces)
|
|
||||||
if err != nil {
|
|
||||||
r.logger.Error("set underlying networks: ", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, networkInterface := range interfaces {
|
|
||||||
networkInterface.RawNetwork = nil
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user