Files
MeshPort/engines/singbox/bridge/platform.go
T
shenandshen c7cc2aa865 Fix VPN internet connectivity and simplify client setup
Initialize Android physical interfaces and preserve macOS scoped routes. Add redacted diagnostics, default DNS, ordered auto-save, simplified controls, scanner rotation settings, and native regression coverage. Restore vendored build sources and document validation and stability checks.
2026-10-12 06:51:25 -07:00

134 lines
5.8 KiB
Go

package bridge
import (
"context"
"errors"
"net"
"net/netip"
"sync"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common/control"
"github.com/sagernet/sing/common/logger"
"github.com/sagernet/sing/common/x/list"
"golang.org/x/sys/unix"
)
// This adapter owns a duplicate of the host's TUN. It never creates OS routes.
// All optional platform capabilities explicitly remain unavailable.
type hostPlatform struct {
config TunConfig
protect func(int) error
monitor *hostMonitor
}
func (p *hostPlatform) Initialize(manager adapter.NetworkManager) error {
// The host monitor already knows the physical default, but sing-box keeps
// its own selectable-interface list. Populate it before outbound startup.
// Android cannot fill this list via the restricted kernel network monitor.
return manager.UpdateInterfaces()
}
func (p *hostPlatform) UsePlatformAutoDetectInterfaceControl() bool { return true }
func (p *hostPlatform) AutoDetectInterfaceControl(fd int) error {
if p.protect == nil {
return errors.New("socket protector missing")
}
return p.protect(fd)
}
func (p *hostPlatform) UsePlatformInterface() bool { return true }
func (p *hostPlatform) OpenInterface(o *tun.Options, _ option.TunPlatformOptions) (tun.Tun, error) {
fd, e := unix.Dup(p.config.FD)
if e != nil {
return nil, e
}
unix.CloseOnExec(fd)
o.FileDescriptor = fd
o.Name = p.config.Name
p.monitor.RegisterMyInterface(o.Name)
result, e := tun.New(*o)
if e != nil {
unix.Close(fd)
}
return result, e
}
func (*hostPlatform) ProcessPlatformOptions(option.TunPlatformOptions) error { return nil }
func (*hostPlatform) UsePlatformDefaultInterfaceMonitor() bool { return true }
func (p *hostPlatform) CreateDefaultInterfaceMonitor(logger.Logger) tun.DefaultInterfaceMonitor {
return p.monitor
}
func (*hostPlatform) UsePlatformNetworkInterfaces() bool { return true }
func (p *hostPlatform) NetworkInterfaces() ([]adapter.NetworkInterface, error) {
return []adapter.NetworkInterface{{Interface: *p.monitor.physical}, {Interface: control.Interface{Name: "lo", Index: 1, MTU: 65536, Flags: net.FlagUp | net.FlagLoopback, Addresses: []netip.Prefix{netip.MustParsePrefix("127.0.0.1/8")}}}}, nil
}
func (*hostPlatform) UnderNetworkExtension() bool { return false }
func (*hostPlatform) NetworkExtensionIncludeAllNetworks() bool { return false }
func (*hostPlatform) ClearDNSCache() {}
func (*hostPlatform) RequestPermissionForWIFIState() error { return errors.New("unsupported") }
func (*hostPlatform) ReadWIFIState(context.Context) adapter.WIFIState { return adapter.WIFIState{} }
func (*hostPlatform) UsePlatformConnectionOwnerFinder() bool { return false }
func (*hostPlatform) FindConnectionOwner(*adapter.FindConnectionOwnerRequest) (*adapter.ConnectionOwner, error) {
return nil, errors.New("unsupported")
}
func (*hostPlatform) UsePlatformWIFIMonitor() bool { return false }
func (*hostPlatform) UsePlatformNotification() bool { return false }
func (*hostPlatform) SendNotification(*adapter.Notification) error { return errors.New("unsupported") }
func (*hostPlatform) CancelNotification(string, int32) error { return errors.New("unsupported") }
func (*hostPlatform) MyInterfaceAddress() []netip.Addr {
return []netip.Addr{netip.MustParseAddr("172.19.0.1"), netip.MustParseAddr("fdfe:dcba:9876::1")}
}
func (*hostPlatform) UsePlatformNeighborResolver() bool { return false }
func (*hostPlatform) StartNeighborMonitor(adapter.NeighborUpdateListener) error {
return errors.New("unsupported")
}
func (*hostPlatform) CloseNeighborMonitor(adapter.NeighborUpdateListener) error { return nil }
func (*hostPlatform) UsePlatformShell() bool { return false }
func (*hostPlatform) CheckPlatformShell() error { return errors.New("unsupported") }
func (*hostPlatform) OpenShellSession(*adapter.PlatformUser, string, []string, string, int32, int32) (adapter.ShellSession, error) {
return nil, errors.New("unsupported")
}
func (*hostPlatform) LookupUser(string) (*adapter.PlatformUser, error) {
return nil, errors.New("unsupported")
}
func (*hostPlatform) LookupSFTPServer() (string, error) { return "", errors.New("unsupported") }
func (*hostPlatform) ReadSystemSSHHostKey() ([]byte, error) { return nil, errors.New("unsupported") }
func (*hostPlatform) TailscaleHostname() string { return "" }
func (*hostPlatform) UsePlatformBridge() bool { return false }
func (*hostPlatform) CreateBridge(adapter.BridgeOptions) (adapter.BridgeSession, error) {
return nil, errors.New("unsupported")
}
type hostMonitor struct {
physical *control.Interface
mu sync.Mutex
names []string
callbacks list.List[tun.DefaultInterfaceUpdateCallback]
}
func (*hostMonitor) Start() error { return nil }
func (*hostMonitor) Close() error { return nil }
func (m *hostMonitor) DefaultInterface() *control.Interface { return m.physical }
func (*hostMonitor) OverrideAndroidVPN() bool { return true }
func (*hostMonitor) AndroidVPNEnabled() bool { return false }
func (m *hostMonitor) RegisterCallback(c tun.DefaultInterfaceUpdateCallback) *list.Element[tun.DefaultInterfaceUpdateCallback] {
m.mu.Lock()
defer m.mu.Unlock()
return m.callbacks.PushBack(c)
}
func (m *hostMonitor) UnregisterCallback(e *list.Element[tun.DefaultInterfaceUpdateCallback]) {
m.mu.Lock()
defer m.mu.Unlock()
m.callbacks.Remove(e)
}
func (m *hostMonitor) RegisterMyInterface(n string) {
m.mu.Lock()
defer m.mu.Unlock()
m.names = append(m.names, n)
}
func (m *hostMonitor) MyInterfaces() []string {
m.mu.Lock()
defer m.mu.Unlock()
return append([]string{}, m.names...)
}