Skip to content

Commit

Permalink
feat: add default server environment variable
Browse files Browse the repository at this point in the history
Closes: #46
  • Loading branch information
natesales committed Mar 31, 2023
1 parent 9efdbac commit adc6710
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ go install
go install -ldflags="-s -w -X main.version=release"
```

### Server Selection

`q` will use a server from the following sources, in order:
1. `@server` argument (e.g. `@9.9.9.9` or `@https://dns.google/dns-query`)
2. `Q_DEFAULT_SERVER` environment variable
3. `/etc/resolv.conf`

### Feature Comparison

#### DNS Transport Protocols
Expand Down
22 changes: 15 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"gopkg.in/yaml.v2"
)

const defaultServerVar = "Q_DEFAULT_SERVER"

// CLI flags
type optsTemplate struct {
Name string `short:"q" long:"qname" description:"Query name"`
Expand Down Expand Up @@ -497,17 +499,23 @@ All long form (--) flags can be toggled with the dig-standard +[no]flag notation

// Set default DNS server
if opts.Server == "" {
conf, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
opts.Server = "https://cloudflare-dns.com/dns-query"
log.Debugf("no server set, using %s", opts.Server)
if os.Getenv(defaultServerVar) != "" {
opts.Server = os.Getenv(defaultServerVar)
log.Debugf("Using %s from %s environment variable", opts.Server, defaultServerVar)
} else {
if len(conf.Servers) == 0 {
log.Debugf("No server specified or %s set, using /etc/resolv.conf", defaultServerVar)
conf, err := dns.ClientConfigFromFile("/etc/resolv.conf")
if err != nil {
opts.Server = "https://cloudflare-dns.com/dns-query"
log.Debugf("no server set, using %s", opts.Server)
} else {
opts.Server = conf.Servers[0]
log.Debugf("found server %s from /etc/resolv.conf", opts.Server)
if len(conf.Servers) == 0 {
opts.Server = "https://cloudflare-dns.com/dns-query"
log.Debugf("no server set, using %s", opts.Server)
} else {
opts.Server = conf.Servers[0]
log.Debugf("found server %s from /etc/resolv.conf", opts.Server)
}
}
}
}
Expand Down

0 comments on commit adc6710

Please sign in to comment.