context.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. mitmAlpn11Key ctx.SessionKey = 11
  23. mitmServerNameKey ctx.SessionKey = 12
  24. )
  25. func ContextWithInbound(ctx context.Context, inbound *Inbound) context.Context {
  26. return context.WithValue(ctx, inboundSessionKey, inbound)
  27. }
  28. func InboundFromContext(ctx context.Context) *Inbound {
  29. if inbound, ok := ctx.Value(inboundSessionKey).(*Inbound); ok {
  30. return inbound
  31. }
  32. return nil
  33. }
  34. func ContextWithOutbounds(ctx context.Context, outbounds []*Outbound) context.Context {
  35. return context.WithValue(ctx, outboundSessionKey, outbounds)
  36. }
  37. func ContextCloneOutboundsAndContent(ctx context.Context) context.Context {
  38. outbounds := OutboundsFromContext(ctx)
  39. newOutbounds := make([]*Outbound, len(outbounds))
  40. for i, ob := range outbounds {
  41. if ob == nil {
  42. continue
  43. }
  44. // copy outbound by value
  45. v := *ob
  46. newOutbounds[i] = &v
  47. }
  48. content := ContentFromContext(ctx)
  49. newContent := Content{}
  50. if content != nil {
  51. newContent = *content
  52. if content.Attributes != nil {
  53. panic("content.Attributes != nil")
  54. }
  55. }
  56. return ContextWithContent(ContextWithOutbounds(ctx, newOutbounds), &newContent)
  57. }
  58. func OutboundsFromContext(ctx context.Context) []*Outbound {
  59. if outbounds, ok := ctx.Value(outboundSessionKey).([]*Outbound); ok {
  60. return outbounds
  61. }
  62. return nil
  63. }
  64. func ContextWithContent(ctx context.Context, content *Content) context.Context {
  65. return context.WithValue(ctx, contentSessionKey, content)
  66. }
  67. func ContentFromContext(ctx context.Context) *Content {
  68. if content, ok := ctx.Value(contentSessionKey).(*Content); ok {
  69. return content
  70. }
  71. return nil
  72. }
  73. // ContextWithMuxPreferred returns a new context with the given bool
  74. func ContextWithMuxPreferred(ctx context.Context, forced bool) context.Context {
  75. return context.WithValue(ctx, muxPreferredSessionKey, forced)
  76. }
  77. // MuxPreferredFromContext returns value in this context, or false if not contained.
  78. func MuxPreferredFromContext(ctx context.Context) bool {
  79. if val, ok := ctx.Value(muxPreferredSessionKey).(bool); ok {
  80. return val
  81. }
  82. return false
  83. }
  84. // ContextWithSockopt returns a new context with Socket configs included
  85. func ContextWithSockopt(ctx context.Context, s *Sockopt) context.Context {
  86. return context.WithValue(ctx, sockoptSessionKey, s)
  87. }
  88. // SockoptFromContext returns Socket configs in this context, or nil if not contained.
  89. func SockoptFromContext(ctx context.Context) *Sockopt {
  90. if sockopt, ok := ctx.Value(sockoptSessionKey).(*Sockopt); ok {
  91. return sockopt
  92. }
  93. return nil
  94. }
  95. func GetForcedOutboundTagFromContext(ctx context.Context) string {
  96. if ContentFromContext(ctx) == nil {
  97. return ""
  98. }
  99. return ContentFromContext(ctx).Attribute("forcedOutboundTag")
  100. }
  101. func SetForcedOutboundTagToContext(ctx context.Context, tag string) context.Context {
  102. if contentFromContext := ContentFromContext(ctx); contentFromContext == nil {
  103. ctx = ContextWithContent(ctx, &Content{})
  104. }
  105. ContentFromContext(ctx).SetAttribute("forcedOutboundTag", tag)
  106. return ctx
  107. }
  108. type TrackedRequestErrorFeedback interface {
  109. SubmitError(err error)
  110. }
  111. func SubmitOutboundErrorToOriginator(ctx context.Context, err error) {
  112. if errorTracker := ctx.Value(trackedConnectionErrorKey); errorTracker != nil {
  113. errorTracker := errorTracker.(TrackedRequestErrorFeedback)
  114. errorTracker.SubmitError(err)
  115. }
  116. }
  117. func TrackedConnectionError(ctx context.Context, tracker TrackedRequestErrorFeedback) context.Context {
  118. return context.WithValue(ctx, trackedConnectionErrorKey, tracker)
  119. }
  120. func ContextWithDispatcher(ctx context.Context, dispatcher routing.Dispatcher) context.Context {
  121. return context.WithValue(ctx, dispatcherKey, dispatcher)
  122. }
  123. func DispatcherFromContext(ctx context.Context) routing.Dispatcher {
  124. if dispatcher, ok := ctx.Value(dispatcherKey).(routing.Dispatcher); ok {
  125. return dispatcher
  126. }
  127. return nil
  128. }
  129. func ContextWithTimeoutOnly(ctx context.Context, only bool) context.Context {
  130. return context.WithValue(ctx, timeoutOnlyKey, only)
  131. }
  132. func TimeoutOnlyFromContext(ctx context.Context) bool {
  133. if val, ok := ctx.Value(timeoutOnlyKey).(bool); ok {
  134. return val
  135. }
  136. return false
  137. }
  138. func ContextWithAllowedNetwork(ctx context.Context, network net.Network) context.Context {
  139. return context.WithValue(ctx, allowedNetworkKey, network)
  140. }
  141. func AllowedNetworkFromContext(ctx context.Context) net.Network {
  142. if val, ok := ctx.Value(allowedNetworkKey).(net.Network); ok {
  143. return val
  144. }
  145. return net.Network_Unknown
  146. }
  147. func ContextWithMitmAlpn11(ctx context.Context, alpn11 bool) context.Context {
  148. return context.WithValue(ctx, mitmAlpn11Key, alpn11)
  149. }
  150. func MitmAlpn11FromContext(ctx context.Context) bool {
  151. if val, ok := ctx.Value(mitmAlpn11Key).(bool); ok {
  152. return val
  153. }
  154. return false
  155. }
  156. func ContextWithMitmServerName(ctx context.Context, serverName string) context.Context {
  157. return context.WithValue(ctx, mitmServerNameKey, serverName)
  158. }
  159. func MitmServerNameFromContext(ctx context.Context) string {
  160. if val, ok := ctx.Value(mitmServerNameKey).(string); ok {
  161. return val
  162. }
  163. return ""
  164. }