Merge pull request #141 from bakape/ip-neutral-captchas
Remove captcha IP-specificity
Cette révision appartient à :
révision
f24e652773
2 fichiers modifiés avec 5 ajouts et 65 suppressions
|
@ -46,7 +46,7 @@ func UploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if r.Method == "GET" {
|
} 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)}
|
htv := UploadTemplateVariables{uploadForm, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)}
|
||||||
err = uploadTemplate.ExecuteTemplate(w, "index.html", htv)
|
err = uploadTemplate.ExecuteTemplate(w, "index.html", htv)
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -3,7 +3,6 @@ package captcha
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/dchest/captcha"
|
"github.com/dchest/captcha"
|
||||||
|
@ -12,24 +11,12 @@ import (
|
||||||
const lifetime = time.Minute * 20
|
const lifetime = time.Minute * 20
|
||||||
|
|
||||||
var (
|
var (
|
||||||
server = captcha.Server(captcha.StdWidth, captcha.StdHeight)
|
server = captcha.Server(captcha.StdWidth, captcha.StdHeight)
|
||||||
captchas = captchaMap{
|
|
||||||
m: make(map[string]store, 64),
|
|
||||||
}
|
|
||||||
|
|
||||||
ErrInvalidCaptcha = errors.New("invalid captcha")
|
ErrInvalidCaptcha = errors.New("invalid captcha")
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
captcha.SetCustomStore(captcha.NewMemoryStore(1<<10, lifetime))
|
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
|
// Captcha is to be embedded into any form struct requiring a captcha
|
||||||
|
@ -37,56 +24,9 @@ type Captcha struct {
|
||||||
CaptchaID, Solution string
|
CaptchaID, Solution string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Captchas are IP-specific and need eventual cleanup
|
// GetID returns a new captcha id
|
||||||
type captchaMap struct {
|
func GetID() string {
|
||||||
sync.Mutex
|
return captcha.New()
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract a Captcha struct from an HTML form
|
// Extract a Captcha struct from an HTML form
|
||||||
|
|
Référencer dans un nouveau ticket