selector.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package outbound
  2. import (
  3. "context"
  4. "net"
  5. "github.com/sagernet/sing-box/adapter"
  6. "github.com/sagernet/sing-box/common/interrupt"
  7. C "github.com/sagernet/sing-box/constant"
  8. "github.com/sagernet/sing-box/log"
  9. "github.com/sagernet/sing-box/option"
  10. E "github.com/sagernet/sing/common/exceptions"
  11. M "github.com/sagernet/sing/common/metadata"
  12. N "github.com/sagernet/sing/common/network"
  13. "github.com/sagernet/sing/service"
  14. )
  15. var (
  16. _ adapter.Outbound = (*Selector)(nil)
  17. _ adapter.OutboundGroup = (*Selector)(nil)
  18. )
  19. type Selector struct {
  20. myOutboundAdapter
  21. ctx context.Context
  22. tags []string
  23. defaultTag string
  24. outbounds map[string]adapter.Outbound
  25. selected adapter.Outbound
  26. interruptGroup *interrupt.Group
  27. interruptExternalConnections bool
  28. }
  29. func NewSelector(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SelectorOutboundOptions) (*Selector, error) {
  30. outbound := &Selector{
  31. myOutboundAdapter: myOutboundAdapter{
  32. protocol: C.TypeSelector,
  33. router: router,
  34. logger: logger,
  35. tag: tag,
  36. dependencies: options.Outbounds,
  37. },
  38. ctx: ctx,
  39. tags: options.Outbounds,
  40. defaultTag: options.Default,
  41. outbounds: make(map[string]adapter.Outbound),
  42. interruptGroup: interrupt.NewGroup(),
  43. interruptExternalConnections: options.InterruptExistConnections,
  44. }
  45. if len(outbound.tags) == 0 {
  46. return nil, E.New("missing tags")
  47. }
  48. return outbound, nil
  49. }
  50. func (s *Selector) Network() []string {
  51. if s.selected == nil {
  52. return []string{N.NetworkTCP, N.NetworkUDP}
  53. }
  54. return s.selected.Network()
  55. }
  56. func (s *Selector) Start() error {
  57. for i, tag := range s.tags {
  58. detour, loaded := s.router.Outbound(tag)
  59. if !loaded {
  60. return E.New("outbound ", i, " not found: ", tag)
  61. }
  62. s.outbounds[tag] = detour
  63. }
  64. if s.tag != "" {
  65. cacheFile := service.FromContext[adapter.CacheFile](s.ctx)
  66. if cacheFile != nil {
  67. selected := cacheFile.LoadSelected(s.tag)
  68. if selected != "" {
  69. detour, loaded := s.outbounds[selected]
  70. if loaded {
  71. s.selected = detour
  72. return nil
  73. }
  74. }
  75. }
  76. }
  77. if s.defaultTag != "" {
  78. detour, loaded := s.outbounds[s.defaultTag]
  79. if !loaded {
  80. return E.New("default outbound not found: ", s.defaultTag)
  81. }
  82. s.selected = detour
  83. return nil
  84. }
  85. s.selected = s.outbounds[s.tags[0]]
  86. return nil
  87. }
  88. func (s *Selector) Now() string {
  89. return s.selected.Tag()
  90. }
  91. func (s *Selector) All() []string {
  92. return s.tags
  93. }
  94. func (s *Selector) SelectOutbound(tag string) bool {
  95. detour, loaded := s.outbounds[tag]
  96. if !loaded {
  97. return false
  98. }
  99. if s.selected == detour {
  100. return true
  101. }
  102. s.selected = detour
  103. if s.tag != "" {
  104. cacheFile := service.FromContext[adapter.CacheFile](s.ctx)
  105. if cacheFile != nil {
  106. err := cacheFile.StoreSelected(s.tag, tag)
  107. if err != nil {
  108. s.logger.Error("store selected: ", err)
  109. }
  110. }
  111. }
  112. s.interruptGroup.Interrupt(s.interruptExternalConnections)
  113. return true
  114. }
  115. func (s *Selector) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  116. conn, err := s.selected.DialContext(ctx, network, destination)
  117. if err != nil {
  118. return nil, err
  119. }
  120. return s.interruptGroup.NewConn(conn, interrupt.IsExternalConnectionFromContext(ctx)), nil
  121. }
  122. func (s *Selector) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  123. conn, err := s.selected.ListenPacket(ctx, destination)
  124. if err != nil {
  125. return nil, err
  126. }
  127. return s.interruptGroup.NewPacketConn(conn, interrupt.IsExternalConnectionFromContext(ctx)), nil
  128. }
  129. func (s *Selector) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  130. ctx = interrupt.ContextWithIsExternalConnection(ctx)
  131. return s.selected.NewConnection(ctx, conn, metadata)
  132. }
  133. func (s *Selector) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  134. ctx = interrupt.ContextWithIsExternalConnection(ctx)
  135. return s.selected.NewPacketConnection(ctx, conn, metadata)
  136. }
  137. func RealTag(detour adapter.Outbound) string {
  138. if group, isGroup := detour.(adapter.OutboundGroup); isGroup {
  139. return group.Now()
  140. }
  141. return detour.Tag()
  142. }