Fix context in v2ray http transports

This commit is contained in:
世界 2025-03-14 17:07:17 +08:00
parent 3ae036e997
commit 68ce9577c6
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
5 changed files with 20 additions and 4 deletions

View file

@ -20,12 +20,16 @@ type ID struct {
} }
func ContextWithNewID(ctx context.Context) context.Context { func ContextWithNewID(ctx context.Context) context.Context {
return context.WithValue(ctx, (*idKey)(nil), ID{ return ContextWithID(ctx, ID{
ID: rand.Uint32(), ID: rand.Uint32(),
CreatedAt: time.Now(), CreatedAt: time.Now(),
}) })
} }
func ContextWithID(ctx context.Context, id ID) context.Context {
return context.WithValue(ctx, (*idKey)(nil), id)
}
func IDFromContext(ctx context.Context) (ID, bool) { func IDFromContext(ctx context.Context) (ID, bool) {
id, loaded := ctx.Value((*idKey)(nil)).(ID) id, loaded := ctx.Value((*idKey)(nil)).(ID)
return id, loaded return id, loaded

View file

@ -2,6 +2,7 @@ package v2rayhttp
import ( import (
std_bufio "bufio" std_bufio "bufio"
"context"
"io" "io"
"net" "net"
"net/http" "net/http"
@ -10,6 +11,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/baderror" "github.com/sagernet/sing/common/baderror"
"github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/buf"
@ -255,3 +257,11 @@ func (w *HTTP2ConnWrapper) Close() error {
func (w *HTTP2ConnWrapper) Upstream() any { func (w *HTTP2ConnWrapper) Upstream() any {
return w.ExtendedConn return w.ExtendedConn
} }
func DupContext(ctx context.Context) context.Context {
id, loaded := log.IDFromContext(ctx)
if !loaded {
return context.Background()
}
return log.ContextWithID(context.Background(), id)
}

View file

@ -132,7 +132,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if requestBody != nil { if requestBody != nil {
conn = bufio.NewCachedConn(conn, requestBody) conn = bufio.NewCachedConn(conn, requestBody)
} }
s.handler.NewConnectionEx(request.Context(), conn, source, M.Socksaddr{}, nil) s.handler.NewConnectionEx(DupContext(request.Context()), conn, source, M.Socksaddr{}, nil)
} else { } else {
writer.WriteHeader(http.StatusOK) writer.WriteHeader(http.StatusOK)
done := make(chan struct{}) done := make(chan struct{})

View file

@ -12,6 +12,7 @@ import (
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/transport/v2rayhttp"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
E "github.com/sagernet/sing/common/exceptions" E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger" "github.com/sagernet/sing/common/logger"
@ -110,7 +111,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
s.invalidRequest(writer, request, http.StatusInternalServerError, E.Cause(err, "hijack failed")) s.invalidRequest(writer, request, http.StatusInternalServerError, E.Cause(err, "hijack failed"))
return return
} }
s.handler.NewConnectionEx(request.Context(), conn, sHttp.SourceAddress(request), M.Socksaddr{}, nil) s.handler.NewConnectionEx(v2rayhttp.DupContext(request.Context()), conn, sHttp.SourceAddress(request), M.Socksaddr{}, nil)
} }
func (s *Server) invalidRequest(writer http.ResponseWriter, request *http.Request, statusCode int, err error) { func (s *Server) invalidRequest(writer http.ResponseWriter, request *http.Request, statusCode int, err error) {

View file

@ -13,6 +13,7 @@ import (
C "github.com/sagernet/sing-box/constant" C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option" "github.com/sagernet/sing-box/option"
"github.com/sagernet/sing-box/transport/v2rayhttp"
"github.com/sagernet/sing/common" "github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf" "github.com/sagernet/sing/common/buf"
"github.com/sagernet/sing/common/bufio" "github.com/sagernet/sing/common/bufio"
@ -114,7 +115,7 @@ func (s *Server) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if len(earlyData) > 0 { if len(earlyData) > 0 {
conn = bufio.NewCachedConn(conn, buf.As(earlyData)) conn = bufio.NewCachedConn(conn, buf.As(earlyData))
} }
s.handler.NewConnectionEx(request.Context(), conn, source, M.Socksaddr{}, nil) s.handler.NewConnectionEx(v2rayhttp.DupContext(request.Context()), conn, source, M.Socksaddr{}, nil)
} }
func (s *Server) invalidRequest(writer http.ResponseWriter, request *http.Request, statusCode int, err error) { func (s *Server) invalidRequest(writer http.ResponseWriter, request *http.Request, statusCode int, err error) {