condition.go 5.0 KB

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