condition.go 6.4 KB

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