context.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. handlerSessionKey
  23. )
  24. // ContextWithID returns a new context with the given ID.
  25. func ContextWithID(ctx context.Context, id ID) context.Context {
  26. return context.WithValue(ctx, idSessionKey, id)
  27. }
  28. // IDFromContext returns ID in this context, or 0 if not contained.
  29. func IDFromContext(ctx context.Context) ID {
  30. if id, ok := ctx.Value(idSessionKey).(ID); ok {
  31. return id
  32. }
  33. return 0
  34. }
  35. func ContextWithInbound(ctx context.Context, inbound *Inbound) context.Context {
  36. return context.WithValue(ctx, inboundSessionKey, inbound)
  37. }
  38. func InboundFromContext(ctx context.Context) *Inbound {
  39. if inbound, ok := ctx.Value(inboundSessionKey).(*Inbound); ok {
  40. return inbound
  41. }
  42. return nil
  43. }
  44. func ContextWithOutbounds(ctx context.Context, outbounds []*Outbound) context.Context {
  45. return context.WithValue(ctx, outboundSessionKey, outbounds)
  46. }
  47. func OutboundsFromContext(ctx context.Context) []*Outbound {
  48. if outbounds, ok := ctx.Value(outboundSessionKey).([]*Outbound); ok {
  49. return outbounds
  50. }
  51. return nil
  52. }
  53. func ContextWithContent(ctx context.Context, content *Content) context.Context {
  54. return context.WithValue(ctx, contentSessionKey, content)
  55. }
  56. func ContentFromContext(ctx context.Context) *Content {
  57. if content, ok := ctx.Value(contentSessionKey).(*Content); ok {
  58. return content
  59. }
  60. return nil
  61. }
  62. // ContextWithMuxPrefered returns a new context with the given bool
  63. func ContextWithMuxPrefered(ctx context.Context, forced bool) context.Context {
  64. return context.WithValue(ctx, muxPreferedSessionKey, forced)
  65. }
  66. // MuxPreferedFromContext returns value in this context, or false if not contained.
  67. func MuxPreferedFromContext(ctx context.Context) bool {
  68. if val, ok := ctx.Value(muxPreferedSessionKey).(bool); ok {
  69. return val
  70. }
  71. return false
  72. }
  73. // ContextWithSockopt returns a new context with Socket configs included
  74. func ContextWithSockopt(ctx context.Context, s *Sockopt) context.Context {
  75. return context.WithValue(ctx, sockoptSessionKey, s)
  76. }
  77. // SockoptFromContext returns Socket configs in this context, or nil if not contained.
  78. func SockoptFromContext(ctx context.Context) *Sockopt {
  79. if sockopt, ok := ctx.Value(sockoptSessionKey).(*Sockopt); ok {
  80. return sockopt
  81. }
  82. return nil
  83. }
  84. func GetForcedOutboundTagFromContext(ctx context.Context) string {
  85. if ContentFromContext(ctx) == nil {
  86. return ""
  87. }
  88. return ContentFromContext(ctx).Attribute("forcedOutboundTag")
  89. }
  90. func SetForcedOutboundTagToContext(ctx context.Context, tag string) context.Context {
  91. if contentFromContext := ContentFromContext(ctx); contentFromContext == nil {
  92. ctx = ContextWithContent(ctx, &Content{})
  93. }
  94. ContentFromContext(ctx).SetAttribute("forcedOutboundTag", tag)
  95. return ctx
  96. }
  97. type TrackedRequestErrorFeedback interface {
  98. SubmitError(err error)
  99. }
  100. func SubmitOutboundErrorToOriginator(ctx context.Context, err error) {
  101. if errorTracker := ctx.Value(trackedConnectionErrorKey); errorTracker != nil {
  102. errorTracker := errorTracker.(TrackedRequestErrorFeedback)
  103. errorTracker.SubmitError(err)
  104. }
  105. }
  106. func TrackedConnectionError(ctx context.Context, tracker TrackedRequestErrorFeedback) context.Context {
  107. return context.WithValue(ctx, trackedConnectionErrorKey, tracker)
  108. }
  109. func ContextWithDispatcher(ctx context.Context, dispatcher routing.Dispatcher) context.Context {
  110. return context.WithValue(ctx, dispatcherKey, dispatcher)
  111. }
  112. func DispatcherFromContext(ctx context.Context) routing.Dispatcher {
  113. if dispatcher, ok := ctx.Value(dispatcherKey).(routing.Dispatcher); ok {
  114. return dispatcher
  115. }
  116. return nil
  117. }
  118. func ContextWithTimeoutOnly(ctx context.Context, only bool) context.Context {
  119. return context.WithValue(ctx, timeoutOnlyKey, only)
  120. }
  121. func TimeoutOnlyFromContext(ctx context.Context) bool {
  122. if val, ok := ctx.Value(timeoutOnlyKey).(bool); ok {
  123. return val
  124. }
  125. return false
  126. }
  127. func ContextWithAllowedNetwork(ctx context.Context, network net.Network) context.Context {
  128. return context.WithValue(ctx, allowedNetworkKey, network)
  129. }
  130. func AllowedNetworkFromContext(ctx context.Context) net.Network {
  131. if val, ok := ctx.Value(allowedNetworkKey).(net.Network); ok {
  132. return val
  133. }
  134. return net.Network_Unknown
  135. }