condition.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. package router
  2. import (
  3. "context"
  4. "net"
  5. "regexp"
  6. "strings"
  7. v2net "v2ray.com/core/common/net"
  8. "v2ray.com/core/common/protocol"
  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. type PlainDomainMatcher struct {
  55. pattern string
  56. }
  57. func NewPlainDomainMatcher(pattern string) *PlainDomainMatcher {
  58. return &PlainDomainMatcher{
  59. pattern: pattern,
  60. }
  61. }
  62. func (v *PlainDomainMatcher) Apply(ctx context.Context) bool {
  63. dest, ok := proxy.TargetFromContext(ctx)
  64. if !ok {
  65. return false
  66. }
  67. if !dest.Address.Family().IsDomain() {
  68. return false
  69. }
  70. domain := dest.Address.Domain()
  71. return strings.Contains(domain, v.pattern)
  72. }
  73. type RegexpDomainMatcher struct {
  74. pattern *regexp.Regexp
  75. }
  76. func NewRegexpDomainMatcher(pattern string) (*RegexpDomainMatcher, error) {
  77. r, err := regexp.Compile(pattern)
  78. if err != nil {
  79. return nil, err
  80. }
  81. return &RegexpDomainMatcher{
  82. pattern: r,
  83. }, nil
  84. }
  85. func (v *RegexpDomainMatcher) Apply(ctx context.Context) bool {
  86. dest, ok := proxy.TargetFromContext(ctx)
  87. if !ok {
  88. return false
  89. }
  90. if !dest.Address.Family().IsDomain() {
  91. return false
  92. }
  93. domain := dest.Address.Domain()
  94. return v.pattern.MatchString(strings.ToLower(domain))
  95. }
  96. type SubDomainMatcher struct {
  97. pattern string
  98. }
  99. func NewSubDomainMatcher(p string) *SubDomainMatcher {
  100. return &SubDomainMatcher{
  101. pattern: p,
  102. }
  103. }
  104. func (m *SubDomainMatcher) Apply(ctx context.Context) bool {
  105. dest, ok := proxy.TargetFromContext(ctx)
  106. if !ok {
  107. return false
  108. }
  109. if !dest.Address.Family().IsDomain() {
  110. return false
  111. }
  112. domain := dest.Address.Domain()
  113. if !strings.HasSuffix(domain, m.pattern) {
  114. return false
  115. }
  116. return len(domain) == len(m.pattern) || domain[len(domain)-len(m.pattern)-1] == '.'
  117. }
  118. type CIDRMatcher struct {
  119. cidr *net.IPNet
  120. onSource bool
  121. }
  122. func NewCIDRMatcher(ip []byte, mask uint32, onSource bool) (*CIDRMatcher, error) {
  123. cidr := &net.IPNet{
  124. IP: net.IP(ip),
  125. Mask: net.CIDRMask(int(mask), len(ip)),
  126. }
  127. return &CIDRMatcher{
  128. cidr: cidr,
  129. onSource: onSource,
  130. }, nil
  131. }
  132. func (v *CIDRMatcher) Apply(ctx context.Context) bool {
  133. ips := make([]net.IP, 0, 4)
  134. if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok {
  135. for _, rip := range resolveIPs {
  136. if !rip.Family().IsIPv6() {
  137. continue
  138. }
  139. ips = append(ips, rip.IP())
  140. }
  141. }
  142. var dest v2net.Destination
  143. var ok bool
  144. if v.onSource {
  145. dest, ok = proxy.SourceFromContext(ctx)
  146. } else {
  147. dest, ok = proxy.TargetFromContext(ctx)
  148. }
  149. if ok && dest.Address.Family().IsIPv6() {
  150. ips = append(ips, dest.Address.IP())
  151. }
  152. for _, ip := range ips {
  153. if v.cidr.Contains(ip) {
  154. return true
  155. }
  156. }
  157. return false
  158. }
  159. type IPv4Matcher struct {
  160. ipv4net *v2net.IPNet
  161. onSource bool
  162. }
  163. func NewIPv4Matcher(ipnet *v2net.IPNet, onSource bool) *IPv4Matcher {
  164. return &IPv4Matcher{
  165. ipv4net: ipnet,
  166. onSource: onSource,
  167. }
  168. }
  169. func (v *IPv4Matcher) Apply(ctx context.Context) bool {
  170. ips := make([]net.IP, 0, 4)
  171. if resolveIPs, ok := proxy.ResolvedIPsFromContext(ctx); ok {
  172. for _, rip := range resolveIPs {
  173. if !rip.Family().IsIPv4() {
  174. continue
  175. }
  176. ips = append(ips, rip.IP())
  177. }
  178. }
  179. var dest v2net.Destination
  180. var ok bool
  181. if v.onSource {
  182. dest, ok = proxy.SourceFromContext(ctx)
  183. } else {
  184. dest, ok = proxy.TargetFromContext(ctx)
  185. }
  186. if ok && dest.Address.Family().IsIPv4() {
  187. ips = append(ips, dest.Address.IP())
  188. }
  189. for _, ip := range ips {
  190. if v.ipv4net.Contains(ip) {
  191. return true
  192. }
  193. }
  194. return false
  195. }
  196. type PortMatcher struct {
  197. port v2net.PortRange
  198. }
  199. func NewPortMatcher(portRange v2net.PortRange) *PortMatcher {
  200. return &PortMatcher{
  201. port: portRange,
  202. }
  203. }
  204. func (v *PortMatcher) Apply(ctx context.Context) bool {
  205. dest, ok := proxy.TargetFromContext(ctx)
  206. if !ok {
  207. return false
  208. }
  209. return v.port.Contains(dest.Port)
  210. }
  211. type NetworkMatcher struct {
  212. network *v2net.NetworkList
  213. }
  214. func NewNetworkMatcher(network *v2net.NetworkList) *NetworkMatcher {
  215. return &NetworkMatcher{
  216. network: network,
  217. }
  218. }
  219. func (v *NetworkMatcher) Apply(ctx context.Context) bool {
  220. dest, ok := proxy.TargetFromContext(ctx)
  221. if !ok {
  222. return false
  223. }
  224. return v.network.HasNetwork(dest.Network)
  225. }
  226. type UserMatcher struct {
  227. user []string
  228. }
  229. func NewUserMatcher(users []string) *UserMatcher {
  230. usersCopy := make([]string, 0, len(users))
  231. for _, user := range users {
  232. if len(user) > 0 {
  233. usersCopy = append(usersCopy, user)
  234. }
  235. }
  236. return &UserMatcher{
  237. user: usersCopy,
  238. }
  239. }
  240. func (v *UserMatcher) Apply(ctx context.Context) bool {
  241. user := protocol.UserFromContext(ctx)
  242. if user == nil {
  243. return false
  244. }
  245. for _, u := range v.user {
  246. if u == user.Email {
  247. return true
  248. }
  249. }
  250. return false
  251. }
  252. type InboundTagMatcher struct {
  253. tags []string
  254. }
  255. func NewInboundTagMatcher(tags []string) *InboundTagMatcher {
  256. tagsCopy := make([]string, 0, len(tags))
  257. for _, tag := range tags {
  258. if len(tag) > 0 {
  259. tagsCopy = append(tagsCopy, tag)
  260. }
  261. }
  262. return &InboundTagMatcher{
  263. tags: tagsCopy,
  264. }
  265. }
  266. func (v *InboundTagMatcher) Apply(ctx context.Context) bool {
  267. tag, ok := proxy.InboundTagFromContext(ctx)
  268. if !ok {
  269. return false
  270. }
  271. for _, t := range v.tags {
  272. if t == tag {
  273. return true
  274. }
  275. }
  276. return false
  277. }