condition.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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, ok := proxy.TargetFromContext(ctx)
  64. if !ok {
  65. return false
  66. }
  67. if !dest.Address.Family().IsDomain() {
  68. return false
  69. }
  70. domain := dest.Address.Domain()
  71. return strings.Contains(domain, v.pattern)
  72. }
  73. type RegexpDomainMatcher struct {
  74. pattern *regexp.Regexp
  75. }
  76. func NewRegexpDomainMatcher(pattern string) (*RegexpDomainMatcher, error) {
  77. r, err := regexp.Compile(pattern)
  78. if err != nil {
  79. return nil, err
  80. }
  81. return &RegexpDomainMatcher{
  82. pattern: r,
  83. }, nil
  84. }
  85. func (v *RegexpDomainMatcher) Apply(ctx context.Context) bool {
  86. dest, ok := proxy.TargetFromContext(ctx)
  87. if !ok {
  88. return false
  89. }
  90. if !dest.Address.Family().IsDomain() {
  91. return false
  92. }
  93. domain := dest.Address.Domain()
  94. return v.pattern.MatchString(strings.ToLower(domain))
  95. }
  96. type CIDRMatcher struct {
  97. cidr *net.IPNet
  98. onSource bool
  99. }
  100. func NewCIDRMatcher(ip []byte, mask uint32, onSource bool) (*CIDRMatcher, error) {
  101. cidr := &net.IPNet{
  102. IP: net.IP(ip),
  103. Mask: net.CIDRMask(int(mask), len(ip)),
  104. }
  105. return &CIDRMatcher{
  106. cidr: cidr,
  107. onSource: onSource,
  108. }, nil
  109. }
  110. func (v *CIDRMatcher) Apply(ctx context.Context) bool {
  111. ips := make([]net.IP, 0, 4)
  112. if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok {
  113. for _, rip := range resolveIPs {
  114. if !rip.Family().IsIPv6() {
  115. continue
  116. }
  117. ips = append(ips, rip.IP())
  118. }
  119. }
  120. var dest v2net.Destination
  121. var ok bool
  122. if v.onSource {
  123. dest, ok = proxy.SourceFromContext(ctx)
  124. } else {
  125. dest, ok = proxy.TargetFromContext(ctx)
  126. }
  127. if ok && dest.Address.Family().IsIPv6() {
  128. ips = append(ips, dest.Address.IP())
  129. }
  130. for _, ip := range ips {
  131. if v.cidr.Contains(ip) {
  132. return true
  133. }
  134. }
  135. return false
  136. }
  137. type IPv4Matcher struct {
  138. ipv4net *v2net.IPNet
  139. onSource bool
  140. }
  141. func NewIPv4Matcher(ipnet *v2net.IPNet, onSource bool) *IPv4Matcher {
  142. return &IPv4Matcher{
  143. ipv4net: ipnet,
  144. onSource: onSource,
  145. }
  146. }
  147. func (v *IPv4Matcher) Apply(ctx context.Context) bool {
  148. ips := make([]net.IP, 0, 4)
  149. if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok {
  150. for _, rip := range resolveIPs {
  151. if !rip.Family().IsIPv4() {
  152. continue
  153. }
  154. ips = append(ips, rip.IP())
  155. }
  156. }
  157. var dest v2net.Destination
  158. var ok bool
  159. if v.onSource {
  160. dest, ok = proxy.SourceFromContext(ctx)
  161. } else {
  162. dest, ok = proxy.TargetFromContext(ctx)
  163. }
  164. if ok && dest.Address.Family().IsIPv4() {
  165. ips = append(ips, dest.Address.IP())
  166. }
  167. for _, ip := range ips {
  168. if v.ipv4net.Contains(ip) {
  169. return true
  170. }
  171. }
  172. return false
  173. }
  174. type PortMatcher struct {
  175. port v2net.PortRange
  176. }
  177. func NewPortMatcher(portRange v2net.PortRange) *PortMatcher {
  178. return &PortMatcher{
  179. port: portRange,
  180. }
  181. }
  182. func (v *PortMatcher) Apply(ctx context.Context) bool {
  183. dest, ok := proxy.TargetFromContext(ctx)
  184. if !ok {
  185. return false
  186. }
  187. return v.port.Contains(dest.Port)
  188. }
  189. type NetworkMatcher struct {
  190. network *v2net.NetworkList
  191. }
  192. func NewNetworkMatcher(network *v2net.NetworkList) *NetworkMatcher {
  193. return &NetworkMatcher{
  194. network: network,
  195. }
  196. }
  197. func (v *NetworkMatcher) Apply(ctx context.Context) bool {
  198. dest, ok := proxy.TargetFromContext(ctx)
  199. if !ok {
  200. return false
  201. }
  202. return v.network.HasNetwork(dest.Network)
  203. }
  204. type UserMatcher struct {
  205. user []string
  206. }
  207. func NewUserMatcher(users []string) *UserMatcher {
  208. return &UserMatcher{
  209. user: users,
  210. }
  211. }
  212. func (v *UserMatcher) Apply(ctx context.Context) bool {
  213. user := protocol.UserFromContext(ctx)
  214. if user == nil {
  215. return false
  216. }
  217. for _, u := range v.user {
  218. if u == user.Email {
  219. return true
  220. }
  221. }
  222. return false
  223. }
  224. type InboundTagMatcher struct {
  225. tags []string
  226. }
  227. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  228. return &InboundTagMatcher{
  229. tags: tags,
  230. }
  231. }
  232. func (v *InboundTagMatcher) Apply(ctx context.Context) bool {
  233. tag, ok := proxy.InboundTagFromContext(ctx)
  234. if !ok {
  235. return false
  236. }
  237. for _, t := range v.tags {
  238. if t == tag {
  239. return true
  240. }
  241. }
  242. return false
  243. }