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: for indexR := 0; indexR < pingOption.GetCount(); indexR++ { 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) } }