fix: webhook notify endpoint with standard ports (#18016)

This commit is contained in:
Anis Eleuch 2023-09-14 20:10:44 -07:00 committed by GitHub
parent dc48cd841a
commit 419e5baf16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -104,6 +104,8 @@ type WebhookTarget struct {
loggerOnce logger.LogOnce
cancel context.CancelFunc
cancelCh <-chan struct{}
addr string // full address ip/dns with a port number, e.g. x.x.x.x:8080
}
// ID - returns target ID.
@ -130,7 +132,7 @@ func (target *WebhookTarget) Store() event.TargetStore {
}
func (target *WebhookTarget) isActive() (bool, error) {
conn, err := net.DialTimeout("tcp", target.args.Endpoint.Host, 5*time.Second)
conn, err := net.DialTimeout("tcp", target.addr, 5*time.Second)
if err != nil {
if xnet.IsNetworkOrHostDown(err, false) {
return false, store.ErrNotConnected
@ -291,6 +293,19 @@ func NewWebhookTarget(ctx context.Context, id string, args WebhookArgs, loggerOn
cancelCh: ctx.Done(),
}
// Calculate the webhook addr with the port number format
target.addr = args.Endpoint.Host
if _, _, err := net.SplitHostPort(args.Endpoint.Host); err != nil && strings.Contains(err.Error(), "missing port in address") {
switch strings.ToLower(args.Endpoint.Scheme) {
case "http":
target.addr += ":80"
case "https":
target.addr += ":443"
default:
return nil, errors.New("unsupported scheme")
}
}
if target.store != nil {
store.StreamItems(target.store, target, target.cancelCh, target.loggerOnce)
}