condition.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // +build !confonly
  2. package router
  3. import (
  4. "strings"
  5. "v2ray.com/core/common/net"
  6. "v2ray.com/core/common/strmatcher"
  7. )
  8. type Condition interface {
  9. Apply(ctx *Context) bool
  10. }
  11. type ConditionChan []Condition
  12. func NewConditionChan() *ConditionChan {
  13. var condChan ConditionChan = make([]Condition, 0, 8)
  14. return &condChan
  15. }
  16. func (v *ConditionChan) Add(cond Condition) *ConditionChan {
  17. *v = append(*v, cond)
  18. return v
  19. }
  20. func (v *ConditionChan) Apply(ctx *Context) bool {
  21. for _, cond := range *v {
  22. if !cond.Apply(ctx) {
  23. return false
  24. }
  25. }
  26. return true
  27. }
  28. func (v *ConditionChan) Len() int {
  29. return len(*v)
  30. }
  31. var matcherTypeMap = map[Domain_Type]strmatcher.Type{
  32. Domain_Plain: strmatcher.Substr,
  33. Domain_Regex: strmatcher.Regex,
  34. Domain_Domain: strmatcher.Domain,
  35. Domain_Full: strmatcher.Full,
  36. }
  37. func domainToMatcher(domain *Domain) (strmatcher.Matcher, error) {
  38. matcherType, f := matcherTypeMap[domain.Type]
  39. if !f {
  40. return nil, newError("unsupported domain type", domain.Type)
  41. }
  42. matcher, err := matcherType.New(domain.Value)
  43. if err != nil {
  44. return nil, newError("failed to create domain matcher").Base(err)
  45. }
  46. return matcher, nil
  47. }
  48. type DomainMatcher struct {
  49. matchers strmatcher.IndexMatcher
  50. }
  51. func NewDomainMatcher(domains []*Domain) (*DomainMatcher, error) {
  52. g := new(strmatcher.MatcherGroup)
  53. for _, d := range domains {
  54. m, err := domainToMatcher(d)
  55. if err != nil {
  56. return nil, err
  57. }
  58. g.Add(m)
  59. }
  60. return &DomainMatcher{
  61. matchers: g,
  62. }, nil
  63. }
  64. func (m *DomainMatcher) ApplyDomain(domain string) bool {
  65. return m.matchers.Match(domain) > 0
  66. }
  67. func (m *DomainMatcher) Apply(ctx *Context) bool {
  68. if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
  69. return false
  70. }
  71. dest := ctx.Outbound.Target
  72. if !dest.Address.Family().IsDomain() {
  73. return false
  74. }
  75. return m.ApplyDomain(dest.Address.Domain())
  76. }
  77. func getIPsFromSource(ctx *Context) []net.IP {
  78. if ctx.Inbound == nil || !ctx.Inbound.Source.IsValid() {
  79. return nil
  80. }
  81. dest := ctx.Inbound.Source
  82. if dest.Address.Family().IsDomain() {
  83. return nil
  84. }
  85. return []net.IP{dest.Address.IP()}
  86. }
  87. func getIPsFromTarget(ctx *Context) []net.IP {
  88. return ctx.GetTargetIPs()
  89. }
  90. type MultiGeoIPMatcher struct {
  91. matchers []*GeoIPMatcher
  92. ipFunc func(*Context) []net.IP
  93. }
  94. func NewMultiGeoIPMatcher(geoips []*GeoIP, onSource bool) (*MultiGeoIPMatcher, error) {
  95. var matchers []*GeoIPMatcher
  96. for _, geoip := range geoips {
  97. matcher, err := globalGeoIPContainer.Add(geoip)
  98. if err != nil {
  99. return nil, err
  100. }
  101. matchers = append(matchers, matcher)
  102. }
  103. matcher := &MultiGeoIPMatcher{
  104. matchers: matchers,
  105. }
  106. if onSource {
  107. matcher.ipFunc = getIPsFromSource
  108. } else {
  109. matcher.ipFunc = getIPsFromTarget
  110. }
  111. return matcher, nil
  112. }
  113. func (m *MultiGeoIPMatcher) Apply(ctx *Context) bool {
  114. ips := m.ipFunc(ctx)
  115. for _, ip := range ips {
  116. for _, matcher := range m.matchers {
  117. if matcher.Match(ip) {
  118. return true
  119. }
  120. }
  121. }
  122. return false
  123. }
  124. type PortMatcher struct {
  125. port net.MemoryPortList
  126. }
  127. func NewPortMatcher(list *net.PortList) *PortMatcher {
  128. return &PortMatcher{
  129. port: net.PortListFromProto(list),
  130. }
  131. }
  132. func (v *PortMatcher) Apply(ctx *Context) bool {
  133. if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
  134. return false
  135. }
  136. return v.port.Contains(ctx.Outbound.Target.Port)
  137. }
  138. type NetworkMatcher struct {
  139. list [8]bool
  140. }
  141. func NewNetworkMatcher(network []net.Network) NetworkMatcher {
  142. var matcher NetworkMatcher
  143. for _, n := range network {
  144. matcher.list[int(n)] = true
  145. }
  146. return matcher
  147. }
  148. func (v NetworkMatcher) Apply(ctx *Context) bool {
  149. if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
  150. return false
  151. }
  152. return v.list[int(ctx.Outbound.Target.Network)]
  153. }
  154. type UserMatcher struct {
  155. user []string
  156. }
  157. func NewUserMatcher(users []string) *UserMatcher {
  158. usersCopy := make([]string, 0, len(users))
  159. for _, user := range users {
  160. if len(user) > 0 {
  161. usersCopy = append(usersCopy, user)
  162. }
  163. }
  164. return &UserMatcher{
  165. user: usersCopy,
  166. }
  167. }
  168. func (v *UserMatcher) Apply(ctx *Context) bool {
  169. if ctx.Inbound == nil {
  170. return false
  171. }
  172. user := ctx.Inbound.User
  173. if user == nil {
  174. return false
  175. }
  176. for _, u := range v.user {
  177. if u == user.Email {
  178. return true
  179. }
  180. }
  181. return false
  182. }
  183. type InboundTagMatcher struct {
  184. tags []string
  185. }
  186. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  187. tagsCopy := make([]string, 0, len(tags))
  188. for _, tag := range tags {
  189. if len(tag) > 0 {
  190. tagsCopy = append(tagsCopy, tag)
  191. }
  192. }
  193. return &InboundTagMatcher{
  194. tags: tagsCopy,
  195. }
  196. }
  197. func (v *InboundTagMatcher) Apply(ctx *Context) bool {
  198. if ctx.Inbound == nil || len(ctx.Inbound.Tag) == 0 {
  199. return false
  200. }
  201. tag := ctx.Inbound.Tag
  202. for _, t := range v.tags {
  203. if t == tag {
  204. return true
  205. }
  206. }
  207. return false
  208. }
  209. type ProtocolMatcher struct {
  210. protocols []string
  211. }
  212. func NewProtocolMatcher(protocols []string) *ProtocolMatcher {
  213. pCopy := make([]string, 0, len(protocols))
  214. for _, p := range protocols {
  215. if len(p) > 0 {
  216. pCopy = append(pCopy, p)
  217. }
  218. }
  219. return &ProtocolMatcher{
  220. protocols: pCopy,
  221. }
  222. }
  223. func (m *ProtocolMatcher) Apply(ctx *Context) bool {
  224. if ctx.Content == nil {
  225. return false
  226. }
  227. protocol := ctx.Content.Protocol
  228. for _, p := range m.protocols {
  229. if strings.HasPrefix(protocol, p) {
  230. return true
  231. }
  232. }
  233. return false
  234. }