Added logging on allowed domains middleware and duplicate requests

This commit is contained in:
Musab Gültekin 2019-11-16 20:34:09 +03:00
parent 9b8a3837bd
commit 6645820408
2 changed files with 13 additions and 3 deletions

View File

@ -3,16 +3,21 @@ package middleware
import ( import (
"github.com/geziyor/geziyor/client" "github.com/geziyor/geziyor/client"
"github.com/geziyor/geziyor/internal" "github.com/geziyor/geziyor/internal"
"log"
"sync"
) )
// AllowedDomains checks for request host if it exists in AllowedDomains // AllowedDomains checks for request host if it exists in AllowedDomains
type AllowedDomains struct { type AllowedDomains struct {
AllowedDomains []string AllowedDomains []string
logOnlyOnce sync.Map
} }
func (a *AllowedDomains) ProcessRequest(r *client.Request) { func (a *AllowedDomains) ProcessRequest(r *client.Request) {
if len(a.AllowedDomains) != 0 && !internal.Contains(a.AllowedDomains, r.Host) { if len(a.AllowedDomains) != 0 && !internal.Contains(a.AllowedDomains, r.Host) {
//log.Printf("Domain not allowed: %s\n", req.Host) if _, logged := a.logOnlyOnce.LoadOrStore(r.Host, struct{}{}); !logged {
log.Printf("Domain not allowed: %s\n", r.Host)
}
r.Cancel() r.Cancel()
return return
} }

View File

@ -2,6 +2,7 @@ package middleware
import ( import (
"github.com/geziyor/geziyor/client" "github.com/geziyor/geziyor/client"
"log"
"sync" "sync"
) )
@ -9,12 +10,16 @@ import (
type DuplicateRequests struct { type DuplicateRequests struct {
RevisitEnabled bool RevisitEnabled bool
visitedURLs sync.Map visitedURLs sync.Map
logOnlyOnce sync.Map
} }
func (a *DuplicateRequests) ProcessRequest(r *client.Request) { func (a *DuplicateRequests) ProcessRequest(r *client.Request) {
if !a.RevisitEnabled && r.Request.Method == "GET" { if !a.RevisitEnabled && r.Request.Method == "GET" {
if _, visited := a.visitedURLs.LoadOrStore(r.Request.URL.String(), struct{}{}); visited { requestURL := r.Request.URL.String()
//log.Printf("URL already visited %s\n") if _, visited := a.visitedURLs.LoadOrStore(requestURL, struct{}{}); visited {
if _, logged := a.logOnlyOnce.LoadOrStore(requestURL, struct{}{}); !logged {
log.Printf("URL already visited %s\n", requestURL)
}
r.Cancel() r.Cancel()
} }
} }