probe/ping/service/connection/connection.go

64 lines
1.4 KiB
Go
Raw Normal View History

2018-09-17 16:25:56 +00:00
package connection
import (
"crypto/tls"
"net"
"strings"
"time"
omd "git.loafle.net/overflow/model/discovery"
omm "git.loafle.net/overflow/model/meta"
ounp "git.loafle.net/overflow/util-go/net/ping"
)
func Ping(service *omd.Service, pingOption ounp.Option) (ounp.Result, error) {
pingResult := &ounp.PingResult{
Responses: make(map[int]ounp.Response, 0),
Summary: &ounp.PingSummary{},
}
LOOP:
2018-09-20 05:57:30 +00:00
for indexR := 0; indexR < pingOption.GetCount(); indexR++ {
2018-09-17 16:25:56 +00:00
conn, err := getConnection(service, pingOption)
if nil != err {
pingResult.Responses[indexR] = &ounp.PingResponse{
Error: err.Error(),
}
continue LOOP
}
pingResult.Responses[indexR] = &ounp.PingResponse{
Time: 0,
}
conn.Close()
}
return pingResult, nil
}
func getConnection(service *omd.Service, pingOption ounp.Option) (net.Conn, error) {
addr := net.JoinHostPort(service.Port.Host.Address, service.Port.PortNumber.String())
portType := strings.ToLower(service.Port.MetaPortType.Key)
switch omm.ToMetaCryptoTypeEnum(service.MetaCryptoType) {
case omm.MetaCryptoTypeEnumTLS:
dialer := &net.Dialer{
Timeout: time.Duration(pingOption.GetDeadline()) * time.Second,
}
return tls.DialWithDialer(
dialer,
portType,
addr,
&tls.Config{
InsecureSkipVerify: true,
ServerName: service.Port.Host.Address,
},
)
default:
return net.DialTimeout(portType, addr, time.Duration(pingOption.GetDeadline())*time.Second)
}
}