rule_ip.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package route
  2. import (
  3. "github.com/sagernet/sing-box/adapter"
  4. C "github.com/sagernet/sing-box/constant"
  5. "github.com/sagernet/sing-box/log"
  6. "github.com/sagernet/sing-box/option"
  7. tun "github.com/sagernet/sing-tun"
  8. "github.com/sagernet/sing/common"
  9. E "github.com/sagernet/sing/common/exceptions"
  10. )
  11. func NewIPRule(router adapter.Router, logger log.ContextLogger, options option.IPRule) (adapter.IPRule, error) {
  12. switch options.Type {
  13. case "", C.RuleTypeDefault:
  14. if !options.DefaultOptions.IsValid() {
  15. return nil, E.New("missing conditions")
  16. }
  17. if common.IsEmpty(options.DefaultOptions.Action) {
  18. return nil, E.New("missing action")
  19. }
  20. return NewDefaultIPRule(router, logger, options.DefaultOptions)
  21. case C.RuleTypeLogical:
  22. if !options.LogicalOptions.IsValid() {
  23. return nil, E.New("missing conditions")
  24. }
  25. if common.IsEmpty(options.DefaultOptions.Action) {
  26. return nil, E.New("missing action")
  27. }
  28. return NewLogicalIPRule(router, logger, options.LogicalOptions)
  29. default:
  30. return nil, E.New("unknown rule type: ", options.Type)
  31. }
  32. }
  33. var _ adapter.IPRule = (*DefaultIPRule)(nil)
  34. type DefaultIPRule struct {
  35. abstractDefaultRule
  36. action tun.ActionType
  37. }
  38. func NewDefaultIPRule(router adapter.Router, logger log.ContextLogger, options option.DefaultIPRule) (*DefaultIPRule, error) {
  39. rule := &DefaultIPRule{
  40. abstractDefaultRule: abstractDefaultRule{
  41. invert: options.Invert,
  42. outbound: options.Outbound,
  43. },
  44. action: tun.ActionType(options.Action),
  45. }
  46. if len(options.Inbound) > 0 {
  47. item := NewInboundRule(options.Inbound)
  48. rule.items = append(rule.items, item)
  49. rule.allItems = append(rule.allItems, item)
  50. }
  51. if options.IPVersion > 0 {
  52. switch options.IPVersion {
  53. case 4, 6:
  54. item := NewIPVersionItem(options.IPVersion == 6)
  55. rule.items = append(rule.items, item)
  56. rule.allItems = append(rule.allItems, item)
  57. default:
  58. return nil, E.New("invalid ip version: ", options.IPVersion)
  59. }
  60. }
  61. if len(options.Network) > 0 {
  62. item := NewNetworkItem(options.Network)
  63. rule.items = append(rule.items, item)
  64. rule.allItems = append(rule.allItems, item)
  65. }
  66. if len(options.Domain) > 0 || len(options.DomainSuffix) > 0 {
  67. item := NewDomainItem(options.Domain, options.DomainSuffix)
  68. rule.destinationAddressItems = append(rule.destinationAddressItems, item)
  69. rule.allItems = append(rule.allItems, item)
  70. }
  71. if len(options.DomainKeyword) > 0 {
  72. item := NewDomainKeywordItem(options.DomainKeyword)
  73. rule.destinationAddressItems = append(rule.destinationAddressItems, item)
  74. rule.allItems = append(rule.allItems, item)
  75. }
  76. if len(options.DomainRegex) > 0 {
  77. item, err := NewDomainRegexItem(options.DomainRegex)
  78. if err != nil {
  79. return nil, E.Cause(err, "domain_regex")
  80. }
  81. rule.destinationAddressItems = append(rule.destinationAddressItems, item)
  82. rule.allItems = append(rule.allItems, item)
  83. }
  84. if len(options.Geosite) > 0 {
  85. item := NewGeositeItem(router, logger, options.Geosite)
  86. rule.destinationAddressItems = append(rule.destinationAddressItems, item)
  87. rule.allItems = append(rule.allItems, item)
  88. }
  89. if len(options.SourceGeoIP) > 0 {
  90. item := NewGeoIPItem(router, logger, true, options.SourceGeoIP)
  91. rule.sourceAddressItems = append(rule.sourceAddressItems, item)
  92. rule.allItems = append(rule.allItems, item)
  93. }
  94. if len(options.SourceIPCIDR) > 0 {
  95. item, err := NewIPCIDRItem(true, options.SourceIPCIDR)
  96. if err != nil {
  97. return nil, E.Cause(err, "source_ipcidr")
  98. }
  99. rule.sourceAddressItems = append(rule.sourceAddressItems, item)
  100. rule.allItems = append(rule.allItems, item)
  101. }
  102. if len(options.SourcePort) > 0 {
  103. item := NewPortItem(true, options.SourcePort)
  104. rule.sourcePortItems = append(rule.sourcePortItems, item)
  105. rule.allItems = append(rule.allItems, item)
  106. }
  107. if len(options.SourcePortRange) > 0 {
  108. item, err := NewPortRangeItem(true, options.SourcePortRange)
  109. if err != nil {
  110. return nil, E.Cause(err, "source_port_range")
  111. }
  112. rule.sourcePortItems = append(rule.sourcePortItems, item)
  113. rule.allItems = append(rule.allItems, item)
  114. }
  115. if len(options.Port) > 0 {
  116. item := NewPortItem(false, options.Port)
  117. rule.destinationPortItems = append(rule.destinationPortItems, item)
  118. rule.allItems = append(rule.allItems, item)
  119. }
  120. if len(options.PortRange) > 0 {
  121. item, err := NewPortRangeItem(false, options.PortRange)
  122. if err != nil {
  123. return nil, E.Cause(err, "port_range")
  124. }
  125. rule.destinationPortItems = append(rule.destinationPortItems, item)
  126. rule.allItems = append(rule.allItems, item)
  127. }
  128. return rule, nil
  129. }
  130. func (r *DefaultIPRule) Action() tun.ActionType {
  131. return r.action
  132. }
  133. var _ adapter.IPRule = (*LogicalIPRule)(nil)
  134. type LogicalIPRule struct {
  135. abstractLogicalRule
  136. action tun.ActionType
  137. }
  138. func NewLogicalIPRule(router adapter.Router, logger log.ContextLogger, options option.LogicalIPRule) (*LogicalIPRule, error) {
  139. r := &LogicalIPRule{
  140. abstractLogicalRule: abstractLogicalRule{
  141. rules: make([]adapter.Rule, len(options.Rules)),
  142. invert: options.Invert,
  143. outbound: options.Outbound,
  144. },
  145. action: tun.ActionType(options.Action),
  146. }
  147. switch options.Mode {
  148. case C.LogicalTypeAnd:
  149. r.mode = C.LogicalTypeAnd
  150. case C.LogicalTypeOr:
  151. r.mode = C.LogicalTypeOr
  152. default:
  153. return nil, E.New("unknown logical mode: ", options.Mode)
  154. }
  155. for i, subRule := range options.Rules {
  156. rule, err := NewDefaultIPRule(router, logger, subRule)
  157. if err != nil {
  158. return nil, E.Cause(err, "sub rule[", i, "]")
  159. }
  160. r.rules[i] = rule
  161. }
  162. return r, nil
  163. }
  164. func (r *LogicalIPRule) Action() tun.ActionType {
  165. return r.action
  166. }