condition.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. 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 sourceFromContext(ctx context.Context) net.Destination {
  115. inbound := session.InboundFromContext(ctx)
  116. if inbound == nil {
  117. return net.Destination{}
  118. }
  119. return inbound.Source
  120. }
  121. func targetFromContent(ctx context.Context) net.Destination {
  122. outbound := session.OutboundFromContext(ctx)
  123. if outbound == nil {
  124. return net.Destination{}
  125. }
  126. return outbound.Target
  127. }
  128. func (v *CIDRMatcher) Apply(ctx context.Context) bool {
  129. ips := make([]net.IP, 0, 4)
  130. if resolver, ok := ResolvedIPsFromContext(ctx); ok {
  131. resolvedIPs := resolver.Resolve()
  132. for _, rip := range resolvedIPs {
  133. if !rip.Family().IsIPv6() {
  134. continue
  135. }
  136. ips = append(ips, rip.IP())
  137. }
  138. }
  139. var dest net.Destination
  140. if v.onSource {
  141. dest = sourceFromContext(ctx)
  142. } else {
  143. dest = targetFromContent(ctx)
  144. }
  145. if dest.IsValid() && dest.Address.Family().IsIPv6() {
  146. ips = append(ips, dest.Address.IP())
  147. }
  148. for _, ip := range ips {
  149. if v.cidr.Contains(ip) {
  150. return true
  151. }
  152. }
  153. return false
  154. }
  155. type IPv4Matcher struct {
  156. ipv4net *net.IPNetTable
  157. onSource bool
  158. }
  159. func NewIPv4Matcher(ipnet *net.IPNetTable, onSource bool) *IPv4Matcher {
  160. return &IPv4Matcher{
  161. ipv4net: ipnet,
  162. onSource: onSource,
  163. }
  164. }
  165. func (v *IPv4Matcher) Apply(ctx context.Context) bool {
  166. ips := make([]net.IP, 0, 4)
  167. if resolver, ok := ResolvedIPsFromContext(ctx); ok {
  168. resolvedIPs := resolver.Resolve()
  169. for _, rip := range resolvedIPs {
  170. if !rip.Family().IsIPv4() {
  171. continue
  172. }
  173. ips = append(ips, rip.IP())
  174. }
  175. }
  176. var dest net.Destination
  177. if v.onSource {
  178. dest = sourceFromContext(ctx)
  179. } else {
  180. dest = targetFromContent(ctx)
  181. }
  182. if dest.IsValid() && dest.Address.Family().IsIPv4() {
  183. ips = append(ips, dest.Address.IP())
  184. }
  185. for _, ip := range ips {
  186. if v.ipv4net.Contains(ip) {
  187. return true
  188. }
  189. }
  190. return false
  191. }
  192. type MultiGeoIPMatcher struct {
  193. matchers []*GeoIPMatcher
  194. onSource bool
  195. }
  196. func NewMultiGeoIPMatcher(geoips []*GeoIP, onSource bool) (*MultiGeoIPMatcher, error) {
  197. var matchers []*GeoIPMatcher
  198. for _, geoip := range geoips {
  199. matcher, err := globalGeoIPContainer.Add(geoip)
  200. if err != nil {
  201. return nil, err
  202. }
  203. matchers = append(matchers, matcher)
  204. }
  205. return &MultiGeoIPMatcher{
  206. matchers: matchers,
  207. onSource: onSource,
  208. }, nil
  209. }
  210. func (m *MultiGeoIPMatcher) Apply(ctx context.Context) bool {
  211. ips := make([]net.IP, 0, 4)
  212. if resolver, ok := ResolvedIPsFromContext(ctx); ok {
  213. resolvedIPs := resolver.Resolve()
  214. for _, rip := range resolvedIPs {
  215. ips = append(ips, rip.IP())
  216. }
  217. }
  218. var dest net.Destination
  219. if m.onSource {
  220. dest = sourceFromContext(ctx)
  221. } else {
  222. dest = targetFromContent(ctx)
  223. }
  224. if dest.IsValid() && (dest.Address.Family().IsIPv4() || dest.Address.Family().IsIPv6()) {
  225. ips = append(ips, dest.Address.IP())
  226. }
  227. for _, ip := range ips {
  228. for _, matcher := range m.matchers {
  229. if matcher.Match(ip) {
  230. return true
  231. }
  232. }
  233. }
  234. return false
  235. }
  236. type PortMatcher struct {
  237. port net.PortRange
  238. }
  239. func NewPortMatcher(portRange net.PortRange) *PortMatcher {
  240. return &PortMatcher{
  241. port: portRange,
  242. }
  243. }
  244. func (v *PortMatcher) Apply(ctx context.Context) bool {
  245. outbound := session.OutboundFromContext(ctx)
  246. if outbound == nil || !outbound.Target.IsValid() {
  247. return false
  248. }
  249. return v.port.Contains(outbound.Target.Port)
  250. }
  251. type NetworkMatcher struct {
  252. network *net.NetworkList
  253. }
  254. func NewNetworkMatcher(network *net.NetworkList) *NetworkMatcher {
  255. return &NetworkMatcher{
  256. network: network,
  257. }
  258. }
  259. func (v *NetworkMatcher) Apply(ctx context.Context) bool {
  260. outbound := session.OutboundFromContext(ctx)
  261. if outbound == nil || !outbound.Target.IsValid() {
  262. return false
  263. }
  264. return v.network.HasNetwork(outbound.Target.Network)
  265. }
  266. type UserMatcher struct {
  267. user []string
  268. }
  269. func NewUserMatcher(users []string) *UserMatcher {
  270. usersCopy := make([]string, 0, len(users))
  271. for _, user := range users {
  272. if len(user) > 0 {
  273. usersCopy = append(usersCopy, user)
  274. }
  275. }
  276. return &UserMatcher{
  277. user: usersCopy,
  278. }
  279. }
  280. func (v *UserMatcher) Apply(ctx context.Context) bool {
  281. inbound := session.InboundFromContext(ctx)
  282. if inbound == nil {
  283. return false
  284. }
  285. user := inbound.User
  286. if user == nil {
  287. return false
  288. }
  289. for _, u := range v.user {
  290. if u == user.Email {
  291. return true
  292. }
  293. }
  294. return false
  295. }
  296. type InboundTagMatcher struct {
  297. tags []string
  298. }
  299. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  300. tagsCopy := make([]string, 0, len(tags))
  301. for _, tag := range tags {
  302. if len(tag) > 0 {
  303. tagsCopy = append(tagsCopy, tag)
  304. }
  305. }
  306. return &InboundTagMatcher{
  307. tags: tagsCopy,
  308. }
  309. }
  310. func (v *InboundTagMatcher) Apply(ctx context.Context) bool {
  311. inbound := session.InboundFromContext(ctx)
  312. if inbound == nil || len(inbound.Tag) == 0 {
  313. return false
  314. }
  315. tag := inbound.Tag
  316. for _, t := range v.tags {
  317. if t == tag {
  318. return true
  319. }
  320. }
  321. return false
  322. }
  323. type ProtocolMatcher struct {
  324. protocols []string
  325. }
  326. func NewProtocolMatcher(protocols []string) *ProtocolMatcher {
  327. pCopy := make([]string, 0, len(protocols))
  328. for _, p := range protocols {
  329. if len(p) > 0 {
  330. pCopy = append(pCopy, p)
  331. }
  332. }
  333. return &ProtocolMatcher{
  334. protocols: pCopy,
  335. }
  336. }
  337. func (m *ProtocolMatcher) Apply(ctx context.Context) bool {
  338. result := dispatcher.SniffingResultFromContext(ctx)
  339. if result == nil {
  340. return false
  341. }
  342. protocol := result.Protocol()
  343. for _, p := range m.protocols {
  344. if strings.HasPrefix(protocol, p) {
  345. return true
  346. }
  347. }
  348. return false
  349. }