Skip to content

Commit

Permalink
increases timeout for database queries, adds caching header
Browse files Browse the repository at this point in the history
  • Loading branch information
timbastin committed Aug 30, 2024
1 parent c205a18 commit b1e429a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
4 changes: 3 additions & 1 deletion packages/honeypot/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/l3montree-dev/oh-my-honeypot/packages/types"
"github.com/l3montree-dev/oh-my-honeypot/packages/utils"
"github.com/sethvargo/go-password/password"

"github.com/spf13/viper"
)

Expand Down Expand Up @@ -353,7 +354,8 @@ func (h *httpHoneypot) Start() error {
}
err := svc.ListenAndServeTLS(h.cert, h.key)
if err != nil {
slog.Error("Error starting HTTPS server", "port", h.port, "err", err)
slog.Error("Error starting HTTPS server", "port", 443, "err", err)
break
}
}
}()
Expand Down
36 changes: 18 additions & 18 deletions packages/store/dbstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (p *PostgreSQL) Listen() chan<- types.Set {

// Insert the attack into the database and sanitize the input by using prepared statements
func (p *PostgreSQL) attackInsert(attackID string, honeypot_id string, t int, port int, ip string, country string, attackType string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

_, err := p.DB.Exec(ctx, `
Expand All @@ -194,7 +194,7 @@ func (p *PostgreSQL) attackInsert(attackID string, honeypot_id string, t int, po
}

func (p *PostgreSQL) loginAttemptInsert(attackID string, service string, username string, password string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, err := p.DB.Exec(ctx, `
INSERT INTO login_attempt (Attack_ID,service,Username,Password)
Expand All @@ -206,7 +206,7 @@ func (p *PostgreSQL) loginAttemptInsert(attackID string, service string, usernam
}

func (p *PostgreSQL) httpInsert(attackID string, method string, path string, acceptLanguage string, useragent []string, referrer string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, err := p.DB.Exec(ctx, `
INSERT INTO http_request (Attack_ID,method,path,accept_language,system,rendering_engine,platform,referrer)
Expand All @@ -218,7 +218,7 @@ func (p *PostgreSQL) httpInsert(attackID string, method string, path string, acc
}

func (p *PostgreSQL) bodyInsert(attackID string, contentType string, payloadSize string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, err := p.DB.Exec(ctx, `
INSERT INTO http_body (Attack_ID,content_type,payload_size)
Expand All @@ -230,7 +230,7 @@ func (p *PostgreSQL) bodyInsert(attackID string, contentType string, payloadSize
}

func (p *PostgreSQL) injectionInsert(attackID string, username string, password string, bot string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, err := p.DB.Exec(ctx, `
INSERT INTO http_injection (Attack_ID,username,password,bot)
Expand All @@ -241,7 +241,7 @@ func (p *PostgreSQL) injectionInsert(attackID string, username string, password
}
}
func (p *PostgreSQL) pwsInsert(attackID string, password string) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, err := p.DB.Exec(ctx, `
INSERT INTO generated_pws (Attack_ID,password)
Expand Down Expand Up @@ -407,7 +407,7 @@ func (p *PostgreSQL) GetCountIn24HoursByCountry() types.CountIn24HoursByCountryR
wg := errgroup.Group{}
wg.SetLimit(10)
mut := sync.Mutex{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

for _, honeypotID := range honeypotIDs {
Expand Down Expand Up @@ -470,7 +470,7 @@ func (p *PostgreSQL) GetLatestAttacks() types.SetResponse {
wg := errgroup.Group{}
wg.SetLimit(10)
mut := sync.Mutex{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

for _, honeypotID := range honeypotIDs {
Expand Down Expand Up @@ -542,7 +542,7 @@ func (p *PostgreSQL) GetCountIn24Hours() types.CountIn24HoursStatsResponse {
wg := errgroup.Group{}
wg.SetLimit(10)
mut := sync.Mutex{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
for _, honeypotID := range honeypotIDs {
wg.Go(func() error {
Expand Down Expand Up @@ -595,7 +595,7 @@ func (p *PostgreSQL) GetCountIn7Days() types.CountIn7DaysStatsResponse {
wg := errgroup.Group{}
wg.SetLimit(10)
mut := sync.Mutex{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
for _, honeypotID := range honeypotIDs {
wg.Go(func() error {
Expand Down Expand Up @@ -646,7 +646,7 @@ func (p *PostgreSQL) GetCountIn6Months() types.CountIn6MonthsStatsResponse {
wg := errgroup.Group{}
wg.SetLimit(10)
mut := sync.Mutex{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
for _, honeypotID := range honeypotIDs {
wg.Go(func() error {
Expand Down Expand Up @@ -697,7 +697,7 @@ func (p *PostgreSQL) GetCountryStats() types.CountryStatsResponse {
wg := errgroup.Group{}
wg.SetLimit(10)
mut := sync.Mutex{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
for _, honeypotID := range honeypotIDs {
wg.Go(func() error {
Expand Down Expand Up @@ -746,7 +746,7 @@ func (p *PostgreSQL) GetIPStats() types.IPStatsResponse {
wg := errgroup.Group{}
wg.SetLimit(10)
mut := sync.Mutex{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
for _, honeypotID := range honeypotIDs {
wg.Go(func() error {
Expand Down Expand Up @@ -797,7 +797,7 @@ func (p *PostgreSQL) GetUsernameStats() types.UsernameStatsResponse {
wg := errgroup.Group{}
wg.SetLimit(10)
mut := sync.Mutex{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
for _, honeypotID := range honeypotIDs {
wg.Go(func() error {
Expand Down Expand Up @@ -847,7 +847,7 @@ func (p *PostgreSQL) GetPasswordStats() types.PasswordStatsResponse {
wg := errgroup.Group{}
wg.SetLimit(10)
mut := sync.Mutex{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
for _, honeypotID := range honeypotIDs {
wg.Go(func() error {
Expand Down Expand Up @@ -897,7 +897,7 @@ func (p *PostgreSQL) GetPortStats() types.PortStatsResponse {
wg := errgroup.Group{}
wg.SetLimit(10)
mut := sync.Mutex{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
for _, honeypotID := range honeypotIDs {
wg.Go(func() error {
Expand Down Expand Up @@ -946,7 +946,7 @@ func (p *PostgreSQL) GetPathStats() types.PathStatsResponse {
wg := errgroup.Group{}
wg.SetLimit(10)
mut := sync.Mutex{}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
for _, honeypotID := range honeypotIDs {
wg.Go(func() error {
Expand Down Expand Up @@ -1003,7 +1003,7 @@ func (p *PostgreSQL) honeypotIds() []string {
}

func (p *PostgreSQL) getHoneypotIDs() ([]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
rows, err := p.DB.Query(
ctx,
Expand Down
37 changes: 26 additions & 11 deletions packages/transport/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,37 @@ func setDefaultHeaders(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
}

func cacheControlMiddleware(maxAge int, staleWhileRevalidateMaxAge int) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set the Cache-Control header
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d, stale-while-revalidate=%d", maxAge, staleWhileRevalidateMaxAge))

// Continue with the next handler
next.ServeHTTP(w, r)
})
}
}

func (h *httpTransport) Listen() {
// create a new http server
mux := http.NewServeMux()
// the response is fresh for 1 hour, it can be served stale for 1 day
cachingMiddleware := cacheControlMiddleware(60*60, 60*60*24)

mux.Handle("GET /realtime", h.handleSSE())
mux.Handle("GET /latest-attacks", h.handleLatestAttacks())
mux.Handle("GET /stats/count-in-24hours-by-country", h.handleCountIn24HoursByCountry())
mux.Handle("GET /stats/count-in-24hours", h.handleCountIn24Hours())
mux.Handle("GET /stats/count-in-7days", h.handleCountIn7Days())
mux.Handle("GET /stats/count-in-6months", h.handleCountIn6Monts())
mux.Handle("GET /stats/country", h.handleCountryStats())
mux.Handle("GET /stats/ip", h.handleIPStats())
mux.Handle("GET /stats/port", h.handlePortStats())
mux.Handle("GET /stats/username", h.handleUsernameStats())
mux.Handle("GET /stats/password", h.handlePasswordStats())
mux.Handle("GET /stats/path", h.handlePathStats())
mux.Handle("GET /health", h.handleHealth())
mux.Handle("GET /stats/count-in-24hours-by-country", cachingMiddleware(h.handleCountIn24HoursByCountry()))
mux.Handle("GET /stats/count-in-24hours", cachingMiddleware(h.handleCountIn24Hours()))
mux.Handle("GET /stats/count-in-7days", cachingMiddleware(h.handleCountIn7Days()))
mux.Handle("GET /stats/count-in-6months", cachingMiddleware(h.handleCountIn6Monts()))
mux.Handle("GET /stats/country", cachingMiddleware(h.handleCountryStats()))
mux.Handle("GET /stats/ip", cachingMiddleware(h.handleIPStats()))
mux.Handle("GET /stats/port", cachingMiddleware(h.handlePortStats()))
mux.Handle("GET /stats/username", cachingMiddleware(h.handleUsernameStats()))
mux.Handle("GET /stats/password", cachingMiddleware(h.handlePasswordStats()))
mux.Handle("GET /stats/path", cachingMiddleware(h.handlePathStats()))
mux.Handle("GET /health", cachingMiddleware(h.handleHealth()))

go http.ListenAndServe(":"+fmt.Sprintf("%d", h.port), mux) // nolint
slog.Info("HTTP transport listening", "port", h.port)
Expand Down

0 comments on commit b1e429a

Please sign in to comment.