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. 23
      pkg/event/target/elasticsearch.go
  2. 18
      pkg/event/target/webhook.go
  3. 24
      pkg/net/url.go

@ -19,7 +19,6 @@ package target
import (
"context"
"fmt"
"net"
"net/url"
"os"
"path/filepath"
@ -83,9 +82,15 @@ 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 {
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,9 +156,15 @@ 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 {
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)
if eErr != nil {
@ -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)

@ -82,16 +82,17 @@ 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 {
if dErr := u.DialHTTP(); dErr != nil {
if urlErr, ok := dErr.(*url.Error); ok {
// To treat "connection refused" errors as errNotConnected.
if IsConnRefusedErr(dErr) {
if IsConnRefusedErr(urlErr.Err) {
return errNotConnected
}
}
return dErr
}
return target.send(eventData)
@ -136,16 +137,17 @@ 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 {
if dErr := u.DialHTTP(); dErr != nil {
if urlErr, ok := dErr.(*url.Error); ok {
// To treat "connection refused" errors as errNotConnected.
if IsConnRefusedErr(dErr) {
if IsConnRefusedErr(urlErr.Err) {
return errNotConnected
}
}
return dErr
}

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

Loading…
Cancel
Save