Skip to content
Merged
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,28 @@ The host list has a **History** column with the last 20 probes (oldest on the le

Latency spikes and loss bursts are visible at a glance. The host details and JSON/CSV output include RTT p50/p95/p99 over the last 100 probes.

## Path and Peer Changes

During maintenance, a route change, a DNS / load balancer switchover or a certificate replacement often keeps the target reachable but changes *what* answers. The TUI host detail panel (`v`) shows the facts that reveal this:

```
Reply TTL: 52 (≈12 hops, est.), range 52–54
TTL Changes: 1
09:12:03 TTL 54→52 (+2 hops)

Remote: 1.1.1.1:443
TLS: TLS 1.3
Certificate: CN example.com / Issuer R3 / expires 2027-01-02 (in 48d)
```

- **ICMP**: the TTL (IPv4) / hop limit (IPv6) of the last echo reply, the hop count estimated from it, the range over the last 100 probes (shown when replies currently arrive with different TTLs, e.g. over several paths) and the most recent TTL changes since start or reset (`+2 hops` = the reply crossed two more hops). A change is recorded once a new TTL is seen on 3 consecutive successful replies, timed at the first of them; replies alternating between ECMP paths of unequal length show up as a range instead of a stream of changes.
- **HTTP(S)**: the IP:port of the connection that served the latest final response (after redirects), including responses rejected by `expect_codes` / `expect_body` such as a 503 from a new peer, and the TLS version and leaf certificate (subject CN, issuer, expiry; yellow within 30 days, red within 7 days or expired). The certificate is shown even with `skip_verify` (the default).

Caveats:
- The hop count is an **estimate of the return path** (responder to mping): the responder's initial TTL is guessed as the smallest of 64, 128 and 255 that fits. The forward path can differ (asymmetric routing) and cannot be seen in an echo reply; use the path view (`p`) for that.
- A change to a path of the same length (e.g. another ECMP branch) does not change the TTL; it only shows in the path view.
- Windows does not report the reply TTL, so the ICMP lines are not shown there.

## Target Expansion

mping supports powerful target expansion syntax for monitoring large numbers of hosts efficiently:
Expand Down
186 changes: 186 additions & 0 deletions internal/prober/conn_info_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package prober

import (
"fmt"
"net"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)

func TestEstimateHops(t *testing.T) {
tests := []struct {
ttl int
hops int
wantOK bool
}{
{0, 0, false},
{-1, 0, false},
{256, 0, false},
{64, 0, true},
{54, 10, true},
{1, 63, true},
{65, 63, true},
{128, 0, true},
{117, 11, true},
{129, 126, true},
{255, 0, true},
{243, 12, true},
}
for _, tt := range tests {
hops, ok := EstimateHops(tt.ttl)
if hops != tt.hops || ok != tt.wantOK {
t.Errorf("EstimateHops(%d) = %d, %v; want %d, %v", tt.ttl, hops, ok, tt.hops, tt.wantOK)
}
}
}

// ttlFakeConn reports a fixed TTL for every packet, like icmpConn does with
// control messages enabled.
type ttlFakeConn struct {
*fakeICMPConn
ttl int
}

func (c *ttlFakeConn) ReadFromTTL(b []byte) (int, int, net.Addr, error) {
n, addr, err := c.ReadFrom(b)
return n, c.ttl, addr, err
}

func icmpReplyTTL(t *testing.T, conn packetConn) int {
t.Helper()
p := newTestProber(t, ICMPV4, conn, []byte("fake"), "icmpv4")
if err := p.Accept("icmpv4://127.0.0.1"); err != nil {
t.Fatal(err)
}
r := make(chan *Event, 100)
done := make(chan error, 1)
go func() { done <- p.Start(r, time.Hour, time.Second) }()
defer func() {
p.Stop()
<-done
}()
for {
select {
case e := <-r:
if e.Result != SUCCESS {
continue
}
if e.Details == nil || e.Details.ICMP == nil {
t.Fatal("no ICMP details")
}
return e.Details.ICMP.ReplyTTL
case <-time.After(3 * time.Second):
t.Fatal("no reply")
}
}
}

func TestICMPProberReplyTTL(t *testing.T) {
if got := icmpReplyTTL(t, &ttlFakeConn{fakeICMPConn: newFakeICMPConn(), ttl: 54}); got != 54 {
t.Errorf("ReplyTTL = %d, want 54", got)
}
// A connection without TTL support leaves it unknown
if got := icmpReplyTTL(t, newFakeICMPConn()); got != 0 {
t.Errorf("ReplyTTL without TTL support = %d, want 0", got)
}
}

func TestHTTPProberConnInfoTLS(t *testing.T) {
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "ok")
}))
defer srv.Close()
host := strings.TrimPrefix(srv.URL, "https://")
cert := srv.Certificate()

