2020-04-15 01:20:00 +02:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2020-10-04 23:01:10 +02:00
|
|
|
"crypto/tls"
|
2020-10-05 01:49:02 +02:00
|
|
|
"net"
|
2020-04-15 01:20:00 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-10-23 22:29:20 +02:00
|
|
|
secureHTTPClient *http.Client
|
|
|
|
insecureHTTPClient *http.Client
|
2020-04-15 01:20:00 +02:00
|
|
|
)
|
|
|
|
|
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{
|
2020-10-04 23:01:10 +02:00
|
|
|
Timeout: time.Second * 10,
|
|
|
|
Transport: &http.Transport{
|
2020-10-30 14:50:40 +01:00
|
|
|
MaxIdleConns: 100,
|
|
|
|
MaxIdleConnsPerHost: 20,
|
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{
|
|
|
|
Timeout: time.Second * 10,
|
2020-10-30 14:51:42 +01:00
|
|
|
Transport: &http.Transport{
|
|
|
|
MaxIdleConns: 100,
|
|
|
|
MaxIdleConnsPerHost: 20,
|
|
|
|
},
|
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-10-23 22:29:20 +02:00
|
|
|
// CanCreateConnectionToTCPService checks whether a connection can be established with a TCP service
|
|
|
|
func CanCreateConnectionToTCPService(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
|
|
|
|
}
|