Skip to content

Commit

Permalink
TeeHee: Essentially rewrite HellPot :^)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunginnanet committed Jun 21, 2024
1 parent 14ba022 commit cc7020f
Show file tree
Hide file tree
Showing 25 changed files with 818 additions and 747 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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 you may synchronize HellPot with your nightmares by using the `-g`/`--grimoire` 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

Expand Down
130 changes: 103 additions & 27 deletions cmd/HellPot/HellPot.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,135 @@
package main

import (
"io"
"os"
"os/signal"
"path/filepath"
"strconv"
"syscall"

"github.com/rs/zerolog"
"time"

"github.com/yunginnanet/HellPot/internal/config"
"github.com/yunginnanet/HellPot/internal/extra"
"github.com/yunginnanet/HellPot/internal/http"
"github.com/yunginnanet/HellPot/internal/logger"
"github.com/yunginnanet/HellPot/internal/version"
)

var (
log zerolog.Logger
version string // set by linker
runningConfig *config.Parameters
)

func init() {
if version != "" {
config.Version = version[1:]
func writeConfig(target string) bool {
var f *os.File
var err error
if f, err = os.Create(target); err != nil {
println("failed to create config file: " + err.Error())
return false
}
config.Init()
if config.BannerOnly {
extra.Banner()
os.Exit(0)
if _, err = io.Copy(f, config.Defaults.IO); err != nil {
println("failed to write default config to file: " + err.Error())
_ = f.Close()
return false
}
println("wrote default config to " + target)
runningConfig, _ = config.Setup(f)
_ = f.Close()
return true
}

switch config.DockerLogging {
case true:
config.CurrentLogFile = "/dev/stdout"
config.NoColor = true
log = config.StartLogger(false, os.Stdout)
default:
log = config.StartLogger(true)
func main() {
conf := config.CLIFlags.Lookup("config").Value
if conf.String() == "" {
conf = config.CLIFlags.Lookup("c").Value
}

extra.Banner()
usingDefaults := true
resolvedConf := conf.String()

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")
uconf, _ := os.UserConfigDir()
if uconf == "" && os.Getenv("HOME") != "" {
uconf = filepath.Join(os.Getenv("HOME"), ".config")
}

}
if resolvedConf == "" {
for _, path := range []string{
"/etc/HellPot/config.toml",
"/usr/local/etc/HellPot/config.toml",
"./config.toml",
filepath.Join(uconf, "HellPot", "config.toml"),
} {
if _, err := os.Stat(path); err == nil {
resolvedConf = path
break
}
}
}

var setupErr error
var err error
var f *os.File

f, err = os.Open(resolvedConf)
if err == nil {
runningConfig, setupErr = config.Setup(f)
}
switch {
case setupErr != nil:
println("failed to setup config: " + setupErr.Error())
case err != nil:
println("failed to open config file for reading: " + err.Error())
println("trying to create it....")
wroteOK := writeConfig(resolvedConf)
if wroteOK {
break
}
case runningConfig != nil:
usingDefaults = false
_ = f.Close()
}

if runningConfig == nil {
if runningConfig, err = config.Setup(nil); err != nil || runningConfig == nil {
panic("failed to setup default config...\n" + err.Error())
return // unreachable, but the linter doesn't seem to realize that

Check failure on line 95 in cmd/HellPot/HellPot.go

View workflow job for this annotation

GitHub Actions / build

unreachable code
}
}

log, _ := logger.New(runningConfig.Logger)

if usingDefaults {
log.Warn().Msg("continuing with default configuration in ")
for i := 5; i > 0; i-- {
print(strconv.Itoa(i))
for i := 0; i < 5; i++ {
time.Sleep(200 * time.Millisecond)
print(".")
}
}
}

if !runningConfig.Logger.NoColor {
extra.Banner()
}

log.Info().Msg("🔥 Starting HellPot 🔥")
log.Info().Msg("Version: " + version.Version)
log.Info().Msg("PID: " + strconv.Itoa(os.Getpid()))
log.Info().Msg("Using config file: " + resolvedConf)
if usingDefaults {
log.Warn().Msg("Using default configuration")
}
log.Debug().Msg("Debug logging enabled")
log.Trace().Msg("Trace logging enabled")

func main() {
stopChan := make(chan os.Signal, 1)
signal.Notify(stopChan, syscall.SIGINT, syscall.SIGTERM)

go func() {
log.Fatal().Err(http.Serve()).Msg("HTTP error")
log.Fatal().Err(http.Serve(runningConfig)).Msg("HTTP error")
}()

<-stopChan // wait for SIGINT
log.Warn().Msg("Shutting down server...")

}
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ require (
github.com/fasthttp/router v1.5.1
github.com/knadh/koanf/parsers/toml v0.1.0
github.com/knadh/koanf/providers/env v0.1.0
github.com/knadh/koanf/providers/file v0.1.0
github.com/knadh/koanf/providers/structs v0.1.0
github.com/knadh/koanf/v2 v2.1.1
github.com/rs/zerolog v1.33.0
github.com/spf13/afero v1.11.0
github.com/valyala/fasthttp v1.55.0
golang.org/x/term v0.21.0
)

require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
Expand All @@ -29,5 +28,4 @@ require (
github.com/savsgio/gotils v0.0.0-20240303185622-093b76447511 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
)
13 changes: 4 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/fasthttp/router v1.5.1 h1:uViy8UYYhm5npJSKEZ4b/ozM//NGzVCfJbh6VJ0VKr8=
github.com/fasthttp/router v1.5.1/go.mod h1:WrmsLo3mrerZP2VEXRV1E8nL8ymJFYCDTr4HmnB8+Zs=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 h1:TQcrn6Wq+sKGkpyPvppOz99zsMBaUOKXq6HSv655U1c=
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand All @@ -19,8 +19,8 @@ github.com/knadh/koanf/parsers/toml v0.1.0 h1:S2hLqS4TgWZYj4/7mI5m1CQQcWurxUz6OD
github.com/knadh/koanf/parsers/toml v0.1.0/go.mod h1:yUprhq6eo3GbyVXFFMdbfZSo928ksS+uo0FFqNMnO18=
github.com/knadh/koanf/providers/env v0.1.0 h1:LqKteXqfOWyx5Ab9VfGHmjY9BvRXi+clwyZozgVRiKg=
github.com/knadh/koanf/providers/env v0.1.0/go.mod h1:RE8K9GbACJkeEnkl8L/Qcj8p4ZyPXZIQ191HJi44ZaQ=
github.com/knadh/koanf/providers/file v0.1.0 h1:fs6U7nrV58d3CFAFh8VTde8TM262ObYf3ODrc//Lp+c=
github.com/knadh/koanf/providers/file v0.1.0/go.mod h1:rjJ/nHQl64iYCtAW2QQnF0eSmDEX/YZ/eNFj5yR6BvA=
github.com/knadh/koanf/providers/structs v0.1.0 h1:wJRteCNn1qvLtE5h8KQBvLJovidSdntfdyIbbCzEyE0=
github.com/knadh/koanf/providers/structs v0.1.0/go.mod h1:sw2YZ3txUcqA3Z27gPlmmBzWn1h8Nt9O6EP/91MkcWE=
github.com/knadh/koanf/v2 v2.1.1 h1:/R8eXqasSTsmDCsAyYj+81Wteg8AqrV9CP6gvsTsOmM=
github.com/knadh/koanf/v2 v2.1.1/go.mod h1:4mnTRbZCK+ALuBXHZMjDfG9y714L7TykVnZkXbMU3Es=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand All @@ -41,22 +41,17 @@ github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/savsgio/gotils v0.0.0-20240303185622-093b76447511 h1:KanIMPX0QdEdB4R3CiimCAbxFrhB3j7h0/OvpYGVQa8=
github.com/savsgio/gotils v0.0.0-20240303185622-093b76447511/go.mod h1:sM7Mt7uEoCeFSCBM+qBrqvEo+/9vdmj19wzp3yzUhmg=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8=
github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.21.0 h1:WVXCp+/EBEHOj53Rvu+7KiT/iElMrO8ACK16SMZ3jaA=
golang.org/x/term v0.21.0/go.mod h1:ooXLefLobQVslOqselCNF4SxFAaoS6KujMbsGzSDmX0=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
nullprogram.com/x/rng v1.1.0 h1:SMU7DHaQSWtKJNTpNFIFt8Wd/KSmOuSDPXrMFp/UMro=
8 changes: 2 additions & 6 deletions heffalump/heffalump.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ import (
"bufio"
"io"
"sync"

"github.com/yunginnanet/HellPot/internal/config"
)

var log = config.GetLogger()

const DefaultBuffSize = 100 * 1 << 10

// Heffalump represents our buffer pool and markov map from Heffalump
Expand Down Expand Up @@ -54,11 +50,11 @@ func (h *Heffalump) WriteHell(bw *bufio.Writer, cType ContentType) (int64, error
var n int64
var err error

defer func() {
/* defer func() {
if r := recover(); r != nil {
log.Error().Interface("caller", r).Msg("panic recovered!")
}
}()
}()*/

buf := h.pool.Get().([]byte)

Expand Down
44 changes: 0 additions & 44 deletions internal/config/arguments.go

This file was deleted.

83 changes: 83 additions & 0 deletions internal/config/client_rules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package config

import (
"bytes"
"fmt"
"regexp"
)

type ClientRules struct {
// See: https://github.com/yunginnanet/HellPot/issues/23
UseragentDisallowStrings []string `koanf:"user_agent_disallow_strings"`
useragentDisallowStrBytes [][]byte `koanf:"-"`
UseragentDisallowRegex []string `koanf:"user_agent_disallow_regex"`
useragentDisallowRegex []*regexp.Regexp `koanf:"-"`
}

func NewClientRules(strs []string, regex []string) (*ClientRules, error) {
if strs == nil && regex == nil {
return &ClientRules{}, nil
}
if regex == nil {
regex = make([]string, 0)
}
if strs == nil {
strs = make([]string, 0)
}
cr := &ClientRules{
UseragentDisallowStrings: strs,
UseragentDisallowRegex: regex,
}
return cr, cr.compile()
}

func (c *ClientRules) compile() error {
dupes := make(map[string]struct{})
for _, v := range c.UseragentDisallowRegex {
if v == "" {
continue
}
if _, ok := dupes[v]; ok {
continue
}
dupes[v] = struct{}{}
var compd *regexp.Regexp
var err error
if compd, err = regexp.Compile(v); err != nil {
return fmt.Errorf("failed to compile regex '%s': %w", v, err)
}
c.useragentDisallowRegex = append(c.useragentDisallowRegex, compd)
}

newStrs := make([]string, 0)
for _, v := range c.UseragentDisallowStrings {
if v == "" {
continue
}
if _, ok := dupes[v]; ok {
continue
}
dupes[v] = struct{}{}
newStrs = append(newStrs, v)
}
c.UseragentDisallowStrings = newStrs
c.useragentDisallowStrBytes = make([][]byte, len(c.UseragentDisallowStrings))
for i, v := range c.UseragentDisallowStrings {
c.useragentDisallowStrBytes[i] = []byte(v)
}
return nil
}

func (c *ClientRules) MatchUseragent(ua []byte) bool {
for _, v := range c.useragentDisallowRegex {
if v.Match(ua) {
return true
}
}
for _, v := range c.useragentDisallowStrBytes {
if bytes.Contains(ua, v) {
return true
}
}
return false
}
Loading

0 comments on commit cc7020f

Please sign in to comment.