condition.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. func resolvedIPFromContext(ctx context.Context) []net.IP {
  95. outbound := session.OutboundFromContext(ctx)
  96. if outbound == nil {
  97. return nil
  98. }
  99. return outbound.ResolvedIPs
  100. }
  101. type MultiGeoIPMatcher struct {
  102. matchers []*GeoIPMatcher
  103. destFunc func(context.Context) net.Destination
  104. resolvedIPFunc func(context.Context) []net.IP
  105. }
  106. func NewMultiGeoIPMatcher(geoips []*GeoIP, onSource bool) (*MultiGeoIPMatcher, error) {
  107. var matchers []*GeoIPMatcher
  108. for _, geoip := range geoips {
  109. matcher, err := globalGeoIPContainer.Add(geoip)
  110. if err != nil {
  111. return nil, err
  112. }
  113. matchers = append(matchers, matcher)
  114. }
  115. matcher := &MultiGeoIPMatcher{
  116. matchers: matchers,
  117. }
  118. if onSource {
  119. matcher.destFunc = sourceFromContext
  120. } else {
  121. matcher.destFunc = targetFromContent
  122. matcher.resolvedIPFunc = resolvedIPFromContext
  123. }
  124. return matcher, nil
  125. }
  126. func (m *MultiGeoIPMatcher) Apply(ctx context.Context) bool {
  127. ips := make([]net.IP, 0, 4)
  128. dest := m.destFunc(ctx)
  129. if dest.IsValid() && dest.Address.Family().IsIP() {
  130. ips = append(ips, dest.Address.IP())
  131. }
  132. if m.resolvedIPFunc != nil {
  133. rips := m.resolvedIPFunc(ctx)
  134. if len(rips) > 0 {
  135. ips = append(ips, rips...)
  136. }
  137. }
  138. for _, ip := range ips {
  139. for _, matcher := range m.matchers {
  140. if matcher.Match(ip) {
  141. return true
  142. }
  143. }
  144. }
  145. return false
  146. }
  147. type PortMatcher struct {
  148. port net.PortRange
  149. }
  150. func NewPortMatcher(portRange net.PortRange) *PortMatcher {
  151. return &PortMatcher{
  152. port: portRange,
  153. }
  154. }
  155. func (v *PortMatcher) Apply(ctx context.Context) bool {
  156. outbound := session.OutboundFromContext(ctx)
  157. if outbound == nil || !outbound.Target.IsValid() {
  158. return false
  159. }
  160. return v.port.Contains(outbound.Target.Port)
  161. }
  162. type NetworkMatcher struct {
  163. list [8]bool
  164. }
  165. func NewNetworkMatcher(network []net.Network) NetworkMatcher {
  166. var matcher NetworkMatcher
  167. for _, n := range network {
  168. matcher.list[int(n)] = true
  169. }
  170. return matcher
  171. }
  172. func (v NetworkMatcher) Apply(ctx context.Context) bool {
  173. outbound := session.OutboundFromContext(ctx)
  174. if outbound == nil || !outbound.Target.IsValid() {
  175. return false
  176. }
  177. return v.list[int(outbound.Target.Network)]
  178. }
  179. type UserMatcher struct {
  180. user []string
  181. }
  182. func NewUserMatcher(users []string) *UserMatcher {
  183. usersCopy := make([]string, 0, len(users))
  184. for _, user := range users {
  185. if len(user) > 0 {
  186. usersCopy = append(usersCopy, user)
  187. }
  188. }
  189. return &UserMatcher{
  190. user: usersCopy,
  191. }
  192. }
  193. func (v *UserMatcher) Apply(ctx context.Context) bool {
  194. inbound := session.InboundFromContext(ctx)
  195. if inbound == nil {
  196. return false
  197. }
  198. user := inbound.User
  199. if user == nil {
  200. return false
  201. }
  202. for _, u := range v.user {
  203. if u == user.Email {
  204. return true
  205. }
  206. }
  207. return false
  208. }
  209. type InboundTagMatcher struct {
  210. tags []string
  211. }
  212. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  213. tagsCopy := make([]string, 0, len(tags))
  214. for _, tag := range tags {
  215. if len(tag) > 0 {
  216. tagsCopy = append(tagsCopy, tag)
  217. }
  218. }
  219. return &InboundTagMatcher{
  220. tags: tagsCopy,
  221. }
  222. }
  223. func (v *InboundTagMatcher) Apply(ctx context.Context) bool {
  224. inbound := session.InboundFromContext(ctx)
  225. if inbound == nil || len(inbound.Tag) == 0 {
  226. return false
  227. }
  228. tag := inbound.Tag
  229. for _, t := range v.tags {
  230. if t == tag {
  231. return true
  232. }
  233. }
  234. return false
  235. }
  236. type ProtocolMatcher struct {
  237. protocols []string
  238. }
  239. func NewProtocolMatcher(protocols []string) *ProtocolMatcher {
  240. pCopy := make([]string, 0, len(protocols))
  241. for _, p := range protocols {
  242. if len(p) > 0 {
  243. pCopy = append(pCopy, p)
  244. }
  245. }
  246. return &ProtocolMatcher{
  247. protocols: pCopy,
  248. }
  249. }
  250. func (m *ProtocolMatcher) Apply(ctx context.Context) bool {
  251. result := dispatcher.SniffingResultFromContext(ctx)
  252. if result == nil {
  253. return false
  254. }
  255. protocol := result.Protocol()
  256. for _, p := range m.protocols {
  257. if strings.HasPrefix(protocol, p) {
  258. return true
  259. }
  260. }
  261. return false
  262. }