2018-09-18 17:09:54 -04:00
|
|
|
package session
|
|
|
|
|
2021-03-06 09:33:20 -05:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
2018-09-18 17:09:54 -04:00
|
|
|
|
|
|
|
type sessionKey int
|
|
|
|
|
|
|
|
const (
|
|
|
|
idSessionKey sessionKey = iota
|
|
|
|
inboundSessionKey
|
|
|
|
outboundSessionKey
|
2019-02-22 10:58:16 -05:00
|
|
|
contentSessionKey
|
2019-11-26 00:30:44 -05:00
|
|
|
muxPreferedSessionKey
|
2020-07-31 14:04:06 -04:00
|
|
|
sockoptSessionKey
|
2021-03-07 07:14:58 -05:00
|
|
|
trackedConnectionErrorKey
|
2018-09-18 17:09:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ContextWithID returns a new context with the given ID.
|
|
|
|
func ContextWithID(ctx context.Context, id ID) context.Context {
|
|
|
|
return context.WithValue(ctx, idSessionKey, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IDFromContext returns ID in this context, or 0 if not contained.
|
|
|
|
func IDFromContext(ctx context.Context) ID {
|
|
|
|
if id, ok := ctx.Value(idSessionKey).(ID); ok {
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContextWithInbound(ctx context.Context, inbound *Inbound) context.Context {
|
|
|
|
return context.WithValue(ctx, inboundSessionKey, inbound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func InboundFromContext(ctx context.Context) *Inbound {
|
|
|
|
if inbound, ok := ctx.Value(inboundSessionKey).(*Inbound); ok {
|
|
|
|
return inbound
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContextWithOutbound(ctx context.Context, outbound *Outbound) context.Context {
|
|
|
|
return context.WithValue(ctx, outboundSessionKey, outbound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func OutboundFromContext(ctx context.Context) *Outbound {
|
|
|
|
if outbound, ok := ctx.Value(outboundSessionKey).(*Outbound); ok {
|
|
|
|
return outbound
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-22 10:58:16 -05:00
|
|
|
|
|
|
|
func ContextWithContent(ctx context.Context, content *Content) context.Context {
|
|
|
|
return context.WithValue(ctx, contentSessionKey, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContentFromContext(ctx context.Context) *Content {
|
|
|
|
if content, ok := ctx.Value(contentSessionKey).(*Content); ok {
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-29 11:43:30 -04:00
|
|
|
|
|
|
|
// ContextWithMuxPrefered returns a new context with the given bool
|
|
|
|
func ContextWithMuxPrefered(ctx context.Context, forced bool) context.Context {
|
2019-11-26 00:30:44 -05:00
|
|
|
return context.WithValue(ctx, muxPreferedSessionKey, forced)
|
2019-06-29 11:43:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// MuxPreferedFromContext returns value in this context, or false if not contained.
|
|
|
|
func MuxPreferedFromContext(ctx context.Context) bool {
|
2019-11-26 00:30:44 -05:00
|
|
|
if val, ok := ctx.Value(muxPreferedSessionKey).(bool); ok {
|
2019-06-29 11:43:30 -04:00
|
|
|
return val
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2020-07-31 14:04:06 -04:00
|
|
|
|
|
|
|
// ContextWithSockopt returns a new context with Socket configs included
|
|
|
|
func ContextWithSockopt(ctx context.Context, s *Sockopt) context.Context {
|
|
|
|
return context.WithValue(ctx, sockoptSessionKey, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SockoptFromContext returns Socket configs in this context, or nil if not contained.
|
|
|
|
func SockoptFromContext(ctx context.Context) *Sockopt {
|
|
|
|
if sockopt, ok := ctx.Value(sockoptSessionKey).(*Sockopt); ok {
|
|
|
|
return sockopt
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-28 13:47:31 -05:00
|
|
|
|
|
|
|
func GetTransportLayerProxyTagFromContext(ctx context.Context) string {
|
2021-02-28 15:42:32 -05:00
|
|
|
if ContentFromContext(ctx) == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2021-02-28 13:47:31 -05:00
|
|
|
return ContentFromContext(ctx).Attribute("transportLayerOutgoingTag")
|
|
|
|
}
|
|
|
|
|
2021-03-06 09:33:20 -05:00
|
|
|
func SetTransportLayerProxyTagToContext(ctx context.Context, tag string) context.Context {
|
|
|
|
if contentFromContext := ContentFromContext(ctx); contentFromContext == nil {
|
|
|
|
ctx = ContextWithContent(ctx, &Content{})
|
|
|
|
}
|
2021-02-28 13:47:31 -05:00
|
|
|
ContentFromContext(ctx).SetAttribute("transportLayerOutgoingTag", tag)
|
2021-03-06 09:33:20 -05:00
|
|
|
return ctx
|
2021-02-28 13:47:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetForcedOutboundTagFromContext(ctx context.Context) string {
|
2021-02-28 15:42:32 -05:00
|
|
|
if ContentFromContext(ctx) == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2021-02-28 13:47:31 -05:00
|
|
|
return ContentFromContext(ctx).Attribute("forcedOutboundTag")
|
|
|
|
}
|
|
|
|
|
2021-03-06 09:33:20 -05:00
|
|
|
func SetForcedOutboundTagToContext(ctx context.Context, tag string) context.Context {
|
|
|
|
if contentFromContext := ContentFromContext(ctx); contentFromContext == nil {
|
|
|
|
ctx = ContextWithContent(ctx, &Content{})
|
|
|
|
}
|
2021-02-28 13:47:31 -05:00
|
|
|
ContentFromContext(ctx).SetAttribute("forcedOutboundTag", tag)
|
2021-03-06 09:33:20 -05:00
|
|
|
return ctx
|
2021-02-28 13:47:31 -05:00
|
|
|
}
|
2021-03-07 07:14:58 -05:00
|
|
|
|
|
|
|
type TrackedRequestErrorFeedback interface {
|
|
|
|
SubmitError(err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SubmitOutboundErrorToOriginator(ctx context.Context, err error) {
|
|
|
|
if errorTracker := ctx.Value(trackedConnectionErrorKey); errorTracker != nil {
|
|
|
|
errorTracker := errorTracker.(TrackedRequestErrorFeedback)
|
|
|
|
errorTracker.SubmitError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TrackedConnectionError(ctx context.Context, tracker TrackedRequestErrorFeedback) context.Context {
|
|
|
|
return context.WithValue(ctx, trackedConnectionErrorKey, tracker)
|
|
|
|
}
|