rule_item_clash_mode.go 778 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package rule
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/sagernet/sing-box/adapter"
  6. "github.com/sagernet/sing/service"
  7. )
  8. var _ RuleItem = (*ClashModeItem)(nil)
  9. type ClashModeItem struct {
  10. ctx context.Context
  11. clashServer adapter.ClashServer
  12. mode string
  13. }
  14. func NewClashModeItem(ctx context.Context, mode string) *ClashModeItem {
  15. return &ClashModeItem{
  16. ctx: ctx,
  17. mode: mode,
  18. }
  19. }
  20. func (r *ClashModeItem) Start() error {
  21. r.clashServer = service.FromContext[adapter.ClashServer](r.ctx)
  22. return nil
  23. }
  24. func (r *ClashModeItem) Match(metadata *adapter.InboundContext) bool {
  25. if r.clashServer == nil {
  26. return false
  27. }
  28. return strings.EqualFold(r.clashServer.Mode(), r.mode)
  29. }
  30. func (r *ClashModeItem) String() string {
  31. return "clash_mode=" + r.mode
  32. }