selector.go 3.4 KB

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