Skip to content

Commit

Permalink
refactor: fix inverted flags (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
natesales committed Dec 13, 2023
1 parent 0f1a98f commit 2c44df0
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 98 deletions.
130 changes: 65 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,73 +34,73 @@ Usage:
All long form (--) flags can be toggled with the dig-standard +[no]flag notation.
Application Options:
-q, --qname= Query name
-s, --server= DNS server(s)
-t, --type= RR type (e.g. A, AAAA, MX, etc.) or type integer
-x, --reverse Reverse lookup
-d, --dnssec Set the DO (DNSSEC OK) bit in the OPT record
-n, --nsid Set EDNS0 NSID opt
--subnet= Set EDNS0 client subnet
-c, --chaos Use CHAOS query class
-C= Set query class (default: IN 0x01) (default: 1)
-p, --odoh-proxy= ODoH proxy
--timeout= Query timeout (default: 10s)
--pad Set EDNS0 padding
--http3 Use HTTP/3 for DoH
--no-id-check Disable checking of DNS response ID
--no-reuse-conn Use a new connection for each query
--txtconcat Concatenate TXT responses
--qid= Set query ID (-1 for random) (default: -1)
-b, --bootstrap-server= DNS server to use for bootstrapping
--bootstrap-timeout= Bootstrapping timeout (default: 5s)
--recaxfr Perform recursive AXFR
-f, --format= Output format (pretty, column, json, yaml, raw) (default: pretty)
--pretty-ttls Format TTLs in human readable format (default: true)
--short-ttls Remove zero components of pretty TTLs. (24h0m0s->24h) (default: true)
--color Enable color output
--question Show question section
--answer Show answer section (default: true)
--authority Show authority section
--additional Show additional section
-S, --stats Show time statistics
--all Show all sections and statistics
-w Resolve ASN/ASName for A and AAAA records
-r, --short Show record values only
-R, --resolve-ips Resolve PTR records for IP addresses in A and AAAA records
--aa Set AA (Authoritative Answer) flag in query
--ad Set AD (Authentic Data) flag in query
--cd Set CD (Checking Disabled) flag in query
--rd Set RD (Recursion Desired) flag in query (default: true)
--ra Set RA (Recursion Available) flag in query
--z Set Z (Zero) flag in query
--t Set TC (Truncated) flag in query
-i, --tls-no-verify Disable TLS certificate verification
--tls-server-name= TLS server name for host verification
--tls-min-version= Minimum TLS version to use (default: 1.0)
--tls-max-version= Maximum TLS version to use (default: 1.3)
--tls-next-protos= TLS next protocols for ALPN
--tls-cipher-suites= TLS cipher suites
--tls-curve-preferences= TLS curve preferences
--tls-client-cert= TLS client certificate file
--tls-client-key= TLS client key file
--tls-key-log-file= TLS key log file [$SSLKEYLOGFILE]
--http-user-agent= HTTP user agent
--http-method= HTTP method (default: GET)
--quic-alpn-tokens= QUIC ALPN tokens (default: doq, doq-i11)
--quic-no-pmtud Disable QUIC PMTU discovery
--quic-no-length-prefix Don't add RFC 9250 compliant length prefix
--dnscrypt-tcp Use TCP for DNSCrypt (default UDP)
--dnscrypt-udp-size= Maximum size of a DNS response this client can sent or receive (default: 0)
--dnscrypt-key= DNSCrypt public key
--dnscrypt-provider= DNSCrypt provider name
--default-rr-types= Default record types (default: A, AAAA, NS, MX, TXT, CNAME)
--udp-buffer= Set EDNS0 UDP size in query (default: 1232)
-v, --verbose Show verbose log messages
--trace Show trace log messages
-V, --version Show version and exit
-q, --qname= Query name
-s, --server= DNS server(s)
-t, --type= RR type (e.g. A, AAAA, MX, etc.) or type integer
-x, --reverse Reverse lookup
-d, --dnssec Set the DO (DNSSEC OK) bit in the OPT record
-n, --nsid Set EDNS0 NSID opt
--subnet= Set EDNS0 client subnet
-c, --chaos Use CHAOS query class
-C= Set query class (default: IN 0x01) (default: 1)
-p, --odoh-proxy= ODoH proxy
--timeout= Query timeout (default: 10s)
--pad Set EDNS0 padding
--http3 Use HTTP/3 for DoH
--id-check Check DNS response ID (default: true)
--reuse-conn Reuse connections across queries to the same server (default: true)
--txtconcat Concatenate TXT responses
--qid= Set query ID (-1 for random) (default: -1)
-b, --bootstrap-server= DNS server to use for bootstrapping
--bootstrap-timeout= Bootstrapping timeout (default: 5s)
--recaxfr Perform recursive AXFR
-f, --format= Output format (pretty, column, json, yaml, raw) (default: pretty)
--pretty-ttls Format TTLs in human readable format (default: true)
--short-ttls Remove zero components of pretty TTLs. (24h0m0s->24h) (default: true)
--color Enable color output
--question Show question section
--answer Show answer section (default: true)
--authority Show authority section
--additional Show additional section
-S, --stats Show time statistics
--all Show all sections and statistics
-w Resolve ASN/ASName for A and AAAA records
-r, --short Show record values only
-R, --resolve-ips Resolve PTR records for IP addresses in A and AAAA records
--aa Set AA (Authoritative Answer) flag in query
--ad Set AD (Authentic Data) flag in query
--cd Set CD (Checking Disabled) flag in query
--rd Set RD (Recursion Desired) flag in query (default: true)
--ra Set RA (Recursion Available) flag in query
--z Set Z (Zero) flag in query
--t Set TC (Truncated) flag in query
-i, --tls-insecure-skip-verify Disable TLS certificate verification
--tls-server-name= TLS server name for host verification
--tls-min-version= Minimum TLS version to use (default: 1.0)
--tls-max-version= Maximum TLS version to use (default: 1.3)
--tls-next-protos= TLS next protocols for ALPN
--tls-cipher-suites= TLS cipher suites
--tls-curve-preferences= TLS curve preferences
--tls-client-cert= TLS client certificate file
--tls-client-key= TLS client key file
--tls-key-log-file= TLS key log file [$SSLKEYLOGFILE]
--http-user-agent= HTTP user agent
--http-method= HTTP method (default: GET)
--pmtud PMTU discovery (default: true)
--quic-alpn-tokens= QUIC ALPN tokens (default: doq, doq-i11)
--quic-length-prefix Add RFC 9250 compliant length prefix (default: true)
--dnscrypt-tcp Use TCP for DNSCrypt (default UDP)
--dnscrypt-udp-size= Maximum size of a DNS response this client can sent or receive (default: 0)
--dnscrypt-key= DNSCrypt public key
--dnscrypt-provider= DNSCrypt provider name
--default-rr-types= Default record types (default: A, AAAA, NS, MX, TXT, CNAME)
--udp-buffer= Set EDNS0 UDP size in query (default: 1232)
-v, --verbose Show verbose log messages
--trace Show trace log messages
-V, --version Show version and exit
Help Options:
-h, --help Show this help message
-h, --help Show this help message
```

### Demo
Expand Down
43 changes: 28 additions & 15 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type Flags struct {
Timeout time.Duration `long:"timeout" description:"Query timeout" default:"10s"`
Pad bool `long:"pad" description:"Set EDNS0 padding"`
HTTP3 bool `long:"http3" description:"Use HTTP/3 for DoH"`
NoIDCheck bool `long:"no-id-check" description:"Disable checking of DNS response ID"`
NoReuseConn bool `long:"no-reuse-conn" description:"Use a new connection for each query"`
IDCheck bool `long:"id-check" description:"Check DNS response ID (default: true)"`
ReuseConn bool `long:"reuse-conn" description:"Reuse connections across queries to the same server (default: true)"`
TXTConcat bool `long:"txtconcat" description:"Concatenate TXT responses"`
ID int `long:"qid" description:"Set query ID (-1 for random)" default:"-1"`
BootstrapServer string `short:"b" long:"bootstrap-server" description:"DNS server to use for bootstrapping"`
Expand Down Expand Up @@ -59,25 +59,26 @@ type Flags struct {
Truncated bool `long:"t" description:"Set TC (Truncated) flag in query"`

// TLS parameters
TLSNoVerify bool `short:"i" long:"tls-no-verify" description:"Disable TLS certificate verification"`
TLSServerName string `long:"tls-server-name" description:"TLS server name for host verification"`
TLSMinVersion string `long:"tls-min-version" description:"Minimum TLS version to use" default:"1.0"`
TLSMaxVersion string `long:"tls-max-version" description:"Maximum TLS version to use" default:"1.3"`
TLSNextProtos []string `long:"tls-next-protos" description:"TLS next protocols for ALPN"`
TLSCipherSuites []string `long:"tls-cipher-suites" description:"TLS cipher suites"`
TLSCurvePreferences []string `long:"tls-curve-preferences" description:"TLS curve preferences"`
TLSClientCertificate string `long:"tls-client-cert" description:"TLS client certificate file"`
TLSClientKey string `long:"tls-client-key" description:"TLS client key file"`
TLSKeyLogFile string `long:"tls-key-log-file" env:"SSLKEYLOGFILE" description:"TLS key log file"`
TLSInsecureSkipVerify bool `short:"i" long:"tls-insecure-skip-verify" description:"Disable TLS certificate verification"`
TLSServerName string `long:"tls-server-name" description:"TLS server name for host verification"`
TLSMinVersion string `long:"tls-min-version" description:"Minimum TLS version to use" default:"1.0"`
TLSMaxVersion string `long:"tls-max-version" description:"Maximum TLS version to use" default:"1.3"`
TLSNextProtos []string `long:"tls-next-protos" description:"TLS next protocols for ALPN"`
TLSCipherSuites []string `long:"tls-cipher-suites" description:"TLS cipher suites"`
TLSCurvePreferences []string `long:"tls-curve-preferences" description:"TLS curve preferences"`
TLSClientCertificate string `long:"tls-client-cert" description:"TLS client certificate file"`
TLSClientKey string `long:"tls-client-key" description:"TLS client key file"`
TLSKeyLogFile string `long:"tls-key-log-file" env:"SSLKEYLOGFILE" description:"TLS key log file"`

// HTTP
HTTPUserAgent string `long:"http-user-agent" description:"HTTP user agent" default:""`
HTTPMethod string `long:"http-method" description:"HTTP method" default:"GET"`

PMTUD bool `long:"pmtud" description:"PMTU discovery (default: true)"`

// QUIC
QUICALPNTokens []string `long:"quic-alpn-tokens" description:"QUIC ALPN tokens" default:"doq" default:"doq-i11"` //nolint:golint,staticcheck
QUICNoPMTUD bool `long:"quic-no-pmtud" description:"Disable QUIC PMTU discovery"`
QUICNoLengthPrefix bool `long:"quic-no-length-prefix" description:"Don't add RFC 9250 compliant length prefix"`
QUICALPNTokens []string `long:"quic-alpn-tokens" description:"QUIC ALPN tokens" default:"doq" default:"doq-i11"` //nolint:golint,staticcheck
QUICLengthPrefix bool `long:"quic-length-prefix" description:"Add RFC 9250 compliant length prefix (default: true)"`

// DNSCrypt
DNSCryptTCP bool `long:"dnscrypt-tcp" description:"Use TCP for DNSCrypt (default UDP)"`
Expand Down Expand Up @@ -116,6 +117,18 @@ func ParsePlusFlags(opts *Flags, args []string) {
}
}

// SetDefaultTrueBools enables boolean flags that are true by default
func SetDefaultTrueBools(opts *Flags) {
v := reflect.Indirect(reflect.ValueOf(opts))
vT := v.Type()
for i := 0; i < v.NumField(); i++ {
defaultTrue := strings.Contains(vT.Field(i).Tag.Get("description"), "default: true")
if vT.Field(i).Type == reflect.TypeOf(true) && defaultTrue {
reflect.ValueOf(opts).Elem().Field(i).SetBool(true)
}
}
}

// ParseRRTypes parses a list of RR types in string format ("A", "AAAA", etc.) or integer format (1, 28, etc.)
func ParseRRTypes(t []string) (map[uint16]bool, error) {
rrTypes := make(map[uint16]bool, len(t))
Expand Down
9 changes: 3 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ var (
// clearOpts sets the default values for the CLI options
func clearOpts() {
opts = cli.Flags{}
opts.RecursionDesired = true
opts.ShowAnswer = true
opts.PrettyTTLs = true
opts.ShortTTLs = true
cli.SetDefaultTrueBools(&opts)

// Enable color output if stdout is a terminal
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 {
Expand Down Expand Up @@ -372,7 +369,7 @@ All long form (--) flags can be toggled with the dig-standard +[no]flag notation

// Create TLS config
tlsConfig := &tls.Config{
InsecureSkipVerify: opts.TLSNoVerify,
InsecureSkipVerify: opts.TLSInsecureSkipVerify,
ServerName: opts.TLSServerName,
MinVersion: tlsutil.Version(opts.TLSMinVersion, tls.VersionTLS10),
MaxVersion: tlsutil.Version(opts.TLSMaxVersion, tls.VersionTLS13),
Expand Down Expand Up @@ -438,7 +435,7 @@ All long form (--) flags can be toggled with the dig-standard +[no]flag notation
return err
}

if transportType != transport.TypeQUIC && !opts.NoIDCheck && reply.Id != msg.Id {
if transportType != transport.TypeQUIC && opts.IDCheck && reply.Id != msg.Id {
return fmt.Errorf("ID mismatch: expected %d, got %d", msg.Id, reply.Id)
}
replies = append(replies, reply)
Expand Down
18 changes: 9 additions & 9 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func newTransport(server string, transportType transport.Type, tlsConfig *tls.Co
Target: server,
Proxy: opts.ODoHProxy,
TLSConfig: tlsConfig,
ReuseConn: !opts.NoReuseConn,
ReuseConn: opts.ReuseConn,
}
} else {
log.Debugf("Using HTTP(s) transport: %s", server)
Expand All @@ -129,8 +129,8 @@ func newTransport(server string, transportType transport.Type, tlsConfig *tls.Co
Method: opts.HTTPMethod,
Timeout: opts.Timeout,
HTTP3: opts.HTTP3,
NoPMTUd: opts.QUICNoPMTUD,
ReuseConn: !opts.NoReuseConn,
NoPMTUd: !opts.PMTUD,
ReuseConn: opts.ReuseConn,
}
}
case transport.TypeDNSCrypt:
Expand All @@ -142,15 +142,15 @@ func newTransport(server string, transportType transport.Type, tlsConfig *tls.Co
TCP: opts.DNSCryptTCP,
Timeout: opts.Timeout,
UDPSize: opts.DNSCryptUDPSize,
ReuseConn: !opts.NoReuseConn,
ReuseConn: opts.ReuseConn,
}
} else {
log.Traceln("Using manual DNSCrypt configuration")
ts = &transport.DNSCrypt{
TCP: opts.DNSCryptTCP,
Timeout: opts.Timeout,
UDPSize: opts.DNSCryptUDPSize,
ReuseConn: !opts.NoReuseConn,
ReuseConn: opts.ReuseConn,
Server: server,
PublicKey: opts.DNSCryptPublicKey,
ProviderName: opts.DNSCryptProvider,
Expand All @@ -165,17 +165,17 @@ func newTransport(server string, transportType transport.Type, tlsConfig *tls.Co
ts = &transport.QUIC{
Server: server,
TLSConfig: tc,
NoPMTUD: opts.QUICNoPMTUD,
AddLengthPrefix: !opts.QUICNoLengthPrefix,
ReuseConn: !opts.NoReuseConn,
PMTUD: opts.PMTUD,
AddLengthPrefix: opts.QUICLengthPrefix,
ReuseConn: opts.ReuseConn,
}
case transport.TypeTLS:
log.Debugf("Using TLS transport: %s", server)
ts = &transport.TLS{
Server: server,
TLSConfig: tlsConfig,
Timeout: opts.Timeout,
ReuseConn: !opts.NoReuseConn,
ReuseConn: opts.ReuseConn,
}
case transport.TypeTCP:
log.Debugf("Using TCP transport: %s", server)
Expand Down
4 changes: 2 additions & 2 deletions transport/quic.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
type QUIC struct {
Server string
TLSConfig *tls.Config
NoPMTUD bool
PMTUD bool
AddLengthPrefix bool
ReuseConn bool

Expand Down Expand Up @@ -62,7 +62,7 @@ func (q *QUIC) Exchange(msg *dns.Msg) (*dns.Msg, error) {
q.Server,
q.TLSConfig,
&quic.Config{
DisablePathMTUDiscovery: q.NoPMTUD,
DisablePathMTUDiscovery: !q.PMTUD,
},
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion transport/quic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "crypto/tls"
func quicTransport() *QUIC {
return &QUIC{
Server: "dns.adguard.com:8853",
NoPMTUD: false,
PMTUD: true,
AddLengthPrefix: true,
TLSConfig: &tls.Config{NextProtos: []string{"doq"}},
}
Expand Down

0 comments on commit 2c44df0

Please sign in to comment.