From e96f19c867b244ae9e2b055d9076d0681cd21bb3 Mon Sep 17 00:00:00 2001 From: Praveen raj Mani Date: Tue, 13 Aug 2019 16:43:11 +0530 Subject: [PATCH] Use default ports for the schemes when dialing (#7960) Fixes #7946 --- pkg/event/target/elasticsearch.go | 27 +++++++++++++++++++-------- pkg/event/target/webhook.go | 26 ++++++++++++++------------ pkg/net/url.go | 24 ++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/pkg/event/target/elasticsearch.go b/pkg/event/target/elasticsearch.go index 85658678d..c9bb3ca37 100644 --- a/pkg/event/target/elasticsearch.go +++ b/pkg/event/target/elasticsearch.go @@ -19,7 +19,6 @@ package target import ( "context" "fmt" - "net" "net/url" "os" "path/filepath" @@ -83,8 +82,14 @@ func (target *ElasticsearchTarget) Save(eventData event.Event) error { if target.store != nil { return target.store.Put(eventData) } - if _, err := net.Dial("tcp", target.args.URL.Host); err != nil { - return errNotConnected + if dErr := target.args.URL.DialHTTP(); dErr != nil { + if urlErr, ok := dErr.(*url.Error); ok { + // To treat "connection refused" errors as errNotConnected. + if IsConnRefusedErr(urlErr.Err) { + return errNotConnected + } + } + return dErr } return target.send(eventData) } @@ -151,8 +156,14 @@ func (target *ElasticsearchTarget) Send(eventKey string) error { } } - if _, err := net.Dial("tcp", target.args.URL.Host); err != nil { - return errNotConnected + if dErr := target.args.URL.DialHTTP(); dErr != nil { + if urlErr, ok := dErr.(*url.Error); ok { + // To treat "connection refused" errors as errNotConnected. + if IsConnRefusedErr(urlErr.Err) { + return errNotConnected + } + } + return dErr } eventData, eErr := target.store.Get(eventKey) @@ -227,10 +238,10 @@ func NewElasticsearchTarget(id string, args ElasticsearchArgs, doneCh <-chan str } } - _, derr := net.Dial("tcp", args.URL.Host) - if derr != nil { + dErr := args.URL.DialHTTP() + if dErr != nil { if store == nil { - return nil, derr + return nil, dErr } } else { client, err = newClient(args) diff --git a/pkg/event/target/webhook.go b/pkg/event/target/webhook.go index c95bdab1d..6172422c1 100644 --- a/pkg/event/target/webhook.go +++ b/pkg/event/target/webhook.go @@ -82,15 +82,16 @@ func (target *WebhookTarget) Save(eventData event.Event) error { if target.store != nil { return target.store.Put(eventData) } - urlStr, pErr := xnet.ParseURL(target.args.Endpoint.String()) + u, pErr := xnet.ParseURL(target.args.Endpoint.String()) if pErr != nil { return pErr } - _, dErr := net.Dial("tcp", urlStr.Host) - if dErr != nil { - // To treat "connection refused" errors as errNotConnected. - if IsConnRefusedErr(dErr) { - return errNotConnected + if dErr := u.DialHTTP(); dErr != nil { + if urlErr, ok := dErr.(*url.Error); ok { + // To treat "connection refused" errors as errNotConnected. + if IsConnRefusedErr(urlErr.Err) { + return errNotConnected + } } return dErr } @@ -136,15 +137,16 @@ func (target *WebhookTarget) send(eventData event.Event) error { // Send - reads an event from store and sends it to webhook. func (target *WebhookTarget) Send(eventKey string) error { - urlStr, pErr := xnet.ParseURL(target.args.Endpoint.String()) + u, pErr := xnet.ParseURL(target.args.Endpoint.String()) if pErr != nil { return pErr } - _, dErr := net.Dial("tcp", urlStr.Host) - if dErr != nil { - // To treat "connection refused" errors as errNotConnected. - if IsConnRefusedErr(dErr) { - return errNotConnected + if dErr := u.DialHTTP(); dErr != nil { + if urlErr, ok := dErr.(*url.Error); ok { + // To treat "connection refused" errors as errNotConnected. + if IsConnRefusedErr(urlErr.Err) { + return errNotConnected + } } return dErr } diff --git a/pkg/net/url.go b/pkg/net/url.go index 8ec122df4..955e5bacc 100644 --- a/pkg/net/url.go +++ b/pkg/net/url.go @@ -19,9 +19,12 @@ package net import ( "encoding/json" "errors" + "net" + "net/http" "net/url" "path" "strings" + "time" ) // URL - improved JSON friendly url.URL. @@ -79,6 +82,27 @@ func (u *URL) UnmarshalJSON(data []byte) (err error) { return nil } +// DialHTTP - dials the url to check the connection. +func (u URL) DialHTTP() error { + var client = &http.Client{ + Transport: &http.Transport{ + DialContext: (&net.Dialer{ + Timeout: 2 * time.Second, + }).DialContext, + }, + } + req, err := http.NewRequest("POST", u.String(), nil) + if err != nil { + return err + } + resp, err := client.Do(req) + if err != nil { + return err + } + resp.Body.Close() + return nil +} + // ParseURL - parses string into URL. func ParseURL(s string) (u *URL, err error) { var uu *url.URL