diff --git a/dnscrypt-proxy/config.go b/dnscrypt-proxy/config.go index 9b9821b4..2ae14493 100644 --- a/dnscrypt-proxy/config.go +++ b/dnscrypt-proxy/config.go @@ -6,6 +6,7 @@ import ( "flag" "fmt" "os" + "path" "path/filepath" "strings" "time" @@ -134,11 +135,22 @@ type ServerSummary struct { Description string `json:"description,omitempty"` } -func ConfigLoad(proxy *Proxy, svcFlag *string) error { +func FindConfigFile() (string, error) { configFile := flag.String("config", DefaultConfigFileName, "Path to the configuration file") if _, err := os.Stat(*configFile); os.IsNotExist(err) { cdLocal() + if _, err := os.Stat(*configFile); err != nil { + return "", err + } } + pwd, err := os.Getwd() + if err != nil { + return "", err + } + return path.Join(pwd, *configFile), nil +} + +func ConfigLoad(configFile *string, proxy *Proxy, svcFlag *string) error { version := flag.Bool("version", false, "print current proxy version") resolve := flag.String("resolve", "", "resolve a name using system libraries") list := flag.Bool("list", false, "print the list of available resolvers for the enabled filters") diff --git a/dnscrypt-proxy/main.go b/dnscrypt-proxy/main.go index d94516a3..aa59763f 100644 --- a/dnscrypt-proxy/main.go +++ b/dnscrypt-proxy/main.go @@ -25,10 +25,18 @@ type App struct { func main() { dlog.Init("dnscrypt-proxy", dlog.SeverityNotice, "DAEMON") + + var err error + configFile, err := FindConfigFile() + if err != nil { + dlog.Fatalf("Unable to load the configuration file: [%s] -- Maybe use the -config command-line switch?", err) + } + svcConfig := &service.Config{ Name: "dnscrypt-proxy", DisplayName: "DNSCrypt client proxy", Description: "Encrypted/authenticated DNS proxy", + Arguments: []string{"-config", configFile}, } svcFlag := flag.String("service", "", fmt.Sprintf("Control the system service: %q", service.ControlAction)) app := &App{} @@ -40,7 +48,7 @@ func main() { app.proxy = NewProxy() app.proxy.xTransport = NewXTransport(30*time.Second, true, false) - if err := ConfigLoad(&app.proxy, svcFlag); err != nil { + if err := ConfigLoad(&configFile, &app.proxy, svcFlag); err != nil { dlog.Fatal(err) } dlog.Noticef("dnscrypt-proxy %s", AppVersion)