1
0

rule.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 DefaultRule 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. Domain Listable[string] `json:"domain,omitempty"`
  66. DomainSuffix Listable[string] `json:"domain_suffix,omitempty"`
  67. DomainKeyword Listable[string] `json:"domain_keyword,omitempty"`
  68. DomainRegex Listable[string] `json:"domain_regex,omitempty"`
  69. Geosite Listable[string] `json:"geosite,omitempty"`
  70. SourceGeoIP Listable[string] `json:"source_geoip,omitempty"`
  71. GeoIP Listable[string] `json:"geoip,omitempty"`
  72. SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
  73. SourceIPIsPrivate bool `json:"source_ip_is_private,omitempty"`
  74. IPCIDR Listable[string] `json:"ip_cidr,omitempty"`
  75. IPIsPrivate bool `json:"ip_is_private,omitempty"`
  76. SourcePort Listable[uint16] `json:"source_port,omitempty"`
  77. SourcePortRange Listable[string] `json:"source_port_range,omitempty"`
  78. Port Listable[uint16] `json:"port,omitempty"`
  79. PortRange Listable[string] `json:"port_range,omitempty"`
  80. ProcessName Listable[string] `json:"process_name,omitempty"`
  81. ProcessPath Listable[string] `json:"process_path,omitempty"`
  82. PackageName Listable[string] `json:"package_name,omitempty"`
  83. User Listable[string] `json:"user,omitempty"`
  84. UserID Listable[int32] `json:"user_id,omitempty"`
  85. ClashMode string `json:"clash_mode,omitempty"`
  86. WIFISSID Listable[string] `json:"wifi_ssid,omitempty"`
  87. WIFIBSSID Listable[string] `json:"wifi_bssid,omitempty"`
  88. RuleSet Listable[string] `json:"rule_set,omitempty"`
  89. RuleSetIPCIDRMatchSource bool `json:"rule_set_ipcidr_match_source,omitempty"`
  90. Invert bool `json:"invert,omitempty"`
  91. Outbound string `json:"outbound,omitempty"`
  92. }
  93. func (r DefaultRule) IsValid() bool {
  94. var defaultValue DefaultRule
  95. defaultValue.Invert = r.Invert
  96. defaultValue.Outbound = r.Outbound
  97. return !reflect.DeepEqual(r, defaultValue)
  98. }
  99. type LogicalRule struct {
  100. Mode string `json:"mode"`
  101. Rules []Rule `json:"rules,omitempty"`
  102. Invert bool `json:"invert,omitempty"`
  103. Outbound string `json:"outbound,omitempty"`
  104. }
  105. func (r LogicalRule) IsValid() bool {
  106. return len(r.Rules) > 0 && common.All(r.Rules, Rule.IsValid)
  107. }