Browse Source

added ip validation, implemented variable numImgsCaptcha

master
arnaucode 7 years ago
parent
commit
9adf62a5df
7 changed files with 34 additions and 12 deletions
  1. +11
    -0
      README.md
  2. +8
    -2
      captcha.go
  3. BIN
      goCaptcha
  4. +4
    -3
      serverConfig.go
  5. +2
    -1
      serverConfig.json
  6. +4
    -4
      serverRoutes.go
  7. +5
    -2
      web/goCaptcha.js

+ 11
- 0
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.

+ 8
- 2
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
}

BIN
goCaptcha


+ 4
- 3
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

+ 2
- 1
serverConfig.json

@ -1,5 +1,6 @@
{
"serverIP": "127.0.0.1",
"serverPort": "3025",
"imgsFolder": "imgs"
"imgsFolder": "imgs",
"numImgsCaptcha": 9
}

+ 4
- 4
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))

+ 5
- 2
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 += "<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>";
}

Loading…
Cancel
Save