fieldrule.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package json
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net"
  6. "regexp"
  7. "strings"
  8. v2net "github.com/v2ray/v2ray-core/common/net"
  9. v2netjson "github.com/v2ray/v2ray-core/common/net/json"
  10. )
  11. type StringList []string
  12. func NewStringList(str ...string) *StringList {
  13. list := StringList(str)
  14. return &list
  15. }
  16. func (this *StringList) UnmarshalJSON(data []byte) error {
  17. var strList []string
  18. err := json.Unmarshal(data, &strList)
  19. if err == nil {
  20. *this = make([]string, len(strList))
  21. copy(*this, strList)
  22. return nil
  23. }
  24. var str string
  25. err = json.Unmarshal(data, &str)
  26. if err == nil {
  27. *this = make([]string, 0, 1)
  28. *this = append(*this, str)
  29. return nil
  30. }
  31. return errors.New("Failed to unmarshal string list: " + string(data))
  32. }
  33. func (this *StringList) Len() int {
  34. return len([]string(*this))
  35. }
  36. type DomainMatcher interface {
  37. Match(domain string) bool
  38. }
  39. type PlainDomainMatcher struct {
  40. pattern string
  41. }
  42. func (this *PlainDomainMatcher) Match(domain string) bool {
  43. return strings.Contains(this.pattern, domain)
  44. }
  45. type RegexpDomainMatcher struct {
  46. pattern *regexp.Regexp
  47. }
  48. func NewRegexpDomainMatcher(pattern string) (*RegexpDomainMatcher, error) {
  49. r, err := regexp.Compile(pattern)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &RegexpDomainMatcher{
  54. pattern: r,
  55. }, nil
  56. }
  57. func (this *RegexpDomainMatcher) Match(domain string) bool {
  58. return this.pattern.MatchString(domain)
  59. }
  60. type FieldRule struct {
  61. Rule
  62. Domain []DomainMatcher
  63. IP []*net.IPNet
  64. Port v2net.PortRange
  65. Network v2net.NetworkList
  66. }
  67. func (this *FieldRule) Apply(dest v2net.Destination) bool {
  68. address := dest.Address()
  69. if len(this.Domain) > 0 {
  70. if !address.IsDomain() {
  71. return false
  72. }
  73. foundMatch := false
  74. for _, domain := range this.Domain {
  75. if domain.Match(address.Domain()) {
  76. foundMatch = true
  77. break
  78. }
  79. }
  80. if !foundMatch {
  81. return false
  82. }
  83. }
  84. if this.IP != nil && len(this.IP) > 0 {
  85. if !(address.IsIPv4() || address.IsIPv6()) {
  86. return false
  87. }
  88. foundMatch := false
  89. for _, ipnet := range this.IP {
  90. if ipnet.Contains(address.IP()) {
  91. foundMatch = true
  92. break
  93. }
  94. }
  95. if !foundMatch {
  96. return false
  97. }
  98. }
  99. if this.Port != nil {
  100. port := address.Port()
  101. if port.Value() < this.Port.From().Value() || port.Value() > this.Port.To().Value() {
  102. return false
  103. }
  104. }
  105. if this.Network != nil {
  106. if !this.Network.HasNetwork(v2net.Network(dest.Network())) {
  107. return false
  108. }
  109. }
  110. return true
  111. }
  112. func (this *FieldRule) UnmarshalJSON(data []byte) error {
  113. type RawFieldRule struct {
  114. Rule
  115. Domain *StringList `json:"domain"`
  116. IP *StringList `json:"ip"`
  117. Port *v2netjson.PortRange `json:"port"`
  118. Network *v2netjson.NetworkList `json:"network"`
  119. }
  120. rawFieldRule := RawFieldRule{}
  121. err := json.Unmarshal(data, &rawFieldRule)
  122. if err != nil {
  123. return err
  124. }
  125. this.Type = rawFieldRule.Type
  126. this.OutboundTag = rawFieldRule.OutboundTag
  127. hasField := false
  128. if rawFieldRule.Domain != nil && rawFieldRule.Domain.Len() > 0 {
  129. this.Domain = make([]DomainMatcher, rawFieldRule.Domain.Len())
  130. for idx, rawDomain := range *(rawFieldRule.Domain) {
  131. var matcher DomainMatcher
  132. if strings.HasPrefix(rawDomain, "regexp:") {
  133. rawMatcher, err := NewRegexpDomainMatcher(rawDomain[7:])
  134. if err != nil {
  135. return err
  136. }
  137. matcher = rawMatcher
  138. } else {
  139. matcher = &PlainDomainMatcher{pattern: rawDomain}
  140. }
  141. this.Domain[idx] = matcher
  142. }
  143. hasField = true
  144. }
  145. if rawFieldRule.IP != nil && rawFieldRule.IP.Len() > 0 {
  146. this.IP = make([]*net.IPNet, 0, rawFieldRule.IP.Len())
  147. for _, ipStr := range *(rawFieldRule.IP) {
  148. _, ipNet, err := net.ParseCIDR(ipStr)
  149. if err != nil {
  150. return errors.New("Invalid IP range in router rule: " + err.Error())
  151. }
  152. this.IP = append(this.IP, ipNet)
  153. }
  154. hasField = true
  155. }
  156. if rawFieldRule.Port != nil {
  157. this.Port = rawFieldRule.Port
  158. hasField = true
  159. }
  160. if rawFieldRule.Network != nil {
  161. this.Network = rawFieldRule.Network
  162. hasField = true
  163. }
  164. if !hasField {
  165. return errors.New("This rule has no effective fields.")
  166. }
  167. return nil
  168. }