condition.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package router
  2. import (
  3. "context"
  4. "strings"
  5. "v2ray.com/core/app/dispatcher"
  6. "v2ray.com/core/common/net"
  7. "v2ray.com/core/common/session"
  8. "v2ray.com/core/common/strmatcher"
  9. )
  10. type Condition interface {
  11. Apply(ctx context.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.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. type AnyCondition []Condition
  34. func NewAnyCondition() *AnyCondition {
  35. var anyCond AnyCondition = make([]Condition, 0, 8)
  36. return &anyCond
  37. }
  38. func (v *AnyCondition) Add(cond Condition) *AnyCondition {
  39. *v = append(*v, cond)
  40. return v
  41. }
  42. func (v *AnyCondition) Apply(ctx context.Context) bool {
  43. for _, cond := range *v {
  44. if cond.Apply(ctx) {
  45. return true
  46. }
  47. }
  48. return false
  49. }
  50. func (v *AnyCondition) Len() int {
  51. return len(*v)
  52. }
  53. var matcherTypeMap = map[Domain_Type]strmatcher.Type{
  54. Domain_Plain: strmatcher.Substr,
  55. Domain_Regex: strmatcher.Regex,
  56. Domain_Domain: strmatcher.Domain,
  57. Domain_Full: strmatcher.Full,
  58. }
  59. func domainToMatcher(domain *Domain) (strmatcher.Matcher, error) {
  60. matcherType, f := matcherTypeMap[domain.Type]
  61. if !f {
  62. return nil, newError("unsupported domain type", domain.Type)
  63. }
  64. matcher, err := matcherType.New(domain.Value)
  65. if err != nil {
  66. return nil, newError("failed to create domain matcher").Base(err)
  67. }
  68. return matcher, nil
  69. }
  70. type DomainMatcher struct {
  71. matchers strmatcher.IndexMatcher
  72. }
  73. func NewDomainMatcher(domains []*Domain) (*DomainMatcher, error) {
  74. g := new(strmatcher.MatcherGroup)
  75. for _, d := range domains {
  76. m, err := domainToMatcher(d)
  77. if err != nil {
  78. return nil, err
  79. }
  80. g.Add(m)
  81. }
  82. return &DomainMatcher{
  83. matchers: g,
  84. }, nil
  85. }
  86. func (m *DomainMatcher) ApplyDomain(domain string) bool {
  87. return m.matchers.Match(domain) > 0
  88. }
  89. func (m *DomainMatcher) Apply(ctx context.Context) bool {
  90. outbound := session.OutboundFromContext(ctx)
  91. if outbound == nil || !outbound.Target.IsValid() {
  92. return false
  93. }
  94. dest := outbound.Target
  95. if !dest.Address.Family().IsDomain() {
  96. return false
  97. }
  98. return m.ApplyDomain(dest.Address.Domain())
  99. }
  100. func sourceFromContext(ctx context.Context) net.Destination {
  101. inbound := session.InboundFromContext(ctx)
  102. if inbound == nil {
  103. return net.Destination{}
  104. }
  105. return inbound.Source
  106. }
  107. func targetFromContent(ctx context.Context) net.Destination {
  108. outbound := session.OutboundFromContext(ctx)
  109. if outbound == nil {
  110. return net.Destination{}
  111. }
  112. return outbound.Target
  113. }
  114. type MultiGeoIPMatcher struct {
  115. matchers []*GeoIPMatcher
  116. onSource bool
  117. }
  118. func NewMultiGeoIPMatcher(geoips []*GeoIP, onSource bool) (*MultiGeoIPMatcher, error) {
  119. var matchers []*GeoIPMatcher
  120. for _, geoip := range geoips {
  121. matcher, err := globalGeoIPContainer.Add(geoip)
  122. if err != nil {
  123. return nil, err
  124. }
  125. matchers = append(matchers, matcher)
  126. }
  127. return &MultiGeoIPMatcher{
  128. matchers: matchers,
  129. onSource: onSource,
  130. }, nil
  131. }
  132. func (m *MultiGeoIPMatcher) Apply(ctx context.Context) bool {
  133. ips := make([]net.IP, 0, 4)
  134. if resolver, ok := ResolvedIPsFromContext(ctx); ok {
  135. resolvedIPs := resolver.Resolve()
  136. for _, rip := range resolvedIPs {
  137. ips = append(ips, rip.IP())
  138. }
  139. }
  140. var dest net.Destination
  141. if m.onSource {
  142. dest = sourceFromContext(ctx)
  143. } else {
  144. dest = targetFromContent(ctx)
  145. }
  146. if dest.IsValid() && (dest.Address.Family().IsIPv4() || dest.Address.Family().IsIPv6()) {
  147. ips = append(ips, dest.Address.IP())
  148. }
  149. for _, ip := range ips {
  150. for _, matcher := range m.matchers {
  151. if matcher.Match(ip) {
  152. return true
  153. }
  154. }
  155. }
  156. return false
  157. }
  158. type PortMatcher struct {
  159. port net.PortRange
  160. }
  161. func NewPortMatcher(portRange net.PortRange) *PortMatcher {
  162. return &PortMatcher{
  163. port: portRange,
  164. }
  165. }
  166. func (v *PortMatcher) Apply(ctx context.Context) bool {
  167. outbound := session.OutboundFromContext(ctx)
  168. if outbound == nil || !outbound.Target.IsValid() {
  169. return false
  170. }
  171. return v.port.Contains(outbound.Target.Port)
  172. }
  173. type NetworkMatcher struct {
  174. network *net.NetworkList
  175. }
  176. func NewNetworkMatcher(network *net.NetworkList) *NetworkMatcher {
  177. return &NetworkMatcher{
  178. network: network,
  179. }
  180. }
  181. func (v *NetworkMatcher) Apply(ctx context.Context) bool {
  182. outbound := session.OutboundFromContext(ctx)
  183. if outbound == nil || !outbound.Target.IsValid() {
  184. return false
  185. }
  186. return v.network.HasNetwork(outbound.Target.Network)
  187. }
  188. type UserMatcher struct {
  189. user []string
  190. }
  191. func NewUserMatcher(users []string) *UserMatcher {
  192. usersCopy := make([]string, 0, len(users))
  193. for _, user := range users {
  194. if len(user) > 0 {
  195. usersCopy = append(usersCopy, user)
  196. }
  197. }
  198. return &UserMatcher{
  199. user: usersCopy,
  200. }
  201. }
  202. func (v *UserMatcher) Apply(ctx context.Context) bool {
  203. inbound := session.InboundFromContext(ctx)
  204. if inbound == nil {
  205. return false
  206. }
  207. user := inbound.User
  208. if user == nil {
  209. return false
  210. }
  211. for _, u := range v.user {
  212. if u == user.Email {
  213. return true
  214. }
  215. }
  216. return false
  217. }
  218. type InboundTagMatcher struct {
  219. tags []string
  220. }
  221. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  222. tagsCopy := make([]string, 0, len(tags))
  223. for _, tag := range tags {
  224. if len(tag) > 0 {
  225. tagsCopy = append(tagsCopy, tag)
  226. }
  227. }
  228. return &InboundTagMatcher{
  229. tags: tagsCopy,
  230. }
  231. }
  232. func (v *InboundTagMatcher) Apply(ctx context.Context) bool {
  233. inbound := session.InboundFromContext(ctx)
  234. if inbound == nil || len(inbound.Tag) == 0 {
  235. return false
  236. }
  237. tag := inbound.Tag
  238. for _, t := range v.tags {
  239. if t == tag {
  240. return true
  241. }
  242. }
  243. return false
  244. }
  245. type ProtocolMatcher struct {
  246. protocols []string
  247. }
  248. func NewProtocolMatcher(protocols []string) *ProtocolMatcher {
  249. pCopy := make([]string, 0, len(protocols))
  250. for _, p := range protocols {
  251. if len(p) > 0 {
  252. pCopy = append(pCopy, p)
  253. }
  254. }
  255. return &ProtocolMatcher{
  256. protocols: pCopy,
  257. }
  258. }
  259. func (m *ProtocolMatcher) Apply(ctx context.Context) bool {
  260. result := dispatcher.SniffingResultFromContext(ctx)
  261. if result == nil {
  262. return false
  263. }
  264. protocol := result.Protocol()
  265. for _, p := range m.protocols {
  266. if strings.HasPrefix(protocol, p) {
  267. return true
  268. }
  269. }
  270. return false
  271. }