From ef69b86dd1ca93968a071e58ec1302fedcb1298f Mon Sep 17 00:00:00 2001 From: Emil Jonathan Eriksson <8700261+ginger51011@users.noreply.github.com> Date: Thu, 18 Jan 2024 21:49:06 +0100 Subject: [PATCH 1/4] Add optional `-b`/`--book` flag to choose source file other than Nietzche While I do think all bots enjoy Nietzche (who doesn't?), I think we should take a stance to educate them. What better way than to be able to choose from any book! Personal suggestions include: - The Sorrows of Young Werther by Goethe - Any political manifesto - The Declaration of Independence etc. etc. --- README.md | 2 +- cmd/HellPot/HellPot.go | 2 +- heffalump/heffalump.go | 5 ++++- heffalump/markov.go | 3 ++- internal/config/arguments.go | 7 +++++++ internal/config/config.go | 24 ++++++++++++++---------- internal/config/help.go | 3 ++- internal/http/router.go | 27 ++++++++++++++++++++++++--- 8 files changed, 55 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 62b380c..c28bf78 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Clients (hopefully bots) that disregard `robots.txt` and connect to your instanc HellPot will send an infinite stream of data that is *just close enough* to being a real website that they might just stick around until their soul is ripped apart and they cease to exist. -Under the hood of this eternal suffering is a markov engine that chucks bits and pieces of [The Birth of Tragedy (Hellenism and Pessimism)](https://www.gutenberg.org/files/51356/51356-h/51356-h.htm) by Friedrich Nietzsche at the client using [fasthttp](https://github.com/valyala/fasthttp). +Under the hood of this eternal suffering is a markov engine that chucks bits and pieces of [The Birth of Tragedy (Hellenism and Pessimism)](https://www.gutenberg.org/files/51356/51356-h/51356-h.htm) by Friedrich Nietzsche at the client using [fasthttp](https://github.com/valyala/fasthttp), or optionally your (least?) favorite text using the `-b`/`--book` flag. ## Building From Source diff --git a/cmd/HellPot/HellPot.go b/cmd/HellPot/HellPot.go index 597d0d9..c360b37 100644 --- a/cmd/HellPot/HellPot.go +++ b/cmd/HellPot/HellPot.go @@ -38,7 +38,7 @@ func init() { extra.Banner() - log.Info().Str("caller", "config").Str("file", config.Filename).Msg(config.Filename) + log.Info().Str("caller", "config").Str("file", config.ConfigFilename).Msg(config.ConfigFilename) log.Info().Str("caller", "logger").Msg(config.CurrentLogFile) log.Debug().Str("caller", "logger").Msg("debug enabled") log.Trace().Str("caller", "logger").Msg("trace enabled") diff --git a/heffalump/heffalump.go b/heffalump/heffalump.go index 298e5a9..f64e318 100644 --- a/heffalump/heffalump.go +++ b/heffalump/heffalump.go @@ -15,7 +15,10 @@ import ( var log = config.GetLogger() // DefaultHeffalump represents a Heffalump type -var DefaultHeffalump *Heffalump +var ( + DefaultHeffalump *Heffalump + DefaultBuffSize int = 100 * 1 << 10 +) // Heffalump represents our buffer pool and markov map from Heffalump type Heffalump struct { diff --git a/heffalump/markov.go b/heffalump/markov.go index ca522ab..f6ce601 100644 --- a/heffalump/markov.go +++ b/heffalump/markov.go @@ -22,8 +22,9 @@ func init() { if len(src) < 1 { panic("failed to unpack source") } + DefaultMarkovMap = MakeMarkovMap(strings.NewReader(src)) - DefaultHeffalump = NewHeffalump(DefaultMarkovMap, 100*1<<10) + DefaultHeffalump = NewHeffalump(DefaultMarkovMap, DefaultBuffSize) } // ScanHTML is a basic split function for a Scanner that returns each diff --git a/internal/config/arguments.go b/internal/config/arguments.go index 89fe1fd..7628867 100644 --- a/internal/config/arguments.go +++ b/internal/config/arguments.go @@ -30,6 +30,13 @@ func argParse() { os.Exit(1) } loadCustomConfig(os.Args[i+1]) + case "-b", "--book": + if len(os.Args) < i+2 { + println("missing book file after -b/--book") + os.Exit(1) + } + BookFilename = os.Args[i+1] + UseCustomHeffalump = true default: continue } diff --git a/internal/config/config.go b/internal/config/config.go index c998cab..d5c20b2 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -29,8 +29,12 @@ var ( Trace bool // Debug is the value of our debug (verbose) on/off toggle as per the current configuration. Debug bool - // Filename returns the current location of our toml config file. - Filename string + // ConfigFilename returns the current location of our toml config file. + ConfigFilename string + // UseCustomHeffalump decides if a custom Heffalump is to be used + UseCustomHeffalump = false + // BookFilename returns the current location of a possible book file + BookFilename string ) func writeConfig() { @@ -40,9 +44,9 @@ func writeConfig() { os.Exit(1) } } - Filename = prefConfigLocation + "/" + "config.toml" - if err := snek.SafeWriteConfigAs(Filename); err != nil { - fmt.Println("Failed to write new configuration file to '" + Filename + "': " + err.Error()) + ConfigFilename = prefConfigLocation + "/" + "config.toml" + if err := snek.SafeWriteConfigAs(ConfigFilename); err != nil { + fmt.Println("Failed to write new configuration file to '" + ConfigFilename + "': " + err.Error()) os.Exit(1) } } @@ -71,8 +75,8 @@ func Init() { writeConfig() } - if len(Filename) < 1 { - Filename = snek.ConfigFileUsed() + if len(ConfigFilename) < 1 { + ConfigFilename = snek.ConfigFileUsed() } snek.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")) @@ -102,9 +106,9 @@ func loadCustomConfig(path string) { os.Exit(1) } - Filename, err = filepath.Abs(path) - if len(Filename) < 1 || err != nil { - Filename = path + ConfigFilename, err = filepath.Abs(path) + if len(ConfigFilename) < 1 || err != nil { + ConfigFilename = path } defer func(f *os.File) { diff --git a/internal/config/help.go b/internal/config/help.go index 8c50059..3bbc357 100644 --- a/internal/config/help.go +++ b/internal/config/help.go @@ -22,7 +22,8 @@ var CLI = help{ 1: {0: "--nocolor", 1: "disable color and banner"}, 2: {0: "--banner", 1: "show banner + version and exit"}, 3: {0: "--genconfig", 1: "write default config to " + Title + ".toml then exit"}, - 4: {0: "--help", 1: "show this help and exit"}, + 4: {0: "--book", "", "Specify a custom file used for text generation"}, + 5: {0: "--help", 1: "show this help and exit"}, }, out: os.Stdout, } diff --git a/internal/http/router.go b/internal/http/router.go index a89284c..766c924 100644 --- a/internal/http/router.go +++ b/internal/http/router.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "net/http" + "os" "runtime" "strings" "time" @@ -16,7 +17,10 @@ import ( "github.com/yunginnanet/HellPot/internal/config" ) -var log *zerolog.Logger +var ( + log *zerolog.Logger + hellpotHeffalump = heffalump.DefaultHeffalump +) func getRealRemote(ctx *fasthttp.RequestCtx) string { xrealip := string(ctx.Request.Header.Peek(config.HeaderName)) @@ -61,7 +65,7 @@ func hellPot(ctx *fasthttp.RequestCtx) { var wn int64 for { - wn, err = heffalump.DefaultHeffalump.WriteHell(bw) + wn, err = hellpotHeffalump.WriteHell(bw) n += wn if err != nil { slog.Trace().Err(err).Msg("END_ON_ERR") @@ -74,7 +78,6 @@ func hellPot(ctx *fasthttp.RequestCtx) { Dur("DURATION", time.Since(s)). Msg("FINISH") }) - } func getSrv(r *router.Router) fasthttp.Server { @@ -120,6 +123,24 @@ func getSrv(r *router.Router) fasthttp.Server { // Serve starts our HTTP server and request router func Serve() error { log = config.GetLogger() + + if config.UseCustomHeffalump { + content, err := os.ReadFile(config.BookFilename) + if err != nil { + panic(err) + } + // Wasteful, but only done once at startup + src := string(content) + log.Info().Msgf("Using custom book file '%s'", config.BookFilename) + + if len(src) < 1 { + panic("book file was empty!") + } + + markovMap := heffalump.MakeMarkovMap(strings.NewReader(src)) + hellpotHeffalump = heffalump.NewHeffalump(markovMap, heffalump.DefaultBuffSize) + } + l := config.HTTPBind + ":" + config.HTTPPort r := router.New() From ac77dccd2eb9b9d8bc2fc31e4060cd918c95f7be Mon Sep 17 00:00:00 2001 From: Emil Jonathan Eriksson <8700261+ginger51011@users.noreply.github.com> Date: Fri, 19 Jan 2024 17:42:45 +0100 Subject: [PATCH 2/4] Remove globals from heffalump/*, add NewDefault{Heffalump,MarkovMap}() This removes globals from `heffalumpt/`, which are hard to reason about, easy to get wrong, and should not be created if they are never used. A possible drawbacks is if you would create multiple new defaults, but this should never be the case. --- heffalump/heffalump.go | 12 +++++++----- heffalump/markov.go | 9 +++------ internal/http/router.go | 5 ++++- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/heffalump/heffalump.go b/heffalump/heffalump.go index f64e318..67c99b9 100644 --- a/heffalump/heffalump.go +++ b/heffalump/heffalump.go @@ -14,11 +14,7 @@ import ( var log = config.GetLogger() -// DefaultHeffalump represents a Heffalump type -var ( - DefaultHeffalump *Heffalump - DefaultBuffSize int = 100 * 1 << 10 -) +const DefaultBuffSize = 100 * 1 << 10 // Heffalump represents our buffer pool and markov map from Heffalump type Heffalump struct { @@ -39,6 +35,12 @@ func NewHeffalump(mm MarkovMap, buffsize int) *Heffalump { } } +// NewDefaultHeffalump instantiates a new default Heffalump from a MarkovMap created using +// using the default source text. +func NewDefaultHeffalump() *Heffalump { + return NewHeffalump(NewDefaultMarkovMap(), DefaultBuffSize) +} + // WriteHell writes markov chain heffalump hell to the provided io.Writer func (h *Heffalump) WriteHell(bw *bufio.Writer) (int64, error) { var n int64 diff --git a/heffalump/markov.go b/heffalump/markov.go index f6ce601..e4a565e 100644 --- a/heffalump/markov.go +++ b/heffalump/markov.go @@ -11,10 +11,8 @@ import ( "git.tcp.direct/kayos/common/squish" ) -var DefaultMarkovMap MarkovMap - -func init() { - // DefaultMarkovMap is a Markov chain based on src. +// NewDefaultMarkovMap creates a new MarkovMap from the default source text. +func NewDefaultMarkovMap() MarkovMap { src, err := squish.UnpackStr(srcGz) if err != nil { panic(err) @@ -23,8 +21,7 @@ func init() { panic("failed to unpack source") } - DefaultMarkovMap = MakeMarkovMap(strings.NewReader(src)) - DefaultHeffalump = NewHeffalump(DefaultMarkovMap, DefaultBuffSize) + return MakeMarkovMap(strings.NewReader(src)) } // ScanHTML is a basic split function for a Scanner that returns each diff --git a/internal/http/router.go b/internal/http/router.go index 766c924..bd597a6 100644 --- a/internal/http/router.go +++ b/internal/http/router.go @@ -19,7 +19,7 @@ import ( var ( log *zerolog.Logger - hellpotHeffalump = heffalump.DefaultHeffalump + hellpotHeffalump *heffalump.Heffalump ) func getRealRemote(ctx *fasthttp.RequestCtx) string { @@ -139,6 +139,9 @@ func Serve() error { markovMap := heffalump.MakeMarkovMap(strings.NewReader(src)) hellpotHeffalump = heffalump.NewHeffalump(markovMap, heffalump.DefaultBuffSize) + } else { + log.Info().Msg("Using default source text") + hellpotHeffalump = heffalump.NewDefaultHeffalump() } l := config.HTTPBind + ":" + config.HTTPPort From 1b8de3df7e923ef33618e284973497b497aaa489 Mon Sep 17 00:00:00 2001 From: Emil Jonathan Eriksson <8700261+ginger51011@users.noreply.github.com> Date: Sat, 20 Jan 2024 10:04:57 +0100 Subject: [PATCH 3/4] Add short flags to help, add `--help` flag This prints the short variants (like `-c` for `--config`) in the help. Also fixes bug where only `-h` flag works, not `--help`. --- internal/config/arguments.go | 2 +- internal/config/help.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/config/arguments.go b/internal/config/arguments.go index 7628867..7ac340c 100644 --- a/internal/config/arguments.go +++ b/internal/config/arguments.go @@ -22,7 +22,7 @@ func argParse() { continue } switch arg { - case "-h": + case "-h", "--help": CLI.printUsage() case "-c", "--config": if len(os.Args) < i+2 { diff --git a/internal/config/help.go b/internal/config/help.go index 3bbc357..30b2d84 100644 --- a/internal/config/help.go +++ b/internal/config/help.go @@ -18,12 +18,12 @@ var CLI = help{ title: Title, version: Version, usage: map[int][]string{ - 0: {0: "--config", 1: "", 2: "Specify config file"}, + 0: {0: "-c, --config", 1: "", 2: "Specify config file"}, 1: {0: "--nocolor", 1: "disable color and banner"}, 2: {0: "--banner", 1: "show banner + version and exit"}, 3: {0: "--genconfig", 1: "write default config to " + Title + ".toml then exit"}, - 4: {0: "--book", "", "Specify a custom file used for text generation"}, - 5: {0: "--help", 1: "show this help and exit"}, + 4: {0: "-b, --book", 1: "", 2: "Specify a custom file used for text generation"}, + 5: {0: "-h, --help", 1: "show this help and exit"}, }, out: os.Stdout, } From 0a3cc6d6386925d8c5136935433e9a768db57cb0 Mon Sep 17 00:00:00 2001 From: Emil Jonathan Eriksson <8700261+ginger51011@users.noreply.github.com> Date: Fri, 2 Feb 2024 18:14:32 +0100 Subject: [PATCH 4/4] Rename --book to --grimoire, fix comments --- README.md | 2 +- cmd/HellPot/HellPot.go | 2 +- internal/config/arguments.go | 6 +++--- internal/config/config.go | 24 ++++++++++++------------ internal/config/defaults.go | 2 +- internal/config/help.go | 4 ++-- internal/http/router.go | 11 ++++++----- 7 files changed, 26 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index c28bf78..d800463 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Clients (hopefully bots) that disregard `robots.txt` and connect to your instanc HellPot will send an infinite stream of data that is *just close enough* to being a real website that they might just stick around until their soul is ripped apart and they cease to exist. -Under the hood of this eternal suffering is a markov engine that chucks bits and pieces of [The Birth of Tragedy (Hellenism and Pessimism)](https://www.gutenberg.org/files/51356/51356-h/51356-h.htm) by Friedrich Nietzsche at the client using [fasthttp](https://github.com/valyala/fasthttp), or optionally your (least?) favorite text using the `-b`/`--book` flag. +Under the hood of this eternal suffering is a markov engine that chucks bits and pieces of [The Birth of Tragedy (Hellenism and Pessimism)](https://www.gutenberg.org/files/51356/51356-h/51356-h.htm) by Friedrich Nietzsche at the client using [fasthttp](https://github.com/valyala/fasthttp), or optionally you may synchronize HellPot with your nightmares by using the `-g`/`--grimoire` flag ## Building From Source diff --git a/cmd/HellPot/HellPot.go b/cmd/HellPot/HellPot.go index c360b37..597d0d9 100644 --- a/cmd/HellPot/HellPot.go +++ b/cmd/HellPot/HellPot.go @@ -38,7 +38,7 @@ func init() { extra.Banner() - log.Info().Str("caller", "config").Str("file", config.ConfigFilename).Msg(config.ConfigFilename) + log.Info().Str("caller", "config").Str("file", config.Filename).Msg(config.Filename) log.Info().Str("caller", "logger").Msg(config.CurrentLogFile) log.Debug().Str("caller", "logger").Msg("debug enabled") log.Trace().Str("caller", "logger").Msg("trace enabled") diff --git a/internal/config/arguments.go b/internal/config/arguments.go index 7ac340c..d23e05e 100644 --- a/internal/config/arguments.go +++ b/internal/config/arguments.go @@ -30,12 +30,12 @@ func argParse() { os.Exit(1) } loadCustomConfig(os.Args[i+1]) - case "-b", "--book": + case "-g", "--grimoire": if len(os.Args) < i+2 { - println("missing book file after -b/--book") + println("missing source of suffering file after -g/--grimoire") os.Exit(1) } - BookFilename = os.Args[i+1] + Grimoire = os.Args[i+1] UseCustomHeffalump = true default: continue diff --git a/internal/config/config.go b/internal/config/config.go index d5c20b2..7ea53f1 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -29,12 +29,12 @@ var ( Trace bool // Debug is the value of our debug (verbose) on/off toggle as per the current configuration. Debug bool - // ConfigFilename returns the current location of our toml config file. - ConfigFilename string + // Filename returns the current location of our toml config file. + Filename string // UseCustomHeffalump decides if a custom Heffalump is to be used UseCustomHeffalump = false - // BookFilename returns the current location of a possible book file - BookFilename string + // Grimoire returns the current location of a possible source of suffering file + Grimoire string ) func writeConfig() { @@ -44,9 +44,9 @@ func writeConfig() { os.Exit(1) } } - ConfigFilename = prefConfigLocation + "/" + "config.toml" - if err := snek.SafeWriteConfigAs(ConfigFilename); err != nil { - fmt.Println("Failed to write new configuration file to '" + ConfigFilename + "': " + err.Error()) + Filename = prefConfigLocation + "/" + "config.toml" + if err := snek.SafeWriteConfigAs(Filename); err != nil { + fmt.Println("Failed to write new configuration file to '" + Filename + "': " + err.Error()) os.Exit(1) } } @@ -75,8 +75,8 @@ func Init() { writeConfig() } - if len(ConfigFilename) < 1 { - ConfigFilename = snek.ConfigFileUsed() + if len(Filename) < 1 { + Filename = snek.ConfigFileUsed() } snek.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_")) @@ -106,9 +106,9 @@ func loadCustomConfig(path string) { os.Exit(1) } - ConfigFilename, err = filepath.Abs(path) - if len(ConfigFilename) < 1 || err != nil { - ConfigFilename = path + Filename, err = filepath.Abs(path) + if len(Filename) < 1 || err != nil { + Filename = path } defer func(f *os.File) { diff --git a/internal/config/defaults.go b/internal/config/defaults.go index f6575fb..0f1ec14 100644 --- a/internal/config/defaults.go +++ b/internal/config/defaults.go @@ -86,7 +86,7 @@ func gen(memfs afero.Fs) { println(err.Error()) os.Exit(1) } - print("default configuration successfully written to " + target) + println("default configuration successfully written to " + target) os.Exit(0) } diff --git a/internal/config/help.go b/internal/config/help.go index 30b2d84..f5e8d37 100644 --- a/internal/config/help.go +++ b/internal/config/help.go @@ -22,8 +22,8 @@ var CLI = help{ 1: {0: "--nocolor", 1: "disable color and banner"}, 2: {0: "--banner", 1: "show banner + version and exit"}, 3: {0: "--genconfig", 1: "write default config to " + Title + ".toml then exit"}, - 4: {0: "-b, --book", 1: "", 2: "Specify a custom file used for text generation"}, - 5: {0: "-h, --help", 1: "show this help and exit"}, + 4: {0: "-g, --grimoire", 1: "", 2: "Specify a custom file used for text generation"}, + 5: {0: "-h,--help", 1: "show this help and exit"}, }, out: os.Stdout, } diff --git a/internal/http/router.go b/internal/http/router.go index bd597a6..18842c2 100644 --- a/internal/http/router.go +++ b/internal/http/router.go @@ -124,22 +124,23 @@ func getSrv(r *router.Router) fasthttp.Server { func Serve() error { log = config.GetLogger() - if config.UseCustomHeffalump { - content, err := os.ReadFile(config.BookFilename) + switch config.UseCustomHeffalump { + case true: + content, err := os.ReadFile(config.Grimoire) if err != nil { panic(err) } // Wasteful, but only done once at startup src := string(content) - log.Info().Msgf("Using custom book file '%s'", config.BookFilename) + log.Info().Msgf("Using custom grimoire file '%s'", config.Grimoire) if len(src) < 1 { - panic("book file was empty!") + panic("grimoire file was empty!") } markovMap := heffalump.MakeMarkovMap(strings.NewReader(src)) hellpotHeffalump = heffalump.NewHeffalump(markovMap, heffalump.DefaultBuffSize) - } else { + default: log.Info().Msg("Using default source text") hellpotHeffalump = heffalump.NewDefaultHeffalump() }