2021-06-11 22:05:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-06-11 23:25:45 +00:00
|
|
|
"HellPot/src/config"
|
2021-06-11 22:05:40 +00:00
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-06-11 23:25:45 +00:00
|
|
|
const robotsTxt = "User-agent: *\r\n"
|
2021-06-11 22:05:40 +00:00
|
|
|
|
|
|
|
func startPot() {
|
2021-06-11 23:25:45 +00:00
|
|
|
var paths string
|
|
|
|
addr := config.BindAddr
|
|
|
|
port := config.BindPort
|
2021-06-11 22:05:40 +00:00
|
|
|
|
|
|
|
// subscribe to SIGINT signals
|
|
|
|
stopChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(stopChan, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
2021-06-11 23:25:45 +00:00
|
|
|
for _, p := range config.Paths {
|
|
|
|
http.HandleFunc(p, func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
DefaultHoneypot(w, r)
|
|
|
|
})
|
|
|
|
paths = paths + "Disallow: " + p + "\r\n"
|
|
|
|
}
|
2021-06-11 22:05:40 +00:00
|
|
|
|
|
|
|
http.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
|
2021-06-11 23:25:45 +00:00
|
|
|
if _, err := io.WriteString(w, robotsTxt+paths+"\r\n"); err != nil {
|
2021-06-11 22:05:40 +00:00
|
|
|
log.Error().Err(err).Msg("SERVE_ROBOTS_ERROR")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
srv := &http.Server{Addr: addr + ":" + port, Handler: http.DefaultServeMux}
|
|
|
|
|
|
|
|
go func() {
|
2021-06-11 23:25:45 +00:00
|
|
|
log.Info().Str("bind_addr", addr).Str("bind_port", port).
|
|
|
|
Msg("Listening and serving HTTP...")
|
2021-06-11 22:05:40 +00:00
|
|
|
// service connections
|
|
|
|
err := srv.ListenAndServe()
|
|
|
|
log.Warn().Err(err).Msg("HTTP_STOP")
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-stopChan // wait for SIGINT
|
|
|
|
log.Warn().Msg("Shutting down server...")
|
|
|
|
|
|
|
|
// shut down gracefully, but wait no longer than 5 seconds before halting
|
|
|
|
ctx, c := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer c()
|
|
|
|
srv.Shutdown(ctx)
|
|
|
|
|
|
|
|
log.Info().Msg("Server gracefully stopped")
|
|
|
|
}
|