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"
|
2021-06-05 22:35:52 +02:00
|
|
|
"errors"
|
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-07-19 02:43:44 +02:00
|
|
|
"runtime"
|
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
|
|
|
)
|
|
|
|
|
2020-10-23 22:29:20 +02:00
|
|
|
// GetHTTPClient returns the shared HTTP client
|
2021-07-29 03:41:26 +02:00
|
|
|
func GetHTTPClient(config *Config) *http.Client {
|
2021-07-30 00:13:37 +02:00
|
|
|
if config == nil {
|
|
|
|
return defaultConfig.getHTTPClient()
|
|
|
|
}
|
|
|
|
return config.getHTTPClient()
|
2020-04-15 01:20:00 +02:00
|
|
|
}
|
2020-10-05 01:49:02 +02:00
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
// CanCreateTCPConnection checks whether a connection can be established with a TCP endpoint
|
2021-07-29 03:41:26 +02:00
|
|
|
func CanCreateTCPConnection(address string, config *Config) bool {
|
|
|
|
conn, err := net.DialTimeout("tcp", address, config.Timeout)
|
2020-10-05 01:49:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_ = conn.Close()
|
|
|
|
return true
|
|
|
|
}
|
2020-12-25 06:07:18 +01:00
|
|
|
|
2021-06-05 22:35:52 +02:00
|
|
|
// CanPerformStartTLS checks whether a connection can be established to an address using the STARTTLS protocol
|
2021-07-29 03:41:26 +02:00
|
|
|
func CanPerformStartTLS(address string, config *Config) (connected bool, certificate *x509.Certificate, err error) {
|
2021-06-05 22:35:52 +02:00
|
|
|
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 21:47:11 +02:00
|
|
|
}
|
2021-10-01 02:56:09 +02:00
|
|
|
connection, err := net.DialTimeout("tcp", address, config.Timeout)
|
2021-09-30 22:15:17 +02:00
|
|
|
if err != nil {
|
2021-10-01 02:45:47 +02:00
|
|
|
return
|
2021-09-30 22:15:17 +02:00
|
|
|
}
|
2021-10-01 02:56:09 +02:00
|
|
|
smtpClient, err := smtp.NewClient(connection, hostAndPort[0])
|
2021-06-05 21:47:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-06-05 22:35:52 +02:00
|
|
|
err = smtpClient.StartTLS(&tls.Config{
|
2021-07-29 03:41:26 +02:00
|
|
|
InsecureSkipVerify: config.Insecure,
|
2021-06-05 22:35:52 +02:00
|
|
|
ServerName: hostAndPort[0],
|
|
|
|
})
|
2021-06-05 21:47:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-06-05 22:35:52 +02:00
|
|
|
if state, ok := smtpClient.TLSConnectionState(); ok {
|
2021-06-05 21:47:11 +02:00
|
|
|
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 21:47:11 +02:00
|
|
|
}
|
2021-06-05 22:35:52 +02:00
|
|
|
return true, certificate, nil
|
2021-06-05 21:47:11 +02:00
|
|
|
}
|
|
|
|
|
2021-09-30 22:15:17 +02:00
|
|
|
// CanPerformTLS checks whether a connection can be established to an address using the TLS protocol
|
|
|
|
func CanPerformTLS(address string, config *Config) (connected bool, certificate *x509.Certificate, err error) {
|
2021-10-01 02:45:47 +02:00
|
|
|
connection, err := tls.DialWithDialer(&net.Dialer{Timeout: config.Timeout}, "tcp", address, nil)
|
2021-09-30 22:15:17 +02:00
|
|
|
if err != nil {
|
2021-10-01 02:45:47 +02:00
|
|
|
return
|
2021-09-30 22:15:17 +02:00
|
|
|
}
|
2021-10-01 02:45:47 +02:00
|
|
|
defer connection.Close()
|
|
|
|
verifiedChains := connection.ConnectionState().VerifiedChains
|
|
|
|
if len(verifiedChains) == 0 || len(verifiedChains[0]) == 0 {
|
|
|
|
return
|
2021-09-30 22:15:17 +02:00
|
|
|
}
|
2021-10-01 02:45:47 +02:00
|
|
|
return true, verifiedChains[0][0], nil
|
2021-09-30 22:15:17 +02:00
|
|
|
}
|
|
|
|
|
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
|
2021-07-29 03:41:26 +02:00
|
|
|
func Ping(address string, config *Config) (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-07-29 03:41:26 +02:00
|
|
|
pinger.Timeout = config.Timeout
|
2021-10-07 07:21:13 +02:00
|
|
|
// Set the pinger's privileged mode to true for every GOOS except darwin
|
2021-10-08 03:28:04 +02:00
|
|
|
// See https://github.com/TwiN/gatus/issues/132
|
2021-10-07 07:21:13 +02:00
|
|
|
//
|
|
|
|
// Note that for this to work on Linux, Gatus must run with sudo privileges.
|
|
|
|
// See https://github.com/go-ping/ping#linux
|
|
|
|
pinger.SetPrivileged(runtime.GOOS != "darwin")
|
2020-12-25 06:07:18 +01:00
|
|
|
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
|
|
|
}
|