From ff4f4cead6b6f9c66f87c510a0ec45594319af33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Gasser?= Date: Mon, 2 Dec 2024 22:36:18 -0500 Subject: [PATCH] detect bots --- go.mod | 5 ++++- go.sum | 2 ++ server/handleLikes.go | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index d74b4d2..397e86c 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 388b9c6..314664f 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/server/handleLikes.go b/server/handleLikes.go index c58286e..3198db5 100644 --- a/server/handleLikes.go +++ b/server/handleLikes.go @@ -9,6 +9,7 @@ import ( "github.com/aureliengasser/planetocd/server/likes" "github.com/gorilla/mux" + "github.com/x-way/crawlerdetect" ) type LikeArticleResponse struct { @@ -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) @@ -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 {