condition.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package router
  2. import (
  3. "context"
  4. "regexp"
  5. "strings"
  6. "v2ray.com/core/common/net"
  7. "v2ray.com/core/common/protocol"
  8. "v2ray.com/core/proxy"
  9. )
  10. type Condition interface {
  11. Apply(ctx context.Context) bool
  12. }
  13. type ConditionChan []Condition
  14. func NewConditionChan() *ConditionChan {
  15. var condChan ConditionChan = make([]Condition, 0, 8)
  16. return &condChan
  17. }
  18. func (v *ConditionChan) Add(cond Condition) *ConditionChan {
  19. *v = append(*v, cond)
  20. return v
  21. }
  22. func (v *ConditionChan) Apply(ctx context.Context) bool {
  23. for _, cond := range *v {
  24. if !cond.Apply(ctx) {
  25. return false
  26. }
  27. }
  28. return true
  29. }
  30. func (v *ConditionChan) Len() int {
  31. return len(*v)
  32. }
  33. type AnyCondition []Condition
  34. func NewAnyCondition() *AnyCondition {
  35. var anyCond AnyCondition = make([]Condition, 0, 8)
  36. return &anyCond
  37. }
  38. func (v *AnyCondition) Add(cond Condition) *AnyCondition {
  39. *v = append(*v, cond)
  40. return v
  41. }
  42. func (v *AnyCondition) Apply(ctx context.Context) bool {
  43. for _, cond := range *v {
  44. if cond.Apply(ctx) {
  45. return true
  46. }
  47. }
  48. return false
  49. }
  50. func (v *AnyCondition) Len() int {
  51. return len(*v)
  52. }
  53. type PlainDomainMatcher string
  54. func NewPlainDomainMatcher(pattern string) Condition {
  55. return PlainDomainMatcher(pattern)
  56. }
  57. func (v PlainDomainMatcher) Apply(ctx context.Context) bool {
  58. dest, ok := proxy.TargetFromContext(ctx)
  59. if !ok {
  60. return false
  61. }
  62. if !dest.Address.Family().IsDomain() {
  63. return false
  64. }
  65. domain := dest.Address.Domain()
  66. return strings.Contains(domain, string(v))
  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(ctx context.Context) bool {
  81. dest, ok := proxy.TargetFromContext(ctx)
  82. if !ok {
  83. return false
  84. }
  85. if !dest.Address.Family().IsDomain() {
  86. return false
  87. }
  88. domain := dest.Address.Domain()
  89. return v.pattern.MatchString(strings.ToLower(domain))
  90. }
  91. type SubDomainMatcher string
  92. func NewSubDomainMatcher(p string) Condition {
  93. return SubDomainMatcher(p)
  94. }
  95. func (m SubDomainMatcher) Apply(ctx context.Context) bool {
  96. dest, ok := proxy.TargetFromContext(ctx)
  97. if !ok {
  98. return false
  99. }
  100. if !dest.Address.Family().IsDomain() {
  101. return false
  102. }
  103. domain := dest.Address.Domain()
  104. pattern := string(m)
  105. if !strings.HasSuffix(domain, pattern) {
  106. return false
  107. }
  108. return len(domain) == len(pattern) || domain[len(domain)-len(pattern)-1] == '.'
  109. }
  110. type CIDRMatcher struct {
  111. cidr *net.IPNet
  112. onSource bool
  113. }
  114. func NewCIDRMatcher(ip []byte, mask uint32, onSource bool) (*CIDRMatcher, error) {
  115. cidr := &net.IPNet{
  116. IP: net.IP(ip),
  117. Mask: net.CIDRMask(int(mask), len(ip)*8),
  118. }
  119. return &CIDRMatcher{
  120. cidr: cidr,
  121. onSource: onSource,
  122. }, nil
  123. }
  124. func (v *CIDRMatcher) Apply(ctx context.Context) bool {
  125. ips := make([]net.IP, 0, 4)
  126. if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok {
  127. for _, rip := range resolveIPs {
  128. if !rip.Family().IsIPv6() {
  129. continue
  130. }
  131. ips = append(ips, rip.IP())
  132. }
  133. }
  134. var dest net.Destination
  135. var ok bool
  136. if v.onSource {
  137. dest, ok = proxy.SourceFromContext(ctx)
  138. } else {
  139. dest, ok = proxy.TargetFromContext(ctx)
  140. }
  141. if ok && dest.Address.Family().IsIPv6() {
  142. ips = append(ips, dest.Address.IP())
  143. }
  144. for _, ip := range ips {
  145. if v.cidr.Contains(ip) {
  146. return true
  147. }
  148. }
  149. return false
  150. }
  151. type IPv4Matcher struct {
  152. ipv4net *net.IPNetTable
  153. onSource bool
  154. }
  155. func NewIPv4Matcher(ipnet *net.IPNetTable, onSource bool) *IPv4Matcher {
  156. return &IPv4Matcher{
  157. ipv4net: ipnet,
  158. onSource: onSource,
  159. }
  160. }
  161. func (v *IPv4Matcher) Apply(ctx context.Context) bool {
  162. ips := make([]net.IP, 0, 4)
  163. if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok {
  164. for _, rip := range resolveIPs {
  165. if !rip.Family().IsIPv4() {
  166. continue
  167. }
  168. ips = append(ips, rip.IP())
  169. }
  170. }
  171. var dest net.Destination
  172. var ok bool
  173. if v.onSource {
  174. dest, ok = proxy.SourceFromContext(ctx)
  175. } else {
  176. dest, ok = proxy.TargetFromContext(ctx)
  177. }
  178. if ok && dest.Address.Family().IsIPv4() {
  179. ips = append(ips, dest.Address.IP())
  180. }
  181. for _, ip := range ips {
  182. if v.ipv4net.Contains(ip) {
  183. return true
  184. }
  185. }
  186. return false
  187. }
  188. type PortMatcher struct {
  189. port net.PortRange
  190. }
  191. func NewPortMatcher(portRange net.PortRange) *PortMatcher {
  192. return &PortMatcher{
  193. port: portRange,
  194. }
  195. }
  196. func (v *PortMatcher) Apply(ctx context.Context) bool {
  197. dest, ok := proxy.TargetFromContext(ctx)
  198. if !ok {
  199. return false
  200. }
  201. return v.port.Contains(dest.Port)
  202. }
  203. type NetworkMatcher struct {
  204. network *net.NetworkList
  205. }
  206. func NewNetworkMatcher(network *net.NetworkList) *NetworkMatcher {
  207. return &NetworkMatcher{
  208. network: network,
  209. }
  210. }
  211. func (v *NetworkMatcher) Apply(ctx context.Context) bool {
  212. dest, ok := proxy.TargetFromContext(ctx)
  213. if !ok {
  214. return false
  215. }
  216. return v.network.HasNetwork(dest.Network)
  217. }
  218. type UserMatcher struct {
  219. user []string
  220. }
  221. func NewUserMatcher(users []string) *UserMatcher {
  222. usersCopy := make([]string, 0, len(users))
  223. for _, user := range users {
  224. if len(user) > 0 {
  225. usersCopy = append(usersCopy, user)
  226. }
  227. }
  228. return &UserMatcher{
  229. user: usersCopy,
  230. }
  231. }
  232. func (v *UserMatcher) Apply(ctx context.Context) bool {
  233. user := protocol.UserFromContext(ctx)
  234. if user == nil {
  235. return false
  236. }
  237. for _, u := range v.user {
  238. if u == user.Email {
  239. return true
  240. }
  241. }
  242. return false
  243. }
  244. type InboundTagMatcher struct {
  245. tags []string
  246. }
  247. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  248. tagsCopy := make([]string, 0, len(tags))
  249. for _, tag := range tags {
  250. if len(tag) > 0 {
  251. tagsCopy = append(tagsCopy, tag)
  252. }
  253. }
  254. return &InboundTagMatcher{
  255. tags: tagsCopy,
  256. }
  257. }
  258. func (v *InboundTagMatcher) Apply(ctx context.Context) bool {
  259. tag, ok := proxy.InboundTagFromContext(ctx)
  260. if !ok {
  261. return false
  262. }
  263. for _, t := range v.tags {
  264. if t == tag {
  265. return true
  266. }
  267. }
  268. return false
  269. }