context.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package session
  2. import (
  3. "context"
  4. _ "unsafe"
  5. "github.com/xtls/xray-core/common/ctx"
  6. "github.com/xtls/xray-core/common/net"
  7. "github.com/xtls/xray-core/features/routing"
  8. )
  9. //go:linkname IndependentCancelCtx context.newCancelCtx
  10. func IndependentCancelCtx(parent context.Context) context.Context
  11. const (
  12. inboundSessionKey ctx.SessionKey = 1
  13. outboundSessionKey ctx.SessionKey = 2
  14. contentSessionKey ctx.SessionKey = 3
  15. muxPreferredSessionKey ctx.SessionKey = 4
  16. sockoptSessionKey ctx.SessionKey = 5
  17. trackedConnectionErrorKey ctx.SessionKey = 6
  18. dispatcherKey ctx.SessionKey = 7
  19. timeoutOnlyKey ctx.SessionKey = 8
  20. allowedNetworkKey ctx.SessionKey = 9
  21. handlerSessionKey ctx.SessionKey = 10
  22. )
  23. func ContextWithInbound(ctx context.Context, inbound *Inbound) context.Context {
  24. return context.WithValue(ctx, inboundSessionKey, inbound)
  25. }
  26. func InboundFromContext(ctx context.Context) *Inbound {
  27. if inbound, ok := ctx.Value(inboundSessionKey).(*Inbound); ok {
  28. return inbound
  29. }
  30. return nil
  31. }
  32. func ContextWithOutbounds(ctx context.Context, outbounds []*Outbound) context.Context {
  33. return context.WithValue(ctx, outboundSessionKey, outbounds)
  34. }
  35. func ContextCloneOutbounds(ctx context.Context) context.Context {
  36. outbounds := OutboundsFromContext(ctx)
  37. newOutbounds := make([]*Outbound, len(outbounds))
  38. for i, ob := range outbounds {
  39. if ob == nil {
  40. continue
  41. }
  42. // copy outbound by value
  43. v := *ob
  44. newOutbounds[i] = &v
  45. }
  46. return ContextWithOutbounds(ctx, newOutbounds)
  47. }
  48. func OutboundsFromContext(ctx context.Context) []*Outbound {
  49. if outbounds, ok := ctx.Value(outboundSessionKey).([]*Outbound); ok {
  50. return outbounds
  51. }
  52. return nil
  53. }
  54. func ContextWithContent(ctx context.Context, content *Content) context.Context {
  55. return context.WithValue(ctx, contentSessionKey, content)
  56. }
  57. func ContentFromContext(ctx context.Context) *Content {
  58. if content, ok := ctx.Value(contentSessionKey).(*Content); ok {
  59. return content
  60. }
  61. return nil
  62. }
  63. // ContextWithMuxPreferred returns a new context with the given bool
  64. func ContextWithMuxPreferred(ctx context.Context, forced bool) context.Context {
  65. return context.WithValue(ctx, muxPreferredSessionKey, forced)
  66. }
  67. // MuxPreferredFromContext returns value in this context, or false if not contained.
  68. func MuxPreferredFromContext(ctx context.Context) bool {
  69. if val, ok := ctx.Value(muxPreferredSessionKey).(bool); ok {
  70. return val
  71. }
  72. return false
  73. }
  74. // ContextWithSockopt returns a new context with Socket configs included
  75. func ContextWithSockopt(ctx context.Context, s *Sockopt) context.Context {
  76. return context.WithValue(ctx, sockoptSessionKey, s)
  77. }
  78. // SockoptFromContext returns Socket configs in this context, or nil if not contained.
  79. func SockoptFromContext(ctx context.Context) *Sockopt {
  80. if sockopt, ok := ctx.Value(sockoptSessionKey).(*Sockopt); ok {
  81. return sockopt
  82. }
  83. return nil
  84. }
  85. func GetForcedOutboundTagFromContext(ctx context.Context) string {
  86. if ContentFromContext(ctx) == nil {
  87. return ""
  88. }
  89. return ContentFromContext(ctx).Attribute("forcedOutboundTag")
  90. }
  91. func SetForcedOutboundTagToContext(ctx context.Context, tag string) context.Context {
  92. if contentFromContext := ContentFromContext(ctx); contentFromContext == nil {
  93. ctx = ContextWithContent(ctx, &Content{})
  94. }
  95. ContentFromContext(ctx).SetAttribute("forcedOutboundTag", tag)
  96. return ctx
  97. }
  98. type TrackedRequestErrorFeedback interface {
  99. SubmitError(err error)
  100. }
  101. func SubmitOutboundErrorToOriginator(ctx context.Context, err error) {
  102. if errorTracker := ctx.Value(trackedConnectionErrorKey); errorTracker != nil {
  103. errorTracker := errorTracker.(TrackedRequestErrorFeedback)
  104. errorTracker.SubmitError(err)
  105. }
  106. }
  107. func TrackedConnectionError(ctx context.Context, tracker TrackedRequestErrorFeedback) context.Context {
  108. return context.WithValue(ctx, trackedConnectionErrorKey, tracker)
  109. }
  110. func ContextWithDispatcher(ctx context.Context, dispatcher routing.Dispatcher) context.Context {
  111. return context.WithValue(ctx, dispatcherKey, dispatcher)
  112. }
  113. func DispatcherFromContext(ctx context.Context) routing.Dispatcher {
  114. if dispatcher, ok := ctx.Value(dispatcherKey).(routing.Dispatcher); ok {
  115. return dispatcher
  116. }
  117. return nil
  118. }
  119. func ContextWithTimeoutOnly(ctx context.Context, only bool) context.Context {
  120. return context.WithValue(ctx, timeoutOnlyKey, only)
  121. }
  122. func TimeoutOnlyFromContext(ctx context.Context) bool {
  123. if val, ok := ctx.Value(timeoutOnlyKey).(bool); ok {
  124. return val
  125. }
  126. return false
  127. }
  128. func ContextWithAllowedNetwork(ctx context.Context, network net.Network) context.Context {
  129. return context.WithValue(ctx, allowedNetworkKey, network)
  130. }
  131. func AllowedNetworkFromContext(ctx context.Context) net.Network {
  132. if val, ok := ctx.Value(allowedNetworkKey).(net.Network); ok {
  133. return val
  134. }
  135. return net.Network_Unknown
  136. }