Skip to content

Instantly share code, notes, and snippets.

@YoRyan
Created May 7, 2025 11:24
Show Gist options
  • Save YoRyan/77e8a4eae15dbc728f113c1873f66492 to your computer and use it in GitHub Desktop.
Save YoRyan/77e8a4eae15dbc728f113c1873f66492 to your computer and use it in GitHub Desktop.
Quick network tester for flaky dual-stack IPv4/IPv6 service.
package main
import (
"context"
"fmt"
"net"
"time"
)
const timeout = 3 * time.Second
type test struct {
name string
address string
}
func main() {
tests := []test{
{"IPv4", "1.1.1.1"},
{"IPv6", "[2606:4700:4700::1111]"},
}
results := make([]chan string, len(tests))
for i, t := range tests {
c := make(chan string)
results[i] = c
go func() {
c <- testDNS(t)
}()
}
for i, c := range results {
t := tests[i]
fmt.Println(t.name, ":", <-c)
}
}
func testDNS(t test) string {
r := new(net.Resolver)
r.PreferGo = true
r.StrictErrors = true
r.Dial = func(ctx context.Context, network, _ string) (net.Conn, error) {
d := new(net.Dialer)
d.FallbackDelay = -1
return d.DialContext(ctx, "udp", t.address+":53")
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
_, err := r.LookupHost(ctx, "cloudflare.com")
if err != nil {
return "FAIL: " + err.Error()
} else {
return "OK"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment