2023-01-16 05:24:07 +04:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2023-02-05 23:05:11 +04:00
|
|
|
"fmt"
|
|
|
|
"image/jpeg"
|
2023-02-05 21:11:29 +04:00
|
|
|
"math"
|
2023-01-16 05:24:07 +04:00
|
|
|
"net/http"
|
2023-02-05 21:11:29 +04:00
|
|
|
"strconv"
|
2023-02-05 00:52:14 +04:00
|
|
|
"strings"
|
2023-01-16 05:24:07 +04:00
|
|
|
"time"
|
|
|
|
|
2023-03-11 22:45:22 +04:00
|
|
|
"git.arav.su/Arav/dwelling-home/pkg/mindflow"
|
|
|
|
"git.arav.su/Arav/dwelling-home/pkg/servicestat"
|
|
|
|
"git.arav.su/Arav/dwelling-home/pkg/util"
|
|
|
|
"git.arav.su/Arav/dwelling-home/web"
|
|
|
|
"git.arav.su/Arav/justcaptcha/pkg/captcha"
|
|
|
|
"git.arav.su/Arav/justcaptcha/pkg/captcha/inmemdb"
|
|
|
|
"git.arav.su/Arav/justcaptcha/pkg/dwcaptcha"
|
|
|
|
gbsqlite "git.arav.su/Arav/justguestbook/database/sqlite"
|
|
|
|
"git.arav.su/Arav/justguestbook/guestbook"
|
2023-01-16 05:24:07 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Handlers struct {
|
2023-02-05 21:10:52 +04:00
|
|
|
guestbookDB *gbsqlite.SQLiteDatabase
|
2023-01-16 05:24:07 +04:00
|
|
|
captchaExpire time.Duration
|
|
|
|
guestbookOwner string
|
2023-02-05 21:09:44 +04:00
|
|
|
guestbookPageSize int64
|
2023-01-16 05:24:07 +04:00
|
|
|
}
|
|
|
|
|
2023-02-05 21:10:52 +04:00
|
|
|
func NewHandlers(captchaExpire time.Duration, dbPath, gbOwner string, gbPageSize int64) *Handlers {
|
|
|
|
gbdb, err := gbsqlite.New(dbPath + "/guestbook.sqlite")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2023-02-05 23:05:11 +04:00
|
|
|
inmemdb.SetExpiry(captchaExpire)
|
|
|
|
|
2023-01-16 05:24:07 +04:00
|
|
|
return &Handlers{
|
2023-02-05 21:10:52 +04:00
|
|
|
guestbookDB: gbdb,
|
2023-01-16 05:24:07 +04:00
|
|
|
captchaExpire: captchaExpire,
|
|
|
|
guestbookOwner: gbOwner,
|
|
|
|
guestbookPageSize: gbPageSize}
|
|
|
|
}
|
|
|
|
|
2023-02-05 21:10:52 +04:00
|
|
|
func (h *Handlers) CloseDB() {
|
|
|
|
h.guestbookDB.Close()
|
|
|
|
}
|
|
|
|
|
2023-01-16 05:24:07 +04:00
|
|
|
func (h *Handlers) AssetsFS() http.FileSystem {
|
|
|
|
return web.Assets()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) Index(w http.ResponseWriter, r *http.Request) {
|
2023-02-05 00:52:14 +04:00
|
|
|
web.Index("", w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) Stuff(w http.ResponseWriter, r *http.Request) {
|
2023-02-05 06:30:39 +04:00
|
|
|
web.Stuff("/ Stuff", util.GetServiceByHost(r.Host, util.ServiceGit),
|
|
|
|
util.GetServiceByHost(r.Host, util.ServiceFiles), w)
|
2023-02-05 00:52:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) Mindflow(w http.ResponseWriter, r *http.Request) {
|
2023-02-05 04:41:11 +04:00
|
|
|
posts := []mindflow.Post{
|
|
|
|
{Category: "Kek", Date: time.Now(), Title: "LMAO", Body: "Testing!"},
|
|
|
|
}
|
|
|
|
web.Mindflow("/ Mindflow", posts, r, w)
|
2023-02-05 00:52:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) About(w http.ResponseWriter, r *http.Request) {
|
2023-02-05 05:13:58 +04:00
|
|
|
var lst servicestat.ServiceList = make(servicestat.ServiceList)
|
|
|
|
reimu, err := servicestat.GatherStatus("http://reimu.arav.home.arpa:14882/processes")
|
|
|
|
if err == nil {
|
|
|
|
for k, v := range reimu {
|
|
|
|
lst[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sakuya, err := servicestat.GatherStatus("http://sakuya.arav.home.arpa:14882/processes")
|
|
|
|
if err == nil {
|
|
|
|
for k, v := range sakuya {
|
|
|
|
lst[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 06:25:22 +04:00
|
|
|
web.About("/ About", util.GetServiceByHost(r.Host, util.ServiceFiles), lst, w)
|
2023-02-05 00:52:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) Article(w http.ResponseWriter, r *http.Request) {
|
2023-02-05 04:41:11 +04:00
|
|
|
name := r.URL.Path[strings.LastIndex(r.URL.Path, "/")+1:]
|
|
|
|
artcl, err := web.GetArticle(name)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusNotFound)
|
2023-02-05 00:52:14 +04:00
|
|
|
return
|
|
|
|
}
|
2023-02-05 04:41:11 +04:00
|
|
|
|
|
|
|
web.Article(artcl.Title, artcl.Description, string(artcl.Body), artcl.Date, r.Host, name, r, w)
|
2023-02-05 00:52:14 +04:00
|
|
|
}
|
|
|
|
|
2023-02-05 21:11:29 +04:00
|
|
|
func (h *Handlers) Guestbook(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var page int64
|
|
|
|
var pageSize int64
|
|
|
|
|
|
|
|
pageStr := r.URL.Query().Get("p")
|
|
|
|
if pageStr != "" {
|
|
|
|
page, _ = strconv.ParseInt(pageStr, 10, 64)
|
|
|
|
} else {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
pageSzStr := r.URL.Query().Get("ps")
|
|
|
|
if pageSzStr != "" {
|
|
|
|
pageSize, _ = strconv.ParseInt(pageSzStr, 10, 64)
|
|
|
|
} else {
|
|
|
|
pageSize = h.guestbookPageSize
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := h.guestbookDB.Entries(page, pageSize)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-11 23:34:08 +04:00
|
|
|
for _, entry := range entries {
|
|
|
|
entry.Message = strings.ReplaceAll(entry.Message, "\\n", "\n")
|
|
|
|
}
|
|
|
|
|
2023-02-05 21:11:29 +04:00
|
|
|
entriesCount, _ := h.guestbookDB.Count()
|
|
|
|
pageCount := int64(math.Ceil(float64(entriesCount) / float64(h.guestbookPageSize)))
|
|
|
|
|
2023-02-05 23:23:55 +04:00
|
|
|
dwc := dwcaptcha.NewDwellingCaptcha(h.captchaExpire)
|
|
|
|
_, id := inmemdb.New(r.RemoteAddr, dwc)
|
|
|
|
|
|
|
|
web.Guestbook("/ Guestbook", h.guestbookOwner, string(id), pageCount, entries, r, w)
|
2023-02-05 21:11:29 +04:00
|
|
|
}
|
|
|
|
|
2023-02-05 21:50:28 +04:00
|
|
|
func (h *Handlers) GuestbookPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var entry *guestbook.Entry
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
|
|
|
|
r.ParseForm()
|
|
|
|
|
2023-02-05 23:23:55 +04:00
|
|
|
if !inmemdb.Solve(captcha.ID(r.FormValue("captcha_id")), captcha.Answer(r.FormValue("captcha_answer"))) {
|
2023-02-06 00:02:13 +04:00
|
|
|
h.ForbiddenError("Wrong answer given.", "Here's your message:"+r.FormValue("message"), w)
|
2023-02-05 23:23:55 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-05 21:50:28 +04:00
|
|
|
if r.FormValue("name") == "" {
|
|
|
|
r.Form.Set("name", "Anonymous")
|
|
|
|
}
|
|
|
|
|
2023-03-11 23:34:08 +04:00
|
|
|
message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n")
|
|
|
|
message = strings.ReplaceAll(message, "\n\r", "\n")
|
|
|
|
message = strings.ReplaceAll(message, "\r", "\n")
|
|
|
|
|
|
|
|
entry, err = guestbook.NewEntry(r.FormValue("name"), message,
|
2023-02-05 21:50:28 +04:00
|
|
|
r.FormValue("website"), r.FormValue("hide_website") != "")
|
|
|
|
if err != nil {
|
2023-02-06 00:02:13 +04:00
|
|
|
h.InternalError(err.Error(), "Here's your message:"+r.FormValue("message"), w)
|
2023-02-05 21:50:28 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.guestbookDB.NewEntry(entry); err != nil {
|
2023-02-06 00:02:13 +04:00
|
|
|
h.InternalError(err.Error(), "Here's your message:"+r.FormValue("message"), w)
|
2023-02-05 21:50:28 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/guestbook", http.StatusMovedPermanently)
|
|
|
|
}
|
|
|
|
|
2023-02-06 01:59:50 +04:00
|
|
|
func (h *Handlers) GuestbookAdmin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
entriesCount, _ := h.guestbookDB.Count()
|
|
|
|
entries, err := h.guestbookDB.Entries(1, entriesCount)
|
2023-03-11 23:34:08 +04:00
|
|
|
for _, entry := range entries {
|
|
|
|
entry.Message = strings.ReplaceAll(entry.Message, "\\n", "\n")
|
|
|
|
}
|
2023-02-06 01:59:50 +04:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
web.GuestbookAdmin("/ Guestbook Administration", h.guestbookOwner, entries, r, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) GuestbookAdminDeleteEntry(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id, err := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = h.guestbookDB.DeleteEntry(id)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) GuestbookAdminUpdateEntry(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) GuestbookAdminDeleteReply(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id, err := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.guestbookDB.DeleteReply(id)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 02:43:26 +04:00
|
|
|
func (h *Handlers) GuestbookAdminReply(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
id, err := strconv.ParseInt(r.FormValue("entry_id"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-02-06 01:59:50 +04:00
|
|
|
|
2023-02-06 02:43:26 +04:00
|
|
|
reply, err := guestbook.NewReply(id, r.FormValue("message"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Query().Has("update") {
|
|
|
|
if err = h.guestbookDB.UpdateReply(reply); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = h.guestbookDB.NewReply(reply)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2023-02-06 01:59:50 +04:00
|
|
|
}
|
|
|
|
|
2023-02-05 00:52:14 +04:00
|
|
|
func (h *Handlers) RSS(w http.ResponseWriter, r *http.Request) {
|
2023-02-05 06:25:22 +04:00
|
|
|
posts := []mindflow.Post{
|
|
|
|
{Category: "Kek", Date: time.Now(), Title: "LMAO", Body: "Testing!"},
|
|
|
|
}
|
|
|
|
web.RSS(r.URL.Scheme+"://"+r.Host, "Arav", posts, r, w)
|
2023-01-16 05:24:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) Robots(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data, _ := web.AssetsGetFile("robots.txt")
|
|
|
|
w.Write(data)
|
|
|
|
}
|
2023-02-05 05:25:20 +04:00
|
|
|
|
2023-02-05 23:23:55 +04:00
|
|
|
/**** Errors ******************************************************************/
|
|
|
|
|
2023-02-06 00:02:13 +04:00
|
|
|
func (h *Handlers) ForbiddenError(err, msg string, w http.ResponseWriter) {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
web.ForbiddenError("/ Forbidden", err, msg, w)
|
2023-02-05 23:23:55 +04:00
|
|
|
}
|
|
|
|
|
2023-02-05 05:25:20 +04:00
|
|
|
func (h *Handlers) NotFound(w http.ResponseWriter, r *http.Request) {
|
2023-02-05 23:23:55 +04:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2023-02-05 05:25:20 +04:00
|
|
|
web.NotFound("Not Found", w)
|
|
|
|
}
|
2023-02-05 23:05:11 +04:00
|
|
|
|
2023-02-06 00:02:13 +04:00
|
|
|
func (h *Handlers) InternalError(err, msg string, w http.ResponseWriter) {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
web.InternalError("/ Internal Error", err, msg, w)
|
2023-02-05 23:23:55 +04:00
|
|
|
}
|
|
|
|
|
2023-02-05 23:05:11 +04:00
|
|
|
/**** CAPTCHA *****************************************************************/
|
|
|
|
|
|
|
|
func (h *Handlers) CaptchaNew(w http.ResponseWriter, r *http.Request) {
|
|
|
|
dwc := dwcaptcha.NewDwellingCaptcha(h.captchaExpire)
|
|
|
|
_, id := inmemdb.New(r.RemoteAddr, dwc)
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
fmt.Fprint(w, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) CaptchaImage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := captcha.ID(GetURLParam(r, "id"))
|
|
|
|
|
|
|
|
image := inmemdb.Image(id, r.URL.Query().Get("style"))
|
|
|
|
if image == nil {
|
|
|
|
http.Error(w, "image not found", http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Content-Disposition", "inline; filename=\""+string(id)+"\"")
|
|
|
|
|
|
|
|
jpeg.Encode(w, *image, &jpeg.Options{Quality: 20})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handlers) CaptchaSolve(w http.ResponseWriter, r *http.Request) {
|
|
|
|
captchaID := captcha.ID(GetURLParam(r, "id"))
|
|
|
|
isJustRemove := r.URL.Query().Has("remove")
|
|
|
|
|
|
|
|
if isJustRemove {
|
|
|
|
inmemdb.Remove(captchaID)
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if solved := inmemdb.IsSolved(captchaID); !solved {
|
|
|
|
http.Error(w, "wrong answer", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|