From 55b4515a0f55232e508b2a5a96b8d1500ce74875 Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Tue, 5 Sep 2017 13:45:58 +0100 Subject: [PATCH] Fix http requests to actually use the resolved ip --- prober/http.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/prober/http.go b/prober/http.go index d035660..dcd81e0 100644 --- a/prober/http.go +++ b/prober/http.go @@ -156,15 +156,16 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr httpConfig.Method = "GET" } - request, err := http.NewRequest(httpConfig.Method, target, nil) - request.Host = targetURL.Host - request = request.WithContext(ctx) + // Replace the host field in the URL with the IP we resolved. + origHost := targetURL.Host if targetPort == "" { - targetURL.Host = ip.String() + targetURL.Host = "[" + ip.String() + "]" } else { targetURL.Host = net.JoinHostPort(ip.String(), targetPort) } - + request, err := http.NewRequest(httpConfig.Method, targetURL.String(), nil) + request.Host = origHost + request = request.WithContext(ctx) if err != nil { level.Error(logger).Log("msg", "Error creating request", "err", err) return @@ -182,7 +183,7 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr if httpConfig.Body != "" { request.Body = ioutil.NopCloser(strings.NewReader(httpConfig.Body)) } - level.Info(logger).Log("msg", "Making HTTP request", "url", request.URL.String()) + level.Info(logger).Log("msg", "Making HTTP request", "url", request.URL.String(), "host", request.Host) resp, err := client.Do(request) // Err won't be nil if redirects were turned off. See https://github.com/golang/go/issues/3795 if err != nil && resp == nil { -- 2.25.1