condition.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/session"
  8. "v2ray.com/core/common/strmatcher"
  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. var matcherTypeMap = map[Domain_Type]strmatcher.Type{
  34. Domain_Plain: strmatcher.Substr,
  35. Domain_Regex: strmatcher.Regex,
  36. Domain_Domain: strmatcher.Domain,
  37. Domain_Full: strmatcher.Full,
  38. }
  39. func domainToMatcher(domain *Domain) (strmatcher.Matcher, error) {
  40. matcherType, f := matcherTypeMap[domain.Type]
  41. if !f {
  42. return nil, newError("unsupported domain type", domain.Type)
  43. }
  44. matcher, err := matcherType.New(domain.Value)
  45. if err != nil {
  46. return nil, newError("failed to create domain matcher").Base(err)
  47. }
  48. return matcher, nil
  49. }
  50. type DomainMatcher struct {
  51. matchers strmatcher.IndexMatcher
  52. }
  53. func NewDomainMatcher(domains []*Domain) (*DomainMatcher, error) {
  54. g := new(strmatcher.MatcherGroup)
  55. for _, d := range domains {
  56. m, err := domainToMatcher(d)
  57. if err != nil {
  58. return nil, err
  59. }
  60. g.Add(m)
  61. }
  62. return &DomainMatcher{
  63. matchers: g,
  64. }, nil
  65. }
  66. func (m *DomainMatcher) ApplyDomain(domain string) bool {
  67. return m.matchers.Match(domain) > 0
  68. }
  69. func (m *DomainMatcher) Apply(ctx context.Context) bool {
  70. outbound := session.OutboundFromContext(ctx)
  71. if outbound == nil || !outbound.Target.IsValid() {
  72. return false
  73. }
  74. dest := outbound.Target
  75. if !dest.Address.Family().IsDomain() {
  76. return false
  77. }
  78. return m.ApplyDomain(dest.Address.Domain())
  79. }
  80. func sourceFromContext(ctx context.Context) net.Destination {
  81. inbound := session.InboundFromContext(ctx)
  82. if inbound == nil {
  83. return net.Destination{}
  84. }
  85. return inbound.Source
  86. }
  87. func targetFromContent(ctx context.Context) net.Destination {
  88. outbound := session.OutboundFromContext(ctx)
  89. if outbound == nil {
  90. return net.Destination{}
  91. }
  92. return outbound.Target
  93. }
  94. type MultiGeoIPMatcher struct {
  95. matchers []*GeoIPMatcher
  96. destFunc func(context.Context) net.Destination
  97. }
  98. func NewMultiGeoIPMatcher(geoips []*GeoIP, onSource bool) (*MultiGeoIPMatcher, error) {
  99. var matchers []*GeoIPMatcher
  100. for _, geoip := range geoips {
  101. matcher, err := globalGeoIPContainer.Add(geoip)
  102. if err != nil {
  103. return nil, err
  104. }
  105. matchers = append(matchers, matcher)
  106. }
  107. var destFunc func(context.Context) net.Destination
  108. if onSource {
  109. destFunc = sourceFromContext
  110. } else {
  111. destFunc = targetFromContent
  112. }
  113. return &MultiGeoIPMatcher{
  114. matchers: matchers,
  115. destFunc: destFunc,
  116. }, nil
  117. }
  118. func (m *MultiGeoIPMatcher) Apply(ctx context.Context) bool {
  119. ips := make([]net.IP, 0, 4)
  120. dest := m.destFunc(ctx)
  121. if dest.IsValid() && (dest.Address.Family().IsIPv4() || dest.Address.Family().IsIPv6()) {
  122. ips = append(ips, dest.Address.IP())
  123. } else if resolver, ok := ResolvedIPsFromContext(ctx); ok {
  124. resolvedIPs := resolver.Resolve()
  125. for _, rip := range resolvedIPs {
  126. ips = append(ips, rip.IP())
  127. }
  128. }
  129. for _, ip := range ips {
  130. for _, matcher := range m.matchers {
  131. if matcher.Match(ip) {
  132. return true
  133. }
  134. }
  135. }
  136. return false
  137. }
  138. type PortMatcher struct {
  139. port net.PortRange
  140. }
  141. func NewPortMatcher(portRange net.PortRange) *PortMatcher {
  142. return &PortMatcher{
  143. port: portRange,
  144. }
  145. }
  146. func (v *PortMatcher) Apply(ctx context.Context) bool {
  147. outbound := session.OutboundFromContext(ctx)
  148. if outbound == nil || !outbound.Target.IsValid() {
  149. return false
  150. }
  151. return v.port.Contains(outbound.Target.Port)
  152. }
  153. type NetworkMatcher struct {
  154. network *net.NetworkList
  155. }
  156. func NewNetworkMatcher(network *net.NetworkList) *NetworkMatcher {
  157. return &NetworkMatcher{
  158. network: network,
  159. }
  160. }
  161. func (v *NetworkMatcher) Apply(ctx context.Context) bool {
  162. outbound := session.OutboundFromContext(ctx)
  163. if outbound == nil || !outbound.Target.IsValid() {
  164. return false
  165. }
  166. return v.network.HasNetwork(outbound.Target.Network)
  167. }
  168. type UserMatcher struct {
  169. user []string
  170. }
  171. func NewUserMatcher(users []string) *UserMatcher {
  172. usersCopy := make([]string, 0, len(users))
  173. for _, user := range users {
  174. if len(user) > 0 {
  175. usersCopy = append(usersCopy, user)
  176. }
  177. }
  178. return &UserMatcher{
  179. user: usersCopy,
  180. }
  181. }
  182. func (v *UserMatcher) Apply(ctx context.Context) bool {
  183. inbound := session.InboundFromContext(ctx)
  184. if inbound == nil {
  185. return false
  186. }
  187. user := inbound.User
  188. if user == nil {
  189. return false
  190. }
  191. for _, u := range v.user {
  192. if u == user.Email {
  193. return true
  194. }
  195. }
  196. return false
  197. }
  198. type InboundTagMatcher struct {
  199. tags []string
  200. }
  201. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  202. tagsCopy := make([]string, 0, len(tags))
  203. for _, tag := range tags {
  204. if len(tag) > 0 {
  205. tagsCopy = append(tagsCopy, tag)
  206. }
  207. }
  208. return &InboundTagMatcher{
  209. tags: tagsCopy,
  210. }
  211. }
  212. func (v *InboundTagMatcher) Apply(ctx context.Context) bool {
  213. inbound := session.InboundFromContext(ctx)
  214. if inbound == nil || len(inbound.Tag) == 0 {
  215. return false
  216. }
  217. tag := inbound.Tag
  218. for _, t := range v.tags {
  219. if t == tag {
  220. return true
  221. }
  222. }
  223. return false
  224. }
  225. type ProtocolMatcher struct {
  226. protocols []string
  227. }
  228. func NewProtocolMatcher(protocols []string) *ProtocolMatcher {
  229. pCopy := make([]string, 0, len(protocols))
  230. for _, p := range protocols {
  231. if len(p) > 0 {
  232. pCopy = append(pCopy, p)
  233. }
  234. }
  235. return &ProtocolMatcher{
  236. protocols: pCopy,
  237. }
  238. }
  239. func (m *ProtocolMatcher) Apply(ctx context.Context) bool {
  240. result := dispatcher.SniffingResultFromContext(ctx)
  241. if result == nil {
  242. return false
  243. }
  244. protocol := result.Protocol()
  245. for _, p := range m.protocols {
  246. if strings.HasPrefix(protocol, p) {
  247. return true
  248. }
  249. }
  250. return false
  251. }