rule.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package option
  2. import (
  3. "reflect"
  4. C "github.com/sagernet/sing-box/constant"
  5. "github.com/sagernet/sing/common"
  6. E "github.com/sagernet/sing/common/exceptions"
  7. "github.com/sagernet/sing/common/json"
  8. )
  9. type _Rule struct {
  10. Type string `json:"type,omitempty"`
  11. DefaultOptions DefaultRule `json:"-"`
  12. LogicalOptions LogicalRule `json:"-"`
  13. }
  14. type Rule _Rule
  15. func (r Rule) MarshalJSON() ([]byte, error) {
  16. var v any
  17. switch r.Type {
  18. case C.RuleTypeDefault:
  19. r.Type = ""
  20. v = r.DefaultOptions
  21. case C.RuleTypeLogical:
  22. v = r.LogicalOptions
  23. default:
  24. return nil, E.New("unknown rule type: " + r.Type)
  25. }
  26. return MarshallObjects((_Rule)(r), v)
  27. }
  28. func (r *Rule) UnmarshalJSON(bytes []byte) error {
  29. err := json.Unmarshal(bytes, (*_Rule)(r))
  30. if err != nil {
  31. return err
  32. }
  33. var v any
  34. switch r.Type {
  35. case "", C.RuleTypeDefault:
  36. r.Type = C.RuleTypeDefault
  37. v = &r.DefaultOptions
  38. case C.RuleTypeLogical:
  39. v = &r.LogicalOptions
  40. default:
  41. return E.New("unknown rule type: " + r.Type)
  42. }
  43. err = UnmarshallExcluded(bytes, (*_Rule)(r), v)
  44. if err != nil {
  45. return err
  46. }
  47. return nil
  48. }
  49. func (r Rule) IsValid() bool {
  50. switch r.Type {
  51. case C.RuleTypeDefault:
  52. return r.DefaultOptions.IsValid()
  53. case C.RuleTypeLogical:
  54. return r.LogicalOptions.IsValid()
  55. default:
  56. panic("unknown rule type: " + r.Type)
  57. }
  58. }
  59. type RawDefaultRule struct {
  60. Inbound Listable[string] `json:"inbound,omitempty"`
  61. IPVersion int `json:"ip_version,omitempty"`
  62. Network Listable[string] `json:"network,omitempty"`
  63. AuthUser Listable[string] `json:"auth_user,omitempty"`
  64. Protocol Listable[string] `json:"protocol,omitempty"`
  65. Client Listable[string] `json:"client,omitempty"`
  66. Domain Listable[string] `json:"domain,omitempty"`
  67. DomainSuffix Listable[string] `json:"domain_suffix,omitempty"`
  68. DomainKeyword Listable[string] `json:"domain_keyword,omitempty"`
  69. DomainRegex Listable[string] `json:"domain_regex,omitempty"`
  70. Geosite Listable[string] `json:"geosite,omitempty"`
  71. SourceGeoIP Listable[string] `json:"source_geoip,omitempty"`
  72. GeoIP Listable[string] `json:"geoip,omitempty"`
  73. SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
  74. SourceIPIsPrivate bool `json:"source_ip_is_private,omitempty"`
  75. IPCIDR Listable[string] `json:"ip_cidr,omitempty"`
  76. IPIsPrivate bool `json:"ip_is_private,omitempty"`
  77. SourcePort Listable[uint16] `json:"source_port,omitempty"`
  78. SourcePortRange Listable[string] `json:"source_port_range,omitempty"`
  79. Port Listable[uint16] `json:"port,omitempty"`
  80. PortRange Listable[string] `json:"port_range,omitempty"`
  81. ProcessName Listable[string] `json:"process_name,omitempty"`
  82. ProcessPath Listable[string] `json:"process_path,omitempty"`
  83. ProcessPathRegex Listable[string] `json:"process_path_regex,omitempty"`
  84. PackageName Listable[string] `json:"package_name,omitempty"`
  85. User Listable[string] `json:"user,omitempty"`
  86. UserID Listable[int32] `json:"user_id,omitempty"`
  87. ClashMode string `json:"clash_mode,omitempty"`
  88. WIFISSID Listable[string] `json:"wifi_ssid,omitempty"`
  89. WIFIBSSID Listable[string] `json:"wifi_bssid,omitempty"`
  90. RuleSet Listable[string] `json:"rule_set,omitempty"`
  91. RuleSetIPCIDRMatchSource bool `json:"rule_set_ip_cidr_match_source,omitempty"`
  92. Invert bool `json:"invert,omitempty"`
  93. // Deprecated: renamed to rule_set_ip_cidr_match_source
  94. Deprecated_RulesetIPCIDRMatchSource bool `json:"rule_set_ipcidr_match_source,omitempty"`
  95. }
  96. type DefaultRule struct {
  97. RawDefaultRule
  98. RuleAction
  99. }
  100. func (r *DefaultRule) MarshalJSON() ([]byte, error) {
  101. return MarshallObjects(r.RawDefaultRule, r.RuleAction)
  102. }
  103. func (r *DefaultRule) UnmarshalJSON(data []byte) error {
  104. err := json.Unmarshal(data, &r.RawDefaultRule)
  105. if err != nil {
  106. return err
  107. }
  108. return UnmarshallExcluded(data, &r.RawDefaultRule, &r.RuleAction)
  109. }
  110. func (r *DefaultRule) IsValid() bool {
  111. var defaultValue DefaultRule
  112. defaultValue.Invert = r.Invert
  113. defaultValue.Action = r.Action
  114. return !reflect.DeepEqual(r, defaultValue)
  115. }
  116. type _LogicalRule struct {
  117. Mode string `json:"mode"`
  118. Rules []Rule `json:"rules,omitempty"`
  119. Invert bool `json:"invert,omitempty"`
  120. }
  121. type LogicalRule struct {
  122. _LogicalRule
  123. RuleAction
  124. }
  125. func (r *LogicalRule) MarshalJSON() ([]byte, error) {
  126. return MarshallObjects(r._LogicalRule, r.RuleAction)
  127. }
  128. func (r *LogicalRule) UnmarshalJSON(data []byte) error {
  129. err := json.Unmarshal(data, &r._LogicalRule)
  130. if err != nil {
  131. return err
  132. }
  133. return UnmarshallExcluded(data, &r._LogicalRule, &r.RuleAction)
  134. }
  135. func (r *LogicalRule) IsValid() bool {
  136. return len(r.Rules) > 0 && common.All(r.Rules, Rule.IsValid)
  137. }