Skip to content

Commit

Permalink
all: fix go-flags
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Aug 19, 2024
1 parent 8fec401 commit 59750f9
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Application Options:
--private-subnets= Private subnets to use for reverse DNS lookups of private addresses
--bogus-nxdomain= Transform the responses containing at least a single IP that matches specified addresses and CIDRs into
NXDOMAIN. Can be specified multiple times.
--hosts-files= List of paths to the hosts files to sresolve from.
--hosts-files= List of paths to the hosts files relative to the root, can be specified multiple times
--timeout= Timeout for outbound DNS queries to remote upstream servers in a human-readable form (default: 10s)
--cache-min-ttl= Minimum TTL value for DNS entries, in seconds. Capped at 3600. Artificially extending TTLs should only be
done with careful consideration.
Expand All @@ -101,7 +101,7 @@ Application Options:
--edns Use EDNS Client Subnet extension
--dns64 If specified, dnsproxy will act as a DNS64 server
--use-private-rdns If specified, use private upstreams for reverse DNS lookups of private addresses
--hosts-file-enabled If specified, use hosts files for resolving
--hosts-file-enabled= If specified, use hosts files for resolving (default: true)
Help Options:
-h, --help Show this help message
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/ameshkov/dnsstamps v1.0.3
github.com/beefsack/go-rate v0.0.0-20220214233405-116f4ca011a0
github.com/bluele/gcache v0.0.2
github.com/jessevdk/go-flags v1.5.0
github.com/jessevdk/go-flags v1.6.1
github.com/miekg/dns v1.1.58
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/quic-go/quic-go v0.44.0
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20240130152714-0ed6a68c8d9e h1:E+3PBMCXn0ma79O7iCrne0iUpKtZ7rIcZvoz+jNtNtw=
github.com/google/pprof v0.0.0-20240130152714-0ed6a68c8d9e/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/jessevdk/go-flags v1.6.1 h1:Cvu5U8UGrLay1rZfv/zP7iLpSHGUZ/Ou68T0iX1bBK4=
github.com/jessevdk/go-flags v1.6.1/go.mod h1:Mk8T1hIAWpOiJiHa9rJASDK2UGWji0EuPGBnNLMooyc=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down Expand Up @@ -64,7 +64,6 @@ golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (opts *Options) initUpstreams(
Logger: l,
HTTPVersions: httpVersions,
InsecureSkipVerify: opts.Insecure,
Timeout: timeout,
Timeout: timeout.Duration,
}
boot, err := initBootstrap(ctx, l, opts.BootstrapDNS, bootOpts)
if err != nil {
Expand All @@ -164,7 +164,7 @@ func (opts *Options) initUpstreams(
HTTPVersions: httpVersions,
InsecureSkipVerify: opts.Insecure,
Bootstrap: boot,
Timeout: timeout,
Timeout: timeout.Duration,
}
upstreams := loadServersList(opts.Upstreams)

Expand All @@ -177,7 +177,7 @@ func (opts *Options) initUpstreams(
Logger: l,
HTTPVersions: httpVersions,
Bootstrap: boot,
Timeout: min(defaultLocalTimeout, timeout),
Timeout: min(defaultLocalTimeout, timeout.Duration),
}
privateUpstreams := loadServersList(opts.PrivateRDNSUpstreams)

Expand Down
55 changes: 51 additions & 4 deletions internal/cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,49 @@ const (
argVersion = "--version"
)

// decodableBool is a boolean that can be unmarshaled from a flags.
//
// TODO(e.burkov): This is a workaround for go-flags, see
// https://github.com/AdguardTeam/dnsproxy/issues/182.
type decodableBool struct {
value bool
}

// type check
var _ goFlags.Unmarshaler = (*decodableBool)(nil)

// UnmarshalFlag implements the [goFlags.Unmarshaler] interface for
// *decodableBool.
func (b *decodableBool) UnmarshalFlag(text string) (err error) {
switch text {
case "true":
b.value = true
case "false":
b.value = false
default:
return fmt.Errorf("invalid boolean value: %s", text)
}

return nil
}

// decodableDuration is a duration that can be unmarshaled from a flags.
//
// TODO(e.burkov): This is a workaround for go-flags, see
// https://github.com/AdguardTeam/dnsproxy/issues/182.
type decodableDuration struct {
timeutil.Duration
}

// type check
var _ goFlags.Unmarshaler = (*decodableDuration)(nil)

// UnmarshalFlag implements the [goFlags.Unmarshaler] interface for
// *decodableDuration.
func (d *decodableDuration) UnmarshalFlag(text string) (err error) {
return d.UnmarshalText([]byte(text))
}

// Options represents console arguments. For further additions, please do not
// use the default option since it will cause some problems when config files
// are used.
Expand Down Expand Up @@ -109,11 +152,11 @@ type Options struct {
BogusNXDomain []string `yaml:"bogus-nxdomain" long:"bogus-nxdomain" description:"Transform the responses containing at least a single IP that matches specified addresses and CIDRs into NXDOMAIN. Can be specified multiple times."`

// HostsFiles is the list of paths to the hosts files to resolve from.
HostsFiles []string `yaml:"hosts-files" long:"hosts-files" description:"List of paths to the hosts files to resolve from."`
HostsFiles []string `yaml:"hosts-files" long:"hosts-files" description:"List of paths to the hosts files relative to the root, can be specified multiple times"`

// Timeout for outbound DNS queries to remote upstream servers in a
// human-readable form. Default is 10s.
Timeout timeutil.Duration `yaml:"timeout" long:"timeout" description:"Timeout for outbound DNS queries to remote upstream servers in a human-readable form" default:"10s"`
Timeout decodableDuration `yaml:"timeout" long:"timeout" description:"Timeout for outbound DNS queries to remote upstream servers in a human-readable form" default:"10s"`

// CacheMinTTL is the minimum TTL value for caching DNS entries, in seconds.
// It overrides the TTL value from the upstream server, if the one is less.
Expand Down Expand Up @@ -195,7 +238,7 @@ type Options struct {

// HostsFileEnabled controls whether hosts files are used for resolving or
// not.
HostsFileEnabled bool `yaml:"hosts-file-enabled" long:"hosts-file-enabled" description:"If specified, use hosts files for resolving" optional:"yes" optional-value:"true"`
HostsFileEnabled decodableBool `yaml:"hosts-file-enabled" long:"hosts-file-enabled" description:"If specified, use hosts files for resolving" default:"true"`
}

// parseOptions returns options parsed from the command args or config file.
Expand Down Expand Up @@ -243,10 +286,14 @@ func parseOptions() (opts *Options, exitCode int, err error) {
// hostsFiles returns the list of hosts files to resolve from. It's empty if
// resolving from hosts files is disabled.
func (opts *Options) hostsFiles(ctx context.Context, l *slog.Logger) (paths []string, err error) {
if !opts.HostsFileEnabled {
if !opts.HostsFileEnabled.value {
l.DebugContext(ctx, "hosts files are disabled")

return nil, nil
}

l.DebugContext(ctx, "hosts files are enabled")

if len(opts.HostsFiles) > 0 {
return opts.HostsFiles, nil
}
Expand Down
9 changes: 8 additions & 1 deletion internal/handler/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"context"
"fmt"
"io/fs"
"net/netip"
"slices"
Expand Down Expand Up @@ -42,6 +43,7 @@ func readHosts(fsys fs.FS, paths []string) (strg hostsfile.Storage, err error) {
for _, path := range paths {
err = readHostsFile(defaultStrg, fsys, path)
if err != nil {
// Don't wrap the error since it's informative enough as is.
errs = append(errs, err)
}
}
Expand Down Expand Up @@ -70,7 +72,12 @@ func readHostsFile(strg *hostsfile.DefaultStorage, fsys fs.FS, path string) (err
}
defer func() { err = errors.WithDeferred(err, f.Close()) }()

return hostsfile.Parse(strg, f, nil)
err = hostsfile.Parse(strg, f, nil)
if err != nil {
return fmt.Errorf("parsing hosts file %q: %w", path, err)
}

return nil
}

// resolveFromHosts resolves the DNS query from the hosts file. It fills the
Expand Down

0 comments on commit 59750f9

Please sign in to comment.