condition.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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, 0, 4)
  106. if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok {
  107. for _, rip := range resolveIPs {
  108. if !rip.Family().IsIPv6() {
  109. continue
  110. }
  111. ips = append(ips, rip.IP())
  112. }
  113. }
  114. var dest v2net.Destination
  115. if v.onSource {
  116. dest = proxy.SourceFromContext(ctx)
  117. } else {
  118. dest = proxy.DestinationFromContext(ctx)
  119. }
  120. if dest.IsValid() && dest.Address.Family().IsIPv6() {
  121. ips = append(ips, dest.Address.IP())
  122. }
  123. for _, ip := range ips {
  124. if v.cidr.Contains(ip) {
  125. return true
  126. }
  127. }
  128. return false
  129. }
  130. type IPv4Matcher struct {
  131. ipv4net *v2net.IPNet
  132. onSource bool
  133. }
  134. func NewIPv4Matcher(ipnet *v2net.IPNet, onSource bool) *IPv4Matcher {
  135. return &IPv4Matcher{
  136. ipv4net: ipnet,
  137. onSource: onSource,
  138. }
  139. }
  140. func (v *IPv4Matcher) Apply(ctx context.Context) bool {
  141. ips := make([]net.IP, 0, 4)
  142. if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok {
  143. for _, rip := range resolveIPs {
  144. if !rip.Family().IsIPv4() {
  145. continue
  146. }
  147. ips = append(ips, rip.IP())
  148. }
  149. }
  150. var dest v2net.Destination
  151. if v.onSource {
  152. dest = proxy.SourceFromContext(ctx)
  153. } else {
  154. dest = proxy.DestinationFromContext(ctx)
  155. }
  156. if dest.IsValid() && dest.Address.Family().IsIPv4() {
  157. ips = append(ips, dest.Address.IP())
  158. }
  159. for _, ip := range ips {
  160. if v.ipv4net.Contains(ip) {
  161. return true
  162. }
  163. }
  164. return false
  165. }
  166. type PortMatcher struct {
  167. port v2net.PortRange
  168. }
  169. func NewPortMatcher(portRange v2net.PortRange) *PortMatcher {
  170. return &PortMatcher{
  171. port: portRange,
  172. }
  173. }
  174. func (v *PortMatcher) Apply(ctx context.Context) bool {
  175. dest := proxy.DestinationFromContext(ctx)
  176. return v.port.Contains(dest.Port)
  177. }
  178. type NetworkMatcher struct {
  179. network *v2net.NetworkList
  180. }
  181. func NewNetworkMatcher(network *v2net.NetworkList) *NetworkMatcher {
  182. return &NetworkMatcher{
  183. network: network,
  184. }
  185. }
  186. func (v *NetworkMatcher) Apply(ctx context.Context) bool {
  187. dest := proxy.DestinationFromContext(ctx)
  188. return v.network.HasNetwork(dest.Network)
  189. }
  190. type UserMatcher struct {
  191. user []string
  192. }
  193. func NewUserMatcher(users []string) *UserMatcher {
  194. return &UserMatcher{
  195. user: users,
  196. }
  197. }
  198. func (v *UserMatcher) Apply(ctx context.Context) bool {
  199. user := protocol.UserFromContext(ctx)
  200. if user == nil {
  201. return false
  202. }
  203. for _, u := range v.user {
  204. if u == user.Email {
  205. return true
  206. }
  207. }
  208. return false
  209. }
  210. type InboundTagMatcher struct {
  211. tags []string
  212. }
  213. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  214. return &InboundTagMatcher{
  215. tags: tags,
  216. }
  217. }
  218. func (v *InboundTagMatcher) Apply(ctx context.Context) bool {
  219. tag := proxy.InboundTagFromContext(ctx)
  220. if len(tag) == 0 {
  221. return false
  222. }
  223. for _, t := range v.tags {
  224. if t == tag {
  225. return true
  226. }
  227. }
  228. return false
  229. }