condition.go 6.9 KB

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