context.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 ContextCloneOutbounds(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. return ContextWithOutbounds(ctx, newOutbounds)
  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. // ContextWithMuxPreferred returns a new context with the given bool
  66. func ContextWithMuxPreferred(ctx context.Context, forced bool) context.Context {
  67. return context.WithValue(ctx, muxPreferredSessionKey, forced)
  68. }
  69. // MuxPreferredFromContext returns value in this context, or false if not contained.
  70. func MuxPreferredFromContext(ctx context.Context) bool {
  71. if val, ok := ctx.Value(muxPreferredSessionKey).(bool); ok {
  72. return val
  73. }
  74. return false
  75. }
  76. // ContextWithSockopt returns a new context with Socket configs included
  77. func ContextWithSockopt(ctx context.Context, s *Sockopt) context.Context {
  78. return context.WithValue(ctx, sockoptSessionKey, s)
  79. }
  80. // SockoptFromContext returns Socket configs in this context, or nil if not contained.
  81. func SockoptFromContext(ctx context.Context) *Sockopt {
  82. if sockopt, ok := ctx.Value(sockoptSessionKey).(*Sockopt); ok {
  83. return sockopt
  84. }
  85. return nil
  86. }
  87. func GetForcedOutboundTagFromContext(ctx context.Context) string {
  88. if ContentFromContext(ctx) == nil {
  89. return ""
  90. }
  91. return ContentFromContext(ctx).Attribute("forcedOutboundTag")
  92. }
  93. func SetForcedOutboundTagToContext(ctx context.Context, tag string) context.Context {
  94. if contentFromContext := ContentFromContext(ctx); contentFromContext == nil {
  95. ctx = ContextWithContent(ctx, &Content{})
  96. }
  97. ContentFromContext(ctx).SetAttribute("forcedOutboundTag", tag)
  98. return ctx
  99. }
  100. type TrackedRequestErrorFeedback interface {
  101. SubmitError(err error)
  102. }
  103. func SubmitOutboundErrorToOriginator(ctx context.Context, err error) {
  104. if errorTracker := ctx.Value(trackedConnectionErrorKey); errorTracker != nil {
  105. errorTracker := errorTracker.(TrackedRequestErrorFeedback)
  106. errorTracker.SubmitError(err)
  107. }
  108. }
  109. func TrackedConnectionError(ctx context.Context, tracker TrackedRequestErrorFeedback) context.Context {
  110. return context.WithValue(ctx, trackedConnectionErrorKey, tracker)
  111. }
  112. func ContextWithDispatcher(ctx context.Context, dispatcher routing.Dispatcher) context.Context {
  113. return context.WithValue(ctx, dispatcherKey, dispatcher)
  114. }
  115. func DispatcherFromContext(ctx context.Context) routing.Dispatcher {
  116. if dispatcher, ok := ctx.Value(dispatcherKey).(routing.Dispatcher); ok {
  117. return dispatcher
  118. }
  119. return nil
  120. }
  121. func ContextWithTimeoutOnly(ctx context.Context, only bool) context.Context {
  122. return context.WithValue(ctx, timeoutOnlyKey, only)
  123. }
  124. func TimeoutOnlyFromContext(ctx context.Context) bool {
  125. if val, ok := ctx.Value(timeoutOnlyKey).(bool); ok {
  126. return val
  127. }
  128. return false
  129. }
  130. func ContextWithAllowedNetwork(ctx context.Context, network net.Network) context.Context {
  131. return context.WithValue(ctx, allowedNetworkKey, network)
  132. }
  133. func AllowedNetworkFromContext(ctx context.Context) net.Network {
  134. if val, ok := ctx.Value(allowedNetworkKey).(net.Network); ok {
  135. return val
  136. }
  137. return net.Network_Unknown
  138. }
  139. func ContextWithMitmAlpn11(ctx context.Context, alpn11 bool) context.Context {
  140. return context.WithValue(ctx, mitmAlpn11Key, alpn11)
  141. }
  142. func MitmAlpn11FromContext(ctx context.Context) bool {
  143. if val, ok := ctx.Value(mitmAlpn11Key).(bool); ok {
  144. return val
  145. }
  146. return false
  147. }
  148. func ContextWithMitmServerName(ctx context.Context, serverName string) context.Context {
  149. return context.WithValue(ctx, mitmServerNameKey, serverName)
  150. }
  151. func MitmServerNameFromContext(ctx context.Context) string {
  152. if val, ok := ctx.Value(mitmServerNameKey).(string); ok {
  153. return val
  154. }
  155. return ""
  156. }