context.go 3.8 KB

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