for _, keepAlive := range []bool{false, true} {
t.Run(fmt.Sprintf("keep_alive=%v", keepAlive), func(t *testing.T) {
p := NewHTTPProber(HTTPS, &HTTPConfig{KeepAlive: keepAlive}, "https")
defer p.Stop()
// Twice, so that keep_alive reuses the connection
for i := range 2 {
e := probeOnce(t, p, "https://"+host)
if e.Result != SUCCESS {
t.Fatalf("probe %d: result = %v (%s)", i, e.Result, e.Message)
}
h := e.Details.HTTP
if h.RemoteAddr != host {
t.Errorf("probe %d: RemoteAddr = %q, want %q", i, h.RemoteAddr, host)
}
if h.TLS == nil {
t.Fatalf("probe %d: no TLS details", i)
}
if !strings.HasPrefix(h.TLS.Version, "TLS 1.") {
t.Errorf("Version = %q", h.TLS.Version)
}
if !h.TLS.NotAfter.Equal(cert.NotAfter) {
t.Errorf("NotAfter = %v, want %v", h.TLS.NotAfter, cert.NotAfter)
}
wantIssuer := cert.Issuer.CommonName
if wantIssuer == "" {
wantIssuer = cert.Issuer.String()
}
if h.TLS.Issuer != wantIssuer || h.TLS.Issuer == "" {
t.Errorf("Issuer = %q, want %q", h.TLS.Issuer, wantIssuer)
}
if h.TLS.SubjectCN != cert.Subject.CommonName {
t.Errorf("SubjectCN = %q, want %q", h.TLS.SubjectCN, cert.Subject.CommonName)
}
}
})
}
}

// A response that is received but rejected still reports its peer, so a
// switchover to a peer answering 503 is visible.
func TestHTTPProberConnInfoOnRejectedResponse(t *testing.T) {
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprint(w, "down")
}))
defer srv.Close()
host := strings.TrimPrefix(srv.URL, "https://")

for name, cfg := range map[string]*HTTPConfig{
"status": {ExpectCodes: "200"},
"body": {ExpectBody: "ok"},
} {
t.Run(name, func(t *testing.T) {
e := probeOnce(t, NewHTTPProber(HTTPS, cfg, "https"), "https://"+host)
if e.Result != FAILED {
t.Fatalf("result = %v (%s), want FAILED", e.Result, e.Message)
}
if e.Details == nil || e.Details.HTTP == nil {
t.Fatal("rejected response without HTTP details")
}
h := e.Details.HTTP
if h.RemoteAddr != host || h.TLS == nil || h.StatusCode != http.StatusServiceUnavailable {
t.Errorf("details = %+v", h)
}
})
}
}

func TestHTTPProberConnInfoRedirect(t *testing.T) {
final := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "ok")
}))
defer final.Close()
first := httptest.NewServer(http.RedirectHandler(final.URL+"/x", http.StatusFound))
defer first.Close()

p := NewHTTPProber(HTTP, &HTTPConfig{}, "http")
e := probeOnce(t, p, "http://"+strings.TrimPrefix(first.URL, "http://"))
if e.Result != SUCCESS {
t.Fatalf("result = %v (%s)", e.Result, e.Message)
}
if want := strings.TrimPrefix(final.URL, "http://"); e.Details.HTTP.RemoteAddr != want {
t.Errorf("RemoteAddr = %q, want final server %q", e.Details.HTTP.RemoteAddr, want)
}
if e.Details.HTTP.TLS != nil {
t.Errorf("plain HTTP has TLS details: %+v", e.Details.HTTP.TLS)
}
}
19 changes: 19 additions & 0 deletions internal/prober/details.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package prober

import "time"

// Probe detail information (TargetIP and TCPDetails removed)
type ProbeDetails struct {
ProbeType string `json:"probe_type"`
Expand All @@ -19,6 +21,9 @@ type ICMPDetails struct {
ICMPCode int `json:"icmp_code"`
Checksum uint16 `json:"checksum"`
Payload string `json:"payload"` // Actual payload content with length limit
// ReplyTTL is the TTL (IPv4) or hop limit (IPv6) of the echo reply as
// received, 0 when unknown (e.g. on Windows). See EstimateHops.
ReplyTTL int `json:"reply_ttl,omitempty"`
}

type HTTPDetails struct {
Expand All @@ -28,6 +33,20 @@ type HTTPDetails struct {
SizeTruncated bool `json:"size_truncated,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Redirects []string `json:"redirects,omitempty"`
// RemoteAddr is the IP:port of the connection that served the final
// response (after redirects).
RemoteAddr string `json:"remote_addr,omitempty"`
// TLS describes the final response's TLS connection; nil for plain HTTP.
TLS *TLSDetails `json:"tls,omitempty"`
}

// TLSDetails summarizes a TLS connection and its leaf certificate. It is
// recorded whether or not the certificate was verified.
type TLSDetails struct {
Version string `json:"version"` // e.g. "TLS 1.3"
SubjectCN string `json:"subject_cn,omitempty"` // leaf certificate subject CN
Issuer string `json:"issuer,omitempty"` // issuer CN, or the full issuer name without a CN
NotAfter time.Time `json:"not_after,omitzero"` // leaf certificate expiry
}

type DNSDetails struct {
Expand Down
Loading
Loading