selector.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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.defaultTag != "" {
  56. detour, loaded := s.outbounds[s.defaultTag]
  57. if !loaded {
  58. return E.New("default outbound not found: ", s.defaultTag)
  59. }
  60. s.selected = detour
  61. } else {
  62. s.selected = s.outbounds[s.tags[0]]
  63. }
  64. return nil
  65. }
  66. func (s *Selector) Now() string {
  67. return s.selected.Tag()
  68. }
  69. func (s *Selector) All() []string {
  70. return s.tags
  71. }
  72. func (s *Selector) SelectOutbound(tag string) bool {
  73. detour, loaded := s.outbounds[tag]
  74. if !loaded {
  75. return false
  76. }
  77. s.selected = detour
  78. return true
  79. }
  80. func (s *Selector) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  81. return s.selected.DialContext(ctx, network, destination)
  82. }
  83. func (s *Selector) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  84. return s.selected.ListenPacket(ctx, destination)
  85. }
  86. func (s *Selector) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  87. return s.selected.NewConnection(ctx, conn, metadata)
  88. }
  89. func (s *Selector) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  90. return s.selected.NewPacketConnection(ctx, conn, metadata)
  91. }
  92. func RealTag(detour adapter.Outbound) string {
  93. if group, isGroup := detour.(adapter.OutboundGroup); isGroup {
  94. return group.Now()
  95. }
  96. return detour.Tag()
  97. }