rule.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package option
  2. import (
  3. "context"
  4. "reflect"
  5. C "github.com/sagernet/sing-box/constant"
  6. "github.com/sagernet/sing/common"
  7. E "github.com/sagernet/sing/common/exceptions"
  8. "github.com/sagernet/sing/common/json"
  9. "github.com/sagernet/sing/common/json/badjson"
  10. "github.com/sagernet/sing/common/json/badoption"
  11. )
  12. type _Rule struct {
  13. Type string `json:"type,omitempty"`
  14. DefaultOptions DefaultRule `json:"-"`
  15. LogicalOptions LogicalRule `json:"-"`
  16. }
  17. type Rule _Rule
  18. func (r Rule) MarshalJSON() ([]byte, error) {
  19. var v any
  20. switch r.Type {
  21. case C.RuleTypeDefault:
  22. r.Type = ""
  23. v = r.DefaultOptions
  24. case C.RuleTypeLogical:
  25. v = r.LogicalOptions
  26. default:
  27. return nil, E.New("unknown rule type: " + r.Type)
  28. }
  29. return badjson.MarshallObjects((_Rule)(r), v)
  30. }
  31. func (r *Rule) UnmarshalJSONContext(ctx context.Context, bytes []byte) error {
  32. err := json.UnmarshalContext(ctx, bytes, (*_Rule)(r))
  33. if err != nil {
  34. return err
  35. }
  36. payload, err := rulePayloadWithoutType(ctx, bytes)
  37. if err != nil {
  38. return err
  39. }
  40. switch r.Type {
  41. case "", C.RuleTypeDefault:
  42. r.Type = C.RuleTypeDefault
  43. return unmarshalDefaultRuleContext(ctx, payload, &r.DefaultOptions)
  44. case C.RuleTypeLogical:
  45. return unmarshalLogicalRuleContext(ctx, payload, &r.LogicalOptions)
  46. default:
  47. return E.New("unknown rule type: " + r.Type)
  48. }
  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 badoption.Listable[string] `json:"inbound,omitempty"`
  62. IPVersion int `json:"ip_version,omitempty"`
  63. Network badoption.Listable[string] `json:"network,omitempty"`
  64. AuthUser badoption.Listable[string] `json:"auth_user,omitempty"`
  65. Protocol badoption.Listable[string] `json:"protocol,omitempty"`
  66. Client badoption.Listable[string] `json:"client,omitempty"`
  67. Domain badoption.Listable[string] `json:"domain,omitempty"`
  68. DomainSuffix badoption.Listable[string] `json:"domain_suffix,omitempty"`
  69. DomainKeyword badoption.Listable[string] `json:"domain_keyword,omitempty"`
  70. DomainRegex badoption.Listable[string] `json:"domain_regex,omitempty"`
  71. Geosite badoption.Listable[string] `json:"geosite,omitempty"`
  72. SourceGeoIP badoption.Listable[string] `json:"source_geoip,omitempty"`
  73. GeoIP badoption.Listable[string] `json:"geoip,omitempty"`
  74. SourceIPCIDR badoption.Listable[string] `json:"source_ip_cidr,omitempty"`
  75. SourceIPIsPrivate bool `json:"source_ip_is_private,omitempty"`
  76. IPCIDR badoption.Listable[string] `json:"ip_cidr,omitempty"`
  77. IPIsPrivate bool `json:"ip_is_private,omitempty"`
  78. SourcePort badoption.Listable[uint16] `json:"source_port,omitempty"`
  79. SourcePortRange badoption.Listable[string] `json:"source_port_range,omitempty"`
  80. Port badoption.Listable[uint16] `json:"port,omitempty"`
  81. PortRange badoption.Listable[string] `json:"port_range,omitempty"`
  82. ProcessName badoption.Listable[string] `json:"process_name,omitempty"`
  83. ProcessPath badoption.Listable[string] `json:"process_path,omitempty"`
  84. ProcessPathRegex badoption.Listable[string] `json:"process_path_regex,omitempty"`
  85. PackageName badoption.Listable[string] `json:"package_name,omitempty"`
  86. PackageNameRegex badoption.Listable[string] `json:"package_name_regex,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. SourceMACAddress badoption.Listable[string] `json:"source_mac_address,omitempty"`
  99. SourceHostname badoption.Listable[string] `json:"source_hostname,omitempty"`
  100. PreferredBy badoption.Listable[string] `json:"preferred_by,omitempty"`
  101. RuleSet badoption.Listable[string] `json:"rule_set,omitempty"`
  102. RuleSetIPCIDRMatchSource bool `json:"rule_set_ip_cidr_match_source,omitempty"`
  103. Invert bool `json:"invert,omitempty"`
  104. // Deprecated: renamed to rule_set_ip_cidr_match_source
  105. Deprecated_RulesetIPCIDRMatchSource bool `json:"rule_set_ipcidr_match_source,omitempty"`
  106. }
  107. type DefaultRule struct {
  108. RawDefaultRule
  109. RuleAction
  110. }
  111. func (r DefaultRule) MarshalJSON() ([]byte, error) {
  112. return badjson.MarshallObjects(r.RawDefaultRule, r.RuleAction)
  113. }
  114. func (r *DefaultRule) UnmarshalJSON(data []byte) error {
  115. err := json.Unmarshal(data, &r.RawDefaultRule)
  116. if err != nil {
  117. return err
  118. }
  119. return badjson.UnmarshallExcluded(data, &r.RawDefaultRule, &r.RuleAction)
  120. }
  121. func (r DefaultRule) IsValid() bool {
  122. var defaultValue DefaultRule
  123. defaultValue.Invert = r.Invert
  124. return !reflect.DeepEqual(r, defaultValue)
  125. }
  126. type RawLogicalRule struct {
  127. Mode string `json:"mode"`
  128. Rules []Rule `json:"rules,omitempty"`
  129. Invert bool `json:"invert,omitempty"`
  130. }
  131. type LogicalRule struct {
  132. RawLogicalRule
  133. RuleAction
  134. }
  135. func (r LogicalRule) MarshalJSON() ([]byte, error) {
  136. return badjson.MarshallObjects(r.RawLogicalRule, r.RuleAction)
  137. }
  138. func (r *LogicalRule) UnmarshalJSON(data []byte) error {
  139. err := json.Unmarshal(data, &r.RawLogicalRule)
  140. if err != nil {
  141. return err
  142. }
  143. return badjson.UnmarshallExcluded(data, &r.RawLogicalRule, &r.RuleAction)
  144. }
  145. func rulePayloadWithoutType(ctx context.Context, data []byte) ([]byte, error) {
  146. var content badjson.JSONObject
  147. err := content.UnmarshalJSONContext(ctx, data)
  148. if err != nil {
  149. return nil, err
  150. }
  151. content.Remove("type")
  152. return content.MarshalJSONContext(ctx)
  153. }
  154. func unmarshalDefaultRuleContext(ctx context.Context, data []byte, rule *DefaultRule) error {
  155. rawAction, routeOptions, err := inspectRouteRuleAction(ctx, data)
  156. if err != nil {
  157. return err
  158. }
  159. err = rejectNestedRouteRuleAction(ctx, data)
  160. if err != nil {
  161. return err
  162. }
  163. depth := nestedRuleDepth(ctx)
  164. err = json.UnmarshalContext(ctx, data, &rule.RawDefaultRule)
  165. if err != nil {
  166. return err
  167. }
  168. err = badjson.UnmarshallExcludedContext(ctx, data, &rule.RawDefaultRule, &rule.RuleAction)
  169. if err != nil {
  170. return err
  171. }
  172. if depth > 0 && rawAction == "" && routeOptions == (RouteActionOptions{}) {
  173. rule.RuleAction = RuleAction{}
  174. }
  175. return nil
  176. }
  177. func unmarshalLogicalRuleContext(ctx context.Context, data []byte, rule *LogicalRule) error {
  178. rawAction, routeOptions, err := inspectRouteRuleAction(ctx, data)
  179. if err != nil {
  180. return err
  181. }
  182. err = rejectNestedRouteRuleAction(ctx, data)
  183. if err != nil {
  184. return err
  185. }
  186. depth := nestedRuleDepth(ctx)
  187. err = json.UnmarshalContext(nestedRuleChildContext(ctx), data, &rule.RawLogicalRule)
  188. if err != nil {
  189. return err
  190. }
  191. err = badjson.UnmarshallExcludedContext(ctx, data, &rule.RawLogicalRule, &rule.RuleAction)
  192. if err != nil {
  193. return err
  194. }
  195. if depth > 0 && rawAction == "" && routeOptions == (RouteActionOptions{}) {
  196. rule.RuleAction = RuleAction{}
  197. }
  198. return nil
  199. }
  200. func (r *LogicalRule) IsValid() bool {
  201. return len(r.Rules) > 0 && common.All(r.Rules, Rule.IsValid)
  202. }