From 443250d1354a7022af76587ea55cbd82470ce1d8 Mon Sep 17 00:00:00 2001 From: jiuker <2818723467@qq.com> Date: Fri, 26 May 2023 00:24:11 +0800 Subject: [PATCH] fix: for Target isActive use net.Dial instead (#17251) --- internal/event/target/webhook.go | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/internal/event/target/webhook.go b/internal/event/target/webhook.go index 7383ddd29..d40a3114d 100644 --- a/internal/event/target/webhook.go +++ b/internal/event/target/webhook.go @@ -25,6 +25,7 @@ import ( "errors" "fmt" "io" + "net" "net/http" "net/url" "os" @@ -128,34 +129,14 @@ func (target *WebhookTarget) Store() event.TargetStore { } func (target *WebhookTarget) isActive() (bool, error) { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - - req, err := http.NewRequestWithContext(ctx, http.MethodHead, target.args.Endpoint.String(), nil) + conn, err := net.DialTimeout("tcp", target.args.Endpoint.Host, 5*time.Second) if err != nil { if xnet.IsNetworkOrHostDown(err, false) { return false, store.ErrNotConnected } return false, err } - tokens := strings.Fields(target.args.AuthToken) - switch len(tokens) { - case 2: - req.Header.Set("Authorization", target.args.AuthToken) - case 1: - req.Header.Set("Authorization", "Bearer "+target.args.AuthToken) - } - - resp, err := target.httpClient.Do(req) - if err != nil { - if xnet.IsNetworkOrHostDown(err, true) { - return false, store.ErrNotConnected - } - return false, err - } - io.Copy(io.Discard, resp.Body) - resp.Body.Close() - // No network failure i.e response from the target means its up + defer conn.Close() return true, nil }