added ip validation, implemented variable numImgsCaptcha
arnaucode idoctnef@openmailbox.org
Wed, 09 Aug 2017 15:03:31 +0200
7 files changed,
34 insertions(+),
12 deletions(-)
M
README.md
→
README.md
@@ -21,6 +21,16 @@ 
### 1.2 - Backend - Put dataset images in the folder 'imgs'. +- Configure serverConfig.json: +```json +{ + "serverIP": "127.0.0.1", + "serverPort": "3025", + "imgsFolder": "imgs", + "numImgsCaptcha": 9 +} +``` + - Run MongoDB. - Go to the folder /goCaptcha, and run: ```@@ -183,3 +193,4 @@
- If the captcha is resolved in less than 1 second, it's not valid. - If the captcha is resolved in more than 1 minute, it's not valid. - The images url, are UUIDs generated each time, in order to give different names for the images each time. +- The ip of requested captcha and answered captcha petitions must be the same.
M
captcha.go
→
captcha.go
@@ -21,6 +21,7 @@ Id string `json:"id"`
ImgsSolution []string `json:"imgssolution"` Question string `json:"question"` //select all X Date int64 `json:"date"` + Ip string `json:"ip"` } type CaptchaAnswer struct { CaptchaId string `json:"captchaid"`@@ -47,7 +48,7 @@ func generateQuestionFromCategoriesArray(imgs []string) string {
n := generateRandInt(0, len(imgs)) return imgs[n] } -func generateCaptcha(count int) Captcha { +func generateCaptcha(count int, ip string) Captcha { var captcha Captcha var captchaSol CaptchaSol@@ -71,11 +72,12 @@ question := generateQuestionFromCategoriesArray(captchaSol.ImgsSolution)
captcha.Question = question captchaSol.Question = question captchaSol.Date = time.Now().Unix() + captchaSol.Ip = ip err := captchaSolCollection.Insert(captchaSol) check(err) return captcha } -func validateCaptcha(captchaAnswer CaptchaAnswer) bool { +func validateCaptcha(captchaAnswer CaptchaAnswer, ip string) bool { var solved bool solved = true captchaSol := CaptchaSol{}@@ -106,6 +108,10 @@ if elapsed.Seconds() < 1 {
solved = false } if elapsed.Seconds() > 60 { + solved = false + } + //ip comprovation + if captchaSol.Ip != ip { solved = false } return solved
M
serverConfig.go
→
serverConfig.go
@@ -20,9 +20,10 @@ }
//server config type ServerConfig struct { - ServerIP string `json:"serverIP"` - ServerPort string `json:"serverPort"` - ImgsFolder string `json:"imgsFolder"` + ServerIP string `json:"serverIP"` + ServerPort string `json:"serverPort"` + ImgsFolder string `json:"imgsFolder"` + NumImgsCaptcha int `json:"numImgsCaptcha"` } var serverConfig ServerConfig
M
serverConfig.json
→
serverConfig.json
@@ -1,5 +1,6 @@
{ "serverIP": "127.0.0.1", "serverPort": "3025", - "imgsFolder": "imgs" + "imgsFolder": "imgs", + "numImgsCaptcha": 9 }
M
serverRoutes.go
→
serverRoutes.go
@@ -56,8 +56,6 @@ imgFakePath := ImgFakePath{}
err := imgFakePathCollection.Find(bson.M{"fake": imageName}).One(&imgFakePath) check(err) - fmt.Println(serverConfig.ImgsFolder + "/" + imgFakePath.Real) - file, err := ioutil.ReadFile(serverConfig.ImgsFolder + "/" + imgFakePath.Real) if err != nil { fmt.Fprintln(w, err)@@ -75,7 +73,8 @@ }
} func GetCaptcha(w http.ResponseWriter, r *http.Request) { - resp := generateCaptcha(6) + ip := strings.Split(r.RemoteAddr, ":")[0] + resp := generateCaptcha(serverConfig.NumImgsCaptcha, ip) jsonResp, err := json.Marshal(resp) check(err) fmt.Fprintln(w, string(jsonResp))@@ -88,7 +87,8 @@ err := decoder.Decode(&captchaAnswer)
check(err) defer r.Body.Close() - resp := validateCaptcha(captchaAnswer) + ip := strings.Split(r.RemoteAddr, ":")[0] + resp := validateCaptcha(captchaAnswer, ip) jsonResp, err := json.Marshal(resp) check(err) fmt.Fprintln(w, string(jsonResp))
M
web/goCaptcha.js
→
web/goCaptcha.js
@@ -1,5 +1,5 @@
-var selection = [0, 0, 0, 0, 0, 0]; +var selection = []; var captchaid = ""; function httpGet(url) {@@ -22,6 +22,10 @@ data = httpGet(goCaptchaURL + "/captcha")
captcha = JSON.parse(data); captchaid = captcha.id; showCaptcha(captcha); + selection=[]; + for(k in captcha.imgs){ + selection.push(0); + } } function showCaptcha(captcha) {@@ -56,7 +60,6 @@ var html = "";
if (resp) { html += "<h2>goCaptcha validated</h2>"; } else { - selection = [0, 0, 0, 0, 0, 0]; html += "<h2>goCaptcha failed</h2>"; html += "<div onclick='getCaptcha()' class='g_button c_red300 g_floatRight'>Reload Captcha</div>"; }