Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional -g/--grimoire flag to choose source file other than Niet… #131

Merged
merged 4 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
ginger51011 marked this conversation as resolved.
Show resolved Hide resolved

## 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
ginger51011 marked this conversation as resolved.
Show resolved Hide resolved
DefaultBuffSize int = 100 * 1 << 10
ginger51011 marked this conversation as resolved.
Show resolved Hide resolved
)

// 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
ginger51011 marked this conversation as resolved.
Show resolved Hide resolved
// 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 {
ginger51011 marked this conversation as resolved.
Show resolved Hide resolved
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