2026-08-26 23:52:31 +03:00
|
|
|
package checkrunner
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"crypto/tls"
|
2026-11-20 21:15:22 +03:00
|
|
|
"fmt"
|
2026-08-26 23:52:31 +03:00
|
|
|
"net"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TLSHandshake performs a real TLS handshake (not just a TCP connect)
|
|
|
|
|
// against host:port and reports success if it completes within timeout.
|
|
|
|
|
// Certificate validation is intentionally skipped: the target is a bare
|
|
|
|
|
// candidate IP under test before any DNS/hostname is attached to it, so
|
|
|
|
|
// there's neither a hostname to validate the cert against nor any reason
|
|
|
|
|
// to expect a signed cert yet. This checks "is there a real TLS listener
|
|
|
|
|
// here", not "is its certificate valid" — same scope-limiting principle
|
|
|
|
|
// SSHBanner already applies (banner only, no auth handshake).
|
2026-11-20 21:15:22 +03:00
|
|
|
func TLSHandshake(host string, port int, version string, timeout time.Duration) func(ctx context.Context) Result {
|
2026-08-26 23:52:31 +03:00
|
|
|
target := net.JoinHostPort(host, strconv.Itoa(port))
|
|
|
|
|
checkType := "tls-" + strconv.Itoa(port)
|
2026-11-20 21:15:22 +03:00
|
|
|
probe := run(checkType, target, func(ctx context.Context) error {
|
2026-08-26 23:52:31 +03:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, timeout)
|
|
|
|
|
defer cancel()
|
2026-11-20 21:15:22 +03:00
|
|
|
config := &tls.Config{InsecureSkipVerify: true}
|
|
|
|
|
switch version {
|
|
|
|
|
case "":
|
|
|
|
|
case "1.2":
|
|
|
|
|
config.MinVersion, config.MaxVersion = tls.VersionTLS12, tls.VersionTLS12
|
|
|
|
|
case "1.3":
|
|
|
|
|
config.MinVersion, config.MaxVersion = tls.VersionTLS13, tls.VersionTLS13
|
|
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("unsupported TLS version %q", version)
|
|
|
|
|
}
|
2026-08-26 23:52:31 +03:00
|
|
|
d := tls.Dialer{
|
|
|
|
|
NetDialer: &net.Dialer{Timeout: timeout},
|
2026-11-20 21:15:22 +03:00
|
|
|
Config: config,
|
2026-08-26 23:52:31 +03:00
|
|
|
}
|
|
|
|
|
conn, err := d.DialContext(ctx, "tcp", target)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return conn.Close()
|
|
|
|
|
})
|
2026-11-20 21:15:22 +03:00
|
|
|
return func(ctx context.Context) Result {
|
|
|
|
|
result := probe(ctx)
|
|
|
|
|
if version != "" {
|
|
|
|
|
result.Detail = "TLS " + version + ": " + result.Detail
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
2026-08-26 23:52:31 +03:00
|
|
|
}
|