diff --git a/outbound/builder.go b/outbound/builder.go index 3fc68dfe..6795e127 100644 --- a/outbound/builder.go +++ b/outbound/builder.go @@ -11,6 +11,11 @@ import ( ) func New(ctx context.Context, router adapter.Router, logger log.ContextLogger, options option.Outbound) (adapter.Outbound, error) { + var metadata *adapter.InboundContext + if options.Tag != "" { + ctx, metadata = adapter.AppendContext(ctx) + metadata.Outbound = options.Tag + } if options.Type == "" { return nil, E.New("missing outbound type") } diff --git a/outbound/shadowsocks.go b/outbound/shadowsocks.go index 0ad6c4c2..0741eb80 100644 --- a/outbound/shadowsocks.go +++ b/outbound/shadowsocks.go @@ -136,6 +136,9 @@ 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) + metadata.Outbound = h.tag + metadata.Destination = destination switch N.NetworkName(network) { case N.NetworkTCP: var outConn net.Conn @@ -161,6 +164,9 @@ 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) + metadata.Outbound = h.tag + metadata.Destination = destination outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr) if err != nil { return nil, err diff --git a/outbound/shadowsocksr.go b/outbound/shadowsocksr.go index b85c0fed..f2a172fb 100644 --- a/outbound/shadowsocksr.go +++ b/outbound/shadowsocksr.go @@ -99,6 +99,9 @@ func NewShadowsocksR(ctx context.Context, router adapter.Router, logger log.Cont } func (h *ShadowsocksR) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { + ctx, metadata := adapter.AppendContext(ctx) + metadata.Outbound = h.tag + metadata.Destination = destination switch network { case N.NetworkTCP: h.logger.InfoContext(ctx, "outbound connection to ", destination) @@ -131,6 +134,9 @@ func (h *ShadowsocksR) DialContext(ctx context.Context, network string, destinat } func (h *ShadowsocksR) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { + ctx, metadata := adapter.AppendContext(ctx) + metadata.Outbound = h.tag + metadata.Destination = destination h.logger.InfoContext(ctx, "outbound packet connection to ", destination) outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr) if err != nil { diff --git a/outbound/shadowtls.go b/outbound/shadowtls.go index 7e06f166..6847d2e6 100644 --- a/outbound/shadowtls.go +++ b/outbound/shadowtls.go @@ -86,23 +86,26 @@ func NewShadowTLS(ctx context.Context, router adapter.Router, logger log.Context return outbound, nil } -func (s *ShadowTLS) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { +func (h *ShadowTLS) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) { + ctx, metadata := adapter.AppendContext(ctx) + metadata.Outbound = h.tag + metadata.Destination = destination switch N.NetworkName(network) { case N.NetworkTCP: - return s.client.DialContext(ctx) + return h.client.DialContext(ctx) default: return nil, os.ErrInvalid } } -func (s *ShadowTLS) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { +func (h *ShadowTLS) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) { return nil, os.ErrInvalid } -func (s *ShadowTLS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { - return NewConnection(ctx, s, conn, metadata) +func (h *ShadowTLS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error { + return NewConnection(ctx, h, conn, metadata) } -func (s *ShadowTLS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error { +func (h *ShadowTLS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error { return os.ErrInvalid }