context.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package session
  2. import (
  3. "context"
  4. "v2ray.com/core/common/net"
  5. "v2ray.com/core/common/session"
  6. "v2ray.com/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.Target
  62. if !dest.Address.Family().IsDomain() {
  63. return ""
  64. }
  65. return dest.Address.Domain()
  66. }
  67. // GetNetwork implements routing.Context.
  68. func (ctx *Context) GetNetwork() net.Network {
  69. if ctx.Outbound == nil {
  70. return net.Network_Unknown
  71. }
  72. return ctx.Outbound.Target.Network
  73. }
  74. // GetProtocol implements routing.Context.
  75. func (ctx *Context) GetProtocol() string {
  76. if ctx.Content == nil {
  77. return ""
  78. }
  79. return ctx.Content.Protocol
  80. }
  81. // GetUser implements routing.Context.
  82. func (ctx *Context) GetUser() string {
  83. if ctx.Inbound == nil || ctx.Inbound.User == nil {
  84. return ""
  85. }
  86. return ctx.Inbound.User.Email
  87. }
  88. // GetAttributes implements routing.Context.
  89. func (ctx *Context) GetAttributes() map[string]string {
  90. if ctx.Content == nil {
  91. return nil
  92. }
  93. return ctx.Content.Attributes
  94. }
  95. // GetSkipDNSResolve implements routing.Context.
  96. func (ctx *Context) GetSkipDNSResolve() bool {
  97. if ctx.Content == nil {
  98. return false
  99. }
  100. return ctx.Content.SkipDNSResolve
  101. }
  102. // AsRoutingContext creates a context from context.context with session info.
  103. func AsRoutingContext(ctx context.Context) routing.Context {
  104. return &Context{
  105. Inbound: session.InboundFromContext(ctx),
  106. Outbound: session.OutboundFromContext(ctx),
  107. Content: session.ContentFromContext(ctx),
  108. }
  109. }