Work on #61: Add support for ICMP

+ Update dependencies
This commit is contained in:
TwinProduction
2020-12-25 00:07:18 -05:00
parent c86173d46f
commit 83a5813daf
1004 changed files with 182274 additions and 64323 deletions

View File

@ -5,6 +5,8 @@ import (
"net"
"net/http"
"time"
"github.com/go-ping/ping"
)
var (
@ -31,7 +33,7 @@ func GetHTTPClient(insecure bool) *http.Client {
}
if secureHTTPClient == nil {
secureHTTPClient = &http.Client{
Timeout: time.Second * 10,
Timeout: 10 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 20,
@ -41,8 +43,8 @@ func GetHTTPClient(insecure bool) *http.Client {
return secureHTTPClient
}
// CanCreateConnectionToTCPService checks whether a connection can be established with a TCP service
func CanCreateConnectionToTCPService(address string) bool {
// 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
@ -50,3 +52,21 @@ func CanCreateConnectionToTCPService(address string) bool {
_ = conn.Close()
return true
}
// CanPing checks if an address can be pinged
// Note that this function takes at least 100ms, even if the address is 127.0.0.1
func CanPing(address string) bool {
pinger, err := ping.NewPinger(address)
if err != nil {
return false
}
pinger.Count = 1
pinger.Timeout = 5 * time.Second
pinger.SetNetwork("ip4")
pinger.SetPrivileged(true)
err = pinger.Run()
if err != nil {
return false
}
return true
}