initialize the log file before reporting config errors (#1426)

* initialize the log file before reporting config errors

* consistently return error instead of calling dlog.Fatal when parsing config
This commit is contained in:
Alison Winters 2020-07-27 07:01:44 -07:00 committed by GitHub
parent d3ff3a6bb1
commit 617629c180
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -294,17 +294,13 @@ func findConfigFile(configFile *string) (string, error) {
func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error { func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
foundConfigFile, err := findConfigFile(flags.ConfigFile) foundConfigFile, err := findConfigFile(flags.ConfigFile)
if err != nil { if err != nil {
dlog.Fatalf("Unable to load the configuration file [%s] -- Maybe use the -config command-line switch?", *flags.ConfigFile) return fmt.Errorf("Unable to load the configuration file [%s] -- Maybe use the -config command-line switch?", *flags.ConfigFile)
} }
config := newConfig() config := newConfig()
md, err := toml.DecodeFile(foundConfigFile, &config) md, err := toml.DecodeFile(foundConfigFile, &config)
if err != nil { if err != nil {
return err return err
} }
undecoded := md.Undecoded()
if len(undecoded) > 0 {
return fmt.Errorf("Unsupported key in configuration file: [%s]", undecoded[0])
}
if err := cdFileDir(foundConfigFile); err != nil { if err := cdFileDir(foundConfigFile); err != nil {
return err return err
} }
@ -326,6 +322,14 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
dlog.SetFileDescriptor(os.NewFile(uintptr(3), "logFile")) dlog.SetFileDescriptor(os.NewFile(uintptr(3), "logFile"))
} }
} }
if !*flags.Child {
dlog.Noticef("dnscrypt-proxy %s", AppVersion)
}
undecoded := md.Undecoded()
if len(undecoded) > 0 {
return fmt.Errorf("Unsupported key in configuration file: [%s]", undecoded[0])
}
proxy.logMaxSize = config.LogMaxSize proxy.logMaxSize = config.LogMaxSize
proxy.logMaxAge = config.LogMaxAge proxy.logMaxAge = config.LogMaxAge
proxy.logMaxBackups = config.LogMaxBackups proxy.logMaxBackups = config.LogMaxBackups
@ -343,7 +347,7 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
if len(config.FallbackResolvers) > 0 { if len(config.FallbackResolvers) > 0 {
for _, resolver := range config.FallbackResolvers { for _, resolver := range config.FallbackResolvers {
if err := isIPAndPort(resolver); err != nil { if err := isIPAndPort(resolver); err != nil {
dlog.Fatalf("Fallback resolver [%v]: %v", resolver, err) return fmt.Errorf("Fallback resolver [%v]: %v", resolver, err)
} }
} }
proxy.xTransport.ignoreSystemDNS = config.IgnoreSystemDNS proxy.xTransport.ignoreSystemDNS = config.IgnoreSystemDNS
@ -355,7 +359,7 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
if len(config.HTTPProxyURL) > 0 { if len(config.HTTPProxyURL) > 0 {
httpProxyURL, err := url.Parse(config.HTTPProxyURL) httpProxyURL, err := url.Parse(config.HTTPProxyURL)
if err != nil { if err != nil {
dlog.Fatalf("Unable to parse the HTTP proxy URL [%v]", config.HTTPProxyURL) return fmt.Errorf("Unable to parse the HTTP proxy URL [%v]", config.HTTPProxyURL)
} }
proxy.xTransport.httpProxyFunction = http.ProxyURL(httpProxyURL) proxy.xTransport.httpProxyFunction = http.ProxyURL(httpProxyURL)
} }
@ -363,11 +367,11 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
if len(config.Proxy) > 0 { if len(config.Proxy) > 0 {
proxyDialerURL, err := url.Parse(config.Proxy) proxyDialerURL, err := url.Parse(config.Proxy)
if err != nil { if err != nil {
dlog.Fatalf("Unable to parse the proxy URL [%v]", config.Proxy) return fmt.Errorf("Unable to parse the proxy URL [%v]", config.Proxy)
} }
proxyDialer, err := netproxy.FromURL(proxyDialerURL, netproxy.Direct) proxyDialer, err := netproxy.FromURL(proxyDialerURL, netproxy.Direct)
if err != nil { if err != nil {
dlog.Fatalf("Unable to use the proxy: [%v]", err) return fmt.Errorf("Unable to use the proxy: [%v]", err)
} }
proxy.xTransport.proxyDialer = &proxyDialer proxy.xTransport.proxyDialer = &proxyDialer
proxy.mainProto = "tcp" proxy.mainProto = "tcp"
@ -428,7 +432,7 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
proxy.listenAddresses = config.ListenAddresses proxy.listenAddresses = config.ListenAddresses
proxy.localDoHListenAddresses = config.LocalDoH.ListenAddresses proxy.localDoHListenAddresses = config.LocalDoH.ListenAddresses
if len(config.LocalDoH.Path) > 0 && config.LocalDoH.Path[0] != '/' { if len(config.LocalDoH.Path) > 0 && config.LocalDoH.Path[0] != '/' {
dlog.Fatalf("local DoH: [%s] cannot be a valid URL path. Read the documentation", config.LocalDoH.Path) return fmt.Errorf("local DoH: [%s] cannot be a valid URL path. Read the documentation", config.LocalDoH.Path)
} }
proxy.localDoHPath = config.LocalDoH.Path proxy.localDoHPath = config.LocalDoH.Path
proxy.localDoHCertFile = config.LocalDoH.CertFile proxy.localDoHCertFile = config.LocalDoH.CertFile
@ -479,7 +483,7 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
proxy.nxLogFormat = config.NxLog.Format proxy.nxLogFormat = config.NxLog.Format
if len(config.BlockName.File) > 0 && len(config.BlockNameLegacy.File) > 0 { if len(config.BlockName.File) > 0 && len(config.BlockNameLegacy.File) > 0 {
dlog.Fatal("Don't specify both [blocked_names] and [blacklist] sections - Update your config file.") return errors.New("Don't specify both [blocked_names] and [blacklist] sections - Update your config file.")
} }
if len(config.BlockNameLegacy.File) > 0 { if len(config.BlockNameLegacy.File) > 0 {
dlog.Notice("Use of [blacklist] is deprecated - Update your config file.") dlog.Notice("Use of [blacklist] is deprecated - Update your config file.")
@ -500,7 +504,7 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
proxy.blockNameLogFile = config.BlockName.LogFile proxy.blockNameLogFile = config.BlockName.LogFile
if len(config.AllowedName.File) > 0 && len(config.WhitelistNameLegacy.File) > 0 { if len(config.AllowedName.File) > 0 && len(config.WhitelistNameLegacy.File) > 0 {
dlog.Fatal("Don't specify both [whitelist] and [allowed_names] sections - Update your config file.") return errors.New("Don't specify both [whitelist] and [allowed_names] sections - Update your config file.")
} }
if len(config.WhitelistNameLegacy.File) > 0 { if len(config.WhitelistNameLegacy.File) > 0 {
dlog.Notice("Use of [whitelist] is deprecated - Update your config file.") dlog.Notice("Use of [whitelist] is deprecated - Update your config file.")
@ -521,7 +525,7 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
proxy.whitelistNameLogFile = config.AllowedName.LogFile proxy.whitelistNameLogFile = config.AllowedName.LogFile
if len(config.BlockIP.File) > 0 && len(config.BlockIPLegacy.File) > 0 { if len(config.BlockIP.File) > 0 && len(config.BlockIPLegacy.File) > 0 {
dlog.Fatal("Don't specify both [blocked_ips] and [ip_blacklist] sections - Update your config file.") return errors.New("Don't specify both [blocked_ips] and [ip_blacklist] sections - Update your config file.")
} }
if len(config.BlockIPLegacy.File) > 0 { if len(config.BlockIPLegacy.File) > 0 {
dlog.Notice("Use of [ip_blacklist] is deprecated - Update your config file.") dlog.Notice("Use of [ip_blacklist] is deprecated - Update your config file.")
@ -561,7 +565,7 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
proxy.anonDirectCertFallback = config.AnonymizedDNS.DirectCertFallback proxy.anonDirectCertFallback = config.AnonymizedDNS.DirectCertFallback
if config.DoHClientX509AuthLegacy.Creds != nil { if config.DoHClientX509AuthLegacy.Creds != nil {
dlog.Fatal("[tls_client_auth] has been renamed to [doh_client_x509_auth] - Update your config file.") return errors.New("[tls_client_auth] has been renamed to [doh_client_x509_auth] - Update your config file.")
} }
configClientCreds := config.DoHClientX509Auth.Creds configClientCreds := config.DoHClientX509Auth.Creds
creds := make(map[string]DOHClientCreds) creds := make(map[string]DOHClientCreds)
@ -608,9 +612,6 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
netprobeAddress = config.FallbackResolvers[0] netprobeAddress = config.FallbackResolvers[0]
} }
proxy.showCerts = *flags.ShowCerts || len(os.Getenv("SHOW_CERTS")) > 0 proxy.showCerts = *flags.ShowCerts || len(os.Getenv("SHOW_CERTS")) > 0
if !proxy.child {
dlog.Noticef("dnscrypt-proxy %s", AppVersion)
}
if !*flags.Check && !*flags.ShowCerts && !*flags.List && !*flags.ListAll { if !*flags.Check && !*flags.ShowCerts && !*flags.List && !*flags.ListAll {
if err := NetProbe(netprobeAddress, netprobeTimeout); err != nil { if err := NetProbe(netprobeAddress, netprobeTimeout); err != nil {
return err return err
@ -622,13 +623,13 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
proxy.addLocalDoHListener(listenAddrStr) proxy.addLocalDoHListener(listenAddrStr)
} }
if err := proxy.addSystemDListeners(); err != nil { if err := proxy.addSystemDListeners(); err != nil {
dlog.Fatal(err) return err
} }
} }
// if 'userName' is set and we are the parent process drop privilege and exit // if 'userName' is set and we are the parent process drop privilege and exit
if len(proxy.userName) > 0 && !proxy.child { if len(proxy.userName) > 0 && !proxy.child {
proxy.dropPrivilege(proxy.userName, FileDescriptors) proxy.dropPrivilege(proxy.userName, FileDescriptors)
dlog.Fatal("Dropping privileges is not supporting on this operating system. Unset `user_name` in the configuration file.") return errors.New("Dropping privileges is not supporting on this operating system. Unset `user_name` in the configuration file.")
} }
if !config.OfflineMode { if !config.OfflineMode {
if err := config.loadSources(proxy); err != nil { if err := config.loadSources(proxy); err != nil {
@ -639,7 +640,9 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
} }
} }
if *flags.List || *flags.ListAll { if *flags.List || *flags.ListAll {
config.printRegisteredServers(proxy, *flags.JSONOutput) if err := config.printRegisteredServers(proxy, *flags.JSONOutput); err != nil {
return err
}
os.Exit(0) os.Exit(0)
} }
if proxy.routes != nil && len(*proxy.routes) > 0 { if proxy.routes != nil && len(*proxy.routes) > 0 {
@ -669,7 +672,7 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
return nil return nil
} }
func (config *Config) printRegisteredServers(proxy *Proxy, jsonOutput bool) { func (config *Config) printRegisteredServers(proxy *Proxy, jsonOutput bool) error {
var summary []ServerSummary var summary []ServerSummary
for _, registeredServer := range proxy.registeredServers { for _, registeredServer := range proxy.registeredServers {
addrStr, port := registeredServer.stamp.ServerAddrStr, stamps.DefaultPort addrStr, port := registeredServer.stamp.ServerAddrStr, stamps.DefaultPort
@ -706,10 +709,11 @@ func (config *Config) printRegisteredServers(proxy *Proxy, jsonOutput bool) {
if jsonOutput { if jsonOutput {
jsonStr, err := json.MarshalIndent(summary, "", " ") jsonStr, err := json.MarshalIndent(summary, "", " ")
if err != nil { if err != nil {
dlog.Fatal(err) return err
} }
fmt.Print(string(jsonStr)) fmt.Print(string(jsonStr))
} }
return nil
} }
func (config *Config) loadSources(proxy *Proxy) error { func (config *Config) loadSources(proxy *Proxy) error {
@ -740,11 +744,11 @@ func (config *Config) loadSources(proxy *Proxy) error {
continue continue
} }
if len(staticConfig.Stamp) == 0 { if len(staticConfig.Stamp) == 0 {
dlog.Fatalf("Missing stamp for the static [%s] definition", serverName) return fmt.Errorf("Missing stamp for the static [%s] definition", serverName)
} }
stamp, err := stamps.NewServerStampFromString(staticConfig.Stamp) stamp, err := stamps.NewServerStampFromString(staticConfig.Stamp)
if err != nil { if err != nil {
dlog.Fatalf("Stamp error for the static [%s] definition: [%v]", serverName, err) return fmt.Errorf("Stamp error for the static [%s] definition: [%v]", serverName, err)
} }
proxy.registeredServers = append(proxy.registeredServers, RegisteredServer{name: serverName, stamp: stamp}) proxy.registeredServers = append(proxy.registeredServers, RegisteredServer{name: serverName, stamp: stamp})
} }