From 76f32869e224d101befdcebadaa2fe6e5d46f6fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 24 Sep 2023 12:00:00 +0800 Subject: [PATCH] Fix shadow-tls user context --- inbound/shadowtls.go | 13 ++++++++++++- inbound/tuic.go | 1 - 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/inbound/shadowtls.go b/inbound/shadowtls.go index 59b7c107..358564f2 100644 --- a/inbound/shadowtls.go +++ b/inbound/shadowtls.go @@ -11,6 +11,7 @@ import ( "github.com/sagernet/sing-box/option" "github.com/sagernet/sing-shadowtls" "github.com/sagernet/sing/common" + "github.com/sagernet/sing/common/auth" N "github.com/sagernet/sing/common/network" ) @@ -66,7 +67,7 @@ func NewShadowTLS(ctx context.Context, router adapter.Router, logger log.Context }, HandshakeForServerName: handshakeForServerName, StrictMode: options.StrictMode, - Handler: inbound.upstreamContextHandler(), + Handler: adapter.NewUpstreamContextHandler(inbound.newConnection, nil, inbound), Logger: logger, }) if err != nil { @@ -80,3 +81,13 @@ func NewShadowTLS(ctx context.Context, router adapter.Router, logger log.Context func (h *ShadowTLS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { return h.service.NewConnection(adapter.WithContext(log.ContextWithNewID(ctx), &metadata), conn, adapter.UpstreamMetadata(metadata)) } + +func (h *ShadowTLS) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { + if userName, _ := auth.UserFromContext[string](ctx); userName != "" { + metadata.User = userName + h.logger.InfoContext(ctx, "[", userName, "] inbound connection to ", metadata.Destination) + } else { + h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination) + } + return h.router.RouteConnection(ctx, conn, metadata) +} diff --git a/inbound/tuic.go b/inbound/tuic.go index 9b5c4ef3..98f9115f 100644 --- a/inbound/tuic.go +++ b/inbound/tuic.go @@ -84,7 +84,6 @@ func NewTUIC(ctx context.Context, router adapter.Router, logger log.ContextLogge func (h *TUIC) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { ctx = log.ContextWithNewID(ctx) - h.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination) metadata = h.createMetadata(conn, metadata) metadata.User, _ = auth.UserFromContext[string](ctx) return h.router.RouteConnection(ctx, conn, metadata)