sing-box/protocol/group/selector.go

183 lines
5.5 KiB
Go
Raw Normal View History

2024-11-01 16:39:02 +00:00
package group
2022-07-21 13:03:41 +00:00
import (
"context"
"net"
"github.com/sagernet/sing-box/adapter"
2024-11-01 16:39:02 +00:00
"github.com/sagernet/sing-box/adapter/outbound"
"github.com/sagernet/sing-box/common/interrupt"
2022-07-21 13:03:41 +00:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
2024-11-24 06:45:40 +00:00
"github.com/sagernet/sing/common/atomic"
2022-07-21 13:03:41 +00:00
E "github.com/sagernet/sing/common/exceptions"
2024-11-01 16:39:02 +00:00
"github.com/sagernet/sing/common/logger"
2022-07-21 13:03:41 +00:00
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
2023-11-28 04:00:28 +00:00
"github.com/sagernet/sing/service"
2022-07-21 13:03:41 +00:00
)
2024-11-01 16:39:02 +00:00
func RegisterSelector(registry *outbound.Registry) {
outbound.Register[option.SelectorOutboundOptions](registry, C.TypeSelector, NewSelector)
}
2024-11-24 06:45:40 +00:00
var (
_ adapter.OutboundGroup = (*Selector)(nil)
_ adapter.ConnectionHandlerEx = (*Selector)(nil)
_ adapter.PacketConnectionHandlerEx = (*Selector)(nil)
)
2022-07-21 13:03:41 +00:00
type Selector struct {
2024-11-01 16:39:02 +00:00
outbound.Adapter
2023-11-28 04:00:28 +00:00
ctx context.Context
2024-11-24 06:45:40 +00:00
outbound adapter.OutboundManager
connection adapter.ConnectionManager
2024-11-01 16:39:02 +00:00
logger logger.ContextLogger
tags []string
defaultTag string
outbounds map[string]adapter.Outbound
2024-11-24 06:45:40 +00:00
selected atomic.TypedValue[adapter.Outbound]
interruptGroup *interrupt.Group
interruptExternalConnections bool
2022-07-21 13:03:41 +00:00
}
2024-11-01 16:39:02 +00:00
func NewSelector(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SelectorOutboundOptions) (adapter.Outbound, error) {
2022-07-21 13:03:41 +00:00
outbound := &Selector{
2024-11-21 10:10:41 +00:00
Adapter: outbound.NewAdapter(C.TypeSelector, tag, nil, options.Outbounds),
2023-11-28 04:00:28 +00:00
ctx: ctx,
2024-11-24 06:45:40 +00:00
outbound: service.FromContext[adapter.OutboundManager](ctx),
connection: service.FromContext[adapter.ConnectionManager](ctx),
2024-11-01 16:39:02 +00:00
logger: logger,
tags: options.Outbounds,
defaultTag: options.Default,
outbounds: make(map[string]adapter.Outbound),
interruptGroup: interrupt.NewGroup(),
interruptExternalConnections: options.InterruptExistConnections,
2022-07-21 13:03:41 +00:00
}
if len(outbound.tags) == 0 {
return nil, E.New("missing tags")
}
return outbound, nil
}
func (s *Selector) Network() []string {
2024-11-24 06:45:40 +00:00
selected := s.selected.Load()
if selected == nil {
2022-07-29 16:29:22 +00:00
return []string{N.NetworkTCP, N.NetworkUDP}
2022-07-21 13:03:41 +00:00
}
2024-11-24 06:45:40 +00:00
return selected.Network()
2022-07-21 13:03:41 +00:00
}
func (s *Selector) Start() error {
for i, tag := range s.tags {
2024-11-24 06:45:40 +00:00
detour, loaded := s.outbound.Outbound(tag)
2022-07-21 13:03:41 +00:00
if !loaded {
return E.New("outbound ", i, " not found: ", tag)
}
s.outbounds[tag] = detour
}
2022-09-10 06:40:16 +00:00
2024-11-01 16:39:02 +00:00
if s.Tag() != "" {
2023-11-28 04:00:28 +00:00
cacheFile := service.FromContext[adapter.CacheFile](s.ctx)
if cacheFile != nil {
2024-11-01 16:39:02 +00:00
selected := cacheFile.LoadSelected(s.Tag())
2022-09-10 06:40:16 +00:00
if selected != "" {
detour, loaded := s.outbounds[selected]
if loaded {
2024-11-24 06:45:40 +00:00
s.selected.Store(detour)
2022-09-10 06:40:16 +00:00
return nil
}
}
}
}
2022-07-21 13:03:41 +00:00
if s.defaultTag != "" {
detour, loaded := s.outbounds[s.defaultTag]
if !loaded {
return E.New("default outbound not found: ", s.defaultTag)
}
2024-11-24 06:45:40 +00:00
s.selected.Store(detour)
2022-09-10 06:40:16 +00:00
return nil
2022-07-21 13:03:41 +00:00
}
2022-09-10 06:40:16 +00:00
2024-11-24 06:45:40 +00:00
s.selected.Store(s.outbounds[s.tags[0]])
2022-07-21 13:03:41 +00:00
return nil
}
func (s *Selector) Now() string {
2024-11-24 06:45:40 +00:00
selected := s.selected.Load()
2024-11-13 05:54:31 +00:00
if selected == nil {
return s.tags[0]
}
return selected.Tag()
2022-07-21 13:03:41 +00:00
}
func (s *Selector) All() []string {
return s.tags
}
func (s *Selector) SelectOutbound(tag string) bool {
detour, loaded := s.outbounds[tag]
if !loaded {
return false
}
2024-11-24 06:45:40 +00:00
if s.selected.Swap(detour) == detour {
return true
}
2024-11-01 16:39:02 +00:00
if s.Tag() != "" {
2023-11-28 04:00:28 +00:00
cacheFile := service.FromContext[adapter.CacheFile](s.ctx)
if cacheFile != nil {
2024-11-01 16:39:02 +00:00
err := cacheFile.StoreSelected(s.Tag(), tag)
2022-09-10 06:40:16 +00:00
if err != nil {
s.logger.Error("store selected: ", err)
}
}
}
s.interruptGroup.Interrupt(s.interruptExternalConnections)
2022-07-21 13:03:41 +00:00
return true
}
func (s *Selector) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
2024-11-24 06:45:40 +00:00
conn, err := s.selected.Load().DialContext(ctx, network, destination)
if err != nil {
return nil, err
}
return s.interruptGroup.NewConn(conn, interrupt.IsExternalConnectionFromContext(ctx)), nil
2022-07-21 13:03:41 +00:00
}
func (s *Selector) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
2024-11-24 06:45:40 +00:00
conn, err := s.selected.Load().ListenPacket(ctx, destination)
if err != nil {
return nil, err
}
return s.interruptGroup.NewPacketConn(conn, interrupt.IsExternalConnectionFromContext(ctx)), nil
2022-07-21 13:03:41 +00:00
}
2024-11-24 06:45:40 +00:00
func (s *Selector) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
ctx = interrupt.ContextWithIsExternalConnection(ctx)
2024-11-24 06:45:40 +00:00
selected := s.selected.Load()
if outboundHandler, isHandler := selected.(adapter.ConnectionHandlerEx); isHandler {
outboundHandler.NewConnectionEx(ctx, conn, metadata, onClose)
2024-10-21 15:38:34 +00:00
} else {
2024-11-24 06:45:40 +00:00
s.connection.NewConnection(ctx, selected, conn, metadata, onClose)
2024-10-21 15:38:34 +00:00
}
2022-07-21 13:03:41 +00:00
}
2024-11-24 06:45:40 +00:00
func (s *Selector) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
ctx = interrupt.ContextWithIsExternalConnection(ctx)
2024-11-24 06:45:40 +00:00
selected := s.selected.Load()
if outboundHandler, isHandler := selected.(adapter.PacketConnectionHandlerEx); isHandler {
outboundHandler.NewPacketConnectionEx(ctx, conn, metadata, onClose)
2024-10-21 15:38:34 +00:00
} else {
2024-11-24 06:45:40 +00:00
s.connection.NewPacketConnection(ctx, selected, conn, metadata, onClose)
2024-10-21 15:38:34 +00:00
}
2022-07-21 13:03:41 +00:00
}
2022-07-28 08:36:31 +00:00
func RealTag(detour adapter.Outbound) string {
if group, isGroup := detour.(adapter.OutboundGroup); isGroup {
return group.Now()
}
return detour.Tag()
}