github.com/metacubex/mihomo@v1.18.5/listener/sing/context.go (about)

     1  package sing
     2  
     3  import (
     4  	"context"
     5  	"golang.org/x/exp/slices"
     6  
     7  	"github.com/metacubex/mihomo/adapter/inbound"
     8  
     9  	"github.com/sagernet/sing/common/auth"
    10  )
    11  
    12  type contextKey string
    13  
    14  var ctxKeyAdditions = contextKey("Additions")
    15  
    16  func WithAdditions(ctx context.Context, additions ...inbound.Addition) context.Context {
    17  	return context.WithValue(ctx, ctxKeyAdditions, additions)
    18  }
    19  
    20  func getAdditions(ctx context.Context) (additions []inbound.Addition) {
    21  	if v := ctx.Value(ctxKeyAdditions); v != nil {
    22  		if a, ok := v.([]inbound.Addition); ok {
    23  			additions = a
    24  		}
    25  	}
    26  	if user, ok := auth.UserFromContext[string](ctx); ok {
    27  		additions = slices.Clone(additions)
    28  		additions = append(additions, inbound.WithInUser(user))
    29  	}
    30  	return
    31  }