Use default ports for the schemes when dialing (#7960)

Fixes #7946
master
Praveen raj Mani 5 years ago committed by Nitish Tiwari
parent aaef18b1a3
commit e96f19c867
  1. 27
      pkg/event/target/elasticsearch.go
  2. 26
      pkg/event/target/webhook.go
  3. 24
      pkg/net/url.go

@ -19,7 +19,6 @@ package target
import ( import (
"context" "context"
"fmt" "fmt"
"net"
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
@ -83,8 +82,14 @@ func (target *ElasticsearchTarget) Save(eventData event.Event) error {
if target.store != nil { if target.store != nil {
return target.store.Put(eventData) return target.store.Put(eventData)
} }
if _, err := net.Dial("tcp", target.args.URL.Host); err != nil { if dErr := target.args.URL.DialHTTP(); dErr != nil {
return errNotConnected 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) 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 { if dErr := target.args.URL.DialHTTP(); dErr != nil {
return errNotConnected 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) 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) dErr := args.URL.DialHTTP()
if derr != nil { if dErr != nil {
if store == nil { if store == nil {
return nil, derr return nil, dErr
} }
} else { } else {
client, err = newClient(args) client, err = newClient(args)

@ -82,15 +82,16 @@ func (target *WebhookTarget) Save(eventData event.Event) error {
if target.store != nil { if target.store != nil {
return target.store.Put(eventData) return target.store.Put(eventData)
} }
urlStr, pErr := xnet.ParseURL(target.args.Endpoint.String()) u, pErr := xnet.ParseURL(target.args.Endpoint.String())
if pErr != nil { if pErr != nil {
return pErr return pErr
} }
_, dErr := net.Dial("tcp", urlStr.Host) if dErr := u.DialHTTP(); dErr != nil {
if dErr != nil { if urlErr, ok := dErr.(*url.Error); ok {
// To treat "connection refused" errors as errNotConnected. // To treat "connection refused" errors as errNotConnected.
if IsConnRefusedErr(dErr) { if IsConnRefusedErr(urlErr.Err) {
return errNotConnected return errNotConnected
}
} }
return dErr 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. // Send - reads an event from store and sends it to webhook.
func (target *WebhookTarget) Send(eventKey string) error { 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 { if pErr != nil {
return pErr return pErr
} }
_, dErr := net.Dial("tcp", urlStr.Host) if dErr := u.DialHTTP(); dErr != nil {
if dErr != nil { if urlErr, ok := dErr.(*url.Error); ok {
// To treat "connection refused" errors as errNotConnected. // To treat "connection refused" errors as errNotConnected.
if IsConnRefusedErr(dErr) { if IsConnRefusedErr(urlErr.Err) {
return errNotConnected return errNotConnected
}
} }
return dErr return dErr
} }

@ -19,9 +19,12 @@ package net
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"net"
"net/http"
"net/url" "net/url"
"path" "path"
"strings" "strings"
"time"
) )
// URL - improved JSON friendly url.URL. // URL - improved JSON friendly url.URL.
@ -79,6 +82,27 @@ func (u *URL) UnmarshalJSON(data []byte) (err error) {
return nil 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. // ParseURL - parses string into URL.
func ParseURL(s string) (u *URL, err error) { func ParseURL(s string) (u *URL, err error) {
var uu *url.URL var uu *url.URL

Loading…
Cancel
Save