Skip to content

Commit

Permalink
cmd: args parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Aug 27, 2024
1 parent c891633 commit 80d96d9
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 37 deletions.
105 changes: 68 additions & 37 deletions internal/cmd/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"os"
"slices"
"strings"
"time"

"github.com/AdguardTeam/dnsproxy/internal/version"
"github.com/AdguardTeam/golibs/osutil"
"github.com/AdguardTeam/golibs/timeutil"
)

// Indexes to help with the [commandLineOptions] initialization.
Expand Down Expand Up @@ -145,134 +147,133 @@ var commandLineOptions = []*commandLineOption{
valueType: "",
},
listenAddrsIdx: {
defaultValue: "",
defaultValue: []string{},
description: "Listening addresses.",
long: "listen",
short: "l",
valueType: "",
},
listenPortsIdx: {
defaultValue: "",
defaultValue: []int{},
description: "Listening ports. Zero value disables TCP and UDP listeners.",
long: "port",
short: "p",
valueType: "",
},
httpsListenPortsIdx: {
defaultValue: "",
defaultValue: []int{},
description: "Listening ports for DNS-over-HTTPS.",
long: "https-port",
short: "s",
valueType: "",
},
tlsListenPortsIdx: {
defaultValue: "",
defaultValue: []int{},
description: "Listening ports for DNS-over-TLS.",
long: "tls-port",
short: "t",
valueType: "",
},
quicListenPortsIdx: {
defaultValue: "",
defaultValue: []int{},
description: "Listening ports for DNS-over-QUIC.",
long: "quic-port",
short: "q",
valueType: "",
},
dnsCryptListenPortsIdx: {
defaultValue: "",
defaultValue: []int{},
description: "Listening ports for DNSCrypt.",
long: "dnscrypt-port",
short: "y",
valueType: "",
},
upstreamsIdx: {
defaultValue: "",
defaultValue: []string{},
description: "An upstream to be used (can be specified multiple times). You can also specify path to a file with the list of servers.",
long: "upstream",
short: "u",
valueType: "",
},
bootstrapDNSIdx: {
defaultValue: "",
defaultValue: []string{},
description: "Bootstrap DNS for DoH and DoT, can be specified multiple times (default: use system-provided).",
long: "bootstrap",
short: "b",
valueType: "",
},
fallbacksIdx: {
defaultValue: "",
defaultValue: []string{},
description: "Fallback resolvers to use when regular ones are unavailable, can be specified multiple times. You can also specify path to a file with the list of servers.",
long: "fallback",
short: "f",
valueType: "",
},
privateRDNSUpstreamsIdx: {
defaultValue: "",
defaultValue: []string{},
description: "Private DNS upstreams to use for reverse DNS lookups of private addresses, can be specified multiple times.",
long: "private-rdns-upstream",
short: "",
valueType: "",
},
dns64PrefixIdx: {
defaultValue: "",
defaultValue: []string{},
description: "Prefix used to handle DNS64. If not specified, dnsproxy uses the 'Well-Known Prefix' 64:ff9b::. Can be specified multiple times.",
long: "dns64-prefix",
short: "",
valueType: "",
},
privateSubnetsIdx: {
defaultValue: "",
defaultValue: []string{},
description: "Private subnets to use for reverse DNS lookups of private addresses.",
long: "private-subnets",
short: "",
valueType: "",
},
bogusNXDomainIdx: {
defaultValue: "",
defaultValue: []string{},
description: "Transform the responses containing at least a single IP that matches specified addresses and CIDRs into NXDOMAIN. Can be specified multiple times.",
long: "bogus-nxdomain",
short: "",
valueType: "",
},
hostsFilesIdx: {
defaultValue: "",
defaultValue: []string{},
description: "List of paths to the hosts files relative to the root, can be specified multiple times.",
long: "hosts-files",
short: "",
valueType: "",
},
timeoutIdx: {
defaultValue: "10s",
defaultValue: timeutil.Duration{Duration: 10 * time.Second},
description: "Timeout for outbound DNS queries to remote upstream servers in a human-readable form",
long: "timeout",
short: "",
valueType: "",
},
cacheMinTTLIdx: {
defaultValue: "",
defaultValue: uint32(0),
description: "Minimum TTL value for DNS entries, in seconds. Capped at 3600. Artificially extending TTLs should only be done with careful consideration.",
long: "cache-min-ttl",
short: "",
valueType: "",
},
cacheMaxTTLIdx: {
defaultValue: "",
defaultValue: uint32(0),
description: "Maximum TTL value for DNS entries, in seconds.",
long: "cache-max-ttl",
short: "",
valueType: "",
},
// TODO(d.kolyshev): Set default.
cacheSizeBytesIdx: {
defaultValue: "",
defaultValue: 64 * 1024,
description: "Cache size (in bytes). Default: 64k.",
long: "cache-size",
short: "",
valueType: "",
},
ratelimitIdx: {
defaultValue: "",
defaultValue: 0,
description: "Ratelimit (requests per second).",
long: "ratelimit",
short: "r",
Expand All @@ -293,119 +294,119 @@ var commandLineOptions = []*commandLineOption{
valueType: "",
},
udpBufferSizeIdx: {
defaultValue: "",
defaultValue: 0,
description: "Set the size of the UDP buffer in bytes. A value <= 0 will use the system default.",
long: "udp-buf-size",
short: "",
valueType: "",
},
maxGoRoutinesIdx: {
defaultValue: "",
defaultValue: uint(0),
description: "Set the maximum number of go routines. A zero value will not not set a maximum.",
long: "max-go-routines",
short: "",
valueType: "",
},
tlsMinVersionIdx: {
defaultValue: "",
defaultValue: float32(0),
description: "Minimum TLS version, for example 1.0.",
long: "tls-min-version",
short: "",
valueType: "",
},
tlsMaxVersionIdx: {
defaultValue: "",
defaultValue: float32(0),
description: "Maximum TLS version, for example 1.3.",
long: "tls-max-version",
short: "",
valueType: "",
},
hostsFileEnabledIdx: {
defaultValue: "",
defaultValue: false,
description: "If specified, use hosts files for resolving.",
long: "hosts-file-enabled",
short: "",
valueType: "",
},
pprofIdx: {
defaultValue: "",
defaultValue: false,
description: "If present, exposes pprof information on localhost:6060.",
long: "pprof",
short: "",
valueType: "",
},
versionIdx: {
defaultValue: "",
defaultValue: false,
description: "Prints the program version.",
long: "version",
short: "",
valueType: "",
},
verboseIdx: {
defaultValue: "",
defaultValue: false,
description: "Verbose output.",
long: "verbose",
short: "v",
valueType: "",
},
insecureIdx: {
defaultValue: "",
defaultValue: false,
description: "Disable secure TLS certificate validation.",
long: "insecure",
short: "",
valueType: "",
},
ipv6DisabledIdx: {
defaultValue: "",
defaultValue: false,
description: "If specified, all AAAA requests will be replied with NoError RCode and empty answer.",
long: "ipv6-disabled",
short: "",
valueType: "",
},
http3Idx: {
defaultValue: "",
defaultValue: false,
description: "Enable HTTP/3 support.",
long: "http3",
short: "",
valueType: "",
},
cacheOptimisticIdx: {
defaultValue: "",
defaultValue: false,
description: "If specified, optimistic DNS cache is enabled.",
long: "cache-optimistic",
short: "",
valueType: "",
},
cacheIdx: {
defaultValue: "",
defaultValue: false,
description: "If specified, DNS cache is enabled.",
long: "cache",
short: "",
valueType: "",
},
refuseAnyIdx: {
defaultValue: "",
defaultValue: false,
description: "If specified, refuses ANY requests.",
long: "refuse-any",
short: "",
valueType: "",
},
enableEDNSSubnetIdx: {
defaultValue: "",
defaultValue: false,
description: "Use EDNS Client Subnet extension.",
long: "edns",
short: "",
valueType: "",
},
dns64Idx: {
defaultValue: "",
defaultValue: false,
description: "If specified, dnsproxy will act as a DNS64 server.",
long: "dns64",
short: "",
valueType: "",
},
usePrivateRDNSIdx: {
defaultValue: "",
defaultValue: false,
description: "If specified, use private upstreams for reverse DNS lookups of private addresses.",
long: "use-private-rdns",
short: "",
Expand Down Expand Up @@ -495,6 +496,36 @@ func addOption(flags *flag.FlagSet, fieldPtr any, o *commandLineOption) {
if o.short != "" {
flags.BoolVar(fieldPtr, o.short, o.defaultValue.(bool), o.description)
}
case *int:
flags.IntVar(fieldPtr, o.long, o.defaultValue.(int), o.description)
if o.short != "" {
flags.IntVar(fieldPtr, o.short, o.defaultValue.(int), o.description)
}
case *uint:
flags.UintVar(fieldPtr, o.long, o.defaultValue.(uint), o.description)
if o.short != "" {
flags.UintVar(fieldPtr, o.short, o.defaultValue.(uint), o.description)
}
case *uint32:
flags.Var(newUint32Value(o.defaultValue.(uint32), fieldPtr), o.long, o.description)
if o.short != "" {
flags.Var(newUint32Value(o.defaultValue.(uint32), fieldPtr), o.short, o.description)
}
case *float32:
flags.Var(newFloat32Value(o.defaultValue.(float32), fieldPtr), o.long, o.description)
if o.short != "" {
flags.Var(newFloat32Value(o.defaultValue.(float32), fieldPtr), o.short, o.description)
}
case *[]int:
flags.Var(newIntListValue(o.defaultValue.([]int), fieldPtr), o.long, o.description)
if o.short != "" {
flags.Var(newIntListValue(o.defaultValue.([]int), fieldPtr), o.short, o.description)
}
case *[]string:
flags.Var(newStringListValue(o.defaultValue.([]string), fieldPtr), o.long, o.description)
if o.short != "" {
flags.Var(newStringListValue(o.defaultValue.([]string), fieldPtr), o.short, o.description)
}
case encoding.TextUnmarshaler:
flags.TextVar(fieldPtr, o.long, o.defaultValue.(encoding.TextMarshaler), o.description)
if o.short != "" {
Expand Down
Loading

0 comments on commit 80d96d9

Please sign in to comment.