condition.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. return &DomainMatcher{
  84. matchers: g,
  85. }, nil
  86. }
  87. func (m *DomainMatcher) ApplyDomain(domain string) bool {
  88. return m.matchers.Match(domain) > 0
  89. }
  90. func (m *DomainMatcher) Apply(ctx context.Context) bool {
  91. dest, ok := proxy.TargetFromContext(ctx)
  92. if !ok {
  93. return false
  94. }
  95. if !dest.Address.Family().IsDomain() {
  96. return false
  97. }
  98. return m.ApplyDomain(dest.Address.Domain())
  99. }
  100. type CIDRMatcher struct {
  101. cidr *net.IPNet
  102. onSource bool
  103. }
  104. func NewCIDRMatcher(ip []byte, mask uint32, onSource bool) (*CIDRMatcher, error) {
  105. cidr := &net.IPNet{
  106. IP: net.IP(ip),
  107. Mask: net.CIDRMask(int(mask), len(ip)*8),
  108. }
  109. return &CIDRMatcher{
  110. cidr: cidr,
  111. onSource: onSource,
  112. }, nil
  113. }
  114. func (v *CIDRMatcher) Apply(ctx context.Context) bool {
  115. ips := make([]net.IP, 0, 4)
  116. if resolver, ok := proxy.ResolvedIPsFromContext(ctx); ok {
  117. resolvedIPs := resolver.Resolve()
  118. for _, rip := range resolvedIPs {
  119. if !rip.Family().IsIPv6() {
  120. continue
  121. }
  122. ips = append(ips, rip.IP())
  123. }
  124. }
  125. var dest net.Destination
  126. var ok bool
  127. if v.onSource {
  128. dest, ok = proxy.SourceFromContext(ctx)
  129. } else {
  130. dest, ok = proxy.TargetFromContext(ctx)
  131. }
  132. if ok && dest.Address.Family().IsIPv6() {
  133. ips = append(ips, dest.Address.IP())
  134. }
  135. for _, ip := range ips {
  136. if v.cidr.Contains(ip) {
  137. return true
  138. }
  139. }
  140. return false
  141. }
  142. type IPv4Matcher struct {
  143. ipv4net *net.IPNetTable
  144. onSource bool
  145. }
  146. func NewIPv4Matcher(ipnet *net.IPNetTable, onSource bool) *IPv4Matcher {
  147. return &IPv4Matcher{
  148. ipv4net: ipnet,
  149. onSource: onSource,
  150. }
  151. }
  152. func (v *IPv4Matcher) Apply(ctx context.Context) bool {
  153. ips := make([]net.IP, 0, 4)
  154. if resolver, ok := proxy.ResolvedIPsFromContext(ctx); ok {
  155. resolvedIPs := resolver.Resolve()
  156. for _, rip := range resolvedIPs {
  157. if !rip.Family().IsIPv4() {
  158. continue
  159. }
  160. ips = append(ips, rip.IP())
  161. }
  162. }
  163. var dest net.Destination
  164. var ok bool
  165. if v.onSource {
  166. dest, ok = proxy.SourceFromContext(ctx)
  167. } else {
  168. dest, ok = proxy.TargetFromContext(ctx)
  169. }
  170. if ok && dest.Address.Family().IsIPv4() {
  171. ips = append(ips, dest.Address.IP())
  172. }
  173. for _, ip := range ips {
  174. if v.ipv4net.Contains(ip) {
  175. return true
  176. }
  177. }
  178. return false
  179. }
  180. type PortMatcher struct {
  181. port net.PortRange
  182. }
  183. func NewPortMatcher(portRange net.PortRange) *PortMatcher {
  184. return &PortMatcher{
  185. port: portRange,
  186. }
  187. }
  188. func (v *PortMatcher) Apply(ctx context.Context) bool {
  189. dest, ok := proxy.TargetFromContext(ctx)
  190. if !ok {
  191. return false
  192. }
  193. return v.port.Contains(dest.Port)
  194. }
  195. type NetworkMatcher struct {
  196. network *net.NetworkList
  197. }
  198. func NewNetworkMatcher(network *net.NetworkList) *NetworkMatcher {
  199. return &NetworkMatcher{
  200. network: network,
  201. }
  202. }
  203. func (v *NetworkMatcher) Apply(ctx context.Context) bool {
  204. dest, ok := proxy.TargetFromContext(ctx)
  205. if !ok {
  206. return false
  207. }
  208. return v.network.HasNetwork(dest.Network)
  209. }
  210. type UserMatcher struct {
  211. user []string
  212. }
  213. func NewUserMatcher(users []string) *UserMatcher {
  214. usersCopy := make([]string, 0, len(users))
  215. for _, user := range users {
  216. if len(user) > 0 {
  217. usersCopy = append(usersCopy, user)
  218. }
  219. }
  220. return &UserMatcher{
  221. user: usersCopy,
  222. }
  223. }
  224. func (v *UserMatcher) Apply(ctx context.Context) bool {
  225. user := protocol.UserFromContext(ctx)
  226. if user == nil {
  227. return false
  228. }
  229. for _, u := range v.user {
  230. if u == user.Email {
  231. return true
  232. }
  233. }
  234. return false
  235. }
  236. type InboundTagMatcher struct {
  237. tags []string
  238. }
  239. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  240. tagsCopy := make([]string, 0, len(tags))
  241. for _, tag := range tags {
  242. if len(tag) > 0 {
  243. tagsCopy = append(tagsCopy, tag)
  244. }
  245. }
  246. return &InboundTagMatcher{
  247. tags: tagsCopy,
  248. }
  249. }
  250. func (v *InboundTagMatcher) Apply(ctx context.Context) bool {
  251. tag, ok := proxy.InboundTagFromContext(ctx)
  252. if !ok {
  253. return false
  254. }
  255. for _, t := range v.tags {
  256. if t == tag {
  257. return true
  258. }
  259. }
  260. return false
  261. }
  262. type ProtocolMatcher struct {
  263. protocols []string
  264. }
  265. func NewProtocolMatcher(protocols []string) *ProtocolMatcher {
  266. pCopy := make([]string, 0, len(protocols))
  267. for _, p := range protocols {
  268. if len(p) > 0 {
  269. pCopy = append(pCopy, p)
  270. }
  271. }
  272. return &ProtocolMatcher{
  273. protocols: pCopy,
  274. }
  275. }
  276. func (m *ProtocolMatcher) Apply(ctx context.Context) bool {
  277. result := dispatcher.SniffingResultFromContext(ctx)
  278. if result == nil {
  279. return false
  280. }
  281. protocol := result.Protocol()
  282. for _, p := range m.protocols {
  283. if strings.HasPrefix(protocol, p) {
  284. return true
  285. }
  286. }
  287. return false
  288. }