Skip to content

Commit

Permalink
detect bots
Browse files Browse the repository at this point in the history
  • Loading branch information
AurelienGasser committed Dec 3, 2024
1 parent 05d709a commit ff4f4ce
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/aureliengasser/planetocd

go 1.23
go 1.23.0

toolchain go1.23.2

require (
cloud.google.com/go v0.81.0
Expand Down Expand Up @@ -31,6 +33,7 @@ require (
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/snabb/diagio v1.0.4 // indirect
github.com/x-way/crawlerdetect v0.2.24 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/net v0.21.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/x-way/crawlerdetect v0.2.24 h1:kZDzSeiXB64M+Bknopn5GddHT+LBocD61jEjqDOufLE=
github.com/x-way/crawlerdetect v0.2.24/go.mod h1:s6iUJZPq/WNBJThPRK+zk8ah7iIbGUZn9nYWMls3YP0=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
9 changes: 9 additions & 0 deletions server/handleLikes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/aureliengasser/planetocd/server/likes"
"github.com/gorilla/mux"
"github.com/x-way/crawlerdetect"
)

type LikeArticleResponse struct {
Expand All @@ -17,6 +18,10 @@ type LikeArticleResponse struct {
}

func handleLikeArticle(w http.ResponseWriter, r *http.Request) {
if crawlerdetect.IsCrawler(strings.Join(r.Header[http.CanonicalHeaderKey("User-Agent")], "")) {
http.NotFound(w, r)
return
}
vars := mux.Vars(r)
idStr := vars["id"]
id, err := strconv.Atoi(idStr)
Expand Down Expand Up @@ -55,6 +60,10 @@ type UpdateArticleLikeRequest struct {
}

func handleUpdateArticleLike(w http.ResponseWriter, r *http.Request) {
if crawlerdetect.IsCrawler(strings.Join(r.Header[http.CanonicalHeaderKey("User-Agent")], "")) {
http.NotFound(w, r)
return
}
var req UpdateArticleLikeRequest
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
Expand Down

0 comments on commit ff4f4ce

Please sign in to comment.