context.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package session
  2. import (
  3. "context"
  4. _ "unsafe"
  5. "github.com/xtls/xray-core/common/net"
  6. "github.com/xtls/xray-core/features/routing"
  7. )
  8. //go:linkname IndependentCancelCtx context.newCancelCtx
  9. func IndependentCancelCtx(parent context.Context) context.Context
  10. type sessionKey int
  11. const (
  12. idSessionKey sessionKey = iota
  13. inboundSessionKey
  14. outboundSessionKey
  15. contentSessionKey
  16. muxPreferedSessionKey
  17. sockoptSessionKey
  18. trackedConnectionErrorKey
  19. dispatcherKey
  20. timeoutOnlyKey
  21. allowedNetworkKey
  22. )
  23. // ContextWithID returns a new context with the given ID.
  24. func ContextWithID(ctx context.Context, id ID) context.Context {
  25. return context.WithValue(ctx, idSessionKey, id)
  26. }
  27. // IDFromContext returns ID in this context, or 0 if not contained.
  28. func IDFromContext(ctx context.Context) ID {
  29. if id, ok := ctx.Value(idSessionKey).(ID); ok {
  30. return id
  31. }
  32. return 0
  33. }
  34. func ContextWithInbound(ctx context.Context, inbound *Inbound) context.Context {
  35. return context.WithValue(ctx, inboundSessionKey, inbound)
  36. }
  37. func InboundFromContext(ctx context.Context) *Inbound {
  38. if inbound, ok := ctx.Value(inboundSessionKey).(*Inbound); ok {
  39. return inbound
  40. }
  41. return nil
  42. }
  43. func ContextWithOutbound(ctx context.Context, outbound *Outbound) context.Context {
  44. return context.WithValue(ctx, outboundSessionKey, outbound)
  45. }
  46. func OutboundFromContext(ctx context.Context) *Outbound {
  47. if outbound, ok := ctx.Value(outboundSessionKey).(*Outbound); ok {
  48. return outbound
  49. }
  50. return nil
  51. }
  52. func ContextWithContent(ctx context.Context, content *Content) context.Context {
  53. return context.WithValue(ctx, contentSessionKey, content)
  54. }
  55. func ContentFromContext(ctx context.Context) *Content {
  56. if content, ok := ctx.Value(contentSessionKey).(*Content); ok {
  57. return content
  58. }
  59. return nil
  60. }
  61. // ContextWithMuxPrefered returns a new context with the given bool
  62. func ContextWithMuxPrefered(ctx context.Context, forced bool) context.Context {
  63. return context.WithValue(ctx, muxPreferedSessionKey, forced)
  64. }
  65. // MuxPreferedFromContext returns value in this context, or false if not contained.
  66. func MuxPreferedFromContext(ctx context.Context) bool {
  67. if val, ok := ctx.Value(muxPreferedSessionKey).(bool); ok {
  68. return val
  69. }
  70. return false
  71. }
  72. // ContextWithSockopt returns a new context with Socket configs included
  73. func ContextWithSockopt(ctx context.Context, s *Sockopt) context.Context {
  74. return context.WithValue(ctx, sockoptSessionKey, s)
  75. }
  76. // SockoptFromContext returns Socket configs in this context, or nil if not contained.
  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. }