condition.go 6.7 KB

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