-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TeeHee: Essentially rewrite HellPot :^)
- Loading branch information
1 parent
14ba022
commit cc7020f
Showing
25 changed files
with
818 additions
and
747 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
||
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...") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.