condition.go 5.8 KB

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