2021-03-26 17:59:53 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/darmiel/yaxc/internal/common"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2021-03-26 18:18:40 +08:00
|
|
|
"strings"
|
2021-03-26 17:59:53 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *yAxCServer) handleGetAnywhere(ctx *fiber.Ctx) (err error) {
|
2021-03-26 18:18:40 +08:00
|
|
|
path := strings.TrimSpace(ctx.Params("anywhere"))
|
2021-03-27 23:42:28 +08:00
|
|
|
|
|
|
|
// validate path
|
|
|
|
if !common.ValidateAnywherePath(path) {
|
|
|
|
return ctx.Status(400).SendString("ERROR: Invalid path")
|
|
|
|
}
|
|
|
|
|
2021-03-26 17:59:53 +08:00
|
|
|
var res string
|
|
|
|
if res, err = s.Backend.Get(path); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encryption
|
|
|
|
if q := ctx.Query("secret"); q != "" {
|
|
|
|
if !s.EnableEncryption {
|
|
|
|
return errEncryptionNotEnabled
|
|
|
|
}
|
|
|
|
// do not fail on error
|
|
|
|
if encrypt, err := common.Decrypt(res, q); err == nil {
|
|
|
|
res = string(encrypt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 18:18:40 +08:00
|
|
|
log.Warning(ctx.IP(), "requested VALUE", path)
|
2021-03-26 17:59:53 +08:00
|
|
|
|
|
|
|
if res == "" {
|
|
|
|
ctx.Status(404)
|
|
|
|
} else {
|
|
|
|
ctx.Status(200)
|
|
|
|
}
|
|
|
|
return ctx.SendString(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *yAxCServer) handleGetHashAnywhere(ctx *fiber.Ctx) (err error) {
|
2021-03-26 18:18:40 +08:00
|
|
|
path := strings.TrimSpace(ctx.Params("anywhere"))
|
2021-03-26 17:59:53 +08:00
|
|
|
var res string
|
|
|
|
if res, err = s.Backend.GetHash(path); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-03-26 18:18:40 +08:00
|
|
|
|
|
|
|
log.Warning(ctx.IP(), "requested HASH", path)
|
|
|
|
|
2021-03-26 17:59:53 +08:00
|
|
|
if res == "" {
|
|
|
|
ctx.Status(404)
|
|
|
|
} else {
|
|
|
|
ctx.Status(200)
|
|
|
|
}
|
|
|
|
return ctx.SendString(res)
|
|
|
|
}
|