context.go 3.8 KB

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