condition.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. onSource bool
  129. }
  130. // NewPortMatcher create a new port matcher that can match source or destination port
  131. func NewPortMatcher(list *net.PortList, onSource bool) *PortMatcher {
  132. return &PortMatcher{
  133. port: net.PortListFromProto(list),
  134. onSource: onSource,
  135. }
  136. }
  137. func (v *PortMatcher) Apply(ctx *Context) bool {
  138. var port net.Port
  139. if v.onSource {
  140. if ctx.Inbound == nil || !ctx.Inbound.Source.IsValid() {
  141. return false
  142. }
  143. port = ctx.Inbound.Source.Port
  144. } else {
  145. if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
  146. return false
  147. }
  148. port = ctx.Outbound.Target.Port
  149. }
  150. return v.port.Contains(port)
  151. }
  152. type NetworkMatcher struct {
  153. list [8]bool
  154. }
  155. func NewNetworkMatcher(network []net.Network) NetworkMatcher {
  156. var matcher NetworkMatcher
  157. for _, n := range network {
  158. matcher.list[int(n)] = true
  159. }
  160. return matcher
  161. }
  162. func (v NetworkMatcher) Apply(ctx *Context) bool {
  163. if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
  164. return false
  165. }
  166. return v.list[int(ctx.Outbound.Target.Network)]
  167. }
  168. type UserMatcher struct {
  169. user []string
  170. }
  171. func NewUserMatcher(users []string) *UserMatcher {
  172. usersCopy := make([]string, 0, len(users))
  173. for _, user := range users {
  174. if len(user) > 0 {
  175. usersCopy = append(usersCopy, user)
  176. }
  177. }
  178. return &UserMatcher{
  179. user: usersCopy,
  180. }
  181. }
  182. func (v *UserMatcher) Apply(ctx *Context) bool {
  183. if ctx.Inbound == nil {
  184. return false
  185. }
  186. user := ctx.Inbound.User
  187. if user == nil {
  188. return false
  189. }
  190. for _, u := range v.user {
  191. if u == user.Email {
  192. return true
  193. }
  194. }
  195. return false
  196. }
  197. type InboundTagMatcher struct {
  198. tags []string
  199. }
  200. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  201. tagsCopy := make([]string, 0, len(tags))
  202. for _, tag := range tags {
  203. if len(tag) > 0 {
  204. tagsCopy = append(tagsCopy, tag)
  205. }
  206. }
  207. return &InboundTagMatcher{
  208. tags: tagsCopy,
  209. }
  210. }
  211. func (v *InboundTagMatcher) Apply(ctx *Context) bool {
  212. if ctx.Inbound == nil || len(ctx.Inbound.Tag) == 0 {
  213. return false
  214. }
  215. tag := ctx.Inbound.Tag
  216. for _, t := range v.tags {
  217. if t == tag {
  218. return true
  219. }
  220. }
  221. return false
  222. }
  223. type ProtocolMatcher struct {
  224. protocols []string
  225. }
  226. func NewProtocolMatcher(protocols []string) *ProtocolMatcher {
  227. pCopy := make([]string, 0, len(protocols))
  228. for _, p := range protocols {
  229. if len(p) > 0 {
  230. pCopy = append(pCopy, p)
  231. }
  232. }
  233. return &ProtocolMatcher{
  234. protocols: pCopy,
  235. }
  236. }
  237. func (m *ProtocolMatcher) Apply(ctx *Context) bool {
  238. if ctx.Content == nil {
  239. return false
  240. }
  241. protocol := ctx.Content.Protocol
  242. for _, p := range m.protocols {
  243. if strings.HasPrefix(protocol, p) {
  244. return true
  245. }
  246. }
  247. return false
  248. }
  249. type AttributeMatcher struct {
  250. program *starlark.Program
  251. }
  252. func NewAttributeMatcher(code string) (*AttributeMatcher, error) {
  253. starFile, err := syntax.Parse("attr.star", "satisfied=("+code+")", 0)
  254. if err != nil {
  255. return nil, newError("attr rule").Base(err)
  256. }
  257. p, err := starlark.FileProgram(starFile, func(name string) bool {
  258. return name == "attrs"
  259. })
  260. if err != nil {
  261. return nil, err
  262. }
  263. return &AttributeMatcher{
  264. program: p,
  265. }, nil
  266. }
  267. func (m *AttributeMatcher) Match(attrs map[string]interface{}) bool {
  268. attrsDict := new(starlark.Dict)
  269. for key, value := range attrs {
  270. var starValue starlark.Value
  271. switch value := value.(type) {
  272. case string:
  273. starValue = starlark.String(value)
  274. }
  275. if starValue != nil {
  276. attrsDict.SetKey(starlark.String(key), starValue)
  277. }
  278. }
  279. predefined := make(starlark.StringDict)
  280. predefined["attrs"] = attrsDict
  281. thread := &starlark.Thread{
  282. Name: "matcher",
  283. }
  284. results, err := m.program.Init(thread, predefined)
  285. if err != nil {
  286. newError("attr matcher").Base(err).WriteToLog()
  287. }
  288. satisfied := results["satisfied"]
  289. return satisfied != nil && bool(satisfied.Truth())
  290. }
  291. func (m *AttributeMatcher) Apply(ctx *Context) bool {
  292. if ctx.Content == nil {
  293. return false
  294. }
  295. return m.Match(ctx.Content.Attributes)
  296. }