rule.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. InterfaceAddress *badjson.TypedMap[string, badoption.Listable[badoption.Prefixable]] `json:"interface_address,omitempty"`
  96. NetworkInterfaceAddress *badjson.TypedMap[InterfaceType, badoption.Listable[badoption.Prefixable]] `json:"network_interface_address,omitempty"`
  97. DefaultInterfaceAddress badoption.Listable[badoption.Prefixable] `json:"default_interface_address,omitempty"`
  98. PreferredBy badoption.Listable[string] `json:"preferred_by,omitempty"`
  99. RuleSet badoption.Listable[string] `json:"rule_set,omitempty"`
  100. RuleSetIPCIDRMatchSource bool `json:"rule_set_ip_cidr_match_source,omitempty"`
  101. Invert bool `json:"invert,omitempty"`
  102. // Deprecated: renamed to rule_set_ip_cidr_match_source
  103. Deprecated_RulesetIPCIDRMatchSource bool `json:"rule_set_ipcidr_match_source,omitempty"`
  104. }
  105. type DefaultRule struct {
  106. RawDefaultRule
  107. RuleAction
  108. }
  109. func (r DefaultRule) MarshalJSON() ([]byte, error) {
  110. return badjson.MarshallObjects(r.RawDefaultRule, r.RuleAction)
  111. }
  112. func (r *DefaultRule) UnmarshalJSON(data []byte) error {
  113. err := json.Unmarshal(data, &r.RawDefaultRule)
  114. if err != nil {
  115. return err
  116. }
  117. return badjson.UnmarshallExcluded(data, &r.RawDefaultRule, &r.RuleAction)
  118. }
  119. func (r DefaultRule) IsValid() bool {
  120. var defaultValue DefaultRule
  121. defaultValue.Invert = r.Invert
  122. return !reflect.DeepEqual(r, defaultValue)
  123. }
  124. type RawLogicalRule struct {
  125. Mode string `json:"mode"`
  126. Rules []Rule `json:"rules,omitempty"`
  127. Invert bool `json:"invert,omitempty"`
  128. }
  129. type LogicalRule struct {
  130. RawLogicalRule
  131. RuleAction
  132. }
  133. func (r LogicalRule) MarshalJSON() ([]byte, error) {
  134. return badjson.MarshallObjects(r.RawLogicalRule, r.RuleAction)
  135. }
  136. func (r *LogicalRule) UnmarshalJSON(data []byte) error {
  137. err := json.Unmarshal(data, &r.RawLogicalRule)
  138. if err != nil {
  139. return err
  140. }
  141. return badjson.UnmarshallExcluded(data, &r.RawLogicalRule, &r.RuleAction)
  142. }
  143. func (r *LogicalRule) IsValid() bool {
  144. return len(r.Rules) > 0 && common.All(r.Rules, Rule.IsValid)
  145. }