selector.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. router adapter.Router
  20. tags []string
  21. defaultTag string
  22. outbounds map[string]adapter.Outbound
  23. selected adapter.Outbound
  24. }
  25. func NewSelector(router adapter.Router, logger log.ContextLogger, tag string, options option.SelectorOutboundOptions) (*Selector, error) {
  26. outbound := &Selector{
  27. myOutboundAdapter: myOutboundAdapter{
  28. protocol: C.TypeSelector,
  29. logger: logger,
  30. tag: tag,
  31. },
  32. router: router,
  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{C.NetworkTCP, C.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.defaultTag != "" {
  57. detour, loaded := s.outbounds[s.defaultTag]
  58. if !loaded {
  59. return E.New("default outbound not found: ", s.defaultTag)
  60. }
  61. s.selected = detour
  62. } else {
  63. s.selected = s.outbounds[s.tags[0]]
  64. }
  65. return nil
  66. }
  67. func (s *Selector) Now() string {
  68. return s.selected.Tag()
  69. }
  70. func (s *Selector) All() []string {
  71. return s.tags
  72. }
  73. func (s *Selector) SelectOutbound(tag string) bool {
  74. detour, loaded := s.outbounds[tag]
  75. if !loaded {
  76. return false
  77. }
  78. s.selected = detour
  79. return true
  80. }
  81. func (s *Selector) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  82. return s.selected.DialContext(ctx, network, destination)
  83. }
  84. func (s *Selector) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  85. return s.selected.ListenPacket(ctx, destination)
  86. }
  87. func (s *Selector) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  88. return s.selected.NewConnection(ctx, conn, metadata)
  89. }
  90. func (s *Selector) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  91. return s.selected.NewPacketConnection(ctx, conn, metadata)
  92. }