rule.go 5.9 KB

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