Files

93 lines
3.1 KiB
Go

// Package ctx provides typed context value helpers for passing per-request
// data (buffer, logger, metadata, recorder objects) through the handler chain.
package ctx
import (
"bytes"
"context"
"github.com/go-gost/core/logger"
"github.com/go-gost/core/metadata"
xrecorder "github.com/go-gost/x/recorder"
)
type bufferKey struct{}
type udpBindDeviceKey struct{}
// ContextWithBuffer returns a copy of ctx with the given buffer stored.
// The buffer is used by dialers and connectors to reuse a *bytes.Buffer
// for protocol handshake data (e.g. TLS ClientHello, HTTP requests).
func ContextWithBuffer(ctx context.Context, buffer *bytes.Buffer) context.Context {
return context.WithValue(ctx, bufferKey{}, buffer)
}
// BufferFromContext returns the *bytes.Buffer stored in ctx, or nil.
func BufferFromContext(ctx context.Context) *bytes.Buffer {
v, _ := ctx.Value(bufferKey{}).(*bytes.Buffer)
return v
}
// ContextWithUDPBindDevice marks an empty-address UDP relay dial as requiring
// an operating-system device bind in addition to its local-address bind.
func ContextWithUDPBindDevice(ctx context.Context) context.Context {
return context.WithValue(ctx, udpBindDeviceKey{}, true)
}
// UDPBindDeviceFromContext reports whether the UDP relay dial explicitly
// requested an operating-system device bind.
func UDPBindDeviceFromContext(ctx context.Context) bool {
v, _ := ctx.Value(udpBindDeviceKey{}).(bool)
return v
}
type loggerKey struct{}
// ContextWithLogger returns a copy of ctx with the given logger stored.
func ContextWithLogger(ctx context.Context, log logger.Logger) context.Context {
return context.WithValue(ctx, loggerKey{}, log)
}
// LoggerFromContext returns the logger stored in ctx, or nil.
func LoggerFromContext(ctx context.Context) logger.Logger {
v, _ := ctx.Value(loggerKey{}).(logger.Logger)
return v
}
type mdKey struct{}
// ContextWithMetadata returns a copy of ctx with the given metadata stored.
func ContextWithMetadata(ctx context.Context, md metadata.Metadata) context.Context {
return context.WithValue(ctx, mdKey{}, md)
}
// MetadataFromContext returns the metadata stored in ctx, or nil.
func MetadataFromContext(ctx context.Context) metadata.Metadata {
v, _ := ctx.Value(mdKey{}).(metadata.Metadata)
return v
}
type recorderObjectCtxKey struct{}
// ContextWithRecorderObject returns a copy of ctx with the given recorder object stored.
func ContextWithRecorderObject(ctx context.Context, ro *xrecorder.HandlerRecorderObject) context.Context {
return context.WithValue(ctx, recorderObjectCtxKey{}, ro)
}
// RecorderObjectFromContext returns the recorder object stored in ctx, or nil.
func RecorderObjectFromContext(ctx context.Context) *xrecorder.HandlerRecorderObject {
v, _ := ctx.Value(recorderObjectCtxKey{}).(*xrecorder.HandlerRecorderObject)
return v
}
type sessionCtxKey struct{}
func ContextWithSession(ctx context.Context, s *xrecorder.Session) context.Context {
return context.WithValue(ctx, sessionCtxKey{}, s)
}
func SessionFromContext(ctx context.Context) *xrecorder.Session {
v, _ := ctx.Value(sessionCtxKey{}).(*xrecorder.Session)
return v
}