Skip to content

Commit

Permalink
Add optional -b/--book flag to choose source file other than Niet…
Browse files Browse the repository at this point in the history
…zche

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.
  • Loading branch information
ginger51011 committed Jan 18, 2024
1 parent e2e590e commit ef69b86
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion cmd/HellPot/HellPot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 4 additions & 1 deletion heffalump/heffalump.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion heffalump/markov.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions internal/config/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
24 changes: 14 additions & 10 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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(".", "_", "-", "_"))
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion internal/config/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<file>", "Specify a custom file used for text generation"},
5: {0: "--help", 1: "show this help and exit"},
},
out: os.Stdout,
}
Expand Down
27 changes: 24 additions & 3 deletions internal/http/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"net/http"
"os"
"runtime"
"strings"
"time"
Expand All @@ -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))
Expand Down Expand Up @@ -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")
Expand All @@ -74,7 +78,6 @@ func hellPot(ctx *fasthttp.RequestCtx) {
Dur("DURATION", time.Since(s)).
Msg("FINISH")
})

}

func getSrv(r *router.Router) fasthttp.Server {
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit ef69b86

Please sign in to comment.