condition.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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().IsIP() {
  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. list [8]bool
  155. }
  156. func NewNetworkMatcher(network *net.NetworkList) NetworkMatcher {
  157. var matcher NetworkMatcher
  158. for _, n := range network.Network {
  159. matcher.list[int(n)] = true
  160. }
  161. return matcher
  162. }
  163. func (v NetworkMatcher) Apply(ctx context.Context) bool {
  164. outbound := session.OutboundFromContext(ctx)
  165. if outbound == nil || !outbound.Target.IsValid() {
  166. return false
  167. }
  168. return v.list[int(outbound.Target.Network)]
  169. }
  170. type UserMatcher struct {
  171. user []string
  172. }
  173. func NewUserMatcher(users []string) *UserMatcher {
  174. usersCopy := make([]string, 0, len(users))
  175. for _, user := range users {
  176. if len(user) > 0 {
  177. usersCopy = append(usersCopy, user)
  178. }
  179. }
  180. return &UserMatcher{
  181. user: usersCopy,
  182. }
  183. }
  184. func (v *UserMatcher) Apply(ctx context.Context) bool {
  185. inbound := session.InboundFromContext(ctx)
  186. if inbound == nil {
  187. return false
  188. }
  189. user := inbound.User
  190. if user == nil {
  191. return false
  192. }
  193. for _, u := range v.user {
  194. if u == user.Email {
  195. return true
  196. }
  197. }
  198. return false
  199. }
  200. type InboundTagMatcher struct {
  201. tags []string
  202. }
  203. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  204. tagsCopy := make([]string, 0, len(tags))
  205. for _, tag := range tags {
  206. if len(tag) > 0 {
  207. tagsCopy = append(tagsCopy, tag)
  208. }
  209. }
  210. return &InboundTagMatcher{
  211. tags: tagsCopy,
  212. }
  213. }
  214. func (v *InboundTagMatcher) Apply(ctx context.Context) bool {
  215. inbound := session.InboundFromContext(ctx)
  216. if inbound == nil || len(inbound.Tag) == 0 {
  217. return false
  218. }
  219. tag := inbound.Tag
  220. for _, t := range v.tags {
  221. if t == tag {
  222. return true
  223. }
  224. }
  225. return false
  226. }
  227. type ProtocolMatcher struct {
  228. protocols []string
  229. }
  230. func NewProtocolMatcher(protocols []string) *ProtocolMatcher {
  231. pCopy := make([]string, 0, len(protocols))
  232. for _, p := range protocols {
  233. if len(p) > 0 {
  234. pCopy = append(pCopy, p)
  235. }
  236. }
  237. return &ProtocolMatcher{
  238. protocols: pCopy,
  239. }
  240. }
  241. func (m *ProtocolMatcher) Apply(ctx context.Context) bool {
  242. result := dispatcher.SniffingResultFromContext(ctx)
  243. if result == nil {
  244. return false
  245. }
  246. protocol := result.Protocol()
  247. for _, p := range m.protocols {
  248. if strings.HasPrefix(protocol, p) {
  249. return true
  250. }
  251. }
  252. return false
  253. }