github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/features/routing/router.go (about)

     1  package routing
     2  
     3  import (
     4  	"github.com/xmplusdev/xmcore/common"
     5  	"github.com/xmplusdev/xmcore/common/serial"
     6  	"github.com/xmplusdev/xmcore/features"
     7  )
     8  
     9  // Router is a feature to choose an outbound tag for the given request.
    10  //
    11  // xray:api:stable
    12  type Router interface {
    13  	features.Feature
    14  
    15  	// PickRoute returns a route decision based on the given routing context.
    16  	PickRoute(ctx Context) (Route, error)
    17  	AddRule(config *serial.TypedMessage, shouldAppend bool) error
    18  	RemoveRule(tag string) error
    19  	
    20  	// Add user routing rule
    21  	AddUsers(tag string, emails []string)
    22  	// Remove user routing rule
    23  	RemoveUsers(emails []string)
    24  }
    25  
    26  // Route is the routing result of Router feature.
    27  //
    28  // xray:api:stable
    29  type Route interface {
    30  	// A Route is also a routing context.
    31  	Context
    32  
    33  	// GetOutboundGroupTags returns the detoured outbound group tags in sequence before a final outbound is chosen.
    34  	GetOutboundGroupTags() []string
    35  
    36  	// GetOutboundTag returns the tag of the outbound the connection was dispatched to.
    37  	GetOutboundTag() string
    38  }
    39  
    40  // RouterType return the type of Router interface. Can be used to implement common.HasType.
    41  //
    42  // xray:api:stable
    43  func RouterType() interface{} {
    44  	return (*Router)(nil)
    45  }
    46  
    47  // DefaultRouter is an implementation of Router, which always returns ErrNoClue for routing decisions.
    48  type DefaultRouter struct{}
    49  
    50  // Type implements common.HasType.
    51  func (DefaultRouter) Type() interface{} {
    52  	return RouterType()
    53  }
    54  
    55  // PickRoute implements Router.
    56  func (DefaultRouter) PickRoute(ctx Context) (Route, error) {
    57  	return nil, common.ErrNoClue
    58  }
    59  
    60  // AddRule implements Router.
    61  func (DefaultRouter) AddRule(config *serial.TypedMessage, shouldAppend bool) error {
    62  	return common.ErrNoClue
    63  }
    64  
    65  // RemoveRule implements Router.
    66  func (DefaultRouter) RemoveRule(tag string) error {
    67  	return common.ErrNoClue
    68  }
    69  
    70  // AddUsers implements Router.
    71  func (DefaultRouter) AddUsers(tag string, emails []string) {}
    72  
    73  // RemoveUsers implements Router.
    74  func (DefaultRouter) RemoveUsers(emails []string)          {}
    75  
    76  // Start implements common.Runnable.
    77  func (DefaultRouter) Start() error {
    78  	return nil
    79  }
    80  
    81  // Close implements common.Closable.
    82  func (DefaultRouter) Close() error {
    83  	return nil
    84  }