Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Remove captcha IP-specificity

It's a notable deployment complication for little gain. Since captchas on Nyaa aren't on many pages, overflowing the stored captcha limit is also hard.
Cette révision appartient à :
bakape 2017-05-07 15:55:46 +03:00
Parent aca2f2dc58
révision dc5ee0ca2e
2 fichiers modifiés avec 5 ajouts et 65 suppressions

Voir le fichier

@ -46,7 +46,7 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
}
}
} else if r.Method == "GET" {
uploadForm.CaptchaID = captcha.GetID(r.RemoteAddr)
uploadForm.CaptchaID = captcha.GetID()
htv := UploadTemplateVariables{uploadForm, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)}
err = uploadTemplate.ExecuteTemplate(w, "index.html", htv)
} else {

Voir le fichier

@ -3,7 +3,6 @@ package captcha
import (
"errors"
"net/http"
"sync"
"time"
"github.com/dchest/captcha"
@ -13,23 +12,11 @@ const lifetime = time.Minute * 20
var (
server = captcha.Server(captcha.StdWidth, captcha.StdHeight)
captchas = captchaMap{
m: make(map[string]store, 64),
}
ErrInvalidCaptcha = errors.New("invalid captcha")
)
func init() {
captcha.SetCustomStore(captcha.NewMemoryStore(1<<10, lifetime))
go func() {
t := time.Tick(time.Minute)
for {
<-t
captchas.cleanUp()
}
}()
}
// Captcha is to be embedded into any form struct requiring a captcha
@ -37,56 +24,9 @@ type Captcha struct {
CaptchaID, Solution string
}
// Captchas are IP-specific and need eventual cleanup
type captchaMap struct {
sync.Mutex
m map[string]store
}
type store struct {
id string
created time.Time
}
// Returns a captcha id by IP. If a captcha for this IP already exists, it is
// reloaded and returned. Otherwise, a new captcha is created.
func (n *captchaMap) get(ip string) string {
n.Lock()
defer n.Unlock()
old, ok := n.m[ip]
// No existing captcha, it expired or this IP already used the captcha
if !ok || !captcha.Reload(old.id) {
id := captcha.New()
n.m[ip] = store{
id: id,
created: time.Now(),
}
return id
}
old.created = time.Now()
n.m[ip] = old
return old.id
}
// Remove expired ip -> captchaID mappings
func (n *captchaMap) cleanUp() {
n.Lock()
defer n.Unlock()
till := time.Now().Add(-lifetime)
for ip, c := range n.m {
if c.created.Before(till) {
delete(n.m, ip)
}
}
}
// GetID returns a new or previous captcha id by IP
func GetID(ip string) string {
return captchas.get(ip)
// GetID returns a new captcha id
func GetID() string {
return captcha.New()
}
// Extract a Captcha struct from an HTML form