context.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/outbound"
  8. "github.com/xtls/xray-core/features/routing"
  9. )
  10. //go:linkname IndependentCancelCtx context.newCancelCtx
  11. func IndependentCancelCtx(parent context.Context) context.Context
  12. const (
  13. inboundSessionKey ctx.SessionKey = 1
  14. outboundSessionKey ctx.SessionKey = 2
  15. contentSessionKey ctx.SessionKey = 3
  16. isReverseMuxKey ctx.SessionKey = 4 // is reverse mux
  17. sockoptSessionKey ctx.SessionKey = 5 // used by dokodemo to only receive sockopt.Mark
  18. trackedConnectionErrorKey ctx.SessionKey = 6 // used by observer to get outbound error
  19. dispatcherKey ctx.SessionKey = 7 // used by ss2022 inbounds to get dispatcher
  20. timeoutOnlyKey ctx.SessionKey = 8 // mux context's child contexts to only cancel when its own traffic times out
  21. allowedNetworkKey ctx.SessionKey = 9 // muxcool server control incoming request tcp/udp
  22. fullHandlerKey ctx.SessionKey = 10 // outbound gets full handler
  23. mitmAlpn11Key ctx.SessionKey = 11 // used by TLS dialer
  24. mitmServerNameKey ctx.SessionKey = 12 // used by TLS dialer
  25. )
  26. func ContextWithInbound(ctx context.Context, inbound *Inbound) context.Context {
  27. return context.WithValue(ctx, inboundSessionKey, inbound)
  28. }
  29. func InboundFromContext(ctx context.Context) *Inbound {
  30. if inbound, ok := ctx.Value(inboundSessionKey).(*Inbound); ok {
  31. return inbound
  32. }
  33. return nil
  34. }
  35. func ContextWithOutbounds(ctx context.Context, outbounds []*Outbound) context.Context {
  36. return context.WithValue(ctx, outboundSessionKey, outbounds)
  37. }
  38. func SubContextFromMuxInbound(ctx context.Context) context.Context {
  39. newOutbounds := []*Outbound{{}}
  40. content := ContentFromContext(ctx)
  41. newContent := Content{}
  42. if content != nil {
  43. newContent = *content
  44. if content.Attributes != nil {
  45. panic("content.Attributes != nil")
  46. }
  47. }
  48. return ContextWithContent(ContextWithOutbounds(ctx, newOutbounds), &newContent)
  49. }
  50. func OutboundsFromContext(ctx context.Context) []*Outbound {
  51. if outbounds, ok := ctx.Value(outboundSessionKey).([]*Outbound); ok {
  52. return outbounds
  53. }
  54. return nil
  55. }
  56. func ContextWithContent(ctx context.Context, content *Content) context.Context {
  57. return context.WithValue(ctx, contentSessionKey, content)
  58. }
  59. func ContentFromContext(ctx context.Context) *Content {
  60. if content, ok := ctx.Value(contentSessionKey).(*Content); ok {
  61. return content
  62. }
  63. return nil
  64. }
  65. func ContextWithIsReverseMux(ctx context.Context, isReverseMux bool) context.Context {
  66. return context.WithValue(ctx, isReverseMuxKey, isReverseMux)
  67. }
  68. func IsReverseMuxFromContext(ctx context.Context) bool {
  69. if val, ok := ctx.Value(isReverseMuxKey).(bool); ok {
  70. return val
  71. }
  72. return false
  73. }
  74. func ContextWithSockopt(ctx context.Context, s *Sockopt) context.Context {
  75. return context.WithValue(ctx, sockoptSessionKey, s)
  76. }
  77. func SockoptFromContext(ctx context.Context) *Sockopt {
  78. if sockopt, ok := ctx.Value(sockoptSessionKey).(*Sockopt); ok {
  79. return sockopt
  80. }
  81. return nil
  82. }
  83. func GetForcedOutboundTagFromContext(ctx context.Context) string {
  84. if ContentFromContext(ctx) == nil {
  85. return ""
  86. }
  87. return ContentFromContext(ctx).Attribute("forcedOutboundTag")
  88. }
  89. func SetForcedOutboundTagToContext(ctx context.Context, tag string) context.Context {
  90. if contentFromContext := ContentFromContext(ctx); contentFromContext == nil {
  91. ctx = ContextWithContent(ctx, &Content{})
  92. }
  93. ContentFromContext(ctx).SetAttribute("forcedOutboundTag", tag)
  94. return ctx
  95. }
  96. type TrackedRequestErrorFeedback interface {
  97. SubmitError(err error)
  98. }
  99. func SubmitOutboundErrorToOriginator(ctx context.Context, err error) {
  100. if errorTracker := ctx.Value(trackedConnectionErrorKey); errorTracker != nil {
  101. errorTracker := errorTracker.(TrackedRequestErrorFeedback)
  102. errorTracker.SubmitError(err)
  103. }
  104. }
  105. func TrackedConnectionError(ctx context.Context, tracker TrackedRequestErrorFeedback) context.Context {
  106. return context.WithValue(ctx, trackedConnectionErrorKey, tracker)
  107. }
  108. func ContextWithDispatcher(ctx context.Context, dispatcher routing.Dispatcher) context.Context {
  109. return context.WithValue(ctx, dispatcherKey, dispatcher)
  110. }
  111. func DispatcherFromContext(ctx context.Context) routing.Dispatcher {
  112. if dispatcher, ok := ctx.Value(dispatcherKey).(routing.Dispatcher); ok {
  113. return dispatcher
  114. }
  115. return nil
  116. }
  117. func ContextWithTimeoutOnly(ctx context.Context, only bool) context.Context {
  118. return context.WithValue(ctx, timeoutOnlyKey, only)
  119. }
  120. func TimeoutOnlyFromContext(ctx context.Context) bool {
  121. if val, ok := ctx.Value(timeoutOnlyKey).(bool); ok {
  122. return val
  123. }
  124. return false
  125. }
  126. func ContextWithAllowedNetwork(ctx context.Context, network net.Network) context.Context {
  127. return context.WithValue(ctx, allowedNetworkKey, network)
  128. }
  129. func AllowedNetworkFromContext(ctx context.Context) net.Network {
  130. if val, ok := ctx.Value(allowedNetworkKey).(net.Network); ok {
  131. return val
  132. }
  133. return net.Network_Unknown
  134. }
  135. func ContextWithFullHandler(ctx context.Context, handler outbound.Handler) context.Context {
  136. return context.WithValue(ctx, fullHandlerKey, handler)
  137. }
  138. func FullHandlerFromContext(ctx context.Context) outbound.Handler {
  139. if val, ok := ctx.Value(fullHandlerKey).(outbound.Handler); ok {
  140. return val
  141. }
  142. return nil
  143. }
  144. func ContextWithMitmAlpn11(ctx context.Context, alpn11 bool) context.Context {
  145. return context.WithValue(ctx, mitmAlpn11Key, alpn11)
  146. }
  147. func MitmAlpn11FromContext(ctx context.Context) bool {
  148. if val, ok := ctx.Value(mitmAlpn11Key).(bool); ok {
  149. return val
  150. }
  151. return false
  152. }
  153. func ContextWithMitmServerName(ctx context.Context, serverName string) context.Context {
  154. return context.WithValue(ctx, mitmServerNameKey, serverName)
  155. }
  156. func MitmServerNameFromContext(ctx context.Context) string {
  157. if val, ok := ctx.Value(mitmServerNameKey).(string); ok {
  158. return val
  159. }
  160. return ""
  161. }