This example demonstrates how to use ForEachAddr to create a TLS probe for each IP address in the DNS record of a given hostname.
()
| 32 | // This example demonstrates how to use ForEachAddr to create a TLS probe for |
| 33 | // each IP address in the DNS record of a given hostname. |
| 34 | func ExampleForEachAddr() { |
| 35 | flag.Parse() |
| 36 | |
| 37 | p := prober.New().WithSpread(true) |
| 38 | if *oneshot { |
| 39 | p = p.WithOnce(true) |
| 40 | } |
| 41 | |
| 42 | // This function is called every time we discover a new IP address to check. |
| 43 | makeTLSProbe := func(addr netip.Addr) []*prober.Probe { |
| 44 | pf := prober.TLSWithIP(netip.AddrPortFrom(addr, 443), &tls.Config{ServerName: *hostname}) |
| 45 | if *verbose { |
| 46 | logger := logger.WithPrefix(log.Printf, fmt.Sprintf("[tls %s]: ", addr)) |
| 47 | pf = probeLogWrapper(logger, pf) |
| 48 | } |
| 49 | |
| 50 | probe := p.Run(fmt.Sprintf("website/%s/tls", addr), every30s, nil, pf) |
| 51 | return []*prober.Probe{probe} |
| 52 | } |
| 53 | |
| 54 | // Determine whether to use IPv4 or IPv6 based on whether we can create |
| 55 | // an IPv6 listening socket on localhost. |
| 56 | sock, err := net.Listen("tcp", "[::1]:0") |
| 57 | supportsIPv6 := err == nil |
| 58 | if sock != nil { |
| 59 | sock.Close() |
| 60 | } |
| 61 | |
| 62 | networks := []string{"ip4"} |
| 63 | if supportsIPv6 { |
| 64 | networks = append(networks, "ip6") |
| 65 | } |
| 66 | |
| 67 | var vlogf logger.Logf = logger.Discard |
| 68 | if *verbose { |
| 69 | vlogf = log.Printf |
| 70 | } |
| 71 | |
| 72 | // This is the outer probe that resolves the hostname and creates a new |
| 73 | // TLS probe for each IP. |
| 74 | p.Run("website/dns", every30s, nil, prober.ForEachAddr(*hostname, makeTLSProbe, prober.ForEachAddrOpts{ |
| 75 | Logf: vlogf, |
| 76 | Networks: networks, |
| 77 | })) |
| 78 | |
| 79 | defer log.Printf("done") |
| 80 | |
| 81 | // Wait until all probes have run if we're running in oneshot mode. |
| 82 | if *oneshot { |
| 83 | p.Wait() |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | // Otherwise, wait until we get a signal. |
| 88 | sigCh := make(chan os.Signal, 1) |
| 89 | signal.Notify(sigCh, os.Interrupt) |
| 90 | <-sigCh |
| 91 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…