fix: for Target isActive use net.Dial instead (#17251)

This commit is contained in:
jiuker 2023-05-26 00:24:11 +08:00 committed by GitHub
parent 9b5829c16e
commit 443250d135
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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
}