condition.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // +build !confonly
  2. package router
  3. import (
  4. "strings"
  5. "go.starlark.net/starlark"
  6. "go.starlark.net/syntax"
  7. "v2ray.com/core/common/net"
  8. "v2ray.com/core/common/strmatcher"
  9. )
  10. type Condition interface {
  11. Apply(ctx *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) 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) bool {
  70. if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
  71. return false
  72. }
  73. dest := ctx.Outbound.Target
  74. if !dest.Address.Family().IsDomain() {
  75. return false
  76. }
  77. return m.ApplyDomain(dest.Address.Domain())
  78. }
  79. func getIPsFromSource(ctx *Context) []net.IP {
  80. if ctx.Inbound == nil || !ctx.Inbound.Source.IsValid() {
  81. return nil
  82. }
  83. dest := ctx.Inbound.Source
  84. if dest.Address.Family().IsDomain() {
  85. return nil
  86. }
  87. return []net.IP{dest.Address.IP()}
  88. }
  89. func getIPsFromTarget(ctx *Context) []net.IP {
  90. return ctx.GetTargetIPs()
  91. }
  92. type MultiGeoIPMatcher struct {
  93. matchers []*GeoIPMatcher
  94. ipFunc func(*Context) []net.IP
  95. }
  96. func NewMultiGeoIPMatcher(geoips []*GeoIP, onSource bool) (*MultiGeoIPMatcher, error) {
  97. var matchers []*GeoIPMatcher
  98. for _, geoip := range geoips {
  99. matcher, err := globalGeoIPContainer.Add(geoip)
  100. if err != nil {
  101. return nil, err
  102. }
  103. matchers = append(matchers, matcher)
  104. }
  105. matcher := &MultiGeoIPMatcher{
  106. matchers: matchers,
  107. }
  108. if onSource {
  109. matcher.ipFunc = getIPsFromSource
  110. } else {
  111. matcher.ipFunc = getIPsFromTarget
  112. }
  113. return matcher, nil
  114. }
  115. func (m *MultiGeoIPMatcher) Apply(ctx *Context) bool {
  116. ips := m.ipFunc(ctx)
  117. for _, ip := range ips {
  118. for _, matcher := range m.matchers {
  119. if matcher.Match(ip) {
  120. return true
  121. }
  122. }
  123. }
  124. return false
  125. }
  126. type PortMatcher struct {
  127. port net.MemoryPortList
  128. }
  129. func NewPortMatcher(list *net.PortList) *PortMatcher {
  130. return &PortMatcher{
  131. port: net.PortListFromProto(list),
  132. }
  133. }
  134. func (v *PortMatcher) Apply(ctx *Context) bool {
  135. if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
  136. return false
  137. }
  138. return v.port.Contains(ctx.Outbound.Target.Port)
  139. }
  140. type NetworkMatcher struct {
  141. list [8]bool
  142. }
  143. func NewNetworkMatcher(network []net.Network) NetworkMatcher {
  144. var matcher NetworkMatcher
  145. for _, n := range network {
  146. matcher.list[int(n)] = true
  147. }
  148. return matcher
  149. }
  150. func (v NetworkMatcher) Apply(ctx *Context) bool {
  151. if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
  152. return false
  153. }
  154. return v.list[int(ctx.Outbound.Target.Network)]
  155. }
  156. type UserMatcher struct {
  157. user []string
  158. }
  159. func NewUserMatcher(users []string) *UserMatcher {
  160. usersCopy := make([]string, 0, len(users))
  161. for _, user := range users {
  162. if len(user) > 0 {
  163. usersCopy = append(usersCopy, user)
  164. }
  165. }
  166. return &UserMatcher{
  167. user: usersCopy,
  168. }
  169. }
  170. func (v *UserMatcher) Apply(ctx *Context) bool {
  171. if ctx.Inbound == nil {
  172. return false
  173. }
  174. user := ctx.Inbound.User
  175. if user == nil {
  176. return false
  177. }
  178. for _, u := range v.user {
  179. if u == user.Email {
  180. return true
  181. }
  182. }
  183. return false
  184. }
  185. type InboundTagMatcher struct {
  186. tags []string
  187. }
  188. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  189. tagsCopy := make([]string, 0, len(tags))
  190. for _, tag := range tags {
  191. if len(tag) > 0 {
  192. tagsCopy = append(tagsCopy, tag)
  193. }
  194. }
  195. return &InboundTagMatcher{
  196. tags: tagsCopy,
  197. }
  198. }
  199. func (v *InboundTagMatcher) Apply(ctx *Context) bool {
  200. if ctx.Inbound == nil || len(ctx.Inbound.Tag) == 0 {
  201. return false
  202. }
  203. tag := ctx.Inbound.Tag
  204. for _, t := range v.tags {
  205. if t == tag {
  206. return true
  207. }
  208. }
  209. return false
  210. }
  211. type ProtocolMatcher struct {
  212. protocols []string
  213. }
  214. func NewProtocolMatcher(protocols []string) *ProtocolMatcher {
  215. pCopy := make([]string, 0, len(protocols))
  216. for _, p := range protocols {
  217. if len(p) > 0 {
  218. pCopy = append(pCopy, p)
  219. }
  220. }
  221. return &ProtocolMatcher{
  222. protocols: pCopy,
  223. }
  224. }
  225. func (m *ProtocolMatcher) Apply(ctx *Context) bool {
  226. if ctx.Content == nil {
  227. return false
  228. }
  229. protocol := ctx.Content.Protocol
  230. for _, p := range m.protocols {
  231. if strings.HasPrefix(protocol, p) {
  232. return true
  233. }
  234. }
  235. return false
  236. }
  237. type AttributeMatcher struct {
  238. program *starlark.Program
  239. }
  240. func NewAttributeMatcher(code string) (*AttributeMatcher, error) {
  241. starFile, err := syntax.Parse("attr.star", "satisfied=("+code+")", 0)
  242. if err != nil {
  243. return nil, newError("attr rule").Base(err)
  244. }
  245. p, err := starlark.FileProgram(starFile, func(name string) bool {
  246. return name == "attrs"
  247. })
  248. if err != nil {
  249. return nil, err
  250. }
  251. return &AttributeMatcher{
  252. program: p,
  253. }, nil
  254. }
  255. func (m *AttributeMatcher) Match(attrs map[string]interface{}) bool {
  256. attrsDict := new(starlark.Dict)
  257. for key, value := range attrs {
  258. var starValue starlark.Value
  259. switch value := value.(type) {
  260. case string:
  261. starValue = starlark.String(value)
  262. }
  263. if starValue != nil {
  264. attrsDict.SetKey(starlark.String(key), starValue)
  265. }
  266. }
  267. predefined := make(starlark.StringDict)
  268. predefined["attrs"] = attrsDict
  269. thread := &starlark.Thread{
  270. Name: "matcher",
  271. }
  272. results, err := m.program.Init(thread, predefined)
  273. if err != nil {
  274. newError("attr matcher").Base(err).WriteToLog()
  275. }
  276. satisfied := results["satisfied"]
  277. return satisfied != nil && bool(satisfied.Truth())
  278. }
  279. func (m *AttributeMatcher) Apply(ctx *Context) bool {
  280. if ctx.Content == nil {
  281. return false
  282. }
  283. return m.Match(ctx.Content.Attributes)
  284. }