diff --git a/bin/vproxy/config.go b/bin/vproxy/config.go index f811eb2..3c37ebe 100644 --- a/bin/vproxy/config.go +++ b/bin/vproxy/config.go @@ -44,6 +44,8 @@ func fileExists(name string) bool { return true } +// findConfig locates a config file at the given locations with either a .conf or .toml extension +// (file format must be TOML, however) func findConfig(files ...string) string { for _, config := range files { if config != "" { @@ -70,14 +72,6 @@ func homeConfPath() string { return "" } -func findClientConfig(path string) string { - return findConfig(path, ".vproxy.conf", homeConfPath(), "/usr/local/etc/vproxy.conf", "/etc/vproxy.conf") -} - -func findDaemonConfig(path string) string { - return findConfig(path, homeConfPath(), "/usr/local/etc/vproxy.conf", "/etc/vproxy.conf") -} - func loadConfigFile(path string) (*Config, error) { t, err := toml.LoadFile(path) if err != nil { @@ -102,48 +96,21 @@ func cleanListenAddr(c *cli.Context) { } func loadClientConfig(c *cli.Context) error { - conf := findClientConfig(c.String("config")) - if cf := c.String("config"); c.IsSet("config") && conf != cf { - log.Fatalf("error: config file not found: %s\n", cf) - } - if conf == "" { - return nil - } - verbose(c, "Loading config file %s", conf) - config, err := loadConfigFile(conf) - if err != nil { - return err - } - if config != nil { - if v := (config.Client.Verbose || config.Verbose); v && !c.IsSet("verbose") { - c.Lineage()[1].Set("verbose", "true") - verbose(c, "Loading config file %s", conf) - verbose(c, "via conf: verbose=true") - } - if v := config.Client.Host; v != "" && !c.IsSet("host") { - verbose(c, "via conf: host=%s", v) - c.Set("host", v) - } - if v := config.Client.HTTP; v > 0 && !c.IsSet("http") { - verbose(c, "via conf: http=%d", v) - c.Set("http", strconv.Itoa(v)) - } - if v := config.Client.Bind; v != "" && !c.IsSet("bind") { - verbose(c, "via conf: bind=%s", v) - c.Set("bind", v) - } - if v := config.Server.CaRootPath; v != "" { - os.Setenv("CAROOT_PATH", v) - verbose(c, "via conf: CAROOT_PATH=%s", v) - } - } - return nil + conf := findConfigFile(c.String("config"), false) + return loadConfig(c, conf) } func loadDaemonConfig(c *cli.Context) error { - conf := findClientConfig(c.String("config")) - if cf := c.String("config"); c.IsSet("config") && conf != cf { - log.Fatalf("error: config file not found: %s\n", cf) + conf := findConfigFile(c.String("config"), true) + return loadConfig(c, conf) +} + +func loadConfig(c *cli.Context, conf string) error { + if c.IsSet("config") { + if cf := c.String("config"); conf != cf { + // config flag was passed but file does not exist + log.Fatalf("error: config file not found: %s\n", cf) + } } if conf == "" { return nil @@ -181,6 +148,29 @@ func loadDaemonConfig(c *cli.Context) error { os.Setenv("CERT_PATH", v) verbose(c, "via conf: CERT_PATH=%s", v) } + + // client configs + if v := (config.Client.Verbose || config.Verbose); v && !c.IsSet("verbose") { + c.Lineage()[1].Set("verbose", "true") + verbose(c, "Loading config file %s", conf) + verbose(c, "via conf: verbose=true") + } + if v := config.Client.Host; v != "" && !c.IsSet("host") { + verbose(c, "via conf: host=%s", v) + c.Set("host", v) + } + if v := config.Client.HTTP; v > 0 && !c.IsSet("http") { + verbose(c, "via conf: http=%d", v) + c.Set("http", strconv.Itoa(v)) + } + if v := config.Client.Bind; v != "" && !c.IsSet("bind") { + verbose(c, "via conf: bind=%s", v) + c.Set("bind", v) + } + if v := config.Server.CaRootPath; v != "" { + os.Setenv("CAROOT_PATH", v) + verbose(c, "via conf: CAROOT_PATH=%s", v) + } } cleanListenAddr(c) return nil diff --git a/bin/vproxy/config_nix.go b/bin/vproxy/config_nix.go new file mode 100644 index 0000000..2a75609 --- /dev/null +++ b/bin/vproxy/config_nix.go @@ -0,0 +1,13 @@ +//go:build linux || darwin + +package main + +func findConfigFile(path string, isDaemon bool) string { + paths := []string{path} + if !isDaemon { + // look for dot file only for clients + paths = append(paths, ".vproxy.conf") + } + paths = append(paths, homeConfPath(), "/usr/local/etc/vproxy.conf", "/etc/vproxy.conf") + return findConfig(paths...) +} diff --git a/bin/vproxy/config_windows.go b/bin/vproxy/config_windows.go new file mode 100644 index 0000000..0fabc50 --- /dev/null +++ b/bin/vproxy/config_windows.go @@ -0,0 +1,13 @@ +//go:build windows + +package main + +func findConfigFile(path string, isDaemon bool) string { + paths := []string{path} + if !isDaemon { + // look for dot file only for clients + paths = append(paths, ".vproxy.conf") + } + paths = append(paths, homeConfPath()) + return findConfig(paths...) +} diff --git a/bin/vproxy/flags.go b/bin/vproxy/flags.go index 3665e0f..00c963c 100644 --- a/bin/vproxy/flags.go +++ b/bin/vproxy/flags.go @@ -227,10 +227,11 @@ vproxy connect hello.local:8888 -- vproxy hello }, }, { - Name: "info", - Usage: "Print vproxy configuration", - Before: loadDaemonConfig, - Action: printInfo, + Name: "info", + Usage: "Print vproxy configuration", + Description: `More verbose info: vproxy -v info`, + Before: loadDaemonConfig, + Action: printInfo, }, { Name: "hello", diff --git a/bin/vproxy/main.go b/bin/vproxy/main.go index ecdb248..26ba678 100644 --- a/bin/vproxy/main.go +++ b/bin/vproxy/main.go @@ -279,8 +279,16 @@ func printInfo(c *cli.Context) error { printVersion(c) fmt.Printf(" CAROOT=%s\n", vproxy.CARootPath()) fmt.Printf(" CERT_PATH=%s\n", vproxy.CertPath()) + + confFile := findConfigFile(c.String("config"), false) + if confFile == "" { + fmt.Printf("\n Config file: [not found]\n") + } else { + fmt.Printf("\n Detected Config file: %s (loaded)\n", confFile) + } + certs, _ := filepath.Glob(filepath.Join(vproxy.CertPath(), "*-key.pem")) - fmt.Printf("\n Nubmer of installed certs: %d\n", len(certs)) + fmt.Printf("\n Number of installed certs: %d\n", len(certs)) fmt.Println(" Certs:") for _, cert := range certs { host := strings.TrimPrefix(strings.TrimSuffix(cert, "-key.pem"), vproxy.CertPath()+string(filepath.Separator))