diff --git a/internal/util/forwarder/sniffer.go b/internal/util/forwarder/sniffer.go index ef06265f..94592731 100644 --- a/internal/util/forwarder/sniffer.go +++ b/internal/util/forwarder/sniffer.go @@ -160,8 +160,9 @@ func (h *Sniffer) HandleHTTP(ctx context.Context, conn net.Conn, opts ...HandleO } if clientIP := xhttp.GetClientIP(req); clientIP != nil { - ro.ClientIP = clientIP.String() - ctx = xctx.ContextWithSrcAddr(ctx, &net.TCPAddr{IP: clientIP}) + clientAddr := &net.TCPAddr{IP: clientIP} + ro.ClientAddr = clientAddr.String() + ctx = xctx.ContextWithSrcAddr(ctx, clientAddr) } // http/2 @@ -250,12 +251,15 @@ func (h *Sniffer) dial(ctx context.Context, conn net.Conn, req *http.Request, ho } } - node = &chain.Node{ - Addr: host, - } + node = &chain.Node{} if ho.Hop != nil { + var clientIP net.IP + if clientAddr, _ := net.ResolveTCPAddr("tcp", ro.ClientAddr); clientAddr != nil { + clientIP = clientAddr.IP + } + node = ho.Hop.Select(ctx, - hop.ClientIPSelectOption(net.ParseIP(ro.ClientIP)), + hop.ClientIPSelectOption(clientIP), hop.ProtocolSelectOption(sniffing.ProtoHTTP), hop.HostSelectOption(host), hop.MethodSelectOption(req.Method), @@ -271,6 +275,12 @@ func (h *Sniffer) dial(ctx context.Context, conn net.Conn, req *http.Request, ho res.Write(conn) return nil, nil, errors.New("node not available") } + if node.Addr == "" { + node = &chain.Node{ + Name: node.Name, + Addr: host, + } + } ro.Host = node.Addr ho.Log = ho.Log.WithFields(map[string]any{ @@ -874,16 +884,17 @@ func (h *Sniffer) dialTLS(ctx context.Context, host string, ho *HandleOptions) ( return } - if host != "" { - node = &chain.Node{ - Addr: host, - } - } + node = &chain.Node{} ro := ho.RecorderObject if ho.Hop != nil { + var clientIP net.IP + if clientAddr, _ := net.ResolveTCPAddr("tcp", ro.ClientAddr); clientAddr != nil { + clientIP = clientAddr.IP + } + node = ho.Hop.Select(ctx, - hop.ClientIPSelectOption(net.ParseIP(ro.ClientIP)), + hop.ClientIPSelectOption(clientIP), hop.HostSelectOption(host), hop.ProtocolSelectOption(sniffing.ProtoTLS), ) @@ -892,6 +903,12 @@ func (h *Sniffer) dialTLS(ctx context.Context, host string, ho *HandleOptions) ( err = errors.New("node not available") return } + if node.Addr == "" { + node = &chain.Node{ + Name: node.Name, + Addr: host, + } + } addr := node.Addr if opts := node.Options(); opts != nil {