platform: Add SendNotification

This commit is contained in:
世界 2024-11-06 12:51:53 +08:00
parent f504fb0d46
commit 88099a304a
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
5 changed files with 52 additions and 10 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/sagernet/sing-box" "github.com/sagernet/sing-box"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/process" "github.com/sagernet/sing-box/common/process"
"github.com/sagernet/sing-box/experimental/libbox/platform"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-tun" "github.com/sagernet/sing-tun"
"github.com/sagernet/sing/common/control" "github.com/sagernet/sing/common/control"
@ -54,7 +55,7 @@ func (s *platformInterfaceStub) UsePlatformAutoDetectInterfaceControl() bool {
return true return true
} }
func (s *platformInterfaceStub) AutoDetectInterfaceControl() control.Func { func (s *platformInterfaceStub) AutoDetectInterfaceControl(fd int) error {
return nil return nil
} }
@ -134,6 +135,10 @@ func (s *interfaceMonitorStub) RegisterCallback(callback tun.DefaultInterfaceUpd
func (s *interfaceMonitorStub) UnregisterCallback(element *list.Element[tun.DefaultInterfaceUpdateCallback]) { func (s *interfaceMonitorStub) UnregisterCallback(element *list.Element[tun.DefaultInterfaceUpdateCallback]) {
} }
func (s *platformInterfaceStub) SendNotification(notification *platform.Notification) error {
return nil
}
func FormatConfig(configContent string) (string, error) { func FormatConfig(configContent string) (string, error) {
options, err := parseConfig(configContent) options, err := parseConfig(configContent)
if err != nil { if err != nil {

View file

@ -22,6 +22,7 @@ type PlatformInterface interface {
IncludeAllNetworks() bool IncludeAllNetworks() bool
ReadWIFIState() *WIFIState ReadWIFIState() *WIFIState
ClearDNSCache() ClearDNSCache()
SendNotification(notification *Notification) error
} }
type TunInterface interface { type TunInterface interface {
@ -55,6 +56,16 @@ type NetworkInterfaceIterator interface {
HasNext() bool HasNext() bool
} }
type Notification struct {
Identifier string
TypeName string
TypeID int32
Title string
Subtitle string
Body string
OpenURL string
}
type OnDemandRule interface { type OnDemandRule interface {
Target() int32 Target() int32
DNSSearchDomainMatch() StringIterator DNSSearchDomainMatch() StringIterator

View file

@ -14,7 +14,7 @@ import (
type Interface interface { type Interface interface {
Initialize(ctx context.Context, router adapter.Router) error Initialize(ctx context.Context, router adapter.Router) error
UsePlatformAutoDetectInterfaceControl() bool UsePlatformAutoDetectInterfaceControl() bool
AutoDetectInterfaceControl() control.Func AutoDetectInterfaceControl(fd int) error
OpenTun(options *tun.Options, platformOptions option.TunPlatformOptions) (tun.Tun, error) OpenTun(options *tun.Options, platformOptions option.TunPlatformOptions) (tun.Tun, error)
UsePlatformDefaultInterfaceMonitor() bool UsePlatformDefaultInterfaceMonitor() bool
CreateDefaultInterfaceMonitor(logger logger.Logger) tun.DefaultInterfaceMonitor CreateDefaultInterfaceMonitor(logger logger.Logger) tun.DefaultInterfaceMonitor
@ -25,4 +25,15 @@ type Interface interface {
ClearDNSCache() ClearDNSCache()
ReadWIFIState() adapter.WIFIState ReadWIFIState() adapter.WIFIState
process.Searcher process.Searcher
SendNotification(notification *Notification) error
}
type Notification struct {
Identifier string
TypeName string
TypeID int32
Title string
Subtitle string
Body string
OpenURL string
} }

View file

@ -116,12 +116,8 @@ func (w *platformInterfaceWrapper) UsePlatformAutoDetectInterfaceControl() bool
return w.iif.UsePlatformAutoDetectInterfaceControl() return w.iif.UsePlatformAutoDetectInterfaceControl()
} }
func (w *platformInterfaceWrapper) AutoDetectInterfaceControl() control.Func { func (w *platformInterfaceWrapper) AutoDetectInterfaceControl(fd int) error {
return func(network, address string, conn syscall.RawConn) error {
return control.Raw(conn, func(fd uintptr) error {
return w.iif.AutoDetectInterfaceControl(int32(fd)) return w.iif.AutoDetectInterfaceControl(int32(fd))
})
}
} }
func (w *platformInterfaceWrapper) OpenTun(options *tun.Options, platformOptions option.TunPlatformOptions) (tun.Tun, error) { func (w *platformInterfaceWrapper) OpenTun(options *tun.Options, platformOptions option.TunPlatformOptions) (tun.Tun, error) {
@ -239,3 +235,7 @@ func (w *platformInterfaceWrapper) DisableColors() bool {
func (w *platformInterfaceWrapper) WriteMessage(level log.Level, message string) { func (w *platformInterfaceWrapper) WriteMessage(level log.Level, message string) {
w.iif.WriteLog(message) w.iif.WriteLog(message)
} }
func (w *platformInterfaceWrapper) SendNotification(notification *platform.Notification) error {
return w.iif.SendNotification((*Notification)(notification))
}

View file

@ -10,6 +10,7 @@ import (
"os/user" "os/user"
"runtime" "runtime"
"strings" "strings"
"syscall"
"time" "time"
"github.com/sagernet/sing-box/adapter" "github.com/sagernet/sing-box/adapter"
@ -211,12 +212,19 @@ func NewRouter(
} else { } else {
detour = dialer.NewDetour(router, server.Detour) detour = dialer.NewDetour(router, server.Detour)
} }
var serverProtocol string
switch server.Address { switch server.Address {
case "local": case "local":
serverProtocol = "local"
default: default:
serverURL, _ := url.Parse(server.Address) serverURL, _ := url.Parse(server.Address)
var serverAddress string var serverAddress string
if serverURL != nil { if serverURL != nil {
if serverURL.Scheme == "" {
serverProtocol = "udp"
} else {
serverProtocol = serverURL.Scheme
}
serverAddress = serverURL.Hostname() serverAddress = serverURL.Hostname()
} }
if serverAddress == "" { if serverAddress == "" {
@ -242,9 +250,12 @@ func NewRouter(
} else if dnsOptions.ClientSubnet != nil { } else if dnsOptions.ClientSubnet != nil {
clientSubnet = dnsOptions.ClientSubnet.Build() clientSubnet = dnsOptions.ClientSubnet.Build()
} }
if serverProtocol == "" {
serverProtocol = "transport"
}
transport, err := dns.CreateTransport(dns.TransportOptions{ transport, err := dns.CreateTransport(dns.TransportOptions{
Context: ctx, Context: ctx,
Logger: logFactory.NewLogger(F.ToString("dns/transport[", tag, "]")), Logger: logFactory.NewLogger(F.ToString("dns/", serverProtocol, "[", tag, "]")),
Name: tag, Name: tag,
Dialer: detour, Dialer: detour,
Address: server.Address, Address: server.Address,
@ -1188,7 +1199,11 @@ func (r *Router) AutoDetectInterface() bool {
func (r *Router) AutoDetectInterfaceFunc() control.Func { func (r *Router) AutoDetectInterfaceFunc() control.Func {
if r.platformInterface != nil && r.platformInterface.UsePlatformAutoDetectInterfaceControl() { if r.platformInterface != nil && r.platformInterface.UsePlatformAutoDetectInterfaceControl() {
return r.platformInterface.AutoDetectInterfaceControl() return func(network, address string, conn syscall.RawConn) error {
return control.Raw(conn, func(fd uintptr) error {
return r.platformInterface.AutoDetectInterfaceControl(int(fd))
})
}
} else { } else {
if r.interfaceMonitor == nil { if r.interfaceMonitor == nil {
return nil return nil