From 1a6047a61b41e7184a81f3292462ed38b79312f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 27 Oct 2024 07:45:15 +0800 Subject: [PATCH] Fix metadata context --- adapter/inbound.go | 9 --------- outbound/builder.go | 5 ----- outbound/direct.go | 4 ++-- outbound/http.go | 2 +- outbound/shadowsocks.go | 8 ++++---- outbound/shadowtls.go | 2 +- outbound/socks.go | 4 ++-- outbound/trojan.go | 2 +- outbound/vless.go | 4 ++-- outbound/vmess.go | 4 ++-- 10 files changed, 15 insertions(+), 29 deletions(-) diff --git a/adapter/inbound.go b/adapter/inbound.go index ffc4c564..82909d01 100644 --- a/adapter/inbound.go +++ b/adapter/inbound.go @@ -91,15 +91,6 @@ func ContextFrom(ctx context.Context) *InboundContext { return metadata.(*InboundContext) } -func AppendContext(ctx context.Context) (context.Context, *InboundContext) { - metadata := ContextFrom(ctx) - if metadata != nil { - return ctx, metadata - } - metadata = new(InboundContext) - return WithContext(ctx, metadata), metadata -} - func ExtendContext(ctx context.Context) (context.Context, *InboundContext) { var newMetadata InboundContext if metadata := ContextFrom(ctx); metadata != nil { diff --git a/outbound/builder.go b/outbound/builder.go index e4d6a80e..b99b49b7 100644 --- a/outbound/builder.go +++ b/outbound/builder.go @@ -11,11 +11,6 @@ import ( ) func New(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.Outbound) (adapter.Outbound, error) { - var metadata *adapter.InboundContext - if tag != "" { - ctx, metadata = adapter.AppendContext(ctx) - metadata.Outbound = tag - } if options.Type == "" { return nil, E.New("missing outbound type") } diff --git a/outbound/direct.go b/outbound/direct.go index 11f650e4..c873941c 100644 --- a/outbound/direct.go +++ b/outbound/direct.go @@ -70,7 +70,7 @@ func NewDirect(router adapter.Router, logger log.ContextLogger, tag string, opti } func (h *Direct) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination switch h.overrideOption { @@ -98,7 +98,7 @@ func (h *Direct) DialContext(ctx context.Context, network string, destination M. } func (h *Direct) DialParallel(ctx context.Context, network string, destination M.Socksaddr, destinationAddresses []netip.Addr) (net.Conn, error) { - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination switch h.overrideOption { diff --git a/outbound/http.go b/outbound/http.go index cfc03216..cbcbce07 100644 --- a/outbound/http.go +++ b/outbound/http.go @@ -54,7 +54,7 @@ func NewHTTP(ctx context.Context, router adapter.Router, logger log.ContextLogge } func (h *HTTP) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination h.logger.InfoContext(ctx, "outbound connection to ", destination) diff --git a/outbound/shadowsocks.go b/outbound/shadowsocks.go index addb6e57..9f8c1cbd 100644 --- a/outbound/shadowsocks.go +++ b/outbound/shadowsocks.go @@ -79,7 +79,7 @@ func NewShadowsocks(ctx context.Context, router adapter.Router, logger log.Conte } func (h *Shadowsocks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination if h.multiplexDialer == nil { @@ -107,7 +107,7 @@ func (h *Shadowsocks) DialContext(ctx context.Context, network string, destinati } func (h *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination if h.multiplexDialer == nil { @@ -149,7 +149,7 @@ var _ N.Dialer = (*shadowsocksDialer)(nil) type shadowsocksDialer Shadowsocks func (h *shadowsocksDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination switch N.NetworkName(network) { @@ -177,7 +177,7 @@ func (h *shadowsocksDialer) DialContext(ctx context.Context, network string, des } func (h *shadowsocksDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr) diff --git a/outbound/shadowtls.go b/outbound/shadowtls.go index 4427301c..38de8c0b 100644 --- a/outbound/shadowtls.go +++ b/outbound/shadowtls.go @@ -92,7 +92,7 @@ func NewShadowTLS(ctx context.Context, router adapter.Router, logger log.Context } func (h *ShadowTLS) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination switch N.NetworkName(network) { diff --git a/outbound/socks.go b/outbound/socks.go index 063f7b95..f4757467 100644 --- a/outbound/socks.go +++ b/outbound/socks.go @@ -65,7 +65,7 @@ func NewSocks(router adapter.Router, logger log.ContextLogger, tag string, optio } func (h *Socks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination switch N.NetworkName(network) { @@ -91,7 +91,7 @@ func (h *Socks) DialContext(ctx context.Context, network string, destination M.S } func (h *Socks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination if h.uotClient != nil { diff --git a/outbound/trojan.go b/outbound/trojan.go index 52d72757..bde251d0 100644 --- a/outbound/trojan.go +++ b/outbound/trojan.go @@ -124,7 +124,7 @@ func (h *Trojan) Close() error { type trojanDialer Trojan func (h *trojanDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination var conn net.Conn diff --git a/outbound/vless.go b/outbound/vless.go index 66080eaf..a81678f0 100644 --- a/outbound/vless.go +++ b/outbound/vless.go @@ -143,7 +143,7 @@ func (h *VLESS) Close() error { type vlessDialer VLESS func (h *vlessDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination var conn net.Conn @@ -186,7 +186,7 @@ func (h *vlessDialer) DialContext(ctx context.Context, network string, destinati func (h *vlessDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { h.logger.InfoContext(ctx, "outbound packet connection to ", destination) - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination var conn net.Conn diff --git a/outbound/vmess.go b/outbound/vmess.go index c7d88b90..3149729c 100644 --- a/outbound/vmess.go +++ b/outbound/vmess.go @@ -157,7 +157,7 @@ func (h *VMess) NewPacketConnection(ctx context.Context, conn N.PacketConn, meta type vmessDialer VMess func (h *vmessDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination var conn net.Conn @@ -185,7 +185,7 @@ func (h *vmessDialer) DialContext(ctx context.Context, network string, destinati } func (h *vmessDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { - ctx, metadata := adapter.AppendContext(ctx) + ctx, metadata := adapter.ExtendContext(ctx) metadata.Outbound = h.tag metadata.Destination = destination var conn net.Conn