1
0

condition.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package router
  2. import (
  3. "context"
  4. "regexp"
  5. "strings"
  6. "github.com/xtls/xray-core/common/errors"
  7. "github.com/xtls/xray-core/common/net"
  8. "github.com/xtls/xray-core/common/strmatcher"
  9. "github.com/xtls/xray-core/features/routing"
  10. )
  11. type Condition interface {
  12. Apply(ctx routing.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. // Apply applies all conditions registered in this chan.
  24. func (v *ConditionChan) Apply(ctx routing.Context) bool {
  25. for _, cond := range *v {
  26. if !cond.Apply(ctx) {
  27. return false
  28. }
  29. }
  30. return true
  31. }
  32. func (v *ConditionChan) Len() int {
  33. return len(*v)
  34. }
  35. var matcherTypeMap = map[Domain_Type]strmatcher.Type{
  36. Domain_Plain: strmatcher.Substr,
  37. Domain_Regex: strmatcher.Regex,
  38. Domain_Domain: strmatcher.Domain,
  39. Domain_Full: strmatcher.Full,
  40. }
  41. type DomainMatcher struct {
  42. matchers strmatcher.IndexMatcher
  43. }
  44. func NewMphMatcherGroup(domains []*Domain) (*DomainMatcher, error) {
  45. g := strmatcher.NewMphMatcherGroup()
  46. for _, d := range domains {
  47. matcherType, f := matcherTypeMap[d.Type]
  48. if !f {
  49. errors.LogError(context.Background(), "ignore unsupported domain type ", d.Type, " of rule ", d.Value)
  50. continue
  51. }
  52. _, err := g.AddPattern(d.Value, matcherType)
  53. if err != nil {
  54. errors.LogErrorInner(context.Background(), err, "ignore domain rule ", d.Type, " ", d.Value)
  55. continue
  56. }
  57. }
  58. g.Build()
  59. return &DomainMatcher{
  60. matchers: g,
  61. }, nil
  62. }
  63. func (m *DomainMatcher) ApplyDomain(domain string) bool {
  64. return len(m.matchers.Match(strings.ToLower(domain))) > 0
  65. }
  66. // Apply implements Condition.
  67. func (m *DomainMatcher) Apply(ctx routing.Context) bool {
  68. domain := ctx.GetTargetDomain()
  69. if len(domain) == 0 {
  70. return false
  71. }
  72. return m.ApplyDomain(domain)
  73. }
  74. type MatcherAsType byte
  75. const (
  76. MatcherAsType_Local MatcherAsType = iota
  77. MatcherAsType_Source
  78. MatcherAsType_Target
  79. MatcherAsType_VlessRoute // for port
  80. )
  81. type IPMatcher struct {
  82. matcher GeoIPMatcher
  83. asType MatcherAsType
  84. }
  85. func NewIPMatcher(geoips []*GeoIP, asType MatcherAsType) (*IPMatcher, error) {
  86. matcher, err := BuildOptimizedGeoIPMatcher(geoips...)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return &IPMatcher{matcher: matcher, asType: asType}, nil
  91. }
  92. // Apply implements Condition.
  93. func (m *IPMatcher) Apply(ctx routing.Context) bool {
  94. var ips []net.IP
  95. switch m.asType {
  96. case MatcherAsType_Local:
  97. ips = ctx.GetLocalIPs()
  98. case MatcherAsType_Source:
  99. ips = ctx.GetSourceIPs()
  100. case MatcherAsType_Target:
  101. ips = ctx.GetTargetIPs()
  102. default:
  103. panic("unk asType")
  104. }
  105. return m.matcher.AnyMatch(ips)
  106. }
  107. type PortMatcher struct {
  108. port net.MemoryPortList
  109. asType MatcherAsType
  110. }
  111. // NewPortMatcher create a new port matcher that can match source or local or destination port
  112. func NewPortMatcher(list *net.PortList, asType MatcherAsType) *PortMatcher {
  113. return &PortMatcher{
  114. port: net.PortListFromProto(list),
  115. asType: asType,
  116. }
  117. }
  118. // Apply implements Condition.
  119. func (v *PortMatcher) Apply(ctx routing.Context) bool {
  120. switch v.asType {
  121. case MatcherAsType_Local:
  122. return v.port.Contains(ctx.GetLocalPort())
  123. case MatcherAsType_Source:
  124. return v.port.Contains(ctx.GetSourcePort())
  125. case MatcherAsType_Target:
  126. return v.port.Contains(ctx.GetTargetPort())
  127. case MatcherAsType_VlessRoute:
  128. return v.port.Contains(ctx.GetVlessRoute())
  129. default:
  130. panic("unk asType")
  131. }
  132. }
  133. type NetworkMatcher struct {
  134. list [8]bool
  135. }
  136. func NewNetworkMatcher(network []net.Network) NetworkMatcher {
  137. var matcher NetworkMatcher
  138. for _, n := range network {
  139. matcher.list[int(n)] = true
  140. }
  141. return matcher
  142. }
  143. // Apply implements Condition.
  144. func (v NetworkMatcher) Apply(ctx routing.Context) bool {
  145. return v.list[int(ctx.GetNetwork())]
  146. }
  147. type UserMatcher struct {
  148. user []string
  149. pattern []*regexp.Regexp
  150. }
  151. func NewUserMatcher(users []string) *UserMatcher {
  152. usersCopy := make([]string, 0, len(users))
  153. patternsCopy := make([]*regexp.Regexp, 0, len(users))
  154. for _, user := range users {
  155. if len(user) > 0 {
  156. if len(user) > 7 && strings.HasPrefix(user, "regexp:") {
  157. if re, err := regexp.Compile(user[7:]); err == nil {
  158. patternsCopy = append(patternsCopy, re)
  159. }
  160. // Items of users slice with an invalid regexp syntax are ignored.
  161. continue
  162. }
  163. usersCopy = append(usersCopy, user)
  164. }
  165. }
  166. return &UserMatcher{
  167. user: usersCopy,
  168. pattern: patternsCopy,
  169. }
  170. }
  171. // Apply implements Condition.
  172. func (v *UserMatcher) Apply(ctx routing.Context) bool {
  173. user := ctx.GetUser()
  174. if len(user) == 0 {
  175. return false
  176. }
  177. for _, u := range v.user {
  178. if u == user {
  179. return true
  180. }
  181. }
  182. for _, re := range v.pattern {
  183. if re.MatchString(user) {
  184. return true
  185. }
  186. }
  187. return false
  188. }
  189. type InboundTagMatcher struct {
  190. tags []string
  191. }
  192. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  193. tagsCopy := make([]string, 0, len(tags))
  194. for _, tag := range tags {
  195. if len(tag) > 0 {
  196. tagsCopy = append(tagsCopy, tag)
  197. }
  198. }
  199. return &InboundTagMatcher{
  200. tags: tagsCopy,
  201. }
  202. }
  203. // Apply implements Condition.
  204. func (v *InboundTagMatcher) Apply(ctx routing.Context) bool {
  205. tag := ctx.GetInboundTag()
  206. if len(tag) == 0 {
  207. return false
  208. }
  209. for _, t := range v.tags {
  210. if t == tag {
  211. return true
  212. }
  213. }
  214. return false
  215. }
  216. type ProtocolMatcher struct {
  217. protocols []string
  218. }
  219. func NewProtocolMatcher(protocols []string) *ProtocolMatcher {
  220. pCopy := make([]string, 0, len(protocols))
  221. for _, p := range protocols {
  222. if len(p) > 0 {
  223. pCopy = append(pCopy, p)
  224. }
  225. }
  226. return &ProtocolMatcher{
  227. protocols: pCopy,
  228. }
  229. }
  230. // Apply implements Condition.
  231. func (m *ProtocolMatcher) Apply(ctx routing.Context) bool {
  232. protocol := ctx.GetProtocol()
  233. if len(protocol) == 0 {
  234. return false
  235. }
  236. for _, p := range m.protocols {
  237. if strings.HasPrefix(protocol, p) {
  238. return true
  239. }
  240. }
  241. return false
  242. }
  243. type AttributeMatcher struct {
  244. configuredKeys map[string]*regexp.Regexp
  245. }
  246. // Match implements attributes matching.
  247. func (m *AttributeMatcher) Match(attrs map[string]string) bool {
  248. // header keys are case insensitive most likely. So we do a convert
  249. httpHeaders := make(map[string]string)
  250. for key, value := range attrs {
  251. httpHeaders[strings.ToLower(key)] = value
  252. }
  253. for key, regex := range m.configuredKeys {
  254. if a, ok := httpHeaders[key]; !ok || !regex.MatchString(a) {
  255. return false
  256. }
  257. }
  258. return true
  259. }
  260. // Apply implements Condition.
  261. func (m *AttributeMatcher) Apply(ctx routing.Context) bool {
  262. attributes := ctx.GetAttributes()
  263. if attributes == nil {
  264. return false
  265. }
  266. return m.Match(attributes)
  267. }