From e343955ca4828d71af32f43663d8fb0016ea67e1 Mon Sep 17 00:00:00 2001 From: Sam Therapy Date: Mon, 31 Oct 2022 17:06:04 +0100 Subject: [PATCH] fix(https): add default endpoint Signed-off-by: Sam Therapy --- cmd/cli.go | 8 ++++++-- cmd/cli_test.go | 22 ++++++++++++++++++++++ cmd/misc.go | 15 +++++++++++++-- cmd/misc_test.go | 2 +- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/cmd/cli.go b/cmd/cli.go index 8b084e5..076c44c 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -26,10 +26,10 @@ func ParseCLI(args []string, version string) (*util.Options, error) { Usage: awl name [@server] [record] domain, IP address, phone number defaults to A - + Arguments may be in any order, including flags. Dig-like +[no]commands are also supported, see dig(1) or dig -h - + Options:`) flagSet.PrintDefaults() } @@ -174,6 +174,10 @@ func ParseCLI(args []string, version string) (*util.Options, error) { ZFlag: uint16(mbz & 0x7FFF), Padding: *padding, }, + HTTPSOptions: util.HTTPSOptions{ + Endpoint: "/dns-query", + Get: false, + }, } // TODO: DRY diff --git a/cmd/cli_test.go b/cmd/cli_test.go index 7e25649..e18d7ee 100644 --- a/cmd/cli_test.go +++ b/cmd/cli_test.go @@ -149,6 +149,28 @@ func TestRetries(t *testing.T) { } } +func TestSetHTTPS(t *testing.T) { + t.Parallel() + + args := [][]string{ + {"awl", "-H", "@dns.froth.zone/dns-query"}, + {"awl", "+https", "@dns.froth.zone"}, + } + for _, test := range args { + test := test + + t.Run(test[1], func(t *testing.T) { + t.Parallel() + + opt, err := cli.ParseCLI(test, "TEST") + + assert.NilError(t, err) + assert.Equal(t, opt.Request.Server, "dns.froth.zone") + assert.Equal(t, opt.HTTPSOptions.Endpoint, "/dns-query") + }) + } +} + func FuzzFlags(f *testing.F) { testcases := []string{"git.froth.zone", "", "!12345", "google.com.edu.org.fr"} diff --git a/cmd/misc.go b/cmd/misc.go index 799a377..2b0fb87 100644 --- a/cmd/misc.go +++ b/cmd/misc.go @@ -50,7 +50,18 @@ func ParseMiscArgs(args []string, opts *util.Options) error { case strings.HasPrefix(arg, "udp://"): opts.Request.Server = strings.TrimPrefix(arg, "udp://") default: - opts.Request.Server = arg + // Allow HTTPS queries to have a fallback default + if opts.HTTPS { + server, endpoint, isSplit := strings.Cut(arg, "/") + if isSplit { + opts.HTTPSOptions.Endpoint = "/" + endpoint + opts.Request.Server = server + } else { + opts.Request.Server = server + } + } else { + opts.Request.Server = arg + } } // Dig-style +queries @@ -114,7 +125,7 @@ func ParseMiscArgs(args []string, opts *util.Options) error { case opts.TLS: opts.Request.Server = "dns.google" case opts.HTTPS: - opts.Request.Server = "https://dns.cloudflare.com/dns-query" + opts.Request.Server = "https://dns.cloudflare.com" case opts.QUIC: opts.Request.Server = "dns.adguard.com" default: diff --git a/cmd/misc_test.go b/cmd/misc_test.go index fa20dbe..4cef6dc 100644 --- a/cmd/misc_test.go +++ b/cmd/misc_test.go @@ -88,7 +88,7 @@ func TestDefaultServer(t *testing.T) { }{ {"DNSCrypt", "sdns://AQMAAAAAAAAAETk0LjE0MC4xNC4xNDo1NDQzINErR_JS3PLCu_iZEIbq95zkSV2LFsigxDIuUso_OQhzIjIuZG5zY3J5cHQuZGVmYXVsdC5uczEuYWRndWFyZC5jb20"}, {"TLS", "dns.google"}, - {"HTTPS", "https://dns.cloudflare.com/dns-query"}, + {"HTTPS", "https://dns.cloudflare.com"}, {"QUIC", "dns.adguard.com"}, }