context.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package session
  2. import (
  3. "context"
  4. "github.com/xtls/xray-core/common/net"
  5. "github.com/xtls/xray-core/common/session"
  6. "github.com/xtls/xray-core/features/routing"
  7. )
  8. // Context is an implementation of routing.Context, which is a wrapper of context.context with session info.
  9. type Context struct {
  10. Inbound *session.Inbound
  11. Outbound *session.Outbound
  12. Content *session.Content
  13. }
  14. // GetInboundTag implements routing.Context.
  15. func (ctx *Context) GetInboundTag() string {
  16. if ctx.Inbound == nil {
  17. return ""
  18. }
  19. return ctx.Inbound.Tag
  20. }
  21. // GetSourceIPs implements routing.Context.
  22. func (ctx *Context) GetSourceIPs() []net.IP {
  23. if ctx.Inbound == nil || !ctx.Inbound.Source.IsValid() {
  24. return nil
  25. }
  26. if ctx.Inbound.Source.Address.Family().IsIP() {
  27. return []net.IP{ctx.Inbound.Source.Address.IP()}
  28. }
  29. return nil
  30. }
  31. // GetSourcePort implements routing.Context.
  32. func (ctx *Context) GetSourcePort() net.Port {
  33. if ctx.Inbound == nil || !ctx.Inbound.Source.IsValid() {
  34. return 0
  35. }
  36. return ctx.Inbound.Source.Port
  37. }
  38. // GetTargetIPs implements routing.Context.
  39. func (ctx *Context) GetTargetIPs() []net.IP {
  40. if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
  41. return nil
  42. }
  43. if ctx.Outbound.Target.Address.Family().IsIP() {
  44. return []net.IP{ctx.Outbound.Target.Address.IP()}
  45. }
  46. return nil
  47. }
  48. // GetTargetPort implements routing.Context.
  49. func (ctx *Context) GetTargetPort() net.Port {
  50. if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
  51. return 0
  52. }
  53. return ctx.Outbound.Target.Port
  54. }
  55. // GetLocalIPs implements routing.Context.
  56. func (ctx *Context) GetLocalIPs() []net.IP {
  57. if ctx.Inbound == nil || !ctx.Inbound.Local.IsValid() {
  58. return nil
  59. }
  60. if ctx.Inbound.Local.Address.Family().IsIP() {
  61. return []net.IP{ctx.Inbound.Local.Address.IP()}
  62. }
  63. return nil
  64. }
  65. // GetLocalPort implements routing.Context.
  66. func (ctx *Context) GetLocalPort() net.Port {
  67. if ctx.Inbound == nil || !ctx.Inbound.Local.IsValid() {
  68. return 0
  69. }
  70. return ctx.Inbound.Local.Port
  71. }
  72. // GetTargetDomain implements routing.Context.
  73. func (ctx *Context) GetTargetDomain() string {
  74. if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
  75. return ""
  76. }
  77. dest := ctx.Outbound.RouteTarget
  78. if dest.IsValid() && dest.Address.Family().IsDomain() {
  79. return dest.Address.Domain()
  80. }
  81. dest = ctx.Outbound.Target
  82. if !dest.Address.Family().IsDomain() {
  83. return ""
  84. }
  85. return dest.Address.Domain()
  86. }
  87. // GetNetwork implements routing.Context.
  88. func (ctx *Context) GetNetwork() net.Network {
  89. if ctx.Outbound == nil {
  90. return net.Network_Unknown
  91. }
  92. return ctx.Outbound.Target.Network
  93. }
  94. // GetProtocol implements routing.Context.
  95. func (ctx *Context) GetProtocol() string {
  96. if ctx.Content == nil {
  97. return ""
  98. }
  99. return ctx.Content.Protocol
  100. }
  101. // GetUser implements routing.Context.
  102. func (ctx *Context) GetUser() string {
  103. if ctx.Inbound == nil || ctx.Inbound.User == nil {
  104. return ""
  105. }
  106. return ctx.Inbound.User.Email
  107. }
  108. // GetVlessRoute implements routing.Context.
  109. func (ctx *Context) GetVlessRoute() net.Port {
  110. if ctx.Inbound == nil {
  111. return 0
  112. }
  113. return ctx.Inbound.VlessRoute
  114. }
  115. // GetAttributes implements routing.Context.
  116. func (ctx *Context) GetAttributes() map[string]string {
  117. if ctx.Content == nil {
  118. return nil
  119. }
  120. return ctx.Content.Attributes
  121. }
  122. // GetSkipDNSResolve implements routing.Context.
  123. func (ctx *Context) GetSkipDNSResolve() bool {
  124. if ctx.Content == nil {
  125. return false
  126. }
  127. return ctx.Content.SkipDNSResolve
  128. }
  129. // AsRoutingContext creates a context from context.context with session info.
  130. func AsRoutingContext(ctx context.Context) routing.Context {
  131. outbounds := session.OutboundsFromContext(ctx)
  132. ob := outbounds[len(outbounds)-1]
  133. return &Context{
  134. Inbound: session.InboundFromContext(ctx),
  135. Outbound: ob,
  136. Content: session.ContentFromContext(ctx),
  137. }
  138. }