Fixing #304 doesn't look trivial

The service module needs to know the arguments right away.

The arguments haven't been parsed yet. And if we do, we will prevent
further arguments to be added to the set. Including the ones added
by the service module itself.

So, we have quite of a circular dependency here.

If someone with some Go knowledge can fix that, that would be amazing.
But it's probably never going to happen.

Meanwhile, we can try to save the current directory and document
that we have to be in that directory when running the install command.

Which is not going to work on Windows, so this is a big fucking mess
This commit is contained in:
Frank Denis 2018-04-03 20:15:33 +02:00
parent c88e480a15
commit a938eeff7b
2 changed files with 22 additions and 14 deletions

View File

@ -135,8 +135,7 @@ type ServerSummary struct {
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
} }
func FindConfigFile() (string, error) { func findConfigFile(configFile *string) (string, error) {
configFile := flag.String("config", DefaultConfigFileName, "Path to the configuration file")
if _, err := os.Stat(*configFile); os.IsNotExist(err) { if _, err := os.Stat(*configFile); os.IsNotExist(err) {
cdLocal() cdLocal()
if _, err := os.Stat(*configFile); err != nil { if _, err := os.Stat(*configFile); err != nil {
@ -147,17 +146,23 @@ func FindConfigFile() (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
if filepath.IsAbs(*configFile) {
return *configFile, nil
}
return path.Join(pwd, *configFile), nil return path.Join(pwd, *configFile), nil
} }
func ConfigLoad(configFile *string, proxy *Proxy, svcFlag *string) error { func ConfigLoad(proxy *Proxy, svcFlag *string) error {
version := flag.Bool("version", false, "print current proxy version") version := flag.Bool("version", false, "print current proxy version")
resolve := flag.String("resolve", "", "resolve a name using system libraries") 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") list := flag.Bool("list", false, "print the list of available resolvers for the enabled filters")
listAll := flag.Bool("list-all", false, "print the complete list of available resolvers, ignoring filters") listAll := flag.Bool("list-all", false, "print the complete list of available resolvers, ignoring filters")
jsonOutput := flag.Bool("json", false, "output list as JSON") jsonOutput := flag.Bool("json", false, "output list as JSON")
check := flag.Bool("check", false, "check the configuration file and exit") check := flag.Bool("check", false, "check the configuration file and exit")
configFile := flag.String("config", DefaultConfigFileName, "Path to the configuration file")
flag.Parse() flag.Parse()
if *svcFlag == "stop" || *svcFlag == "uninstall" { if *svcFlag == "stop" || *svcFlag == "uninstall" {
return nil return nil
} }
@ -169,8 +174,13 @@ func ConfigLoad(configFile *string, proxy *Proxy, svcFlag *string) error {
Resolve(*resolve) Resolve(*resolve)
os.Exit(0) os.Exit(0)
} }
foundConfigFile, err := findConfigFile(configFile)
if err != nil {
dlog.Fatalf("Unable to load the configuration file [%s] -- Maybe use the -config command-line switch?", *configFile)
}
config := newConfig() config := newConfig()
md, err := toml.DecodeFile(*configFile, &config) md, err := toml.DecodeFile(foundConfigFile, &config)
if err != nil { if err != nil {
return err return err
} }
@ -178,7 +188,7 @@ func ConfigLoad(configFile *string, proxy *Proxy, svcFlag *string) error {
if len(undecoded) > 0 { if len(undecoded) > 0 {
return fmt.Errorf("Unsupported key in configuration file: [%s]", undecoded[0]) return fmt.Errorf("Unsupported key in configuration file: [%s]", undecoded[0])
} }
cdFileDir(*configFile) cdFileDir(foundConfigFile)
if config.LogLevel >= 0 && config.LogLevel < int(dlog.SeverityLast) { if config.LogLevel >= 0 && config.LogLevel < int(dlog.SeverityLast) {
dlog.SetLogLevel(dlog.Severity(config.LogLevel)) dlog.SetLogLevel(dlog.Severity(config.LogLevel))
} }

View File

@ -26,17 +26,15 @@ type App struct {
func main() { func main() {
dlog.Init("dnscrypt-proxy", dlog.SeverityNotice, "DAEMON") dlog.Init("dnscrypt-proxy", dlog.SeverityNotice, "DAEMON")
var err error pwd, err := os.Getwd()
configFile, err := FindConfigFile()
if err != nil { if err != nil {
dlog.Fatalf("Unable to load the configuration file: [%s] -- Maybe use the -config command-line switch?", err) dlog.Fatal("Unable to find the path to the current directory")
} }
svcConfig := &service.Config{ svcConfig := &service.Config{
Name: "dnscrypt-proxy", Name: "dnscrypt-proxy",
DisplayName: "DNSCrypt client proxy", DisplayName: "DNSCrypt client proxy",
Description: "Encrypted/authenticated DNS proxy", Description: "Encrypted/authenticated DNS proxy",
Arguments: []string{"-config", configFile}, WorkingDirectory: pwd,
} }
svcFlag := flag.String("service", "", fmt.Sprintf("Control the system service: %q", service.ControlAction)) svcFlag := flag.String("service", "", fmt.Sprintf("Control the system service: %q", service.ControlAction))
app := &App{} app := &App{}
@ -48,7 +46,7 @@ func main() {
app.proxy = NewProxy() app.proxy = NewProxy()
app.proxy.xTransport = NewXTransport(30*time.Second, true, false) app.proxy.xTransport = NewXTransport(30*time.Second, true, false)
if err := ConfigLoad(&configFile, &app.proxy, svcFlag); err != nil { if err := ConfigLoad(&app.proxy, svcFlag); err != nil {
dlog.Fatal(err) dlog.Fatal(err)
} }
dlog.Noticef("dnscrypt-proxy %s", AppVersion) dlog.Noticef("dnscrypt-proxy %s", AppVersion)