github.com/v2fly/v2ray-core/v4@v4.45.2/common/session/context.go (about)

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