mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-12-27 17:55:37 +08:00
165 lines
4.4 KiB
Go
165 lines
4.4 KiB
Go
package outbound
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
"github.com/sagernet/sing-box/common/interrupt"
|
|
C "github.com/sagernet/sing-box/constant"
|
|
"github.com/sagernet/sing-box/log"
|
|
"github.com/sagernet/sing-box/option"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
N "github.com/sagernet/sing/common/network"
|
|
"github.com/sagernet/sing/service"
|
|
)
|
|
|
|
var (
|
|
_ adapter.Outbound = (*Selector)(nil)
|
|
_ adapter.OutboundGroup = (*Selector)(nil)
|
|
)
|
|
|
|
type Selector struct {
|
|
myOutboundAdapter
|
|
ctx context.Context
|
|
tags []string
|
|
defaultTag string
|
|
outbounds map[string]adapter.Outbound
|
|
selected adapter.Outbound
|
|
interruptGroup *interrupt.Group
|
|
interruptExternalConnections bool
|
|
}
|
|
|
|
func NewSelector(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SelectorOutboundOptions) (*Selector, error) {
|
|
outbound := &Selector{
|
|
myOutboundAdapter: myOutboundAdapter{
|
|
protocol: C.TypeSelector,
|
|
router: router,
|
|
logger: logger,
|
|
tag: tag,
|
|
dependencies: options.Outbounds,
|
|
},
|
|
ctx: ctx,
|
|
tags: options.Outbounds,
|
|
defaultTag: options.Default,
|
|
outbounds: make(map[string]adapter.Outbound),
|
|
interruptGroup: interrupt.NewGroup(),
|
|
interruptExternalConnections: options.InterruptExistConnections,
|
|
}
|
|
if len(outbound.tags) == 0 {
|
|
return nil, E.New("missing tags")
|
|
}
|
|
return outbound, nil
|
|
}
|
|
|
|
func (s *Selector) Network() []string {
|
|
if s.selected == nil {
|
|
return []string{N.NetworkTCP, N.NetworkUDP}
|
|
}
|
|
return s.selected.Network()
|
|
}
|
|
|
|
func (s *Selector) Start() error {
|
|
for i, tag := range s.tags {
|
|
detour, loaded := s.router.Outbound(tag)
|
|
if !loaded {
|
|
return E.New("outbound ", i, " not found: ", tag)
|
|
}
|
|
s.outbounds[tag] = detour
|
|
}
|
|
|
|
if s.tag != "" {
|
|
cacheFile := service.FromContext[adapter.CacheFile](s.ctx)
|
|
if cacheFile != nil {
|
|
selected := cacheFile.LoadSelected(s.tag)
|
|
if selected != "" {
|
|
detour, loaded := s.outbounds[selected]
|
|
if loaded {
|
|
s.selected = detour
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if s.defaultTag != "" {
|
|
detour, loaded := s.outbounds[s.defaultTag]
|
|
if !loaded {
|
|
return E.New("default outbound not found: ", s.defaultTag)
|
|
}
|
|
s.selected = detour
|
|
return nil
|
|
}
|
|
|
|
s.selected = s.outbounds[s.tags[0]]
|
|
return nil
|
|
}
|
|
|
|
func (s *Selector) Now() string {
|
|
selected := s.selected
|
|
if selected == nil {
|
|
return s.tags[0]
|
|
}
|
|
return selected.Tag()
|
|
}
|
|
|
|
func (s *Selector) All() []string {
|
|
return s.tags
|
|
}
|
|
|
|
func (s *Selector) SelectOutbound(tag string) bool {
|
|
detour, loaded := s.outbounds[tag]
|
|
if !loaded {
|
|
return false
|
|
}
|
|
if s.selected == detour {
|
|
return true
|
|
}
|
|
s.selected = detour
|
|
if s.tag != "" {
|
|
cacheFile := service.FromContext[adapter.CacheFile](s.ctx)
|
|
if cacheFile != nil {
|
|
err := cacheFile.StoreSelected(s.tag, tag)
|
|
if err != nil {
|
|
s.logger.Error("store selected: ", err)
|
|
}
|
|
}
|
|
}
|
|
s.interruptGroup.Interrupt(s.interruptExternalConnections)
|
|
return true
|
|
}
|
|
|
|
func (s *Selector) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
|
conn, err := s.selected.DialContext(ctx, network, destination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.interruptGroup.NewConn(conn, interrupt.IsExternalConnectionFromContext(ctx)), nil
|
|
}
|
|
|
|
func (s *Selector) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
|
conn, err := s.selected.ListenPacket(ctx, destination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.interruptGroup.NewPacketConn(conn, interrupt.IsExternalConnectionFromContext(ctx)), nil
|
|
}
|
|
|
|
func (s *Selector) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
ctx = interrupt.ContextWithIsExternalConnection(ctx)
|
|
return s.selected.NewConnection(ctx, conn, metadata)
|
|
}
|
|
|
|
func (s *Selector) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
ctx = interrupt.ContextWithIsExternalConnection(ctx)
|
|
return s.selected.NewPacketConnection(ctx, conn, metadata)
|
|
}
|
|
|
|
func RealTag(detour adapter.Outbound) string {
|
|
if group, isGroup := detour.(adapter.OutboundGroup); isGroup {
|
|
return group.Now()
|
|
}
|
|
return detour.Tag()
|
|
}
|