2022-10-20 22:52:13 +04:00
|
|
|
package inmemdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2023-01-08 18:49:33 +04:00
|
|
|
|
2023-03-05 00:18:52 +04:00
|
|
|
"git.arav.su/Arav/justcaptcha/pkg/captcha"
|
2022-10-20 22:52:13 +04:00
|
|
|
)
|
|
|
|
|
2023-01-12 04:41:09 +04:00
|
|
|
// InMemoryCaptchaDB implementation that lives in a memory (map).
|
2022-10-20 22:52:13 +04:00
|
|
|
type InMemoryCaptchaDB struct {
|
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
db map[captcha.ID]captcha.Captcha
|
|
|
|
expiry time.Duration
|
|
|
|
expiryScanInterval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewInMemoryCaptchaDB returns an initialised instance of an InMemoryCaptchaDB.
|
2023-01-12 04:41:09 +04:00
|
|
|
// An expiry is a scan interval for expired CAPTCHAs (if passed a longer one,
|
|
|
|
// resets to a default (captcha.DefaultExpiredScanInterval)).
|
2022-10-20 22:52:13 +04:00
|
|
|
func NewInMemoryCaptchaDB(expiry time.Duration) *InMemoryCaptchaDB {
|
|
|
|
db := &InMemoryCaptchaDB{
|
|
|
|
db: make(map[captcha.ID]captcha.Captcha),
|
|
|
|
expiry: expiry}
|
|
|
|
|
|
|
|
if expiry < captcha.DefaultExpiredScanInterval {
|
|
|
|
db.expiryScanInterval = expiry
|
|
|
|
} else {
|
|
|
|
db.expiryScanInterval = captcha.DefaultExpiredScanInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
go db.cleanExpired()
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2023-01-12 04:41:09 +04:00
|
|
|
// New accepts a CAPTCHA instance, generates an ID and store it in a database.
|
2022-10-20 22:52:13 +04:00
|
|
|
// A data string is an additional random data used to generate an ID,
|
|
|
|
// e.g. an IP-address.
|
|
|
|
func (imcdb *InMemoryCaptchaDB) New(data string, cptcha captcha.Captcha) (captcha.Captcha, captcha.ID) {
|
|
|
|
id := captcha.NewID(data, cptcha.Answer())
|
|
|
|
|
|
|
|
imcdb.Lock()
|
|
|
|
imcdb.db[id] = cptcha
|
|
|
|
imcdb.Unlock()
|
|
|
|
|
|
|
|
return cptcha, id
|
|
|
|
}
|
|
|
|
|
2023-01-12 04:41:09 +04:00
|
|
|
// GetExpiry returns an expiry for a CAPTCHA.
|
2022-10-20 22:52:13 +04:00
|
|
|
func (imcdb *InMemoryCaptchaDB) GetExpiry() time.Duration {
|
|
|
|
return imcdb.expiry
|
|
|
|
}
|
|
|
|
|
2023-01-12 04:41:09 +04:00
|
|
|
// SetExpiry changes an expiry for a CAPTCHA and a scan interval. Scan interval
|
|
|
|
// cannot be longer than a default, so if it is, then resets to a default.
|
2022-10-20 22:52:13 +04:00
|
|
|
func (imcdb *InMemoryCaptchaDB) SetExpiry(expiry time.Duration) {
|
2023-01-12 04:40:54 +04:00
|
|
|
imcdb.expiry = expiry
|
2022-10-20 22:52:13 +04:00
|
|
|
if expiry < captcha.DefaultExpiredScanInterval {
|
|
|
|
imcdb.expiryScanInterval = expiry
|
|
|
|
} else {
|
|
|
|
imcdb.expiryScanInterval = captcha.DefaultExpiredScanInterval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 04:41:09 +04:00
|
|
|
// Image returns a freshly generated image for a CAPTCHA with style if
|
|
|
|
// applicable.
|
2022-10-21 00:01:19 +04:00
|
|
|
func (imcdb *InMemoryCaptchaDB) Image(id captcha.ID, style string) *image.Image {
|
2022-10-20 22:52:13 +04:00
|
|
|
imcdb.Lock()
|
|
|
|
defer imcdb.Unlock()
|
|
|
|
if c, ok := imcdb.db[id]; ok {
|
2022-10-21 00:01:19 +04:00
|
|
|
return c.Image(style)
|
2022-10-20 22:52:13 +04:00
|
|
|
}
|
2022-10-21 00:01:19 +04:00
|
|
|
return nil
|
2022-10-20 22:52:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Solve compares given answer with a stored one and if failed
|
|
|
|
// deletes a CAPTCHA from database.
|
2022-10-21 00:01:19 +04:00
|
|
|
func (imcdb *InMemoryCaptchaDB) Solve(id captcha.ID, answer captcha.Answer) bool {
|
2022-10-20 22:52:13 +04:00
|
|
|
imcdb.Lock()
|
|
|
|
defer imcdb.Unlock()
|
|
|
|
if c, ok := imcdb.db[id]; ok {
|
|
|
|
ok = c.Solve(answer)
|
|
|
|
if !ok {
|
|
|
|
delete(imcdb.db, id)
|
|
|
|
}
|
2022-10-21 00:01:19 +04:00
|
|
|
return ok
|
2022-10-20 22:52:13 +04:00
|
|
|
}
|
2022-10-21 00:01:19 +04:00
|
|
|
return false
|
2022-10-20 22:52:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsSolved checks if CAPTCHA was solved and removes it
|
|
|
|
// from a database.
|
2022-10-21 00:01:19 +04:00
|
|
|
func (imcdb *InMemoryCaptchaDB) IsSolved(id captcha.ID) bool {
|
2022-10-20 22:52:13 +04:00
|
|
|
imcdb.Lock()
|
|
|
|
defer imcdb.Unlock()
|
|
|
|
if c, ok := imcdb.db[id]; ok {
|
|
|
|
delete(imcdb.db, id)
|
2022-10-21 00:01:19 +04:00
|
|
|
return c.IsSolved()
|
2022-10-20 22:52:13 +04:00
|
|
|
}
|
2022-10-21 00:01:19 +04:00
|
|
|
return false
|
2022-10-20 22:52:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a CAPTCHA from a database.
|
2022-10-21 00:01:19 +04:00
|
|
|
func (imcdb *InMemoryCaptchaDB) Remove(id captcha.ID) {
|
2022-10-20 22:52:13 +04:00
|
|
|
imcdb.Lock()
|
|
|
|
defer imcdb.Unlock()
|
2023-01-12 04:39:22 +04:00
|
|
|
delete(imcdb.db, id)
|
2022-10-20 22:52:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// cleanExpired removes expired CAPTCHAs in a loop.
|
|
|
|
func (imcdb *InMemoryCaptchaDB) cleanExpired() {
|
|
|
|
for {
|
|
|
|
sleepFor := imcdb.expiryScanInterval - (time.Duration(time.Now().Second()) % imcdb.expiryScanInterval)
|
|
|
|
time.Sleep(sleepFor)
|
|
|
|
|
|
|
|
imcdb.Lock()
|
|
|
|
for id, captcha := range imcdb.db {
|
|
|
|
if time.Since(captcha.Expiry()) >= imcdb.expiry {
|
|
|
|
delete(imcdb.db, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
imcdb.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// An instance of InMemoryCaptchaDB
|
|
|
|
|
|
|
|
var imcdb = NewInMemoryCaptchaDB(captcha.DefaultExpiredScanInterval)
|
|
|
|
|
|
|
|
func GetExpiry() time.Duration {
|
|
|
|
return imcdb.GetExpiry()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetExpiry(expiry time.Duration) {
|
|
|
|
imcdb.SetExpiry(expiry)
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(data string, captcha captcha.Captcha) (captcha.Captcha, captcha.ID) {
|
|
|
|
return imcdb.New(data, captcha)
|
|
|
|
}
|
|
|
|
|
2022-10-21 00:01:19 +04:00
|
|
|
func Image(id captcha.ID, style string) *image.Image {
|
2022-10-20 22:52:13 +04:00
|
|
|
return imcdb.Image(id, style)
|
|
|
|
}
|
|
|
|
|
2022-10-21 00:01:19 +04:00
|
|
|
func Solve(id captcha.ID, answer captcha.Answer) bool {
|
2022-10-20 22:52:13 +04:00
|
|
|
return imcdb.Solve(id, answer)
|
|
|
|
}
|
|
|
|
|
2022-10-21 00:01:19 +04:00
|
|
|
func IsSolved(id captcha.ID) bool {
|
2022-10-20 22:52:13 +04:00
|
|
|
return imcdb.IsSolved(id)
|
|
|
|
}
|
|
|
|
|
2022-10-21 00:01:19 +04:00
|
|
|
func Remove(id captcha.ID) {
|
|
|
|
imcdb.Remove(id)
|
2022-10-20 22:52:13 +04:00
|
|
|
}
|