route.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package option
  2. import (
  3. "github.com/sagernet/sing/common"
  4. E "github.com/sagernet/sing/common/exceptions"
  5. C "github.com/sagernet/sing-box/constant"
  6. "github.com/goccy/go-json"
  7. )
  8. type RouteOptions struct {
  9. GeoIP *GeoIPOptions `json:"geoip,omitempty"`
  10. Geosite *GeositeOptions `json:"geosite,omitempty"`
  11. Rules []Rule `json:"rules,omitempty"`
  12. DefaultDetour string `json:"default_detour,omitempty"`
  13. }
  14. func (o RouteOptions) Equals(other RouteOptions) bool {
  15. return common.ComparablePtrEquals(o.GeoIP, other.GeoIP) &&
  16. common.ComparablePtrEquals(o.Geosite, other.Geosite) &&
  17. common.SliceEquals(o.Rules, other.Rules)
  18. }
  19. type GeoIPOptions struct {
  20. Path string `json:"path,omitempty"`
  21. DownloadURL string `json:"download_url,omitempty"`
  22. DownloadDetour string `json:"download_detour,omitempty"`
  23. }
  24. type GeositeOptions struct {
  25. Path string `json:"path,omitempty"`
  26. DownloadURL string `json:"download_url,omitempty"`
  27. DownloadDetour string `json:"download_detour,omitempty"`
  28. }
  29. type _Rule struct {
  30. Type string `json:"type,omitempty"`
  31. DefaultOptions DefaultRule `json:"-"`
  32. LogicalOptions LogicalRule `json:"-"`
  33. }
  34. type Rule _Rule
  35. func (r Rule) Equals(other Rule) bool {
  36. return r.Type == other.Type &&
  37. r.DefaultOptions.Equals(other.DefaultOptions) &&
  38. r.LogicalOptions.Equals(other.LogicalOptions)
  39. }
  40. func (r Rule) MarshalJSON() ([]byte, error) {
  41. var v any
  42. switch r.Type {
  43. case C.RuleTypeDefault:
  44. v = r.DefaultOptions
  45. case C.RuleTypeLogical:
  46. v = r.LogicalOptions
  47. default:
  48. return nil, E.New("unknown rule type: " + r.Type)
  49. }
  50. return MarshallObjects((_Rule)(r), v)
  51. }
  52. func (r *Rule) UnmarshalJSON(bytes []byte) error {
  53. err := json.Unmarshal(bytes, (*_Rule)(r))
  54. if err != nil {
  55. return err
  56. }
  57. if r.Type == "" {
  58. r.Type = C.RuleTypeDefault
  59. }
  60. var v any
  61. switch r.Type {
  62. case C.RuleTypeDefault:
  63. v = &r.DefaultOptions
  64. case C.RuleTypeLogical:
  65. v = &r.LogicalOptions
  66. default:
  67. return E.New("unknown rule type: " + r.Type)
  68. }
  69. err = UnmarshallExcluded(bytes, (*_Rule)(r), v)
  70. if err != nil {
  71. return E.Cause(err, "route rule")
  72. }
  73. return nil
  74. }
  75. type DefaultRule struct {
  76. Inbound Listable[string] `json:"inbound,omitempty"`
  77. IPVersion int `json:"ip_version,omitempty"`
  78. Network string `json:"network,omitempty"`
  79. Protocol Listable[string] `json:"protocol,omitempty"`
  80. Domain Listable[string] `json:"domain,omitempty"`
  81. DomainSuffix Listable[string] `json:"domain_suffix,omitempty"`
  82. DomainKeyword Listable[string] `json:"domain_keyword,omitempty"`
  83. DomainRegex Listable[string] `json:"domain_regex,omitempty"`
  84. Geosite Listable[string] `json:"geosite,omitempty"`
  85. SourceGeoIP Listable[string] `json:"source_geoip,omitempty"`
  86. GeoIP Listable[string] `json:"geoip,omitempty"`
  87. SourceIPCIDR Listable[string] `json:"source_ip_cidr,omitempty"`
  88. IPCIDR Listable[string] `json:"ip_cidr,omitempty"`
  89. SourcePort Listable[uint16] `json:"source_port,omitempty"`
  90. Port Listable[uint16] `json:"port,omitempty"`
  91. // ProcessName Listable[string] `json:"process_name,omitempty"`
  92. // ProcessPath Listable[string] `json:"process_path,omitempty"`
  93. Outbound string `json:"outbound,omitempty"`
  94. }
  95. func (r DefaultRule) IsValid() bool {
  96. var defaultValue DefaultRule
  97. defaultValue.Outbound = r.Outbound
  98. return !r.Equals(defaultValue)
  99. }
  100. func (r DefaultRule) Equals(other DefaultRule) bool {
  101. return common.ComparableSliceEquals(r.Inbound, other.Inbound) &&
  102. r.IPVersion == other.IPVersion &&
  103. r.Network == other.Network &&
  104. common.ComparableSliceEquals(r.Protocol, other.Protocol) &&
  105. common.ComparableSliceEquals(r.Domain, other.Domain) &&
  106. common.ComparableSliceEquals(r.DomainSuffix, other.DomainSuffix) &&
  107. common.ComparableSliceEquals(r.DomainKeyword, other.DomainKeyword) &&
  108. common.ComparableSliceEquals(r.DomainRegex, other.DomainRegex) &&
  109. common.ComparableSliceEquals(r.Geosite, other.Geosite) &&
  110. common.ComparableSliceEquals(r.SourceGeoIP, other.SourceGeoIP) &&
  111. common.ComparableSliceEquals(r.GeoIP, other.GeoIP) &&
  112. common.ComparableSliceEquals(r.SourceIPCIDR, other.SourceIPCIDR) &&
  113. common.ComparableSliceEquals(r.IPCIDR, other.IPCIDR) &&
  114. common.ComparableSliceEquals(r.SourcePort, other.SourcePort) &&
  115. common.ComparableSliceEquals(r.Port, other.Port) &&
  116. r.Outbound == other.Outbound
  117. }
  118. type LogicalRule struct {
  119. Mode string `json:"mode"`
  120. Rules []DefaultRule `json:"rules,omitempty"`
  121. Outbound string `json:"outbound,omitempty"`
  122. }
  123. func (r LogicalRule) IsValid() bool {
  124. return len(r.Rules) > 0 && common.All(r.Rules, DefaultRule.IsValid)
  125. }
  126. func (r LogicalRule) Equals(other LogicalRule) bool {
  127. return r.Mode == other.Mode &&
  128. common.SliceEquals(r.Rules, other.Rules) &&
  129. r.Outbound == other.Outbound
  130. }