added domain support and more verbose logging and better tls determination
This commit is contained in:
26
main.go
26
main.go
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"flag"
|
||||
"log"
|
||||
"net"
|
||||
@@ -15,6 +16,7 @@ type Config struct {
|
||||
StaticIP string
|
||||
Port string
|
||||
UseHTTPS bool
|
||||
Domain string
|
||||
NormalInterval time.Duration
|
||||
FailInterval time.Duration
|
||||
Timeout time.Duration
|
||||
@@ -105,6 +107,7 @@ func parseFlags() *Config {
|
||||
flag.StringVar(&cfg.StaticIP, "ip", "", "Static IP to check")
|
||||
flag.StringVar(&cfg.Port, "port", "80", "Port to check")
|
||||
flag.BoolVar(&cfg.UseHTTPS, "https", false, "Use HTTPS")
|
||||
flag.StringVar(&cfg.Domain, "domain", "", "Domain for HTTPS SNI/Host")
|
||||
flag.DurationVar(&cfg.NormalInterval, "interval", 120*time.Second, "Normal check interval")
|
||||
flag.DurationVar(&cfg.FailInterval, "fail-interval", 10*time.Second, "Interval during failure detection")
|
||||
flag.DurationVar(&cfg.Timeout, "timeout", 5*time.Second, "HTTP timeout")
|
||||
@@ -139,18 +142,37 @@ func checkHealth(cfg *Config) bool {
|
||||
},
|
||||
}
|
||||
|
||||
if cfg.UseHTTPS && cfg.Domain != "" {
|
||||
transport.TLSClientConfig = &tls.Config{
|
||||
ServerName: cfg.Domain,
|
||||
}
|
||||
}
|
||||
|
||||
client := &http.Client{
|
||||
Transport: transport,
|
||||
Timeout: cfg.Timeout,
|
||||
}
|
||||
|
||||
req, _ := http.NewRequestWithContext(ctx, "GET", scheme+"://"+cfg.StaticIP+cfg.HealthURL, nil)
|
||||
host := cfg.StaticIP
|
||||
if cfg.UseHTTPS && cfg.Domain != "" {
|
||||
host = cfg.Domain
|
||||
}
|
||||
|
||||
req, _ := http.NewRequestWithContext(ctx, "GET", scheme+"://"+host+cfg.HealthURL, nil)
|
||||
|
||||
if cfg.UseHTTPS && cfg.Domain != "" {
|
||||
req.Host = cfg.Domain
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
defer func() {
|
||||
if err := resp.Body.Close(); err != nil && cfg.Verbose {
|
||||
log.Println("body close:", err)
|
||||
}
|
||||
}()
|
||||
|
||||
return resp.StatusCode == 200
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user