1
4
mirror of https://github.com/yunginnanet/HellPot synced 2024-06-27 08:18:03 +00:00

Refactor config

This commit is contained in:
kayos@tcp.direct 2022-02-07 02:12:02 -08:00
parent 4c68c8bf1d
commit dfaccefdd4
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
6 changed files with 132 additions and 123 deletions

7
.gitignore vendored

@ -1,2 +1,7 @@
go.sum
/dist/
/dist
/art
.idea
HellPot
config.toml*
config.alt*

39
config/arguments.go Normal file

@ -0,0 +1,39 @@
package config
import "os"
var usage = []string{
"\n" + Title + " v" + Version + " Usage\n",
"-c <config.toml> - Specify config file",
"--nocolor - disable color and banner ",
"--banner - show banner + version and exit",
"--genconfig - write default config to 'default.toml' then exit",
}
func printUsage() {
println(usage)
os.Exit(0)
}
// TODO: should probably just make a proper CLI with flags or something
func argParse() {
for i, arg := range os.Args {
switch arg {
case "-h":
printUsage()
case "--genconfig":
GenConfig = true
case "--nocolor":
noColorForce = true
case "--banner":
BannerOnly = true
case "-c", "--config":
if len(os.Args) <= i-1 {
panic("syntax error! expected file after -c")
}
loadCustomConfig(os.Args[i+1])
default:
continue
}
}
}

@ -12,6 +12,25 @@ import (
"github.com/spf13/viper"
)
// generic vars
var (
f *os.File
err error
noColorForce = false
customconfig = false
home string
prefConfigLocation string
snek *viper.Viper
)
// exported generic vars
var (
// Debug is the value of our debug on/off toggle as per the current configuration.
Debug bool
// Filename returns the current location of our toml config file.
Filename string
)
func init() {
if home, err = os.UserHomeDir(); err != nil {
panic(err)
@ -61,10 +80,9 @@ func Init() {
return
}
setConfigFileLocations()
setDefaults()
for _, loc := range configLocations {
for _, loc := range getConfigPaths() {
snek.AddConfigPath(loc)
}
@ -79,66 +97,15 @@ func Init() {
associateExportedVariables()
}
func setDefaults() {
var (
configSections = []string{"logger", "http", "performance", "deception", "ssh"}
deflogdir = home + "/.config/" + Title + "/logs/"
defNoColor = false
)
if runtime.GOOS == "windows" {
deflogdir = "logs/"
defNoColor = true
}
Opt := make(map[string]map[string]interface{})
Opt["logger"] = map[string]interface{}{
"debug": true,
"directory": deflogdir,
"nocolor": defNoColor,
"use_date_filename": true,
}
Opt["http"] = map[string]interface{}{
"use_unix_socket": false,
"unix_socket_path": "/var/run/hellpot",
"unix_socket_permissions": "0666",
"bind_addr": "127.0.0.1",
"bind_port": "8080",
"paths": []string{
"wp-login.php",
"wp-login",
},
}
Opt["performance"] = map[string]interface{}{
"restrict_concurrency": false,
"max_workers": 256,
}
Opt["deception"] = map[string]interface{}{
"server_name": "nginx",
}
for _, def := range configSections {
snek.SetDefault(def, Opt[def])
}
if GenConfig {
if err = snek.SafeWriteConfigAs("./config.toml"); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
os.Exit(0)
}
}
func setConfigFileLocations() {
configLocations = append(configLocations, "./")
func getConfigPaths() (paths []string) {
paths = append(paths, "./")
if runtime.GOOS != "windows" {
configLocations = append(configLocations,
paths = append(paths,
prefConfigLocation, "/etc/"+Title+"/", "../", "../../")
}
return
}
func loadCustomConfig(path string) {
@ -159,38 +126,6 @@ func loadCustomConfig(path string) {
customconfig = true
}
func printUsage() {
println("\n" + Title + " v" + Version + " Usage\n")
println("-c <config.toml> - Specify config file")
println("--nocolor - disable color and banner ")
println("--banner - show banner + version and exit")
println("--genconfig - write default config to 'default.toml' then exit")
os.Exit(0)
}
// TODO: should probably just make a proper CLI with flags or something
func argParse() {
for i, arg := range os.Args {
switch arg {
case "-h":
printUsage()
case "--genconfig":
GenConfig = true
case "--nocolor":
noColorForce = true
case "--banner":
BannerOnly = true
case "-c", "--config":
if len(os.Args) <= i-1 {
panic("syntax error! expected file after -c")
}
loadCustomConfig(os.Args[i+1])
default:
continue
}
}
}
func processOpts() {
// string options and their exported variables
stringOpt := map[string]*string{

@ -1,11 +1,5 @@
package config
import (
"os"
"github.com/spf13/viper"
)
const (
// Version roughly represents the applications current version.
Version = "0.3"
@ -52,28 +46,3 @@ var (
// FakeServerName is our configured value for the "Server: " response header when serving HTTP clients
FakeServerName string
)
var (
// Filename returns the current location of our toml config file.
Filename string
)
var (
f *os.File
err error
)
var (
noColorForce = false
customconfig = false
home string
configLocations []string
)
var (
// Debug is our global debug toggle
Debug bool
prefConfigLocation string
snek *viper.Viper
)

60
config/defaults.go Normal file

@ -0,0 +1,60 @@
package config
import (
"fmt"
"os"
"runtime"
)
var (
configSections = []string{"logger", "http", "performance", "deception", "ssh"}
deflogdir = home + "/.config/" + Title + "/logs/"
defNoColor = false
)
var defOpts = map[string]map[string]interface{}{
"logger": {
"debug": true,
"directory": deflogdir,
"nocolor": defNoColor,
"use_date_filename": true,
},
"http": {
"use_unix_socket": false,
"unix_socket_path": "/var/run/hellpot",
"unix_socket_permissions": "0666",
"bind_addr": "127.0.0.1",
"bind_port": "8080",
"paths": []string{
"wp-login.php",
"wp-login",
},
},
"performance": {
"restrict_concurrency": false,
"max_workers": 256,
},
"deception": {
"server_name": "nginx",
},
}
func setDefaults() {
if runtime.GOOS == "windows" {
deflogdir = "logs/"
defNoColor = true
}
for _, def := range configSections {
snek.SetDefault(def, defOpts[def])
}
if GenConfig {
if err = snek.SafeWriteConfigAs("./config.toml"); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
os.Exit(0)
}
}

@ -93,7 +93,7 @@ func getSrv(r *router.Router) fasthttp.Server {
DisableKeepalive: true,
Handler: r.Handler,
Logger: log,
Logger: log,
}
}
@ -104,6 +104,7 @@ func Serve() error {
r := router.New()
r.GET("/robots.txt", robotsTXT)
for _, p := range config.Paths {
r.GET(fmt.Sprintf("/%s", p), hellPot)
}