code.dwrz.net

Go monorepo.
Log | Files | Refs

commit 60e4d329c8deae3336cd72bc6b02027be8dbb4c0
parent 067012781ca8481713ae66ac1fcf29e56d7d5016
Author: dwrz <dwrz@dwrz.net>
Date:   Fri, 14 Oct 2022 00:33:20 +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) +}