mirror of https://github.com/statping/statping
Add TLS support
Signed-off-by: thatInfrastructureGuy <thatInfrastructureGuy@gmail.com>pull/806/head
parent
eb9792d184
commit
876e6d5766
|
@ -4,9 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
"github.com/statping/statping/types/metrics"
|
|
||||||
"google.golang.org/grpc"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -14,6 +11,11 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/statping/statping/types/metrics"
|
||||||
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/grpc/credentials"
|
||||||
|
|
||||||
"github.com/statping/statping/types/failures"
|
"github.com/statping/statping/types/failures"
|
||||||
"github.com/statping/statping/types/hits"
|
"github.com/statping/statping/types/hits"
|
||||||
"github.com/statping/statping/utils"
|
"github.com/statping/statping/utils"
|
||||||
|
@ -115,6 +117,19 @@ func CheckGrpc(s *Service, record bool) (*Service, error) {
|
||||||
timer := prometheus.NewTimer(metrics.ServiceTimer(s.Name))
|
timer := prometheus.NewTimer(metrics.ServiceTimer(s.Name))
|
||||||
defer timer.ObserveDuration()
|
defer timer.ObserveDuration()
|
||||||
|
|
||||||
|
// Strip URL scheme if present. Eg: https:// , http://
|
||||||
|
if strings.Contains(s.Domain, "://") {
|
||||||
|
u, err := url.Parse(s.Domain)
|
||||||
|
if err != nil {
|
||||||
|
// Unable to parse.
|
||||||
|
log.Warnln(fmt.Sprintf("GRPC Service: '%s', Unable to parse URL: '%v'", s.Name, s.Domain))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set domain as hostname without port number.
|
||||||
|
s.Domain = u.Hostname()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate DNS check time
|
||||||
dnsLookup, err := dnsCheck(s)
|
dnsLookup, err := dnsCheck(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if record {
|
if record {
|
||||||
|
@ -122,6 +137,18 @@ func CheckGrpc(s *Service, record bool) (*Service, error) {
|
||||||
}
|
}
|
||||||
return s, err
|
return s, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Connect to grpc service without TLS certs.
|
||||||
|
grpcOption := grpc.WithInsecure()
|
||||||
|
|
||||||
|
// Check if TLS is enabled
|
||||||
|
// Upgrade GRPC connection if using TLS
|
||||||
|
// Force to connect on HTTP2 with TLS. Needed when using a reverse proxy such as nginx.
|
||||||
|
if s.VerifySSL.Bool {
|
||||||
|
h2creds := credentials.NewTLS(&tls.Config{NextProtos: []string{"h2"}})
|
||||||
|
grpcOption = grpc.WithTransportCredentials(h2creds)
|
||||||
|
}
|
||||||
|
|
||||||
s.PingTime = dnsLookup
|
s.PingTime = dnsLookup
|
||||||
t1 := utils.Now()
|
t1 := utils.Now()
|
||||||
domain := fmt.Sprintf("%v", s.Domain)
|
domain := fmt.Sprintf("%v", s.Domain)
|
||||||
|
@ -131,7 +158,8 @@ func CheckGrpc(s *Service, record bool) (*Service, error) {
|
||||||
domain = fmt.Sprintf("[%v]:%v", s.Domain, s.Port)
|
domain = fmt.Sprintf("[%v]:%v", s.Domain, s.Port)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
conn, err := grpc.Dial(domain, grpc.WithInsecure(), grpc.WithBlock())
|
|
||||||
|
conn, err := grpc.Dial(domain, grpcOption, grpc.WithBlock())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("did not connect: %v", err)
|
log.Fatalf("did not connect: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -147,6 +175,7 @@ func CheckGrpc(s *Service, record bool) (*Service, error) {
|
||||||
}
|
}
|
||||||
return s, err
|
return s, err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Latency = utils.Now().Sub(t1).Microseconds()
|
s.Latency = utils.Now().Sub(t1).Microseconds()
|
||||||
s.LastResponse = ""
|
s.LastResponse = ""
|
||||||
s.Online = true
|
s.Online = true
|
||||||
|
|
Loading…
Reference in New Issue