justcaptcha/pkg/captcha/inmemdb/inmemdb.go

160 lines
3.7 KiB
Go
Raw Normal View History

package inmemdb
import (
"image"
"sync"
"time"
2023-03-05 00:18:52 +04:00
"git.arav.su/Arav/justcaptcha/pkg/captcha"
)
// InMemoryDB implementation that lives in a memory (map).
type InMemoryDB struct {
sync.Mutex
db map[captcha.ID]captcha.Captcha
expiry time.Duration
expiryScanInterval time.Duration
}
// NewInMemoryDB returns an initialised instance of an InMemoryDB.
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)).
func NewInMemoryDB(expiry time.Duration) *InMemoryDB {
db := &InMemoryDB{
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.
// A data string is an additional random data used to generate an ID,
// e.g. an IP-address.
func (imcdb *InMemoryDB) 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.
func (imcdb *InMemoryDB) 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.
func (imcdb *InMemoryDB) SetExpiry(expiry time.Duration) {
2023-01-12 04:40:54 +04:00
imcdb.expiry = expiry
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.
func (imcdb *InMemoryDB) Image(id captcha.ID, style string) *image.Image {
imcdb.Lock()
defer imcdb.Unlock()
if c, ok := imcdb.db[id]; ok {
return c.Image(style)
}
return nil
}
// Solve compares given answer with a stored one and if failed
// deletes a CAPTCHA from database.
func (imcdb *InMemoryDB) Solve(id captcha.ID, answer captcha.Answer) bool {
imcdb.Lock()
defer imcdb.Unlock()
if c, ok := imcdb.db[id]; ok {
ok = c.Solve(answer)
if !ok {
delete(imcdb.db, id)
}
return ok
}
return false
}
// IsSolved checks if CAPTCHA was solved and removes it
// from a database.
func (imcdb *InMemoryDB) IsSolved(id captcha.ID) bool {
imcdb.Lock()
defer imcdb.Unlock()
if c, ok := imcdb.db[id]; ok {
delete(imcdb.db, id)
return c.IsSolved()
}
return false
}
// Remove a CAPTCHA from a database.
func (imcdb *InMemoryDB) Remove(id captcha.ID) {
imcdb.Lock()
defer imcdb.Unlock()
delete(imcdb.db, id)
}
// cleanExpired removes expired CAPTCHAs in a loop.
func (imcdb *InMemoryDB) 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 InMemoryDB
var imcdb = NewInMemoryDB(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)
}
func Image(id captcha.ID, style string) *image.Image {
return imcdb.Image(id, style)
}
func Solve(id captcha.ID, answer captcha.Answer) bool {
return imcdb.Solve(id, answer)
}
func IsSolved(id captcha.ID) bool {
return imcdb.IsSolved(id)
}
func Remove(id captcha.ID) {
imcdb.Remove(id)
}