diff --git a/router/uploadHandler.go b/router/uploadHandler.go index 0c4118ea..afe905ba 100644 --- a/router/uploadHandler.go +++ b/router/uploadHandler.go @@ -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 { diff --git a/service/captcha/captcha.go b/service/captcha/captcha.go index 39a8500f..bc5cb821 100644 --- a/service/captcha/captcha.go +++ b/service/captcha/captcha.go @@ -3,7 +3,6 @@ package captcha import ( "errors" "net/http" - "sync" "time" "github.com/dchest/captcha" @@ -12,24 +11,12 @@ import ( const lifetime = time.Minute * 20 var ( - server = captcha.Server(captcha.StdWidth, captcha.StdHeight) - captchas = captchaMap{ - m: make(map[string]store, 64), - } - + server = captcha.Server(captcha.StdWidth, captcha.StdHeight) 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