mirror of
https://github.com/arnaucube/goCaptcha.git
synced 2026-02-07 03:26:41 +01:00
added ip validation, implemented variable numImgsCaptcha
This commit is contained in:
11
README.md
11
README.md
@@ -21,6 +21,16 @@ It will place the goCaptcha box in the div:
|
|||||||
|
|
||||||
### 1.2 - Backend
|
### 1.2 - Backend
|
||||||
- Put dataset images in the folder 'imgs'.
|
- Put dataset images in the folder 'imgs'.
|
||||||
|
- Configure serverConfig.json:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"serverIP": "127.0.0.1",
|
||||||
|
"serverPort": "3025",
|
||||||
|
"imgsFolder": "imgs",
|
||||||
|
"numImgsCaptcha": 9
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
- Run MongoDB.
|
- Run MongoDB.
|
||||||
- Go to the folder /goCaptcha, and run:
|
- 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 less than 1 second, it's not valid.
|
||||||
- If the captcha is resolved in more than 1 minute, 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 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.
|
||||||
|
|||||||
10
captcha.go
10
captcha.go
@@ -21,6 +21,7 @@ type CaptchaSol struct {
|
|||||||
ImgsSolution []string `json:"imgssolution"`
|
ImgsSolution []string `json:"imgssolution"`
|
||||||
Question string `json:"question"` //select all X
|
Question string `json:"question"` //select all X
|
||||||
Date int64 `json:"date"`
|
Date int64 `json:"date"`
|
||||||
|
Ip string `json:"ip"`
|
||||||
}
|
}
|
||||||
type CaptchaAnswer struct {
|
type CaptchaAnswer struct {
|
||||||
CaptchaId string `json:"captchaid"`
|
CaptchaId string `json:"captchaid"`
|
||||||
@@ -47,7 +48,7 @@ func generateQuestionFromCategoriesArray(imgs []string) string {
|
|||||||
n := generateRandInt(0, len(imgs))
|
n := generateRandInt(0, len(imgs))
|
||||||
return imgs[n]
|
return imgs[n]
|
||||||
}
|
}
|
||||||
func generateCaptcha(count int) Captcha {
|
func generateCaptcha(count int, ip string) Captcha {
|
||||||
var captcha Captcha
|
var captcha Captcha
|
||||||
var captchaSol CaptchaSol
|
var captchaSol CaptchaSol
|
||||||
|
|
||||||
@@ -71,11 +72,12 @@ func generateCaptcha(count int) Captcha {
|
|||||||
captcha.Question = question
|
captcha.Question = question
|
||||||
captchaSol.Question = question
|
captchaSol.Question = question
|
||||||
captchaSol.Date = time.Now().Unix()
|
captchaSol.Date = time.Now().Unix()
|
||||||
|
captchaSol.Ip = ip
|
||||||
err := captchaSolCollection.Insert(captchaSol)
|
err := captchaSolCollection.Insert(captchaSol)
|
||||||
check(err)
|
check(err)
|
||||||
return captcha
|
return captcha
|
||||||
}
|
}
|
||||||
func validateCaptcha(captchaAnswer CaptchaAnswer) bool {
|
func validateCaptcha(captchaAnswer CaptchaAnswer, ip string) bool {
|
||||||
var solved bool
|
var solved bool
|
||||||
solved = true
|
solved = true
|
||||||
captchaSol := CaptchaSol{}
|
captchaSol := CaptchaSol{}
|
||||||
@@ -108,5 +110,9 @@ func validateCaptcha(captchaAnswer CaptchaAnswer) bool {
|
|||||||
if elapsed.Seconds() > 60 {
|
if elapsed.Seconds() > 60 {
|
||||||
solved = false
|
solved = false
|
||||||
}
|
}
|
||||||
|
//ip comprovation
|
||||||
|
if captchaSol.Ip != ip {
|
||||||
|
solved = false
|
||||||
|
}
|
||||||
return solved
|
return solved
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ type ServerConfig struct {
|
|||||||
ServerIP string `json:"serverIP"`
|
ServerIP string `json:"serverIP"`
|
||||||
ServerPort string `json:"serverPort"`
|
ServerPort string `json:"serverPort"`
|
||||||
ImgsFolder string `json:"imgsFolder"`
|
ImgsFolder string `json:"imgsFolder"`
|
||||||
|
NumImgsCaptcha int `json:"numImgsCaptcha"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var serverConfig ServerConfig
|
var serverConfig ServerConfig
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"serverIP": "127.0.0.1",
|
"serverIP": "127.0.0.1",
|
||||||
"serverPort": "3025",
|
"serverPort": "3025",
|
||||||
"imgsFolder": "imgs"
|
"imgsFolder": "imgs",
|
||||||
|
"numImgsCaptcha": 9
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,8 +56,6 @@ func GetImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
err := imgFakePathCollection.Find(bson.M{"fake": imageName}).One(&imgFakePath)
|
err := imgFakePathCollection.Find(bson.M{"fake": imageName}).One(&imgFakePath)
|
||||||
check(err)
|
check(err)
|
||||||
|
|
||||||
fmt.Println(serverConfig.ImgsFolder + "/" + imgFakePath.Real)
|
|
||||||
|
|
||||||
file, err := ioutil.ReadFile(serverConfig.ImgsFolder + "/" + imgFakePath.Real)
|
file, err := ioutil.ReadFile(serverConfig.ImgsFolder + "/" + imgFakePath.Real)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(w, err)
|
fmt.Fprintln(w, err)
|
||||||
@@ -75,7 +73,8 @@ func GetImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
func GetCaptcha(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)
|
jsonResp, err := json.Marshal(resp)
|
||||||
check(err)
|
check(err)
|
||||||
fmt.Fprintln(w, string(jsonResp))
|
fmt.Fprintln(w, string(jsonResp))
|
||||||
@@ -88,7 +87,8 @@ func AnswerCaptcha(w http.ResponseWriter, r *http.Request) {
|
|||||||
check(err)
|
check(err)
|
||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
resp := validateCaptcha(captchaAnswer)
|
ip := strings.Split(r.RemoteAddr, ":")[0]
|
||||||
|
resp := validateCaptcha(captchaAnswer, ip)
|
||||||
jsonResp, err := json.Marshal(resp)
|
jsonResp, err := json.Marshal(resp)
|
||||||
check(err)
|
check(err)
|
||||||
fmt.Fprintln(w, string(jsonResp))
|
fmt.Fprintln(w, string(jsonResp))
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
var selection = [0, 0, 0, 0, 0, 0];
|
var selection = [];
|
||||||
var captchaid = "";
|
var captchaid = "";
|
||||||
|
|
||||||
function httpGet(url) {
|
function httpGet(url) {
|
||||||
@@ -22,6 +22,10 @@ function getCaptcha() {
|
|||||||
captcha = JSON.parse(data);
|
captcha = JSON.parse(data);
|
||||||
captchaid = captcha.id;
|
captchaid = captcha.id;
|
||||||
showCaptcha(captcha);
|
showCaptcha(captcha);
|
||||||
|
selection=[];
|
||||||
|
for(k in captcha.imgs){
|
||||||
|
selection.push(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function showCaptcha(captcha) {
|
function showCaptcha(captcha) {
|
||||||
@@ -56,7 +60,6 @@ function validateCaptcha() {
|
|||||||
if (resp) {
|
if (resp) {
|
||||||
html += "<h2>goCaptcha validated</h2>";
|
html += "<h2>goCaptcha validated</h2>";
|
||||||
} else {
|
} else {
|
||||||
selection = [0, 0, 0, 0, 0, 0];
|
|
||||||
html += "<h2>goCaptcha failed</h2>";
|
html += "<h2>goCaptcha failed</h2>";
|
||||||
html += "<div onclick='getCaptcha()' class='g_button c_red300 g_floatRight'>Reload Captcha</div>";
|
html += "<div onclick='getCaptcha()' class='g_button c_red300 g_floatRight'>Reload Captcha</div>";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user