context.go 3.3 KB

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