rule.go 5.1 KB

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