rule.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package route
  2. import (
  3. "strings"
  4. "github.com/sagernet/sing-box/adapter"
  5. C "github.com/sagernet/sing-box/constant"
  6. "github.com/sagernet/sing-box/log"
  7. "github.com/sagernet/sing-box/option"
  8. "github.com/sagernet/sing/common"
  9. E "github.com/sagernet/sing/common/exceptions"
  10. F "github.com/sagernet/sing/common/format"
  11. )
  12. func NewRule(router adapter.Router, logger log.Logger, options option.Rule) (adapter.Rule, error) {
  13. if common.IsEmptyByEquals(options) {
  14. return nil, E.New("empty rule config")
  15. }
  16. switch options.Type {
  17. case "", C.RuleTypeDefault:
  18. if !options.DefaultOptions.IsValid() {
  19. return nil, E.New("missing conditions")
  20. }
  21. if options.DefaultOptions.Outbound == "" {
  22. return nil, E.New("missing outbound field")
  23. }
  24. return NewDefaultRule(router, logger, options.DefaultOptions)
  25. case C.RuleTypeLogical:
  26. if !options.LogicalOptions.IsValid() {
  27. return nil, E.New("missing conditions")
  28. }
  29. if options.LogicalOptions.Outbound == "" {
  30. return nil, E.New("missing outbound field")
  31. }
  32. return NewLogicalRule(router, logger, options.LogicalOptions)
  33. default:
  34. return nil, E.New("unknown rule type: ", options.Type)
  35. }
  36. }
  37. var _ adapter.Rule = (*DefaultRule)(nil)
  38. type DefaultRule struct {
  39. index int
  40. outbound string
  41. items []RuleItem
  42. }
  43. type RuleItem interface {
  44. Match(metadata *adapter.InboundContext) bool
  45. String() string
  46. }
  47. func NewDefaultRule(router adapter.Router, logger log.Logger, options option.DefaultRule) (*DefaultRule, error) {
  48. rule := &DefaultRule{
  49. outbound: options.Outbound,
  50. }
  51. if len(options.Inbound) > 0 {
  52. rule.items = append(rule.items, NewInboundRule(options.Inbound))
  53. }
  54. if options.IPVersion > 0 {
  55. switch options.IPVersion {
  56. case 4, 6:
  57. rule.items = append(rule.items, NewIPVersionItem(options.IPVersion == 6))
  58. default:
  59. return nil, E.New("invalid ip version: ", options.IPVersion)
  60. }
  61. }
  62. if options.Network != "" {
  63. switch options.Network {
  64. case C.NetworkTCP, C.NetworkUDP:
  65. rule.items = append(rule.items, NewNetworkItem(options.Network))
  66. default:
  67. return nil, E.New("invalid network: ", options.Network)
  68. }
  69. }
  70. if len(options.Protocol) > 0 {
  71. rule.items = append(rule.items, NewProtocolItem(options.Protocol))
  72. }
  73. if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
  74. rule.items = append(rule.items, NewDomainItem(options.Domain, options.DomainSuffix))
  75. }
  76. if len(options.DomainKeyword) > 0 {
  77. rule.items = append(rule.items, NewDomainKeywordItem(options.DomainKeyword))
  78. }
  79. if len(options.DomainRegex) > 0 {
  80. item, err := NewDomainRegexItem(options.DomainRegex)
  81. if err != nil {
  82. return nil, E.Cause(err, "domain_regex")
  83. }
  84. rule.items = append(rule.items, item)
  85. }
  86. if len(options.SourceGeoIP) > 0 {
  87. rule.items = append(rule.items, NewGeoIPItem(router, logger, true, options.SourceGeoIP))
  88. }
  89. if len(options.GeoIP) > 0 {
  90. rule.items = append(rule.items, NewGeoIPItem(router, logger, false, options.GeoIP))
  91. }
  92. if len(options.SourceIPCIDR) > 0 {
  93. item, err := NewIPCIDRItem(true, options.SourceIPCIDR)
  94. if err != nil {
  95. return nil, E.Cause(err, "source_ipcidr")
  96. }
  97. rule.items = append(rule.items, item)
  98. }
  99. if len(options.IPCIDR) > 0 {
  100. item, err := NewIPCIDRItem(false, options.IPCIDR)
  101. if err != nil {
  102. return nil, E.Cause(err, "ipcidr")
  103. }
  104. rule.items = append(rule.items, item)
  105. }
  106. if len(options.SourcePort) > 0 {
  107. rule.items = append(rule.items, NewPortItem(true, options.SourcePort))
  108. }
  109. if len(options.Port) > 0 {
  110. rule.items = append(rule.items, NewPortItem(false, options.Port))
  111. }
  112. return rule, nil
  113. }
  114. func (r *DefaultRule) Match(metadata *adapter.InboundContext) bool {
  115. for _, item := range r.items {
  116. if item.Match(metadata) {
  117. return true
  118. }
  119. }
  120. return false
  121. }
  122. func (r *DefaultRule) Outbound() string {
  123. return r.outbound
  124. }
  125. func (r *DefaultRule) String() string {
  126. return strings.Join(common.Map(r.items, F.ToString0[RuleItem]), " ")
  127. }