2022-06-24 23:09:46 +04:00
|
|
|
package captcha
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
2022-07-30 15:44:02 +04:00
|
|
|
"errors"
|
2022-06-24 23:09:46 +04:00
|
|
|
"image"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-07-30 15:44:02 +04:00
|
|
|
var errorNotFound = errors.New("captcha not found")
|
|
|
|
|
2022-08-19 02:15:34 +04:00
|
|
|
var defaultExpiredScanInterval = 60 * time.Second
|
2022-06-27 01:02:24 +04:00
|
|
|
|
2022-06-27 00:26:03 +04:00
|
|
|
type ID string
|
|
|
|
|
2022-06-27 01:02:24 +04:00
|
|
|
// NewID generates an ID as a sha256 hash of additionalData, current time
|
|
|
|
// and answer encoded with base64 in raw URL variant.
|
|
|
|
func NewID(additionalData string, answer Answer) ID {
|
|
|
|
idHash := sha256.New()
|
|
|
|
|
|
|
|
idHash.Write([]byte(additionalData))
|
|
|
|
idHash.Write([]byte(strconv.FormatInt(time.Now().UnixMicro(), 16)))
|
|
|
|
idHash.Write([]byte(answer))
|
|
|
|
|
|
|
|
return ID(base64.RawURLEncoding.EncodeToString(idHash.Sum(nil)))
|
|
|
|
}
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-07-30 15:39:19 +04:00
|
|
|
type CaptchaDB interface {
|
2022-08-19 02:15:34 +04:00
|
|
|
New(data string, captcha Captcha) (Captcha, ID)
|
2022-06-26 21:28:22 +04:00
|
|
|
GetExpiry() time.Duration
|
2022-08-19 02:15:34 +04:00
|
|
|
Image(id ID, style string) (*image.Image, error)
|
2022-06-24 23:09:46 +04:00
|
|
|
Solve(id ID, answer Answer) (bool, error)
|
2022-08-19 02:15:34 +04:00
|
|
|
IsSolved(id ID) (bool, error)
|
|
|
|
cleanExpired()
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
|
2022-07-30 15:39:19 +04:00
|
|
|
type InMemoryCaptchaDB struct {
|
2022-06-27 01:03:28 +04:00
|
|
|
sync.Mutex
|
2022-07-30 15:39:19 +04:00
|
|
|
|
2022-08-19 02:15:34 +04:00
|
|
|
db map[ID]Captcha
|
|
|
|
expireIn time.Duration
|
|
|
|
expireScanInterval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInMemoryCaptchaDB(expire time.Duration) *InMemoryCaptchaDB {
|
|
|
|
db := &InMemoryCaptchaDB{
|
|
|
|
db: make(map[ID]Captcha),
|
|
|
|
expireIn: expire}
|
|
|
|
|
|
|
|
if expire < defaultExpiredScanInterval {
|
|
|
|
db.expireScanInterval = expire
|
|
|
|
} else {
|
|
|
|
db.expireScanInterval = defaultExpiredScanInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
db.cleanExpired()
|
|
|
|
|
|
|
|
return db
|
2022-07-30 15:39:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// New accepts an Captcha instance, generates an ID and store it in a database.
|
|
|
|
// `data` string is an additional random data used to generate an ID,
|
|
|
|
// e.g. IP-address.
|
|
|
|
func (cdb *InMemoryCaptchaDB) New(data string, captcha Captcha) (Captcha, ID) {
|
2022-08-19 02:24:00 +04:00
|
|
|
id := NewID(data, captcha.Answer())
|
2022-07-30 15:39:19 +04:00
|
|
|
|
|
|
|
cdb.Lock()
|
2022-08-19 02:15:34 +04:00
|
|
|
cdb.db[id] = captcha
|
2022-07-30 15:39:19 +04:00
|
|
|
cdb.Unlock()
|
|
|
|
|
|
|
|
return captcha, id
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
|
2022-08-19 02:15:34 +04:00
|
|
|
// cleanExpired starts a goroutine that deletes expired CAPTCHAs.
|
|
|
|
func (cdb *InMemoryCaptchaDB) cleanExpired() {
|
2022-06-24 23:09:46 +04:00
|
|
|
go func() {
|
|
|
|
for {
|
2022-08-19 02:15:34 +04:00
|
|
|
sleepFor := cdb.expireScanInterval - (time.Duration(time.Now().Second()) % cdb.expireScanInterval)
|
2022-06-24 23:09:46 +04:00
|
|
|
time.Sleep(sleepFor)
|
|
|
|
|
2022-06-27 02:48:55 +04:00
|
|
|
cdb.Lock()
|
2022-08-19 02:15:34 +04:00
|
|
|
for id, captcha := range cdb.db {
|
|
|
|
if time.Since(captcha.Expiry()) >= cdb.expireIn {
|
|
|
|
delete(cdb.db, id)
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
}
|
2022-06-27 02:48:55 +04:00
|
|
|
cdb.Unlock()
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2022-07-30 15:39:19 +04:00
|
|
|
// GetExpiry returns time for how long captcha will last.
|
|
|
|
func (cdb *InMemoryCaptchaDB) GetExpiry() time.Duration {
|
2022-08-19 02:15:34 +04:00
|
|
|
return cdb.expireIn
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Image returns image for a captcha.
|
2022-08-17 21:47:27 +04:00
|
|
|
func (cdb *InMemoryCaptchaDB) Image(id ID, style string) (*image.Image, error) {
|
2022-06-27 02:48:55 +04:00
|
|
|
cdb.Lock()
|
|
|
|
defer cdb.Unlock()
|
2022-08-19 02:15:34 +04:00
|
|
|
if c, ok := cdb.db[id]; ok {
|
2022-07-30 15:39:19 +04:00
|
|
|
return c.Image(style), nil
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
return nil, errorNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// Solve compares given answer with a stored one and if failed
|
|
|
|
// deletes a captcha from database.
|
2022-07-30 15:39:19 +04:00
|
|
|
func (cdb *InMemoryCaptchaDB) Solve(id ID, answer Answer) (bool, error) {
|
2022-06-27 01:03:28 +04:00
|
|
|
cdb.Lock()
|
|
|
|
defer cdb.Unlock()
|
2022-08-19 02:15:34 +04:00
|
|
|
if c, ok := cdb.db[id]; ok {
|
2022-06-24 23:09:46 +04:00
|
|
|
ok = c.Solve(answer)
|
|
|
|
if !ok {
|
2022-08-19 02:15:34 +04:00
|
|
|
delete(cdb.db, id)
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
return ok, nil
|
|
|
|
}
|
|
|
|
return false, errorNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSolved checks if captcha was solved and removes it
|
|
|
|
// from a database.
|
2022-07-30 15:39:19 +04:00
|
|
|
func (cdb *InMemoryCaptchaDB) IsSolved(id ID) (bool, error) {
|
2022-06-27 01:03:28 +04:00
|
|
|
cdb.Lock()
|
|
|
|
defer cdb.Unlock()
|
2022-08-19 02:15:34 +04:00
|
|
|
if c, ok := cdb.db[id]; ok {
|
2022-06-24 23:09:46 +04:00
|
|
|
ok = c.IsSolved()
|
2022-08-19 02:15:34 +04:00
|
|
|
delete(cdb.db, id)
|
2022-06-24 23:09:46 +04:00
|
|
|
return ok, nil
|
|
|
|
}
|
|
|
|
return false, errorNotFound
|
|
|
|
}
|