diff --git a/README.md b/README.md index 97ac23f..a0f30be 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,16 @@ It will place the goCaptcha box in the div: ### 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 selection is correct, returns 'true', if the selection is not correct, re - 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. diff --git a/captcha.go b/captcha.go index bdbf31b..308251a 100644 --- a/captcha.go +++ b/captcha.go @@ -21,6 +21,7 @@ type CaptchaSol struct { 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 @@ func generateCaptcha(count int) Captcha { 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{} @@ -108,5 +110,9 @@ func validateCaptcha(captchaAnswer CaptchaAnswer) bool { if elapsed.Seconds() > 60 { solved = false } + //ip comprovation + if captchaSol.Ip != ip { + solved = false + } return solved } diff --git a/goCaptcha b/goCaptcha index 6d436d8..ecc4a74 100755 Binary files a/goCaptcha and b/goCaptcha differ diff --git a/serverConfig.go b/serverConfig.go index 2d56dac..e45e688 100644 --- a/serverConfig.go +++ b/serverConfig.go @@ -20,9 +20,10 @@ type Route struct { //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 diff --git a/serverConfig.json b/serverConfig.json index d3f940b..b12ab8f 100644 --- a/serverConfig.json +++ b/serverConfig.json @@ -1,5 +1,6 @@ { "serverIP": "127.0.0.1", "serverPort": "3025", - "imgsFolder": "imgs" + "imgsFolder": "imgs", + "numImgsCaptcha": 9 } diff --git a/serverRoutes.go b/serverRoutes.go index 2b58377..d1e77b7 100644 --- a/serverRoutes.go +++ b/serverRoutes.go @@ -56,8 +56,6 @@ func GetImage(w http.ResponseWriter, r *http.Request) { 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 GetImage(w http.ResponseWriter, r *http.Request) { } 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 @@ func AnswerCaptcha(w http.ResponseWriter, r *http.Request) { 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)) diff --git a/web/goCaptcha.js b/web/goCaptcha.js index cfd389c..c96d224 100644 --- a/web/goCaptcha.js +++ b/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 @@ function getCaptcha() { 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 @@ function validateCaptcha() { if (resp) { html += "