@ -0,0 +1,67 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"math/rand" |
|||
"os/exec" |
|||
"strings" |
|||
) |
|||
|
|||
type Captcha struct { |
|||
Id string `json:"id"` |
|||
Imgs []string `json:"imgs"` |
|||
Question string `json:"question"` |
|||
Date string `json:"date"` |
|||
} |
|||
type CaptchaSolution struct { |
|||
Id string `json:"id"` |
|||
Imgs []string `json:"imgs"` |
|||
ImgsSolution []string `json:"imgssolution"` |
|||
Question string `json:"question"` |
|||
Date string `json:"date"` |
|||
} |
|||
type ImgFakePath struct { |
|||
CaptchaId string `json:"captchaid"` |
|||
Real string `json:"real"` |
|||
Fake string `json:"fake"` |
|||
} |
|||
|
|||
func generateUUID() string { |
|||
out, err := exec.Command("uuidgen").Output() |
|||
check(err) |
|||
uuid := string(out) |
|||
uuid = strings.Replace(uuid, "\n", "", -1) |
|||
return uuid |
|||
} |
|||
func generateRandInt(min int, max int) int { |
|||
//rand.Seed(time.Now().UTC().UnixNano())
|
|||
return rand.Intn(max-min) + min |
|||
} |
|||
func generateCaptcha(count int) Captcha { |
|||
var captcha Captcha |
|||
var captchaSol CaptchaSolution |
|||
|
|||
captcha.Id = generateUUID() |
|||
captchaSol.Id = captcha.Id |
|||
|
|||
for i := 0; i < count; i++ { |
|||
nCateg := generateRandInt(0, len(categDataset)) |
|||
nImg := generateRandInt(0, len(dataset[categDataset[nCateg]])) |
|||
//imgFakePath
|
|||
var imgFakePath ImgFakePath |
|||
imgFakePath.CaptchaId = captcha.Id |
|||
imgFakePath.Real = categDataset[nCateg] + "/" + dataset[categDataset[nCateg]][nImg] |
|||
imgFakePath.Fake = generateUUID() + ".png" |
|||
err := imgFakePathCollection.Insert(imgFakePath) |
|||
check(err) |
|||
captcha.Imgs = append(captcha.Imgs, imgFakePath.Fake) |
|||
captchaSol.Imgs = append(captchaSol.Imgs, dataset[categDataset[nCateg]][nImg]) |
|||
captchaSol.ImgsSolution = append(captchaSol.ImgsSolution, categDataset[nCateg]) |
|||
} |
|||
captcha.Question = "Select all leopards" |
|||
captchaSol.Question = "Select all leopards" |
|||
err := captchaCollection.Insert(captcha) |
|||
check(err) |
|||
err = captchaSolutionCollection.Insert(captchaSol) |
|||
check(err) |
|||
return captcha |
|||
} |
@ -0,0 +1,51 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"fmt" |
|||
"io/ioutil" |
|||
|
|||
mgo "gopkg.in/mgo.v2" |
|||
) |
|||
|
|||
//MongoConfig stores the configuration of mongodb to connect
|
|||
type MongoConfig struct { |
|||
Ip string `json:"ip"` |
|||
Database string `json:"database"` |
|||
} |
|||
|
|||
var mongoConfig MongoConfig |
|||
|
|||
var captchaCollection *mgo.Collection |
|||
var captchaSolutionCollection *mgo.Collection |
|||
var imgFakePathCollection *mgo.Collection |
|||
|
|||
func readMongodbConfig(path string) { |
|||
file, e := ioutil.ReadFile(path) |
|||
if e != nil { |
|||
fmt.Println("error:", e) |
|||
} |
|||
content := string(file) |
|||
json.Unmarshal([]byte(content), &mongoConfig) |
|||
} |
|||
|
|||
func getSession() (*mgo.Session, error) { |
|||
session, err := mgo.Dial("mongodb://" + mongoConfig.Ip) |
|||
if err != nil { |
|||
panic(err) |
|||
} |
|||
//defer session.Close()
|
|||
|
|||
// Optional. Switch the session to a monotonic behavior.
|
|||
session.SetMode(mgo.Monotonic, true) |
|||
|
|||
// Optional. Switch the session to a monotonic behavior.
|
|||
session.SetMode(mgo.Monotonic, true) |
|||
|
|||
return session, err |
|||
} |
|||
func getCollection(session *mgo.Session, collection string) *mgo.Collection { |
|||
|
|||
c := session.DB(mongoConfig.Database).C(collection) |
|||
return c |
|||
} |
@ -0,0 +1,4 @@ |
|||
{ |
|||
"ip": "127.0.0.1", |
|||
"database": "goCaptcha" |
|||
} |
@ -0,0 +1,21 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"io/ioutil" |
|||
) |
|||
|
|||
var dataset map[string][]string |
|||
var categDataset []string |
|||
|
|||
func readDataset(path string) { |
|||
dataset = make(map[string][]string) |
|||
|
|||
folders, _ := ioutil.ReadDir(path) |
|||
for _, folder := range folders { |
|||
categDataset = append(categDataset, folder.Name()) |
|||
folderFiles, _ := ioutil.ReadDir(path + "/" + folder.Name()) |
|||
for _, file := range folderFiles { |
|||
dataset[folder.Name()] = append(dataset[folder.Name()], file.Name()) |
|||
} |
|||
} |
|||
} |