condition.go 6.4 KB

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