fix(io,tunnel): nil deref in RecorderObjectFromContext, CloseRead checked Writer, SetReadDeadline no unwrap

- entrypoint.go: guard ictx.RecorderObjectFromContext with nil checks on
  ClientID and Redirect assignments
- io.go: fix CloseRead checking Writer instead of Reader, add Reader
  fallback to CloseWrite, unwrap *readWriter/*readWriteCloser in
  SetReadDeadline to reach underlying net.Conn
- docs: add package doc and exported symbol comments to internal/ctx
  and internal/io
This commit is contained in:
ginuerzh
2026-05-24 17:51:16 +08:00
parent 501051ce4d
commit b991baaf72
3 changed files with 77 additions and 4 deletions
+12
View File
@@ -1,3 +1,5 @@
// Package ctx provides typed context value helpers for passing per-request
// data (buffer, logger, metadata, recorder objects) through the handler chain.
package ctx
import (
@@ -11,10 +13,14 @@ import (
type bufferKey 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
@@ -22,10 +28,12 @@ func BufferFromContext(ctx context.Context) *bytes.Buffer {
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
@@ -33,10 +41,12 @@ func LoggerFromContext(ctx context.Context) logger.Logger {
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
@@ -44,10 +54,12 @@ func MetadataFromContext(ctx context.Context) metadata.Metadata {
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