context.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package session
  2. import (
  3. "context"
  4. )
  5. type sessionKey int
  6. const (
  7. idSessionKey sessionKey = iota
  8. inboundSessionKey
  9. outboundSessionKey
  10. contentSessionKey
  11. muxPreferedSessionKey
  12. sockoptSessionKey
  13. trackedConnectionErrorKey
  14. handlerSessionKey
  15. environmentKey
  16. )
  17. // ContextWithID returns a new context with the given ID.
  18. func ContextWithID(ctx context.Context, id ID) context.Context {
  19. return context.WithValue(ctx, idSessionKey, id)
  20. }
  21. // IDFromContext returns ID in this context, or 0 if not contained.
  22. func IDFromContext(ctx context.Context) ID {
  23. if id, ok := ctx.Value(idSessionKey).(ID); ok {
  24. return id
  25. }
  26. return 0
  27. }
  28. func ContextWithInbound(ctx context.Context, inbound *Inbound) context.Context {
  29. return context.WithValue(ctx, inboundSessionKey, inbound)
  30. }
  31. func InboundFromContext(ctx context.Context) *Inbound {
  32. if inbound, ok := ctx.Value(inboundSessionKey).(*Inbound); ok {
  33. return inbound
  34. }
  35. return nil
  36. }
  37. func ContextWithOutbound(ctx context.Context, outbound *Outbound) context.Context {
  38. return context.WithValue(ctx, outboundSessionKey, outbound)
  39. }
  40. func OutboundFromContext(ctx context.Context) *Outbound {
  41. if outbound, ok := ctx.Value(outboundSessionKey).(*Outbound); ok {
  42. return outbound
  43. }
  44. return nil
  45. }
  46. func ContextWithContent(ctx context.Context, content *Content) context.Context {
  47. return context.WithValue(ctx, contentSessionKey, content)
  48. }
  49. func ContentFromContext(ctx context.Context) *Content {
  50. if content, ok := ctx.Value(contentSessionKey).(*Content); ok {
  51. return content
  52. }
  53. return nil
  54. }
  55. // ContextWithMuxPrefered returns a new context with the given bool
  56. func ContextWithMuxPrefered(ctx context.Context, forced bool) context.Context {
  57. return context.WithValue(ctx, muxPreferedSessionKey, forced)
  58. }
  59. // MuxPreferedFromContext returns value in this context, or false if not contained.
  60. func MuxPreferedFromContext(ctx context.Context) bool {
  61. if val, ok := ctx.Value(muxPreferedSessionKey).(bool); ok {
  62. return val
  63. }
  64. return false
  65. }
  66. // ContextWithSockopt returns a new context with Socket configs included
  67. func ContextWithSockopt(ctx context.Context, s *Sockopt) context.Context {
  68. return context.WithValue(ctx, sockoptSessionKey, s)
  69. }
  70. // SockoptFromContext returns Socket configs in this context, or nil if not contained.
  71. func SockoptFromContext(ctx context.Context) *Sockopt {
  72. if sockopt, ok := ctx.Value(sockoptSessionKey).(*Sockopt); ok {
  73. return sockopt
  74. }
  75. return nil
  76. }
  77. func GetTransportLayerProxyTagFromContext(ctx context.Context) string {
  78. if ContentFromContext(ctx) == nil {
  79. return ""
  80. }
  81. return ContentFromContext(ctx).Attribute("transportLayerOutgoingTag")
  82. }
  83. func SetTransportLayerProxyTagToContext(ctx context.Context, tag string) context.Context {
  84. if contentFromContext := ContentFromContext(ctx); contentFromContext == nil {
  85. ctx = ContextWithContent(ctx, &Content{})
  86. }
  87. ContentFromContext(ctx).SetAttribute("transportLayerOutgoingTag", tag)
  88. return ctx
  89. }
  90. func GetForcedOutboundTagFromContext(ctx context.Context) string {
  91. if ContentFromContext(ctx) == nil {
  92. return ""
  93. }
  94. return ContentFromContext(ctx).Attribute("forcedOutboundTag")
  95. }
  96. func SetForcedOutboundTagToContext(ctx context.Context, tag string) context.Context {
  97. if contentFromContext := ContentFromContext(ctx); contentFromContext == nil {
  98. ctx = ContextWithContent(ctx, &Content{})
  99. }
  100. ContentFromContext(ctx).SetAttribute("forcedOutboundTag", tag)
  101. return ctx
  102. }
  103. type TrackedRequestErrorFeedback interface {
  104. SubmitError(err error)
  105. }
  106. func SubmitOutboundErrorToOriginator(ctx context.Context, err error) {
  107. if errorTracker := ctx.Value(trackedConnectionErrorKey); errorTracker != nil {
  108. errorTracker := errorTracker.(TrackedRequestErrorFeedback)
  109. errorTracker.SubmitError(err)
  110. }
  111. }
  112. func TrackedConnectionError(ctx context.Context, tracker TrackedRequestErrorFeedback) context.Context {
  113. return context.WithValue(ctx, trackedConnectionErrorKey, tracker)
  114. }
  115. func ContextWithEnvironment(ctx context.Context, environment interface{}) context.Context {
  116. return context.WithValue(ctx, environmentKey, environment)
  117. }
  118. func EnvironmentFromContext(ctx context.Context) interface{} {
  119. if environment, ok := ctx.Value(environmentKey).(interface{}); ok {
  120. return environment
  121. }
  122. return nil
  123. }