condition.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // +build !confonly
  2. package router
  3. import (
  4. "strings"
  5. "go.starlark.net/starlark"
  6. "go.starlark.net/syntax"
  7. "github.com/v2fly/v2ray-core/v4/common/net"
  8. "github.com/v2fly/v2ray-core/v4/common/strmatcher"
  9. "github.com/v2fly/v2ray-core/v4/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. func domainToMatcher(domain *Domain) (strmatcher.Matcher, error) {
  42. matcherType, f := matcherTypeMap[domain.Type]
  43. if !f {
  44. return nil, newError("unsupported domain type", domain.Type)
  45. }
  46. matcher, err := matcherType.New(domain.Value)
  47. if err != nil {
  48. return nil, newError("failed to create domain matcher").Base(err)
  49. }
  50. return matcher, nil
  51. }
  52. type DomainMatcher struct {
  53. matchers strmatcher.IndexMatcher
  54. }
  55. func NewACAutomatonDomainMatcher(domains []*Domain) (*DomainMatcher, error) {
  56. g := strmatcher.NewACAutomatonMatcherGroup()
  57. for _, d := range domains {
  58. matcherType, f := matcherTypeMap[d.Type]
  59. if !f {
  60. return nil, newError("unsupported domain type", d.Type)
  61. }
  62. _, err := g.AddPattern(d.Value, matcherType)
  63. if err != nil {
  64. return nil, err
  65. }
  66. }
  67. g.Build()
  68. return &DomainMatcher{
  69. matchers: g,
  70. }, nil
  71. }
  72. func NewDomainMatcher(domains []*Domain) (*DomainMatcher, error) {
  73. g := new(strmatcher.MatcherGroup)
  74. for _, d := range domains {
  75. m, err := domainToMatcher(d)
  76. if err != nil {
  77. return nil, err
  78. }
  79. g.Add(m)
  80. }
  81. return &DomainMatcher{
  82. matchers: g,
  83. }, nil
  84. }
  85. func (m *DomainMatcher) ApplyDomain(domain string) bool {
  86. return len(m.matchers.Match(domain)) > 0
  87. }
  88. // Apply implements Condition.
  89. func (m *DomainMatcher) Apply(ctx routing.Context) bool {
  90. domain := ctx.GetTargetDomain()
  91. if len(domain) == 0 {
  92. return false
  93. }
  94. return m.ApplyDomain(domain)
  95. }
  96. type MultiGeoIPMatcher struct {
  97. matchers []*GeoIPMatcher
  98. onSource bool
  99. }
  100. func NewMultiGeoIPMatcher(geoips []*GeoIP, onSource bool) (*MultiGeoIPMatcher, error) {
  101. var matchers []*GeoIPMatcher
  102. for _, geoip := range geoips {
  103. matcher, err := globalGeoIPContainer.Add(geoip)
  104. if err != nil {
  105. return nil, err
  106. }
  107. matchers = append(matchers, matcher)
  108. }
  109. matcher := &MultiGeoIPMatcher{
  110. matchers: matchers,
  111. onSource: onSource,
  112. }
  113. return matcher, nil
  114. }
  115. // Apply implements Condition.
  116. func (m *MultiGeoIPMatcher) Apply(ctx routing.Context) bool {
  117. var ips []net.IP
  118. if m.onSource {
  119. ips = ctx.GetSourceIPs()
  120. } else {
  121. ips = ctx.GetTargetIPs()
  122. }
  123. for _, ip := range ips {
  124. for _, matcher := range m.matchers {
  125. if matcher.Match(ip) {
  126. return true
  127. }
  128. }
  129. }
  130. return false
  131. }
  132. type PortMatcher struct {
  133. port net.MemoryPortList
  134. onSource bool
  135. }
  136. // NewPortMatcher create a new port matcher that can match source or destination port
  137. func NewPortMatcher(list *net.PortList, onSource bool) *PortMatcher {
  138. return &PortMatcher{
  139. port: net.PortListFromProto(list),
  140. onSource: onSource,
  141. }
  142. }
  143. // Apply implements Condition.
  144. func (v *PortMatcher) Apply(ctx routing.Context) bool {
  145. if v.onSource {
  146. return v.port.Contains(ctx.GetSourcePort())
  147. }
  148. return v.port.Contains(ctx.GetTargetPort())
  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. }
  167. func NewUserMatcher(users []string) *UserMatcher {
  168. usersCopy := make([]string, 0, len(users))
  169. for _, user := range users {
  170. if len(user) > 0 {
  171. usersCopy = append(usersCopy, user)
  172. }
  173. }
  174. return &UserMatcher{
  175. user: usersCopy,
  176. }
  177. }
  178. // Apply implements Condition.
  179. func (v *UserMatcher) Apply(ctx routing.Context) bool {
  180. user := ctx.GetUser()
  181. if len(user) == 0 {
  182. return false
  183. }
  184. for _, u := range v.user {
  185. if u == user {
  186. return true
  187. }
  188. }
  189. return false
  190. }
  191. type InboundTagMatcher struct {
  192. tags []string
  193. }
  194. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  195. tagsCopy := make([]string, 0, len(tags))
  196. for _, tag := range tags {
  197. if len(tag) > 0 {
  198. tagsCopy = append(tagsCopy, tag)
  199. }
  200. }
  201. return &InboundTagMatcher{
  202. tags: tagsCopy,
  203. }
  204. }
  205. // Apply implements Condition.
  206. func (v *InboundTagMatcher) Apply(ctx routing.Context) bool {
  207. tag := ctx.GetInboundTag()
  208. if len(tag) == 0 {
  209. return false
  210. }
  211. for _, t := range v.tags {
  212. if t == tag {
  213. return true
  214. }
  215. }
  216. return false
  217. }
  218. type ProtocolMatcher struct {
  219. protocols []string
  220. }
  221. func NewProtocolMatcher(protocols []string) *ProtocolMatcher {
  222. pCopy := make([]string, 0, len(protocols))
  223. for _, p := range protocols {
  224. if len(p) > 0 {
  225. pCopy = append(pCopy, p)
  226. }
  227. }
  228. return &ProtocolMatcher{
  229. protocols: pCopy,
  230. }
  231. }
  232. // Apply implements Condition.
  233. func (m *ProtocolMatcher) Apply(ctx routing.Context) bool {
  234. protocol := ctx.GetProtocol()
  235. if len(protocol) == 0 {
  236. return false
  237. }
  238. for _, p := range m.protocols {
  239. if strings.HasPrefix(protocol, p) {
  240. return true
  241. }
  242. }
  243. return false
  244. }
  245. type AttributeMatcher struct {
  246. program *starlark.Program
  247. }
  248. func NewAttributeMatcher(code string) (*AttributeMatcher, error) {
  249. starFile, err := syntax.Parse("attr.star", "satisfied=("+code+")", 0)
  250. if err != nil {
  251. return nil, newError("attr rule").Base(err)
  252. }
  253. p, err := starlark.FileProgram(starFile, func(name string) bool {
  254. return name == "attrs"
  255. })
  256. if err != nil {
  257. return nil, err
  258. }
  259. return &AttributeMatcher{
  260. program: p,
  261. }, nil
  262. }
  263. // Match implements attributes matching.
  264. func (m *AttributeMatcher) Match(attrs map[string]string) bool {
  265. attrsDict := new(starlark.Dict)
  266. for key, value := range attrs {
  267. attrsDict.SetKey(starlark.String(key), starlark.String(value))
  268. }
  269. predefined := make(starlark.StringDict)
  270. predefined["attrs"] = attrsDict
  271. thread := &starlark.Thread{
  272. Name: "matcher",
  273. }
  274. results, err := m.program.Init(thread, predefined)
  275. if err != nil {
  276. newError("attr matcher").Base(err).WriteToLog()
  277. }
  278. satisfied := results["satisfied"]
  279. return satisfied != nil && bool(satisfied.Truth())
  280. }
  281. // Apply implements Condition.
  282. func (m *AttributeMatcher) Apply(ctx routing.Context) bool {
  283. attributes := ctx.GetAttributes()
  284. if attributes == nil {
  285. return false
  286. }
  287. return m.Match(attributes)
  288. }