condition.go 6.9 KB

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