condition.go 6.4 KB

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