selector.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. },
  32. tags: options.Outbounds,
  33. defaultTag: options.Default,
  34. outbounds: make(map[string]adapter.Outbound),
  35. }
  36. if len(outbound.tags) == 0 {
  37. return nil, E.New("missing tags")
  38. }
  39. return outbound, nil
  40. }
  41. func (s *Selector) Network() []string {
  42. if s.selected == nil {
  43. return []string{N.NetworkTCP, N.NetworkUDP}
  44. }
  45. return s.selected.Network()
  46. }
  47. func (s *Selector) Start() error {
  48. for i, tag := range s.tags {
  49. detour, loaded := s.router.Outbound(tag)
  50. if !loaded {
  51. return E.New("outbound ", i, " not found: ", tag)
  52. }
  53. s.outbounds[tag] = detour
  54. }
  55. if s.tag != "" {
  56. if clashServer := s.router.ClashServer(); clashServer != nil && clashServer.StoreSelected() {
  57. selected := clashServer.CacheFile().LoadSelected(s.tag)
  58. if selected != "" {
  59. detour, loaded := s.outbounds[selected]
  60. if loaded {
  61. s.selected = detour
  62. return nil
  63. }
  64. }
  65. }
  66. }
  67. if s.defaultTag != "" {
  68. detour, loaded := s.outbounds[s.defaultTag]
  69. if !loaded {
  70. return E.New("default outbound not found: ", s.defaultTag)
  71. }
  72. s.selected = detour
  73. return nil
  74. }
  75. s.selected = s.outbounds[s.tags[0]]
  76. return nil
  77. }
  78. func (s *Selector) Now() string {
  79. return s.selected.Tag()
  80. }
  81. func (s *Selector) All() []string {
  82. return s.tags
  83. }
  84. func (s *Selector) SelectOutbound(tag string) bool {
  85. detour, loaded := s.outbounds[tag]
  86. if !loaded {
  87. return false
  88. }
  89. s.selected = detour
  90. if s.tag != "" {
  91. if clashServer := s.router.ClashServer(); clashServer != nil && clashServer.StoreSelected() {
  92. err := clashServer.CacheFile().StoreSelected(s.tag, tag)
  93. if err != nil {
  94. s.logger.Error("store selected: ", err)
  95. }
  96. }
  97. }
  98. return true
  99. }
  100. func (s *Selector) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  101. return s.selected.DialContext(ctx, network, destination)
  102. }
  103. func (s *Selector) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  104. return s.selected.ListenPacket(ctx, destination)
  105. }
  106. func (s *Selector) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  107. return s.selected.NewConnection(ctx, conn, metadata)
  108. }
  109. func (s *Selector) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  110. return s.selected.NewPacketConnection(ctx, conn, metadata)
  111. }
  112. func RealTag(detour adapter.Outbound) string {
  113. if group, isGroup := detour.(adapter.OutboundGroup); isGroup {
  114. return group.Now()
  115. }
  116. return detour.Tag()
  117. }