gatus/client/client.go

127 lines
3.4 KiB
Go
Raw Normal View History

package client
import (
"crypto/tls"
"crypto/x509"
2021-06-05 22:35:52 +02:00
"errors"
"net"
"net/http"
"net/smtp"
2021-07-19 02:43:44 +02:00
"runtime"
"strings"
"time"
"github.com/go-ping/ping"
)
var (
2020-10-23 22:29:20 +02:00
secureHTTPClient *http.Client
insecureHTTPClient *http.Client
2021-01-13 03:26:28 +01:00
2021-01-13 03:37:21 +01:00
// pingTimeout is the timeout for the Ping function
// This is mainly exposed for testing purposes
2021-01-13 03:26:28 +01:00
pingTimeout = 5 * time.Second
// httpTimeout is the timeout for secureHTTPClient and insecureHTTPClient
httpTimeout = 10 * time.Second
)
2020-10-23 22:29:20 +02:00
// GetHTTPClient returns the shared HTTP client
func GetHTTPClient(insecure bool) *http.Client {
if insecure {
2020-10-23 22:29:20 +02:00
if insecureHTTPClient == nil {
insecureHTTPClient = &http.Client{
Timeout: httpTimeout,
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 20,
2021-02-19 01:03:38 +01:00
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
2021-07-07 04:01:46 +02:00
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse // Don't follow redirects
},
}
}
2020-10-23 22:29:20 +02:00
return insecureHTTPClient
}
if secureHTTPClient == nil {
secureHTTPClient = &http.Client{
Timeout: httpTimeout,
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 20,
2021-02-19 01:03:38 +01:00
Proxy: http.ProxyFromEnvironment,
},
2021-07-07 04:01:46 +02:00
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse // Don't follow redirects
},
}
}
2020-10-23 22:29:20 +02:00
return secureHTTPClient
}
// CanCreateTCPConnection checks whether a connection can be established with a TCP service
func CanCreateTCPConnection(address string) bool {
conn, err := net.DialTimeout("tcp", address, 5*time.Second)
if err != nil {
return false
}
_ = conn.Close()
return true
}
2021-06-05 22:35:52 +02:00
// CanPerformStartTLS checks whether a connection can be established to an address using the STARTTLS protocol
func CanPerformStartTLS(address string, insecure bool) (connected bool, certificate *x509.Certificate, err error) {
hostAndPort := strings.Split(address, ":")
if len(hostAndPort) != 2 {
return false, nil, errors.New("invalid address for starttls, format must be host:port")
}
2021-06-05 22:35:52 +02:00
smtpClient, err := smtp.Dial(address)
if err != nil {
return
}
2021-06-05 22:35:52 +02:00
err = smtpClient.StartTLS(&tls.Config{
InsecureSkipVerify: insecure,
ServerName: hostAndPort[0],
})
if err != nil {
return
}
2021-06-05 22:35:52 +02:00
if state, ok := smtpClient.TLSConnectionState(); ok {
certificate = state.PeerCertificates[0]
} else {
2021-06-05 22:35:52 +02:00
return false, nil, errors.New("could not get TLS connection state")
}
2021-06-05 22:35:52 +02:00
return true, certificate, nil
}
// Ping checks if an address can be pinged and returns the round-trip time if the address can be pinged
//
// Note that this function takes at least 100ms, even if the address is 127.0.0.1
func Ping(address string) (bool, time.Duration) {
pinger, err := ping.NewPinger(address)
if err != nil {
return false, 0
}
pinger.Count = 1
2021-01-13 03:26:28 +01:00
pinger.Timeout = pingTimeout
2021-07-19 02:43:44 +02:00
// Set the pinger's privileged mode to true for every operating system except darwin
// https://github.com/TwinProduction/gatus/issues/132
pinger.SetPrivileged(runtime.GOOS != "darwin")
err = pinger.Run()
if err != nil {
return false, 0
}
if pinger.Statistics() != nil {
// If the packet loss is 100, it means that the packet didn't reach the host
if pinger.Statistics().PacketLoss == 100 {
return false, pinger.Timeout
}
return true, pinger.Statistics().MaxRtt
}
return true, 0
}