2020-04-15 01:20:00 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2020-10-04 23:01:10 +02:00
|
|
|
"crypto/tls"
|
2021-06-05 21:47:11 +02:00
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
2020-10-05 01:49:02 +02:00
|
|
|
"net"
|
2020-04-15 01:20:00 +02:00
|
|
|
"net/http"
|
2021-06-05 21:47:11 +02:00
|
|
|
"net/smtp"
|
2021-05-01 04:58:14 +02:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2021-06-05 21:47:11 +02:00
|
|
|
"strings"
|
2020-04-15 01:20:00 +02:00
|
|
|
"time"
|
2020-12-25 06:07:18 +01:00
|
|
|
|
|
|
|
"github.com/go-ping/ping"
|
2020-04-15 01:20:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2021-05-01 04:58:14 +02:00
|
|
|
|
|
|
|
// httpTimeout is the timeout for secureHTTPClient and insecureHTTPClient
|
|
|
|
httpTimeout = 10 * time.Second
|
2020-04-15 01:20:00 +02:00
|
|
|
)
|
|
|
|
|
2021-05-01 04:58:14 +02:00
|
|
|
func init() {
|
|
|
|
// XXX: This is an undocumented feature. See https://github.com/TwinProduction/gatus/issues/104.
|
|
|
|
httpTimeoutInSecondsFromEnvironmentVariable := os.Getenv("HTTP_CLIENT_TIMEOUT_IN_SECONDS")
|
|
|
|
if len(httpTimeoutInSecondsFromEnvironmentVariable) > 0 {
|
|
|
|
if httpTimeoutInSeconds, err := strconv.Atoi(httpTimeoutInSecondsFromEnvironmentVariable); err == nil {
|
|
|
|
httpTimeout = time.Duration(httpTimeoutInSeconds) * time.Second
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 22:29:20 +02:00
|
|
|
// GetHTTPClient returns the shared HTTP client
|
|
|
|
func GetHTTPClient(insecure bool) *http.Client {
|
2020-10-04 23:01:10 +02:00
|
|
|
if insecure {
|
2020-10-23 22:29:20 +02:00
|
|
|
if insecureHTTPClient == nil {
|
|
|
|
insecureHTTPClient = &http.Client{
|
2021-05-01 04:58:14 +02:00
|
|
|
Timeout: httpTimeout,
|
2020-10-04 23:01:10 +02:00
|
|
|
Transport: &http.Transport{
|
2020-10-30 14:50:40 +01:00
|
|
|
MaxIdleConns: 100,
|
|
|
|
MaxIdleConnsPerHost: 20,
|
2021-02-19 01:03:38 +01:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2020-10-04 23:01:10 +02:00
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
2020-10-23 22:29:20 +02:00
|
|
|
return insecureHTTPClient
|
|
|
|
}
|
|
|
|
if secureHTTPClient == nil {
|
|
|
|
secureHTTPClient = &http.Client{
|
2021-05-01 04:58:14 +02:00
|
|
|
Timeout: httpTimeout,
|
2020-10-30 14:51:42 +01:00
|
|
|
Transport: &http.Transport{
|
|
|
|
MaxIdleConns: 100,
|
|
|
|
MaxIdleConnsPerHost: 20,
|
2021-02-19 01:03:38 +01:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2020-10-30 14:51:42 +01:00
|
|
|
},
|
2020-10-04 23:01:10 +02:00
|
|
|
}
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
2020-10-23 22:29:20 +02:00
|
|
|
return secureHTTPClient
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
2020-10-05 01:49:02 +02:00
|
|
|
|
2020-12-25 06:07:18 +01:00
|
|
|
// CanCreateTCPConnection checks whether a connection can be established with a TCP service
|
|
|
|
func CanCreateTCPConnection(address string) bool {
|
2020-10-05 01:49:02 +02:00
|
|
|
conn, err := net.DialTimeout("tcp", address, 5*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_ = conn.Close()
|
|
|
|
return true
|
|
|
|
}
|
2020-12-25 06:07:18 +01:00
|
|
|
|
2021-06-05 21:47:11 +02:00
|
|
|
func CanPerformStartTls(address string, insecure bool) (connected bool, certificate *x509.Certificate, err error) {
|
|
|
|
tokens := strings.Split(address, ":")
|
|
|
|
if len(tokens) != 2 {
|
|
|
|
err = fmt.Errorf("invalid address for starttls, must HOST:PORT")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tlsconfig := &tls.Config{
|
|
|
|
InsecureSkipVerify: insecure,
|
|
|
|
ServerName: tokens[0],
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := smtp.Dial(address)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.StartTLS(tlsconfig)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if state, ok := c.TLSConnectionState(); ok {
|
|
|
|
certificate = state.PeerCertificates[0]
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("could not get TLS connection state")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
connected = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-27 23:07:50 +01:00
|
|
|
// Ping checks if an address can be pinged and returns the round-trip time if the address can be pinged
|
|
|
|
//
|
2020-12-25 06:07:18 +01:00
|
|
|
// Note that this function takes at least 100ms, even if the address is 127.0.0.1
|
2020-12-27 23:07:50 +01:00
|
|
|
func Ping(address string) (bool, time.Duration) {
|
2020-12-25 06:07:18 +01:00
|
|
|
pinger, err := ping.NewPinger(address)
|
|
|
|
if err != nil {
|
2020-12-27 23:07:50 +01:00
|
|
|
return false, 0
|
2020-12-25 06:07:18 +01:00
|
|
|
}
|
|
|
|
pinger.Count = 1
|
2021-01-13 03:26:28 +01:00
|
|
|
pinger.Timeout = pingTimeout
|
2020-12-25 06:07:18 +01:00
|
|
|
pinger.SetPrivileged(true)
|
|
|
|
err = pinger.Run()
|
|
|
|
if err != nil {
|
2020-12-27 23:07:50 +01:00
|
|
|
return false, 0
|
2020-12-25 06:07:18 +01:00
|
|
|
}
|
2020-12-27 23:07:50 +01:00
|
|
|
if pinger.Statistics() != nil {
|
2021-01-13 03:08:18 +01:00
|
|
|
// 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
|
|
|
|
}
|
2020-12-27 23:07:50 +01:00
|
|
|
return true, pinger.Statistics().MaxRtt
|
|
|
|
}
|
|
|
|
return true, 0
|
2020-12-25 06:07:18 +01:00
|
|
|
}
|