Skip to content

Commit

Permalink
cmd: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Sep 9, 2024
1 parent 8978f06 commit 21c7d22
Showing 1 changed file with 135 additions and 123 deletions.
258 changes: 135 additions & 123 deletions internal/cmd/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ type commandLineOption struct {
// binary.
var commandLineOptions = []*commandLineOption{
configPathIdx: {
description: "YAML configuration file. Minimal working configuration in config.yaml.dist. Options passed through command line will override the ones from this file.",
long: "config-path",
short: "",
valueType: "path",
description: "YAML configuration file. Minimal working configuration in config.yaml.dist." +
" Options passed through command line will override the ones from this file.",
long: "config-path",
short: "",
valueType: "path",
},
logOutputIdx: {
description: `Path to the log file.`,
Expand All @@ -109,16 +110,18 @@ var commandLineOptions = []*commandLineOption{
valueType: "name",
},
httpsUserinfoIdx: {
description: "If set, all DoH queries are required to have this basic authentication information.",
long: "https-userinfo",
short: "",
valueType: "name",
description: "If set, all DoH queries are required to have this basic authentication " +
"information.",
long: "https-userinfo",
short: "",
valueType: "name",
},
dnsCryptConfigPathIdx: {
description: "Path to a file with DNSCrypt configuration. You can generate one using https://github.com/ameshkov/dnscrypt.",
long: "dnscrypt-config",
short: "g",
valueType: "path",
description: "Path to a file with DNSCrypt configuration. You can generate one using " +
"https://github.com/ameshkov/dnscrypt.",
long: "dnscrypt-config",
short: "g",
valueType: "path",
},
ednsAddrIdx: {
description: "Send EDNS Client Address.",
Expand All @@ -127,10 +130,11 @@ var commandLineOptions = []*commandLineOption{
valueType: "address",
},
upstreamModeIdx: {
description: "Defines the upstreams logic mode, possible values: load_balance, parallel, fastest_addr (default: load_balance).",
long: "upstream-mode",
short: "",
valueType: "mode",
description: "Defines the upstreams logic mode, possible values: load_balance, parallel, " +
"fastest_addr (default: load_balance).",
long: "upstream-mode",
short: "",
valueType: "mode",
},
listenAddrsIdx: {
description: "Listening addresses.",
Expand Down Expand Up @@ -169,34 +173,39 @@ var commandLineOptions = []*commandLineOption{
valueType: "port",
},
upstreamsIdx: {
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: "",
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: {
description: "Bootstrap DNS for DoH and DoT, can be specified multiple times (default: use system-provided).",
long: "bootstrap",
short: "b",
valueType: "",
description: "Bootstrap DNS for DoH and DoT, can be specified multiple times (default: " +
"use system-provided).",
long: "bootstrap",
short: "b",
valueType: "",
},
fallbacksIdx: {
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: "",
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: {
description: "Private DNS upstreams to use for reverse DNS lookups of private addresses, can be specified multiple times.",
long: "private-rdns-upstream",
short: "",
valueType: "",
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: {
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: "subnet",
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: "subnet",
},
privateSubnetsIdx: {
description: "Private subnets to use for reverse DNS lookups of private addresses.",
Expand All @@ -205,28 +214,32 @@ var commandLineOptions = []*commandLineOption{
valueType: "subnet",
},
bogusNXDomainIdx: {
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: "subnet",
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: "subnet",
},
hostsFilesIdx: {
description: "List of paths to the hosts files relative to the root, can be specified multiple times.",
long: "hosts-files",
short: "",
valueType: "path",
description: "List of paths to the hosts files relative to the root, can be specified " +
"multiple times.",
long: "hosts-files",
short: "",
valueType: "path",
},
timeoutIdx: {
description: "Timeout for outbound DNS queries to remote upstream servers in a human-readable form",
long: "timeout",
short: "",
valueType: "duration",
description: "Timeout for outbound DNS queries to remote upstream servers in a " +
"human-readable form",
long: "timeout",
short: "",
valueType: "duration",
},
cacheMinTTLIdx: {
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: "uint32",
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: "uint32",
},
cacheMaxTTLIdx: {
description: "Maximum TTL value for DNS entries, in seconds.",
Expand Down Expand Up @@ -259,16 +272,18 @@ var commandLineOptions = []*commandLineOption{
valueType: "int",
},
udpBufferSizeIdx: {
description: "Set the size of the UDP buffer in bytes. A value <= 0 will use the system default.",
long: "udp-buf-size",
short: "",
valueType: "int",
description: "Set the size of the UDP buffer in bytes. A value <= 0 will use the system " +
"default.",
long: "udp-buf-size",
short: "",
valueType: "int",
},
maxGoRoutinesIdx: {
description: "Set the maximum number of go routines. A zero value will not not set a maximum.",
long: "max-go-routines",
short: "",
valueType: "uint",
description: "Set the maximum number of go routines. A zero value will not not set a " +
"maximum.",
long: "max-go-routines",
short: "",
valueType: "uint",
},
tlsMinVersionIdx: {
description: "Minimum TLS version, for example 1.0.",
Expand Down Expand Up @@ -319,10 +334,11 @@ var commandLineOptions = []*commandLineOption{
valueType: "",
},
ipv6DisabledIdx: {
description: "If specified, all AAAA requests will be replied with NoError RCode and empty answer.",
long: "ipv6-disabled",
short: "",
valueType: "",
description: "If specified, all AAAA requests will be replied with NoError RCode and " +
"empty answer.",
long: "ipv6-disabled",
short: "",
valueType: "",
},
http3Idx: {
description: "Enable HTTP/3 support.",
Expand Down Expand Up @@ -361,10 +377,11 @@ var commandLineOptions = []*commandLineOption{
valueType: "",
},
usePrivateRDNSIdx: {
description: "If specified, use private upstreams for reverse DNS lookups of private addresses.",
long: "use-private-rdns",
short: "",
valueType: "",
description: "If specified, use private upstreams for reverse DNS lookups of private " +
"addresses.",
long: "use-private-rdns",
short: "",
valueType: "",
},
}

Expand All @@ -373,7 +390,56 @@ func parseCmdLineOptions(conf *configuration) (err error) {
cmdName, args := os.Args[0], os.Args[1:]

flags := flag.NewFlagSet(cmdName, flag.ContinueOnError)
for i, fieldPtr := range mapOptions(conf) {
for i, fieldPtr := range []any{
configPathIdx: &conf.ConfigPath,
logOutputIdx: &conf.LogOutput,
tlsCertPathIdx: &conf.TLSCertPath,
tlsKeyPathIdx: &conf.TLSKeyPath,
httpsServerNameIdx: &conf.HTTPSServerName,
httpsUserinfoIdx: &conf.HTTPSUserinfo,
dnsCryptConfigPathIdx: &conf.DNSCryptConfigPath,
ednsAddrIdx: &conf.EDNSAddr,
upstreamModeIdx: &conf.UpstreamMode,
listenAddrsIdx: &conf.ListenAddrs,
listenPortsIdx: &conf.ListenPorts,
httpsListenPortsIdx: &conf.HTTPSListenPorts,
tlsListenPortsIdx: &conf.TLSListenPorts,
quicListenPortsIdx: &conf.QUICListenPorts,
dnsCryptListenPortsIdx: &conf.DNSCryptListenPorts,
upstreamsIdx: &conf.Upstreams,
bootstrapDNSIdx: &conf.BootstrapDNS,
fallbacksIdx: &conf.Fallbacks,
privateRDNSUpstreamsIdx: &conf.PrivateRDNSUpstreams,
dns64PrefixIdx: &conf.DNS64Prefix,
privateSubnetsIdx: &conf.PrivateSubnets,
bogusNXDomainIdx: &conf.BogusNXDomain,
hostsFilesIdx: &conf.HostsFiles,
timeoutIdx: &conf.Timeout,
cacheMinTTLIdx: &conf.CacheMinTTL,
cacheMaxTTLIdx: &conf.CacheMaxTTL,
cacheSizeBytesIdx: &conf.CacheSizeBytes,
ratelimitIdx: &conf.Ratelimit,
ratelimitSubnetLenIPv4Idx: &conf.RatelimitSubnetLenIPv4,
ratelimitSubnetLenIPv6Idx: &conf.RatelimitSubnetLenIPv6,
udpBufferSizeIdx: &conf.UDPBufferSize,
maxGoRoutinesIdx: &conf.MaxGoRoutines,
tlsMinVersionIdx: &conf.TLSMinVersion,
tlsMaxVersionIdx: &conf.TLSMaxVersion,
helpIdx: &conf.help,
hostsFileEnabledIdx: &conf.HostsFileEnabled,
pprofIdx: &conf.Pprof,
versionIdx: &conf.Version,
verboseIdx: &conf.Verbose,
insecureIdx: &conf.Insecure,
ipv6DisabledIdx: &conf.IPv6Disabled,
http3Idx: &conf.HTTP3,
cacheOptimisticIdx: &conf.CacheOptimistic,
cacheIdx: &conf.Cache,
refuseAnyIdx: &conf.RefuseAny,
enableEDNSSubnetIdx: &conf.EnableEDNSSubnet,
dns64Idx: &conf.DNS64,
usePrivateRDNSIdx: &conf.UsePrivateRDNS,
} {
addOption(flags, fieldPtr, commandLineOptions[i])
}

Expand Down Expand Up @@ -513,57 +579,3 @@ func processCmdLineOptions(conf *configuration, parseErr error) (exitCode int, n

return osutil.ExitCodeSuccess, false
}

// mapOptions returns configuration mapped with commandLineOptions.
func mapOptions(conf *configuration) (m map[int]any) {
return map[int]any{
configPathIdx: &conf.ConfigPath,
logOutputIdx: &conf.LogOutput,
tlsCertPathIdx: &conf.TLSCertPath,
tlsKeyPathIdx: &conf.TLSKeyPath,
httpsServerNameIdx: &conf.HTTPSServerName,
httpsUserinfoIdx: &conf.HTTPSUserinfo,
dnsCryptConfigPathIdx: &conf.DNSCryptConfigPath,
ednsAddrIdx: &conf.EDNSAddr,
upstreamModeIdx: &conf.UpstreamMode,
listenAddrsIdx: &conf.ListenAddrs,
listenPortsIdx: &conf.ListenPorts,
httpsListenPortsIdx: &conf.HTTPSListenPorts,
tlsListenPortsIdx: &conf.TLSListenPorts,
quicListenPortsIdx: &conf.QUICListenPorts,
dnsCryptListenPortsIdx: &conf.DNSCryptListenPorts,
upstreamsIdx: &conf.Upstreams,
bootstrapDNSIdx: &conf.BootstrapDNS,
fallbacksIdx: &conf.Fallbacks,
privateRDNSUpstreamsIdx: &conf.PrivateRDNSUpstreams,
dns64PrefixIdx: &conf.DNS64Prefix,
privateSubnetsIdx: &conf.PrivateSubnets,
bogusNXDomainIdx: &conf.BogusNXDomain,
hostsFilesIdx: &conf.HostsFiles,
timeoutIdx: &conf.Timeout,
cacheMinTTLIdx: &conf.CacheMinTTL,
cacheMaxTTLIdx: &conf.CacheMaxTTL,
cacheSizeBytesIdx: &conf.CacheSizeBytes,
ratelimitIdx: &conf.Ratelimit,
ratelimitSubnetLenIPv4Idx: &conf.RatelimitSubnetLenIPv4,
ratelimitSubnetLenIPv6Idx: &conf.RatelimitSubnetLenIPv6,
udpBufferSizeIdx: &conf.UDPBufferSize,
maxGoRoutinesIdx: &conf.MaxGoRoutines,
tlsMinVersionIdx: &conf.TLSMinVersion,
tlsMaxVersionIdx: &conf.TLSMaxVersion,
helpIdx: &conf.help,
hostsFileEnabledIdx: &conf.HostsFileEnabled,
pprofIdx: &conf.Pprof,
versionIdx: &conf.Version,
verboseIdx: &conf.Verbose,
insecureIdx: &conf.Insecure,
ipv6DisabledIdx: &conf.IPv6Disabled,
http3Idx: &conf.HTTP3,
cacheOptimisticIdx: &conf.CacheOptimistic,
cacheIdx: &conf.Cache,
refuseAnyIdx: &conf.RefuseAny,
enableEDNSSubnetIdx: &conf.EnableEDNSSubnet,
dns64Idx: &conf.DNS64,
usePrivateRDNSIdx: &conf.UsePrivateRDNS,
}
}

0 comments on commit 21c7d22

Please sign in to comment.