Skip to content

Commit

Permalink
feat: Add serve_ignore option (#15)
Browse files Browse the repository at this point in the history
This commit introduces a new option `serve_ignore` to the
Defender middleware. When enabled, it serves a robots.txt
file with a "Disallow: /" directive to discourage search
engine crawlers.

The robots.txt file now allows specific bots like
Googlebot, Bingbot, and DuckDuckBot while blocking others.
This helps in managing how search engines interact with
the protected resource.

Signed-off-by: Jason Cameron <git@jasoncameron.dev>
  • Loading branch information
JasonLovesDoggo committed Jan 30, 2025
1 parent a74c28b commit 5c9db4e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/garbage/Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

defender garbage {
ranges private
serve_ignore
}
respond "This is what a human sees"
}
Expand Down
20 changes: 18 additions & 2 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@ import (
// serveIgnore is a helper function to serve a robots.txt file if the ServeIgnore option is enabled.
// It returns true if the request was handled, false otherwise.
func (m Defender) serveGitignore(w http.ResponseWriter, r *http.Request) bool {
if !m.ServeIgnore && r.URL.Path != "/robots.txt" && r.Method != http.MethodGet {
m.log.Debug("ServeIgnore", zap.Bool("serveIgnore", m.ServeIgnore), zap.String("path", r.URL.Path), zap.String("method", r.Method))
// Serve robots.txt only if ServeIgnore is enabled, the path is "/robots.txt", and the method is GET.
if !m.ServeIgnore || r.URL.Path != "/robots.txt" || r.Method != http.MethodGet {
return false
}
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte("User-agent: *\nDisallow: /\n"))
// Build the robots.txt content to allow specific bots and block others.
robotsTxt := `
User-agent: Googlebot
Disallow:
User-agent: Bingbot
Disallow:
User-agent: DuckDuckBot
Disallow:
User-agent: *
Disallow: /
`
w.Write([]byte(robotsTxt))
return true
}

Expand Down
3 changes: 3 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ type Defender struct {
// Required. Must be one of: "block", "garbage", "custom"
RawResponder string `json:"raw_responder,omitempty"`

// ServeIgnore specifies whether to serve a robots.txt file with a "Disallow: /" directive
// Default: false
ServeIgnore bool `json:"serve_ignore,omitempty"`
// responder is the internal implementation of the response strategy
responder responders.Responder
ipChecker *ip.IPChecker
Expand Down

0 comments on commit 5c9db4e

Please sign in to comment.