Clash.Meta/listener/sing/context.go
2023-11-03 21:58:21 +08:00

32 lines
727 B
Go

package sing
import (
"context"
"golang.org/x/exp/slices"
"github.com/metacubex/mihomo/adapter/inbound"
"github.com/sagernet/sing/common/auth"
)
type contextKey string
var ctxKeyAdditions = contextKey("Additions")
func WithAdditions(ctx context.Context, additions ...inbound.Addition) context.Context {
return context.WithValue(ctx, ctxKeyAdditions, additions)
}
func getAdditions(ctx context.Context) (additions []inbound.Addition) {
if v := ctx.Value(ctxKeyAdditions); v != nil {
if a, ok := v.([]inbound.Addition); ok {
additions = a
}
}
if user, ok := auth.UserFromContext[string](ctx); ok {
additions = slices.Clone(additions)
additions = append(additions, inbound.WithInUser(user))
}
return
}