context.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 // unused
  16. sockoptSessionKey ctx.SessionKey = 5 // used by dokodemo to only receive sockopt.Mark
  17. trackedConnectionErrorKey ctx.SessionKey = 6 // used by observer to get outbound error
  18. dispatcherKey ctx.SessionKey = 7 // used by ss2022 inbounds to get dispatcher
  19. timeoutOnlyKey ctx.SessionKey = 8 // mux context's child contexts to only cancel when its own traffic times out
  20. allowedNetworkKey ctx.SessionKey = 9 // muxcool server control incoming request tcp/udp
  21. handlerSessionKey ctx.SessionKey = 10 // unused
  22. mitmAlpn11Key ctx.SessionKey = 11 // used by TLS dialer
  23. mitmServerNameKey ctx.SessionKey = 12 // used by TLS dialer
  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 SubContextFromMuxInbound(ctx context.Context) context.Context {
  38. newOutbounds := []*Outbound{{}}
  39. content := ContentFromContext(ctx)
  40. newContent := Content{}
  41. if content != nil {
  42. newContent = *content
  43. if content.Attributes != nil {
  44. panic("content.Attributes != nil")
  45. }
  46. }
  47. return ContextWithContent(ContextWithOutbounds(ctx, newOutbounds), &newContent)
  48. }
  49. func OutboundsFromContext(ctx context.Context) []*Outbound {
  50. if outbounds, ok := ctx.Value(outboundSessionKey).([]*Outbound); ok {
  51. return outbounds
  52. }
  53. return nil
  54. }
  55. func ContextWithContent(ctx context.Context, content *Content) context.Context {
  56. return context.WithValue(ctx, contentSessionKey, content)
  57. }
  58. func ContentFromContext(ctx context.Context) *Content {
  59. if content, ok := ctx.Value(contentSessionKey).(*Content); ok {
  60. return content
  61. }
  62. return nil
  63. }
  64. // ContextWithMuxPreferred returns a new context with the given bool
  65. func ContextWithMuxPreferred(ctx context.Context, forced bool) context.Context {
  66. return context.WithValue(ctx, muxPreferredSessionKey, forced)
  67. }
  68. // MuxPreferredFromContext returns value in this context, or false if not contained.
  69. func MuxPreferredFromContext(ctx context.Context) bool {
  70. if val, ok := ctx.Value(muxPreferredSessionKey).(bool); ok {
  71. return val
  72. }
  73. return false
  74. }
  75. // ContextWithSockopt returns a new context with Socket configs included
  76. func ContextWithSockopt(ctx context.Context, s *Sockopt) context.Context {
  77. return context.WithValue(ctx, sockoptSessionKey, s)
  78. }
  79. // SockoptFromContext returns Socket configs in this context, or nil if not contained.
  80. func SockoptFromContext(ctx context.Context) *Sockopt {
  81. if sockopt, ok := ctx.Value(sockoptSessionKey).(*Sockopt); ok {
  82. return sockopt
  83. }
  84. return nil
  85. }
  86. func GetForcedOutboundTagFromContext(ctx context.Context) string {
  87. if ContentFromContext(ctx) == nil {
  88. return ""
  89. }
  90. return ContentFromContext(ctx).Attribute("forcedOutboundTag")
  91. }
  92. func SetForcedOutboundTagToContext(ctx context.Context, tag string) context.Context {
  93. if contentFromContext := ContentFromContext(ctx); contentFromContext == nil {
  94. ctx = ContextWithContent(ctx, &Content{})
  95. }
  96. ContentFromContext(ctx).SetAttribute("forcedOutboundTag", tag)
  97. return ctx
  98. }
  99. type TrackedRequestErrorFeedback interface {
  100. SubmitError(err error)
  101. }
  102. func SubmitOutboundErrorToOriginator(ctx context.Context, err error) {
  103. if errorTracker := ctx.Value(trackedConnectionErrorKey); errorTracker != nil {
  104. errorTracker := errorTracker.(TrackedRequestErrorFeedback)
  105. errorTracker.SubmitError(err)
  106. }
  107. }
  108. func TrackedConnectionError(ctx context.Context, tracker TrackedRequestErrorFeedback) context.Context {
  109. return context.WithValue(ctx, trackedConnectionErrorKey, tracker)
  110. }
  111. func ContextWithDispatcher(ctx context.Context, dispatcher routing.Dispatcher) context.Context {
  112. return context.WithValue(ctx, dispatcherKey, dispatcher)
  113. }
  114. func DispatcherFromContext(ctx context.Context) routing.Dispatcher {
  115. if dispatcher, ok := ctx.Value(dispatcherKey).(routing.Dispatcher); ok {
  116. return dispatcher
  117. }
  118. return nil
  119. }
  120. func ContextWithTimeoutOnly(ctx context.Context, only bool) context.Context {
  121. return context.WithValue(ctx, timeoutOnlyKey, only)
  122. }
  123. func TimeoutOnlyFromContext(ctx context.Context) bool {
  124. if val, ok := ctx.Value(timeoutOnlyKey).(bool); ok {
  125. return val
  126. }
  127. return false
  128. }
  129. func ContextWithAllowedNetwork(ctx context.Context, network net.Network) context.Context {
  130. return context.WithValue(ctx, allowedNetworkKey, network)
  131. }
  132. func AllowedNetworkFromContext(ctx context.Context) net.Network {
  133. if val, ok := ctx.Value(allowedNetworkKey).(net.Network); ok {
  134. return val
  135. }
  136. return net.Network_Unknown
  137. }
  138. func ContextWithMitmAlpn11(ctx context.Context, alpn11 bool) context.Context {
  139. return context.WithValue(ctx, mitmAlpn11Key, alpn11)
  140. }
  141. func MitmAlpn11FromContext(ctx context.Context) bool {
  142. if val, ok := ctx.Value(mitmAlpn11Key).(bool); ok {
  143. return val
  144. }
  145. return false
  146. }
  147. func ContextWithMitmServerName(ctx context.Context, serverName string) context.Context {
  148. return context.WithValue(ctx, mitmServerNameKey, serverName)
  149. }
  150. func MitmServerNameFromContext(ctx context.Context) string {
  151. if val, ok := ctx.Value(mitmServerNameKey).(string); ok {
  152. return val
  153. }
  154. return ""
  155. }