context.go 4.3 KB

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