feat: Allow specifying the ALPN

This commit is contained in:
PapaTutuWawa 2024-01-07 15:11:08 +01:00
parent 1e7e2ad81d
commit 52af228ce7

17
main.go
View File

@ -22,15 +22,17 @@ import (
type CertificateMetrics struct { type CertificateMetrics struct {
Domain string Domain string
Port int Port int
ALPN []string
ExpiryIn prometheus.Gauge ExpiryIn prometheus.Gauge
IsValid prometheus.Gauge IsValid prometheus.Gauge
} }
func NewCertificateMetrics(domain string, port int) CertificateMetrics { func NewCertificateMetrics(domain string, port int, alpn []string) CertificateMetrics {
metrics := CertificateMetrics{ metrics := CertificateMetrics{
Domain: domain, Domain: domain,
Port: port, Port: port,
ALPN: alpn,
ExpiryIn: prometheus.NewGauge( ExpiryIn: prometheus.NewGauge(
prometheus.GaugeOpts{ prometheus.GaugeOpts{
Name: "cert_status_expiry_in", Name: "cert_status_expiry_in",
@ -59,8 +61,11 @@ func (c *CertificateMetrics) register(registry *prometheus.Registry) {
} }
func (c *CertificateMetrics) checkTls() { func (c *CertificateMetrics) checkTls() {
conn, err := tls.Dial("tcp", c.Domain+":"+fmt.Sprint(c.Port), &tls.Config{}) conn, err := tls.Dial("tcp", c.Domain+":"+fmt.Sprint(c.Port), &tls.Config{
NextProtos: c.ALPN,
})
if err != nil { if err != nil {
log.Debugf("Failed to dial %s:%d@%s using ALPN %v: %v", c.Domain, c.Port, "tcp", c.ALPN, err)
c.IsValid.Set(0) c.IsValid.Set(0)
return return
} }
@ -107,9 +112,9 @@ func run(ctx *cli.Context) error {
for _, d := range domains { for _, d := range domains {
log.Debugf("Parsing '%s'...", d) log.Debugf("Parsing '%s'...", d)
parts := strings.Split(d, ":") parts := strings.Split(d, ":")
if len(parts) != 2 { if len(parts) != 3 {
log.Errorf("Invalid domain format for '%s'", d) log.Errorf("Invalid domain format for '%s'", d)
return errors.New("Invalid domain format: Expects <domain>:<port>") return errors.New("Invalid domain format: Expects <domain>:<port>:<alpn>")
} }
port, err := strconv.Atoi(parts[1]) port, err := strconv.Atoi(parts[1])
@ -119,9 +124,13 @@ func run(ctx *cli.Context) error {
} }
// Create the metric, and register it // Create the metric, and register it
// TODO: Make this prettier
alpn := strings.Split(parts[2], ";")
log.Debugf("Using ALPNs: %v", alpn)
metric := NewCertificateMetrics( metric := NewCertificateMetrics(
parts[0], parts[0],
port, port,
alpn,
) )
metric.register(registry) metric.register(registry)
metrics = append(metrics, metric) metrics = append(metrics, metric)