Enable SSH and TLS handshake on prober for TCP22 and TCP443 ports

This commit is contained in:
ayurishchev committed 2026-08-26 23:52:31 +03:00
1 parent af3453f19f
commit 550ce3fec4
11 files changed
+308 -20

No files matched your search

+28 -8
View File
@@ -109,26 +109,46 @@ func (p *Prober) pollOnce(ctx context.Context) {
}
}
// extraChecksForPort returns checks to run in addition to the base
// TCPConnect for ports where a bare TCP handshake doesn't actually prove
// the expected service is behind it: a real TLS handshake on 443, a real
// SSH banner exchange on 22. nil for every other port.
func extraChecksForPort(host string, port int, timeout time.Duration) []func(ctx context.Context) checkrunner.Result {
switch port {
case 443:
return []func(ctx context.Context) checkrunner.Result{
checkrunner.TLSHandshake(host, port, timeout),
}
case 22:
return []func(ctx context.Context) checkrunner.Result{
checkrunner.SSHBanner(host, timeout),
}
default:
return nil
}
}
func (p *Prober) probeOne(ctx context.Context, a assignment) {
tcpTimeout := time.Duration(p.cfg.Checks.TCPTimeoutSeconds) * time.Second
icmpTimeout := time.Duration(p.cfg.Checks.ICMPTimeoutSeconds) * time.Second
var results []resultDTO
for _, port := range a.Ports {
res := checkrunner.TCPConnect(a.IPAddress, port, tcpTimeout)(ctx)
record := func(res checkrunner.Result) {
results = append(results, resultDTO{
IPID: a.IPID, IPAddress: a.IPAddress, CheckType: res.CheckType,
Success: res.Success, LatencyMS: res.LatencyMS, Detail: res.Detail,
CheckedAt: res.CheckedAt.Format(time.RFC3339Nano),
})
}
for _, port := range a.Ports {
record(checkrunner.TCPConnect(a.IPAddress, port, tcpTimeout)(ctx))
for _, extra := range extraChecksForPort(a.IPAddress, port, tcpTimeout) {
record(extra(ctx))
}
}
if a.ICMP {
res := checkrunner.ICMPEcho(a.IPAddress, p.cfg.Checks.ICMPCount, icmpTimeout)(ctx)
results = append(results, resultDTO{
IPID: a.IPID, IPAddress: a.IPAddress, CheckType: res.CheckType,
Success: res.Success, LatencyMS: res.LatencyMS, Detail: res.Detail,
CheckedAt: res.CheckedAt.Format(time.RFC3339Nano),
})
record(checkrunner.ICMPEcho(a.IPAddress, p.cfg.Checks.ICMPCount, icmpTimeout)(ctx))
}
if len(results) > 0 {
results[len(results)-1].Complete = true
+33
View File
@@ -94,3 +94,36 @@ func TestPollOnceSkipsAssignmentsWhenHeartbeatFails(t *testing.T) {
t.Fatalf("expected only [heartbeat] (assignments skipped on heartbeat failure), got %v", calls)
}
}
// TestExtraChecksForPort confirms only ports 443 (TLS) and 22 (SSH) get an
// extra protocol check beyond the baseline TCPConnect every port gets.
func TestExtraChecksForPort(t *testing.T) {
// Use a host nothing is listening on so the extra check's own network
// call fails fast — only its CheckType is asserted here, the
// port->check mapping, not real connectivity.
closedHost := "127.0.0.1"
cases := []struct {
port int
wantLen int
wantCheckType string
}{
{port: 443, wantLen: 1, wantCheckType: "tls-443"},
{port: 22, wantLen: 1, wantCheckType: "ssh"},
{port: 80, wantLen: 0},
{port: 8080, wantLen: 0},
}
for _, tc := range cases {
extra := extraChecksForPort(closedHost, tc.port, 100*time.Millisecond)
if len(extra) != tc.wantLen {
t.Fatalf("port %d: expected %d extra checks, got %d", tc.port, tc.wantLen, len(extra))
}
if tc.wantLen == 0 {
continue
}
res := extra[0](context.Background())
if res.CheckType != tc.wantCheckType {
t.Fatalf("port %d: expected check type %q, got %q", tc.port, tc.wantCheckType, res.CheckType)
}
}
}