feat(check): add http, tcp, tls, dns and icmp checks
Validate normalizes settings for the API; Run executes them on checkers. Targets on private networks are refused unless explicitly allowed. Refs #12 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
bc8e934568
commit
55559c8b0a
6 files changed
+710
-1
No files matched your search
@@ -0,0 +1,264 @@
|
||||
package check
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/netip"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/icmp"
|
||||
"golang.org/x/net/ipv4"
|
||||
"golang.org/x/net/ipv6"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/netguard"
|
||||
)
|
||||
|
||||
func validHostPort(target string) bool {
|
||||
host, port, err := net.SplitHostPort(target)
|
||||
if err != nil || host == "" {
|
||||
return false
|
||||
}
|
||||
p, err := strconv.Atoi(port)
|
||||
return err == nil && p > 0 && p < 65536
|
||||
}
|
||||
|
||||
func validHost(host string) bool {
|
||||
if _, err := netip.ParseAddr(host); err == nil {
|
||||
return true
|
||||
}
|
||||
if len(host) == 0 || len(host) > 253 || strings.ContainsAny(host, "/:@ ") {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type tcpCheck struct{}
|
||||
|
||||
func (tcpCheck) validate(target string, config json.RawMessage) (json.RawMessage, error) {
|
||||
if !validHostPort(target) {
|
||||
return nil, invalid("invalid_target")
|
||||
}
|
||||
return json.RawMessage("{}"), nil
|
||||
}
|
||||
|
||||
func (tcpCheck) run(ctx context.Context, s Spec) Outcome {
|
||||
start := time.Now()
|
||||
conn, err := netguard.Dialer(s.AllowPrivate, s.Timeout).DialContext(ctx, "tcp", s.Target)
|
||||
if err != nil {
|
||||
return Outcome{Error: describeNetErr(err)}
|
||||
}
|
||||
o := Outcome{OK: true, Latency: time.Since(start)}
|
||||
_ = conn.Close()
|
||||
return o
|
||||
}
|
||||
|
||||
// TLSConfig configures a certificate expiry check.
|
||||
type TLSConfig struct {
|
||||
// MinDaysValid fails the check when the leaf certificate expires sooner.
|
||||
MinDaysValid int `json:"min_days_valid"`
|
||||
// ServerName overrides SNI and verification name; defaults to the host.
|
||||
ServerName string `json:"server_name,omitempty"`
|
||||
}
|
||||
|
||||
type tlsCheck struct{}
|
||||
|
||||
func (tlsCheck) validate(target string, config json.RawMessage) (json.RawMessage, error) {
|
||||
if !validHostPort(target) {
|
||||
return nil, invalid("invalid_target")
|
||||
}
|
||||
c := TLSConfig{MinDaysValid: 14}
|
||||
if err := json.Unmarshal(config, &c); err != nil {
|
||||
return nil, invalid("invalid_config")
|
||||
}
|
||||
if c.MinDaysValid < 0 || c.MinDaysValid > 365 || (c.ServerName != "" && !validHost(c.ServerName)) {
|
||||
return nil, invalid("invalid_config")
|
||||
}
|
||||
return encode(c), nil
|
||||
}
|
||||
|
||||
func (tlsCheck) run(ctx context.Context, s Spec) Outcome {
|
||||
c, err := decode[TLSConfig](s.Config)
|
||||
if err != nil {
|
||||
return Outcome{Error: "invalid configuration"}
|
||||
}
|
||||
host, _, _ := net.SplitHostPort(s.Target)
|
||||
if c.ServerName != "" {
|
||||
host = c.ServerName
|
||||
}
|
||||
d := &tls.Dialer{NetDialer: netguard.Dialer(s.AllowPrivate, s.Timeout), Config: &tls.Config{ServerName: host}}
|
||||
start := time.Now()
|
||||
conn, err := d.DialContext(ctx, "tcp", s.Target)
|
||||
if err != nil {
|
||||
return Outcome{Error: describeNetErr(err)}
|
||||
}
|
||||
latency := time.Since(start)
|
||||
defer func() { _ = conn.Close() }()
|
||||
certs := conn.(*tls.Conn).ConnectionState().PeerCertificates
|
||||
if len(certs) == 0 {
|
||||
return Outcome{Latency: latency, Error: "no certificate presented"}
|
||||
}
|
||||
left := time.Until(certs[0].NotAfter)
|
||||
if days := int(left.Hours() / 24); days < c.MinDaysValid {
|
||||
return Outcome{Latency: latency, Error: fmt.Sprintf("certificate expires in %d days", days)}
|
||||
}
|
||||
return Outcome{OK: true, Latency: latency}
|
||||
}
|
||||
|
||||
// DNSConfig configures a DNS check.
|
||||
type DNSConfig struct {
|
||||
Record string `json:"record"`
|
||||
// Resolver is host:port of the DNS server to ask; empty uses the
|
||||
// checker's resolver.
|
||||
Resolver string `json:"resolver,omitempty"`
|
||||
// Expect, if set, must be contained in one of the answers.
|
||||
Expect string `json:"expect,omitempty"`
|
||||
}
|
||||
|
||||
var dnsRecords = []string{"A", "AAAA", "CNAME", "MX", "NS", "TXT"}
|
||||
|
||||
type dnsCheck struct{}
|
||||
|
||||
func (dnsCheck) validate(target string, config json.RawMessage) (json.RawMessage, error) {
|
||||
if !validHost(target) {
|
||||
return nil, invalid("invalid_target")
|
||||
}
|
||||
c := DNSConfig{Record: "A"}
|
||||
if err := json.Unmarshal(config, &c); err != nil {
|
||||
return nil, invalid("invalid_config")
|
||||
}
|
||||
c.Record = strings.ToUpper(c.Record)
|
||||
if !slices.Contains(dnsRecords, c.Record) {
|
||||
return nil, invalid("invalid_record")
|
||||
}
|
||||
if c.Resolver != "" && !validHostPort(c.Resolver) {
|
||||
return nil, invalid("invalid_resolver")
|
||||
}
|
||||
if len(c.Expect) > 500 {
|
||||
return nil, invalid("invalid_config")
|
||||
}
|
||||
return encode(c), nil
|
||||
}
|
||||
|
||||
func (dnsCheck) run(ctx context.Context, s Spec) Outcome {
|
||||
c, err := decode[DNSConfig](s.Config)
|
||||
if err != nil {
|
||||
return Outcome{Error: "invalid configuration"}
|
||||
}
|
||||
r := net.DefaultResolver
|
||||
if c.Resolver != "" {
|
||||
d := netguard.Dialer(s.AllowPrivate, s.Timeout)
|
||||
r = &net.Resolver{PreferGo: true, Dial: func(ctx context.Context, network, _ string) (net.Conn, error) {
|
||||
return d.DialContext(ctx, network, c.Resolver)
|
||||
}}
|
||||
}
|
||||
var answers []string
|
||||
switch c.Record {
|
||||
case "A", "AAAA":
|
||||
network := "ip4"
|
||||
if c.Record == "AAAA" {
|
||||
network = "ip6"
|
||||
}
|
||||
ips, e := r.LookupNetIP(ctx, network, s.Target)
|
||||
for _, ip := range ips {
|
||||
answers = append(answers, ip.String())
|
||||
}
|
||||
err = e
|
||||
case "CNAME":
|
||||
var cname string
|
||||
cname, err = r.LookupCNAME(ctx, s.Target)
|
||||
answers = []string{cname}
|
||||
case "MX":
|
||||
var mx []*net.MX
|
||||
mx, err = r.LookupMX(ctx, s.Target)
|
||||
for _, m := range mx {
|
||||
answers = append(answers, m.Host)
|
||||
}
|
||||
case "NS":
|
||||
var ns []*net.NS
|
||||
ns, err = r.LookupNS(ctx, s.Target)
|
||||
for _, n := range ns {
|
||||
answers = append(answers, n.Host)
|
||||
}
|
||||
case "TXT":
|
||||
answers, err = r.LookupTXT(ctx, s.Target)
|
||||
}
|
||||
if err != nil {
|
||||
return Outcome{Error: describeNetErr(err)}
|
||||
}
|
||||
if len(answers) == 0 {
|
||||
return Outcome{Error: "no " + c.Record + " records"}
|
||||
}
|
||||
if c.Expect != "" && !slices.ContainsFunc(answers, func(a string) bool { return strings.Contains(a, c.Expect) }) {
|
||||
return Outcome{Error: "expected answer not found"}
|
||||
}
|
||||
return Outcome{OK: true}
|
||||
}
|
||||
|
||||
type icmpCheck struct{}
|
||||
|
||||
func (icmpCheck) validate(target string, config json.RawMessage) (json.RawMessage, error) {
|
||||
if !validHost(target) {
|
||||
return nil, invalid("invalid_target")
|
||||
}
|
||||
return json.RawMessage("{}"), nil
|
||||
}
|
||||
|
||||
// run sends one echo request over an unprivileged ICMP socket. The host must
|
||||
// allow it via net.ipv4.ping_group_range; otherwise the check reports that.
|
||||
func (icmpCheck) run(ctx context.Context, s Spec) Outcome {
|
||||
var addrs []netip.Addr
|
||||
var err error
|
||||
if s.AllowPrivate {
|
||||
addrs, err = net.DefaultResolver.LookupNetIP(ctx, "ip", s.Target)
|
||||
} else {
|
||||
addrs, err = netguard.CheckHost(ctx, s.Target)
|
||||
}
|
||||
if err != nil {
|
||||
return Outcome{Error: describeNetErr(err)}
|
||||
}
|
||||
if len(addrs) == 0 {
|
||||
return Outcome{Error: "host not found"}
|
||||
}
|
||||
addr := addrs[0].Unmap()
|
||||
network, proto, echo := "udp4", 1, icmp.Type(ipv4.ICMPTypeEcho)
|
||||
if addr.Is6() {
|
||||
network, proto, echo = "udp6", 58, ipv6.ICMPTypeEchoRequest
|
||||
}
|
||||
conn, err := icmp.ListenPacket(network, "")
|
||||
if err != nil {
|
||||
return Outcome{Error: "icmp not permitted on this checker: " + err.Error()}
|
||||
}
|
||||
defer func() { _ = conn.Close() }()
|
||||
if dl, ok := ctx.Deadline(); ok {
|
||||
_ = conn.SetDeadline(dl)
|
||||
}
|
||||
msg := icmp.Message{Type: echo, Body: &icmp.Echo{ID: 1, Seq: 1, Data: []byte("uptimesebi")}}
|
||||
b, err := msg.Marshal(nil)
|
||||
if err != nil {
|
||||
return Outcome{Error: err.Error()}
|
||||
}
|
||||
start := time.Now()
|
||||
if _, err := conn.WriteTo(b, &net.UDPAddr{IP: addr.AsSlice()}); err != nil {
|
||||
return Outcome{Error: describeNetErr(err)}
|
||||
}
|
||||
buf := make([]byte, 1500)
|
||||
for {
|
||||
n, _, err := conn.ReadFrom(buf)
|
||||
if err != nil {
|
||||
return Outcome{Error: "no echo reply"}
|
||||
}
|
||||
reply, err := icmp.ParseMessage(proto, buf[:n])
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if reply.Type == ipv4.ICMPTypeEchoReply || reply.Type == ipv6.ICMPTypeEchoReply {
|
||||
return Outcome{OK: true, Latency: time.Since(start)}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user