diff --git a/config/config.go b/config/config.go index 8303876..697c6b6 100644 --- a/config/config.go +++ b/config/config.go @@ -15,14 +15,16 @@ const ( // Version roughly represents the applications current version. Version = "0.3" // Title is the name of the application used throughout the configuration process. - Title = "HellPot" + Title = "HellPot" ) var ( // BannerOnly when toggled causes HellPot to only print the banner and version then exit. BannerOnly = false - // ConfigGen when toggled causes HellPot to write its default config to the cwd and then exit. - ConfigGen = false + // GenConfig when toggled causes HellPot to write its default config to the cwd and then exit. + GenConfig = false + // NoColor when true will disable the banner and any colored console output. + NoColor bool ) // "http" @@ -36,7 +38,7 @@ var ( Paths []string // UseUnixSocket when toggled disables the TCP listener and listens on the given UnixSocketPath. - UseUnixSocket bool + UseUnixSocket bool // UnixSocketPath is the path of the unix socket used when UseUnixSocket is toggled. UnixSocketPath = "" ) @@ -61,21 +63,15 @@ var ( var ( f *os.File err error +) - NoColorForce = false - NoColor bool +var ( + noColorForce = false customconfig = false home string configLocations []string ) -/* -Opt represents our program options. - nitially the values that are defined in Opt will be used to define details. - Beyond that, default values will be replaced by options from our config file. -*/ -var Opt map[string]map[string]interface{} - var ( // Debug is our global debug toggle Debug bool @@ -89,10 +85,37 @@ func init() { panic(err) } prefConfigLocation = home + "/.config/" + Title - Opt = make(map[string]map[string]interface{}) snek = viper.New() } +func writeConfig() { + if runtime.GOOS != "windows" { + if _, err := os.Stat(prefConfigLocation); os.IsNotExist(err) { + if err = os.Mkdir(prefConfigLocation, 0755); err != nil { + println("error writing new config: " + err.Error()) + } + } + newconfig := prefConfigLocation + "/" + "config.toml" + if err = snek.SafeWriteConfigAs(newconfig); err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + Filename = newconfig + return + } + + newconfig := "hellpot-config" + snek.SetConfigName(newconfig) + if err = snek.MergeInConfig(); err != nil { + if err = snek.SafeWriteConfigAs(newconfig + ".toml"); err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + } + + Filename = newconfig +} + // Init will initialize our toml configuration engine and define our default configuration values which can be written to a new configuration file if desired func Init() { snek.SetConfigType("toml") @@ -113,34 +136,8 @@ func Init() { snek.AddConfigPath(loc) } - if err = snek.MergeInConfig(); err != nil && runtime.GOOS != "windows" { - if _, err := os.Stat(prefConfigLocation); os.IsNotExist(err) { - if err = os.Mkdir(prefConfigLocation, 0755); err != nil { - println("error writing new config: " + err.Error()) - } - } - - newconfig := prefConfigLocation + "/" + "config.toml" - - if err = snek.SafeWriteConfigAs(newconfig); err != nil { - fmt.Println(err.Error()) - os.Exit(1) - } - - Filename = newconfig - } - - if runtime.GOOS == "windows" { - newconfig := "hellpot-config" - snek.SetConfigName(newconfig) - if err = snek.MergeInConfig(); err != nil { - if err = snek.SafeWriteConfigAs(newconfig + ".toml"); err != nil { - fmt.Println(err.Error()) - os.Exit(1) - } - } - - Filename = newconfig + if err = snek.MergeInConfig(); err != nil { + writeConfig() } if len(Filename) < 1 { @@ -162,6 +159,8 @@ func setDefaults() { defNoColor = true } + Opt := make(map[string]map[string]interface{}) + Opt["logger"] = map[string]interface{}{ "debug": true, "directory": deflogdir, @@ -190,7 +189,7 @@ func setDefaults() { snek.SetDefault(def, Opt[def]) } - if ConfigGen { + if GenConfig { if err = snek.SafeWriteConfigAs("./config.toml"); err != nil { fmt.Println(err.Error()) os.Exit(1) @@ -230,7 +229,7 @@ func loadCustomConfig(path string) { } func printUsage() { - println("\n"+Title+" v"+Version+" Usage\n") + println("\n" + Title + " v" + Version + " Usage\n") println("-c - Specify config file") println("--nocolor - disable color and banner ") println("--banner - show banner + version and exit") @@ -245,9 +244,9 @@ func argParse() { case "-h": printUsage() case "--genconfig": - ConfigGen = true + GenConfig = true case "--nocolor": - NoColorForce = true + noColorForce = true case "--banner": BannerOnly = true case "--config": @@ -263,31 +262,39 @@ func argParse() { } } -func associate() { - var newOpt map[string]map[string]interface{} - newOpt = Opt - for category, opt := range Opt { - for optname, value := range opt { - if snek.IsSet(category + "." + optname) { - newOpt[category][optname] = value - } - } - } +func bl(key string) bool { + return snek.GetBool(key) +} +func st(key string) string { + return snek.GetString(key) +} +func sl(key string) []string { + return snek.GetStringSlice(key) +} +func it(key string) int { + return snek.GetInt(key) +} - Opt = newOpt - Debug = snek.GetBool("logger.debug") - logDir = snek.GetString("logger.directory") - BindAddr = snek.GetString("http.bind_addr") - BindPort = snek.GetString("http.bind_port") - Paths = snek.GetStringSlice("http.paths") - UseUnixSocket = snek.GetBool("http.use_unix_socket") - FakeServerName = snek.GetString("deception.server_name") - RestrictConcurrency = snek.GetBool("performance.restrict_concurrency") - MaxWorkers = snek.GetInt("performance.max_workers") - NoColor = snek.GetBool("logger.nocolor") - if NoColorForce { + +func associate() { + BindAddr = st("http.bind_addr") + BindPort = st("http.bind_port") + Paths = sl("http.paths") + UseUnixSocket = bl("http.use_unix_socket") + // + Debug = bl("logger.debug") + logDir = st("logger.directory") + NoColor = bl("logger.nocolor") + // + FakeServerName = st("deception.server_name") + // + RestrictConcurrency = bl("performance.restrict_concurrency") + MaxWorkers = it("performance.max_workers") + + if noColorForce { NoColor = true } + if UseUnixSocket { UnixSocketPath = snek.GetString("http.unix_socket_path") } diff --git a/go.mod b/go.mod index 8c02938..2182eb6 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/spf13/viper v1.7.1 github.com/stretchr/testify v1.6.1 // indirect github.com/valyala/fasthttp v1.30.0 + golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a gopkg.in/ini.v1 v1.53.0 // indirect gopkg.in/yaml.v2 v2.3.0 // indirect )