condition.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package router
  2. import (
  3. "net"
  4. "regexp"
  5. "strings"
  6. v2net "v2ray.com/core/common/net"
  7. "v2ray.com/core/proxy"
  8. )
  9. type Condition interface {
  10. Apply(session *proxy.SessionInfo) bool
  11. }
  12. type ConditionChan []Condition
  13. func NewConditionChan() *ConditionChan {
  14. var condChan ConditionChan = make([]Condition, 0, 8)
  15. return &condChan
  16. }
  17. func (v *ConditionChan) Add(cond Condition) *ConditionChan {
  18. *v = append(*v, cond)
  19. return v
  20. }
  21. func (v *ConditionChan) Apply(session *proxy.SessionInfo) bool {
  22. for _, cond := range *v {
  23. if !cond.Apply(session) {
  24. return false
  25. }
  26. }
  27. return true
  28. }
  29. func (v *ConditionChan) Len() int {
  30. return len(*v)
  31. }
  32. type AnyCondition []Condition
  33. func NewAnyCondition() *AnyCondition {
  34. var anyCond AnyCondition = make([]Condition, 0, 8)
  35. return &anyCond
  36. }
  37. func (v *AnyCondition) Add(cond Condition) *AnyCondition {
  38. *v = append(*v, cond)
  39. return v
  40. }
  41. func (v *AnyCondition) Apply(session *proxy.SessionInfo) bool {
  42. for _, cond := range *v {
  43. if cond.Apply(session) {
  44. return true
  45. }
  46. }
  47. return false
  48. }
  49. func (v *AnyCondition) Len() int {
  50. return len(*v)
  51. }
  52. type PlainDomainMatcher struct {
  53. pattern string
  54. }
  55. func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher {
  56. return &PlainDomainMatcher{
  57. pattern: pattern,
  58. }
  59. }
  60. func (v *PlainDomainMatcher) Apply(session *proxy.SessionInfo) bool {
  61. dest := session.Destination
  62. if !dest.Address.Family().IsDomain() {
  63. return false
  64. }
  65. domain := dest.Address.Domain()
  66. return strings.Contains(domain, v.pattern)
  67. }
  68. type RegexpDomainMatcher struct {
  69. pattern *regexp.Regexp
  70. }
  71. func NewRegexpDomainMatcher(pattern string) (*RegexpDomainMatcher, error) {
  72. r, err := regexp.Compile(pattern)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return &RegexpDomainMatcher{
  77. pattern: r,
  78. }, nil
  79. }
  80. func (v *RegexpDomainMatcher) Apply(session *proxy.SessionInfo) bool {
  81. dest := session.Destination
  82. if !dest.Address.Family().IsDomain() {
  83. return false
  84. }
  85. domain := dest.Address.Domain()
  86. return v.pattern.MatchString(strings.ToLower(domain))
  87. }
  88. type CIDRMatcher struct {
  89. cidr *net.IPNet
  90. onSource bool
  91. }
  92. func NewCIDRMatcher(ip []byte, mask uint32, onSource bool) (*CIDRMatcher, error) {
  93. cidr := &net.IPNet{
  94. IP: net.IP(ip),
  95. Mask: net.CIDRMask(int(mask), len(ip)),
  96. }
  97. return &CIDRMatcher{
  98. cidr: cidr,
  99. onSource: onSource,
  100. }, nil
  101. }
  102. func (v *CIDRMatcher) Apply(session *proxy.SessionInfo) bool {
  103. dest := session.Destination
  104. if v.onSource {
  105. dest = session.Source
  106. }
  107. if !dest.Address.Family().Either(v2net.AddressFamilyIPv4, v2net.AddressFamilyIPv6) {
  108. return false
  109. }
  110. return v.cidr.Contains(dest.Address.IP())
  111. }
  112. type IPv4Matcher struct {
  113. ipv4net *v2net.IPNet
  114. onSource bool
  115. }
  116. func NewIPv4Matcher(ipnet *v2net.IPNet, onSource bool) *IPv4Matcher {
  117. return &IPv4Matcher{
  118. ipv4net: ipnet,
  119. onSource: onSource,
  120. }
  121. }
  122. func (v *IPv4Matcher) Apply(session *proxy.SessionInfo) bool {
  123. dest := session.Destination
  124. if v.onSource {
  125. dest = session.Source
  126. }
  127. if !dest.Address.Family().Either(v2net.AddressFamilyIPv4) {
  128. return false
  129. }
  130. return v.ipv4net.Contains(dest.Address.IP())
  131. }
  132. type PortMatcher struct {
  133. port v2net.PortRange
  134. }
  135. func NewPortMatcher(portRange v2net.PortRange) *PortMatcher {
  136. return &PortMatcher{
  137. port: portRange,
  138. }
  139. }
  140. func (v *PortMatcher) Apply(session *proxy.SessionInfo) bool {
  141. return v.port.Contains(session.Destination.Port)
  142. }
  143. type NetworkMatcher struct {
  144. network *v2net.NetworkList
  145. }
  146. func NewNetworkMatcher(network *v2net.NetworkList) *NetworkMatcher {
  147. return &NetworkMatcher{
  148. network: network,
  149. }
  150. }
  151. func (v *NetworkMatcher) Apply(session *proxy.SessionInfo) bool {
  152. return v.network.HasNetwork(session.Destination.Network)
  153. }
  154. type UserMatcher struct {
  155. user []string
  156. }
  157. func NewUserMatcher(users []string) *UserMatcher {
  158. return &UserMatcher{
  159. user: users,
  160. }
  161. }
  162. func (v *UserMatcher) Apply(session *proxy.SessionInfo) bool {
  163. if session.User == nil {
  164. return false
  165. }
  166. for _, u := range v.user {
  167. if u == session.User.Email {
  168. return true
  169. }
  170. }
  171. return false
  172. }
  173. type InboundTagMatcher struct {
  174. tags []string
  175. }
  176. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  177. return &InboundTagMatcher{
  178. tags: tags,
  179. }
  180. }
  181. func (v *InboundTagMatcher) Apply(session *proxy.SessionInfo) bool {
  182. if session.Inbound == nil || len(session.Inbound.Tag) == 0 {
  183. return false
  184. }
  185. for _, t := range v.tags {
  186. if t == session.Inbound.Tag {
  187. return true
  188. }
  189. }
  190. return false
  191. }