2023-01-08 17:49:03 +04:00
|
|
|
package http
|
2022-06-24 23:09:46 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-28 23:30:38 +04:00
|
|
|
"image/jpeg"
|
2022-06-24 23:09:46 +04:00
|
|
|
"net/http"
|
2022-10-20 22:58:14 +04:00
|
|
|
"time"
|
2023-01-08 18:49:33 +04:00
|
|
|
|
|
|
|
"git.arav.top/Arav/justcaptcha/pkg/captcha"
|
|
|
|
"git.arav.top/Arav/justcaptcha/pkg/captcha/inmemdb"
|
|
|
|
"git.arav.top/Arav/justcaptcha/pkg/dwcaptcha"
|
2022-06-24 23:09:46 +04:00
|
|
|
)
|
|
|
|
|
2022-10-20 22:58:14 +04:00
|
|
|
const errMsgWrongAnswer = "An answer provided was wrong"
|
|
|
|
const errMsgImageNotFound = "cannot get an image for a non-existing CAPTCHA"
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-10-20 22:58:14 +04:00
|
|
|
type CaptchaHandlers struct {
|
|
|
|
expiry time.Duration
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
|
2023-01-08 17:49:03 +04:00
|
|
|
func NewCaptchaHandlers(expiry time.Duration) *CaptchaHandlers {
|
2022-10-20 22:58:14 +04:00
|
|
|
inmemdb.SetExpiry(expiry)
|
|
|
|
return &CaptchaHandlers{expiry: expiry}
|
|
|
|
}
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-10-20 22:58:14 +04:00
|
|
|
func (h *CaptchaHandlers) New(w http.ResponseWriter, r *http.Request) {
|
|
|
|
dc := dwcaptcha.NewDwellingCaptcha(h.expiry)
|
|
|
|
_, id := inmemdb.New(r.RemoteAddr, dc)
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
2022-06-24 23:09:46 +04:00
|
|
|
fmt.Fprint(w, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CaptchaHandlers) Image(w http.ResponseWriter, r *http.Request) {
|
2023-01-08 17:53:13 +04:00
|
|
|
captchaID := captcha.ID(getURLParam(r, "captcha"))
|
2022-06-27 01:20:36 +04:00
|
|
|
captchaStyle := r.URL.Query().Get("style")
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-10-21 00:01:19 +04:00
|
|
|
captchaImage := inmemdb.Image(captchaID, captchaStyle)
|
2022-06-24 23:09:46 +04:00
|
|
|
if captchaImage == nil {
|
2022-10-21 00:01:19 +04:00
|
|
|
http.Error(w, errMsgImageNotFound, http.StatusNotFound)
|
2022-06-24 23:09:46 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Content-Disposition", "inline; filename=\""+string(captchaID)+"\"")
|
|
|
|
|
2022-08-28 23:30:38 +04:00
|
|
|
jpeg.Encode(w, *captchaImage, &jpeg.Options{Quality: 20})
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CaptchaHandlers) Solve(w http.ResponseWriter, r *http.Request) {
|
2023-01-08 17:53:13 +04:00
|
|
|
captchaID := captcha.ID(getURLParam(r, "captcha"))
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-10-20 22:58:14 +04:00
|
|
|
r.ParseForm()
|
|
|
|
answer := captcha.Answer(r.FormValue("answer"))
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-10-21 00:01:19 +04:00
|
|
|
if ok := inmemdb.Solve(captchaID, answer); !ok {
|
2022-10-20 23:16:30 +04:00
|
|
|
http.Error(w, errMsgWrongAnswer, http.StatusForbidden)
|
2022-10-20 22:58:14 +04:00
|
|
|
return
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
2022-10-20 22:58:14 +04:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CaptchaHandlers) IsSolved(w http.ResponseWriter, r *http.Request) {
|
2023-01-08 17:53:13 +04:00
|
|
|
captchaID := captcha.ID(getURLParam(r, "captcha"))
|
2022-08-28 23:32:16 +04:00
|
|
|
isJustRemove := r.URL.Query().Has("remove")
|
|
|
|
|
|
|
|
if isJustRemove {
|
2022-10-20 22:58:14 +04:00
|
|
|
inmemdb.Remove(captchaID)
|
2022-10-20 23:16:30 +04:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2022-08-28 23:32:16 +04:00
|
|
|
return
|
|
|
|
}
|
2022-06-24 23:09:46 +04:00
|
|
|
|
2022-10-21 00:01:19 +04:00
|
|
|
if solved := inmemdb.IsSolved(captchaID); !solved {
|
2022-10-20 23:16:30 +04:00
|
|
|
http.Error(w, errMsgWrongAnswer, http.StatusForbidden)
|
2022-10-21 00:01:19 +04:00
|
|
|
return
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|
2022-10-20 23:16:30 +04:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2022-06-24 23:09:46 +04:00
|
|
|
}
|