context.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. dest := ctx.Inbound.Source
  27. if dest.Address.Family().IsDomain() {
  28. return nil
  29. }
  30. return []net.IP{dest.Address.IP()}
  31. }
  32. // GetSourcePort implements routing.Context.
  33. func (ctx *Context) GetSourcePort() net.Port {
  34. if ctx.Inbound == nil || !ctx.Inbound.Source.IsValid() {
  35. return 0
  36. }
  37. return ctx.Inbound.Source.Port
  38. }
  39. // GetTargetIPs implements routing.Context.
  40. func (ctx *Context) GetTargetIPs() []net.IP {
  41. if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
  42. return nil
  43. }
  44. if ctx.Outbound.Target.Address.Family().IsIP() {
  45. return []net.IP{ctx.Outbound.Target.Address.IP()}
  46. }
  47. return nil
  48. }
  49. // GetTargetPort implements routing.Context.
  50. func (ctx *Context) GetTargetPort() net.Port {
  51. if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
  52. return 0
  53. }
  54. return ctx.Outbound.Target.Port
  55. }
  56. // GetTargetDomain implements routing.Context.
  57. func (ctx *Context) GetTargetDomain() string {
  58. if ctx.Outbound == nil || !ctx.Outbound.Target.IsValid() {
  59. return ""
  60. }
  61. dest := ctx.Outbound.RouteTarget
  62. if dest.IsValid() && dest.Address.Family().IsDomain() {
  63. return dest.Address.Domain()
  64. }
  65. dest = ctx.Outbound.Target
  66. if !dest.Address.Family().IsDomain() {
  67. return ""
  68. }
  69. return dest.Address.Domain()
  70. }
  71. // GetNetwork implements routing.Context.
  72. func (ctx *Context) GetNetwork() net.Network {
  73. if ctx.Outbound == nil {
  74. return net.Network_Unknown
  75. }
  76. return ctx.Outbound.Target.Network
  77. }
  78. // GetProtocol implements routing.Context.
  79. func (ctx *Context) GetProtocol() string {
  80. if ctx.Content == nil {
  81. return ""
  82. }
  83. return ctx.Content.Protocol
  84. }
  85. // GetUser implements routing.Context.
  86. func (ctx *Context) GetUser() string {
  87. if ctx.Inbound == nil || ctx.Inbound.User == nil {
  88. return ""
  89. }
  90. return ctx.Inbound.User.Email
  91. }
  92. // GetAttributes implements routing.Context.
  93. func (ctx *Context) GetAttributes() map[string]string {
  94. if ctx.Content == nil {
  95. return nil
  96. }
  97. return ctx.Content.Attributes
  98. }
  99. // GetSkipDNSResolve implements routing.Context.
  100. func (ctx *Context) GetSkipDNSResolve() bool {
  101. if ctx.Content == nil {
  102. return false
  103. }
  104. return ctx.Content.SkipDNSResolve
  105. }
  106. // AsRoutingContext creates a context from context.context with session info.
  107. func AsRoutingContext(ctx context.Context) routing.Context {
  108. outbounds := session.OutboundsFromContext(ctx)
  109. ob := outbounds[len(outbounds) - 1]
  110. return &Context{
  111. Inbound: session.InboundFromContext(ctx),
  112. Outbound: ob,
  113. Content: session.ContentFromContext(ctx),
  114. }
  115. }