From 5593a54da4f52aeb927988298f40d3ed966bff8b Mon Sep 17 00:00:00 2001 From: "kayos@tcp.direct" Date: Sun, 17 Oct 2021 10:09:28 -0700 Subject: [PATCH] Fix: config bug --- .github/workflows/go.yml | 18 ++++++++--------- .gitignore | 2 ++ README.md | 4 ++-- config/config.go | 42 ++++++++++++++++++++-------------------- config/logger.go | 1 + extra/banner.go | 14 +++++++++++--- extra/decorate.go | 14 +++++++++++++- go.mod | 1 - heffalump/util.go | 10 ++++++++-- http/router_unix.go | 3 ++- http/router_windows.go | 1 + 11 files changed, 70 insertions(+), 40 deletions(-) create mode 100644 .gitignore diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 08d4d24..c7ecea6 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -11,15 +11,15 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v2 - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.17 + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.17 - - name: Build - run: go build -v ./... + - name: Build + run: go build -v ./... - - name: Test - run: go test -v ./... + - name: Test + run: go test -v ./... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ebdfb15 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +go.sum +/dist/ diff --git a/README.md b/README.md index 36baeab..feaffdd 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ # HellPot + [![GoDoc](https://godoc.org/github.com/yunginnanet/HellPot?status.svg)](https://godoc.org/github.com/yunginnanet/HellPot) [![Go Report Card](https://goreportcard.com/badge/github.com/yunginnanet/HellPot)](https://goreportcard.com/report/github.com/yunginnanet/HellPot) [![IRC](https://img.shields.io/badge/ircd.chat-%23tcpdirect-blue.svg)](ircs://ircd.chat:6697/#tcpdirect) + ## Summary HellPot is an endless honeypot based on [Heffalump](https://github.com/carlmjohnson/heffalump) that sends unruly HTTP bots to hell. Notably it implements a [toml configuration file](https://github.com/spf13/viper), has [JSON logging](https://github.com/rs/zerolog), and comes with significant performance gains. - -[![GoDoc](https://godoc.org/github.com/yunginnanet/HellPot?status.svg)](https://godoc.org/github.com/yunginnanet/HellPot) [![Go Report Card](https://goreportcard.com/badge/github.com/yunginnanet/HellPot)](https://goreportcard.com/report/github.com/yunginnanet/HellPot) [![IRC](https://img.shields.io/badge/ircd.chat-%23tcpdirect-blue.svg)](ircs://ircd.chat:6697/#tcpdirect) ![Exploding Heffalump](hellgif.gif) diff --git a/config/config.go b/config/config.go index c63b711..88704ad 100644 --- a/config/config.go +++ b/config/config.go @@ -21,30 +21,31 @@ func init() { } 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()) + 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) } } - 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()) + if _, err := os.Stat(prefConfigLocation); os.IsNotExist(err) { + if err = os.MkdirAll(prefConfigLocation, 0755); err != nil { + println("error writing new config: " + err.Error()) os.Exit(1) } } + newconfig := prefConfigLocation + "/" + "config.toml" + if err = snek.SafeWriteConfigAs(newconfig); err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + Filename = newconfig } @@ -207,18 +208,17 @@ func processOpts() { "http.paths": &Paths, } // bool options and their exported variables - boolOpt := map[string]*bool { - "http.use_unix_socket": &UseUnixSocket, - "logger.debug": &Debug, + boolOpt := map[string]*bool{ + "http.use_unix_socket": &UseUnixSocket, + "logger.debug": &Debug, "performance.restrict_concurrency": &RestrictConcurrency, - "logger.nocolor": &NoColor, + "logger.nocolor": &NoColor, } // integer options and their exported variables - intOpt := map[string]*int { + intOpt := map[string]*int{ "performance.max_workers": &MaxWorkers, } - for key, opt := range stringOpt { *opt = snek.GetString(key) } diff --git a/config/logger.go b/config/logger.go index 87a379c..07d1645 100644 --- a/config/logger.go +++ b/config/logger.go @@ -9,6 +9,7 @@ import ( ) var ( + // CurrentLogFile is used for accessing the location of the currently used log file across packages. CurrentLogFile string logFile *os.File logDir string diff --git a/extra/banner.go b/extra/banner.go index 4cbc925..59d855e 100644 --- a/extra/banner.go +++ b/extra/banner.go @@ -20,9 +20,11 @@ func b64d(str string) []byte { data, _ = base64.StdEncoding.DecodeString(str) return data } + func rc(s []string) string { return strings.TrimSpace(s[ru()%uint32(len(s))]) } + func process(in string) (s string) { var v = strings.Split(config.Version, "") maj := v[0] @@ -46,13 +48,19 @@ func process(in string) (s string) { return } func gz(data []byte) string { - gz, _ := gzip.NewReader(bytes.NewReader(data)) - out, _ := ioutil.ReadAll(gz) + gz, err1 := gzip.NewReader(bytes.NewReader(data)) + out, err2 := ioutil.ReadAll(gz) + if err1 != nil || err2 != nil { + bannerFail(err1, err2) + } return string(out) } + func ru() uint32 { b := make([]byte, 8192) - crip.Read(b) + if _, err := crip.Read(b); err != nil { + bannerFail(err) + } return binary.LittleEndian.Uint32(b) } diff --git a/extra/decorate.go b/extra/decorate.go index 0b692c3..1695cb7 100644 --- a/extra/decorate.go +++ b/extra/decorate.go @@ -1,10 +1,22 @@ package extra import ( - "github.com/yunginnanet/HellPot/config" + "os" "runtime" + + "github.com/yunginnanet/HellPot/config" ) +func bannerFail(errs ...error) { + println("failed printing banner, consider using --nocolor") + for _, err := range errs { + if err != nil { + println(err.Error()) + } + } + os.Exit(1) +} + // Banner prints out our banner (using spooky magic) func Banner() { if runtime.GOOS == "windows" || config.NoColor { diff --git a/go.mod b/go.mod index 2182eb6..8c02938 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,6 @@ 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 ) diff --git a/heffalump/util.go b/heffalump/util.go index bd5cceb..a911c37 100644 --- a/heffalump/util.go +++ b/heffalump/util.go @@ -8,8 +8,14 @@ import ( ) func gz(data []byte) string { - gz, _ := gzip.NewReader(bytes.NewReader(data)) - out, _ := ioutil.ReadAll(gz) + gz, err := gzip.NewReader(bytes.NewReader(data)) + if err != nil { + panic(err) + } + out, err := ioutil.ReadAll(gz) + if err != nil { + panic(err) + } return string(out) } diff --git a/http/router_unix.go b/http/router_unix.go index 1475d07..995ffa6 100644 --- a/http/router_unix.go +++ b/http/router_unix.go @@ -1,4 +1,5 @@ -//+build linux darwin freebsd +//go:build linux || darwin || freebsd +// +build linux darwin freebsd package http diff --git a/http/router_windows.go b/http/router_windows.go index 46998d1..4d9c7b7 100644 --- a/http/router_windows.go +++ b/http/router_windows.go @@ -5,6 +5,7 @@ package http import ( "errors" + "github.com/fasthttp/router" )