src

Go monorepo.
Log | Files | Refs

commit 4227303c59f462e2c8f52528a350d6b2a053d951
parent e0c884408bea8087122d2e60e689ba1873aa3a61
Author: dwrz <dwrz@dwrz.net>
Date:   Fri,  2 Dec 2022 14:19:16 +0000

Add digip pkg

Diffstat:
Apkg/digip/digip.go | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apkg/digip/digip_test.go | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 139 insertions(+), 0 deletions(-)

diff --git a/pkg/digip/digip.go b/pkg/digip/digip.go @@ -0,0 +1,85 @@ +package digip + +import ( + "context" + "fmt" + "net" + "os/exec" + "strings" +) + +type Version string + +const ( + V4 = "-4" + V6 = "-6" +) + +type Record string + +const ( + A = "A" + AAAA = "AAAA" +) + +// dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com +// dig -6 TXT +short o-o.myaddr.l.google.com @ns1.google.com +func Google(ctx context.Context, v Version) (net.IP, error) { + cmd := exec.CommandContext( + ctx, + "dig", + "TXT", + "+short", + string(v), + "o-o.myaddr.l.google.com", + "@ns1.google.com", + ) + output, err := cmd.CombinedOutput() + if err != nil { + return nil, fmt.Errorf("dig failed: %v", err) + } + if string(output) == "" { + return nil, fmt.Errorf("dig failed: no output") + } + if len(output) < 2 { + return nil, fmt.Errorf( + "dig failed: unexpected output: %s", string(output), + ) + } + + // Google returns the IP with quotes. + // We need to trim the quotes. + trimmed := strings.TrimSpace(string(output)) + ip := net.ParseIP(trimmed[1 : len(trimmed)-1]) + + return ip, nil +} + +// dig A +short myip.opendns.com @resolver1.opendns.com +// dig AAAA +short myip.opendns.com @resolver1.opendns.com +func OpenDNS(ctx context.Context, r Record) (net.IP, error) { + cmd := exec.CommandContext( + ctx, + "dig", + "+short", + string(r), + "myip.opendns.com", + "@resolver1.opendns.com", + ) + output, err := cmd.CombinedOutput() + if err != nil { + return nil, fmt.Errorf("dig failed: %v", err) + } + if string(output) == "" { + return nil, fmt.Errorf("dig failed: no output") + } + if len(output) < 2 { + return nil, fmt.Errorf( + "dig failed: unexpected output: %s", string(output), + ) + } + + ip := net.ParseIP(strings.TrimSpace(string(output))) + + return ip, nil +} diff --git a/pkg/digip/digip_test.go b/pkg/digip/digip_test.go @@ -0,0 +1,54 @@ +package digip + +import ( + "context" + "testing" +) + +func TestGoogleV4(t *testing.T) { + ctx := context.Background() + + ip, err := Google(ctx, V4) + if err != nil { + t.Errorf("google v4 failed: %v", err) + return + } + + t.Logf("got ip: %s", ip) +} + +func TestGoogleV6(t *testing.T) { + ctx := context.Background() + + ip, err := Google(ctx, V6) + if err != nil { + t.Errorf("google v6 failed: %v", err) + return + } + + t.Logf("got ip: %s", ip) +} + +func TestOpenDNSV4(t *testing.T) { + ctx := context.Background() + + ip, err := OpenDNS(ctx, A) + if err != nil { + t.Errorf("opendns A failed: %v", err) + return + } + + t.Logf("got ip: %s", ip) +} + +func TestOpenDNSV6(t *testing.T) { + ctx := context.Background() + + ip, err := OpenDNS(ctx, AAAA) + if err != nil { + t.Errorf("opendns AAAA failed: %v", err) + return + } + + t.Logf("got ip: %s", ip) +}