added captcha system that generates every 5 minutes
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gitlab.com/rawleyifowler/site-rework/models"
|
"gitlab.com/rawleyifowler/site-rework/models"
|
||||||
@@ -35,6 +36,7 @@ var (
|
|||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
apiKey string
|
apiKey string
|
||||||
recentPosters map[string]uint
|
recentPosters map[string]uint
|
||||||
|
captchaVals [2]int
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterBlogGroup(r *gin.RouterGroup) {
|
func RegisterBlogGroup(r *gin.RouterGroup) {
|
||||||
@@ -56,6 +58,7 @@ func RegisterBlogGroup(r *gin.RouterGroup) {
|
|||||||
r.POST("/post/comment", CreateComment)
|
r.POST("/post/comment", CreateComment)
|
||||||
// Clear the commenters cache every 15 minutes
|
// Clear the commenters cache every 15 minutes
|
||||||
go utils.TimedClearMap(&recentPosters, 180000*5)
|
go utils.TimedClearMap(&recentPosters, 180000*5)
|
||||||
|
go utils.UpdateCaptcha(&captchaVals)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RenderBlogPage(c *gin.Context) {
|
func RenderBlogPage(c *gin.Context) {
|
||||||
@@ -72,7 +75,10 @@ func RenderIndividualBlogPost(c *gin.Context) {
|
|||||||
if bp == nil {
|
if bp == nil {
|
||||||
c.HTML(http.StatusNotFound, "not_found.tmpl", gin.H{})
|
c.HTML(http.StatusNotFound, "not_found.tmpl", gin.H{})
|
||||||
} else {
|
} else {
|
||||||
c.HTML(http.StatusOK, "blog_post.tmpl", bp)
|
c.HTML(http.StatusOK, "blog_post.tmpl", struct {
|
||||||
|
Post *models.BlogPost
|
||||||
|
Captcha [2]int
|
||||||
|
}{Post: bp, Captcha: captchaVals})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,8 +118,13 @@ func CreateComment(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
a := []string{c.Request.Form.Get("author"),
|
a := []string{c.Request.Form.Get("author"),
|
||||||
c.Request.Form.Get("content"),
|
c.Request.Form.Get("content"),
|
||||||
c.Request.Form.Get("url")}
|
c.Request.Form.Get("url"),
|
||||||
|
c.Request.Form.Get("captcha")}
|
||||||
|
i, err := strconv.ParseInt(a[3], 10, 32)
|
||||||
|
if err != nil || int(i) != (captchaVals[0]+captchaVals[1]) {
|
||||||
|
c.HTML(http.StatusNotAcceptable, "comment_post_failed.tmpl", CommentDto{Url: a[2]})
|
||||||
|
return
|
||||||
|
}
|
||||||
if GetNumberOfRecentPosts(c) > 3 {
|
if GetNumberOfRecentPosts(c) > 3 {
|
||||||
c.HTML(http.StatusOK, "comment_post_spam.tmpl", CommentDto{Url: a[2]})
|
c.HTML(http.StatusOK, "comment_post_spam.tmpl", CommentDto{Url: a[2]})
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
{{ template "header.tmpl" . }}
|
{{ template "header.tmpl" . }}
|
||||||
<div>
|
<div>
|
||||||
<h2>{{ .Title }}</h2>
|
<h2>{{ .Post.Title }}</h2>
|
||||||
<div>{{ .Content }}</div>
|
<div>{{ .Post.Content }}</div>
|
||||||
<h4>Comments</h4>
|
<h4>Comments</h4>
|
||||||
{{ range $comment := .Comments }}
|
{{ range $comment := .Post.Comments }}
|
||||||
<div class="comment">
|
<div class="comment">
|
||||||
<div>
|
<div>
|
||||||
{{ $comment.Author }} at {{ $comment.Date }}
|
{{ $comment.Author }} at {{ $comment.Date }}
|
||||||
@@ -19,7 +19,8 @@
|
|||||||
<form method="POST" action="/blog/post/comment">
|
<form method="POST" action="/blog/post/comment">
|
||||||
<input type="text" required pattern=".{3,}" title="Author name must be atleast 3 chars." placeholder="Name" maxlength="50" name="author"/>
|
<input type="text" required pattern=".{3,}" title="Author name must be atleast 3 chars." placeholder="Name" maxlength="50" name="author"/>
|
||||||
<input type="field" required pattern=".{15,}" title="Your comment must be atleast 15 chars." placeholder="Comment" maxlength="500" name="content" />
|
<input type="field" required pattern=".{15,}" title="Your comment must be atleast 15 chars." placeholder="Comment" maxlength="500" name="content" />
|
||||||
<input type="text" required style="display: none;" name="url" value="{{ .Url }}" readonly />
|
<input type="text" required pattern="^\d+" title="The captcha must be a number." placeholder="What is {{ index .Captcha 0 }} + {{ index .Captcha 1 }}?" maxlength="10" name="captcha" />
|
||||||
|
<input type="text" required style="display: none;" name="url" value="{{ .Post.Url }}" readonly />
|
||||||
<button type="submit">Post</button>
|
<button type="submit">Post</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
17
utils/captcha.go
Normal file
17
utils/captcha.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenerateCaptchaValues() (int, int) {
|
||||||
|
return rand.Intn(100), rand.Intn(100)
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateCaptcha(captcha *[2]int) {
|
||||||
|
for {
|
||||||
|
captcha[0], captcha[1] = GenerateCaptchaValues()
|
||||||
|
time.Sleep(5 * time.Minute)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user