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-06-27 01:02:24 +04:00
|
|
|
var expiredScanInterval = 60 * time.Second
|
|
|
|
|
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 {
|
|
|
|
New(data string) (Captcha, ID)
|
2022-06-26 20:49:43 +04:00
|
|
|
SetExpiry(expiry time.Duration)
|
2022-06-26 21:28:22 +04:00
|
|
|
GetExpiry() time.Duration
|
2022-06-24 23:09:46 +04:00
|
|
|
Image(id ID) (*image.Image, error)
|
|
|
|
Solve(id ID, answer Answer) (bool, error)
|
|
|
|
IsSolved(id ID) bool
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
DB map[ID]Captcha
|
|
|
|
ExpireIn time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
id := NewID(data, captcha.GetAnswer())
|
|
|
|
|
|
|
|
cdb.Lock()
|
|
|
|
cdb.DB[id] = captcha
|
|
|
|
cdb.Unlock()
|
|
|
|
|
|
|
|
return captcha, id
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
|
2022-06-26 20:49:43 +04:00
|
|
|
// SetExpiry stores expire value and starts a goroutine
|
2022-07-30 15:39:19 +04:00
|
|
|
// that checks for expired CAPTCHAs.
|
|
|
|
func (cdb *InMemoryCaptchaDB) SetExpiry(expire time.Duration) {
|
2022-06-26 20:49:43 +04:00
|
|
|
cdb.ExpireIn = expire
|
2022-06-24 23:09:46 +04:00
|
|
|
if expire < expiredScanInterval {
|
|
|
|
expiredScanInterval = expire
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2022-06-26 20:36:36 +04:00
|
|
|
sleepFor := expiredScanInterval - (time.Duration(time.Now().Second()) % expiredScanInterval)
|
2022-06-24 23:09:46 +04:00
|
|
|
time.Sleep(sleepFor)
|
|
|
|
|
2022-06-27 02:48:55 +04:00
|
|
|
cdb.Lock()
|
2022-06-24 23:09:46 +04:00
|
|
|
for id, captcha := range cdb.DB {
|
2022-06-27 02:48:55 +04:00
|
|
|
if time.Since(captcha.Expiry()) >= cdb.ExpireIn {
|
2022-06-24 23:09:46 +04:00
|
|
|
delete(cdb.DB, id)
|
|
|
|
}
|
|
|
|
}
|
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 {
|
|
|
|
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-06-24 23:09:46 +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-06-24 23:09:46 +04:00
|
|
|
if c, ok := cdb.DB[id]; ok {
|
|
|
|
ok = c.Solve(answer)
|
|
|
|
if !ok {
|
|
|
|
delete(cdb.DB, id)
|
|
|
|
}
|
|
|
|
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-06-24 23:09:46 +04:00
|
|
|
if c, ok := cdb.DB[id]; ok {
|
|
|
|
ok = c.IsSolved()
|
|
|
|
delete(cdb.DB, id)
|
|
|
|
return ok, nil
|
|
|
|
}
|
|
|
|
return false, errorNotFound
|
|
|
|
}
|