2022-12-05 10:12:53 +08:00
|
|
|
package sing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-05-28 17:19:57 +08:00
|
|
|
"golang.org/x/exp/slices"
|
2022-12-05 10:12:53 +08:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
2023-05-28 17:19:57 +08:00
|
|
|
|
|
|
|
"github.com/sagernet/sing/common/auth"
|
2022-12-05 10:12:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type contextKey string
|
|
|
|
|
|
|
|
var ctxKeyAdditions = contextKey("Additions")
|
|
|
|
|
|
|
|
func WithAdditions(ctx context.Context, additions ...inbound.Addition) context.Context {
|
|
|
|
return context.WithValue(ctx, ctxKeyAdditions, additions)
|
|
|
|
}
|
|
|
|
|
2023-10-11 22:54:19 +08:00
|
|
|
func getAdditions(ctx context.Context) (additions []inbound.Addition) {
|
2022-12-05 10:12:53 +08:00
|
|
|
if v := ctx.Value(ctxKeyAdditions); v != nil {
|
|
|
|
if a, ok := v.([]inbound.Addition); ok {
|
2023-10-11 22:54:19 +08:00
|
|
|
additions = a
|
2022-12-05 10:12:53 +08:00
|
|
|
}
|
|
|
|
}
|
2023-05-28 17:19:57 +08:00
|
|
|
if user, ok := auth.UserFromContext[string](ctx); ok {
|
2023-10-11 22:54:19 +08:00
|
|
|
additions = slices.Clone(additions)
|
2023-05-28 17:19:57 +08:00
|
|
|
additions = append(additions, inbound.WithInUser(user))
|
|
|
|
}
|
2023-10-11 22:54:19 +08:00
|
|
|
return
|
2023-05-28 17:19:57 +08:00
|
|
|
}
|