From 21c7d22618d7952c215a7aeba7b0bf14104941bd Mon Sep 17 00:00:00 2001 From: Dimitry Kolyshev Date: Mon, 9 Sep 2024 11:11:46 +0700 Subject: [PATCH] cmd: imp code --- internal/cmd/args.go | 258 ++++++++++++++++++++++--------------------- 1 file changed, 135 insertions(+), 123 deletions(-) diff --git a/internal/cmd/args.go b/internal/cmd/args.go index 017455a0..26ff7e8f 100644 --- a/internal/cmd/args.go +++ b/internal/cmd/args.go @@ -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.`, @@ -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.", @@ -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.", @@ -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.", @@ -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.", @@ -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.", @@ -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.", @@ -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: "", }, } @@ -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]) } @@ -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, - } -}