2021-03-24 23:11:23 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2021-03-25 21:25:51 +08:00
|
|
|
"errors"
|
2021-03-25 21:11:01 +08:00
|
|
|
"github.com/darmiel/yaxc/internal/common"
|
2021-03-24 23:11:23 +08:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-03-25 21:25:51 +08:00
|
|
|
var errEncryptionNotEnabled = errors.New("encryption not enabled")
|
|
|
|
|
2021-03-24 23:11:23 +08:00
|
|
|
func (s *yAxCServer) handlePostAnywhere(ctx *fiber.Ctx) (err error) {
|
|
|
|
path := ctx.Params("anywhere")
|
|
|
|
|
2021-03-25 21:11:01 +08:00
|
|
|
// Read content
|
2021-03-24 23:11:23 +08:00
|
|
|
bytes := ctx.Body()
|
2021-03-24 23:33:28 +08:00
|
|
|
if s.MaxBodyLength > 0 && len(bytes) > s.MaxBodyLength {
|
2021-03-24 23:11:23 +08:00
|
|
|
return s.errBodyLen
|
|
|
|
}
|
|
|
|
content := string(bytes)
|
|
|
|
|
2021-03-25 21:11:01 +08:00
|
|
|
// TTL
|
2021-03-24 23:11:23 +08:00
|
|
|
ttl := s.DefaultTTL
|
|
|
|
if q := ctx.Query("ttl"); q != "" {
|
|
|
|
if ttl, err = time.ParseDuration(q); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 21:11:01 +08:00
|
|
|
// Encryption
|
|
|
|
if q := ctx.Query("secret"); q != "" {
|
2021-03-25 21:25:51 +08:00
|
|
|
if !s.EnableEncryption {
|
|
|
|
return errEncryptionNotEnabled
|
|
|
|
}
|
2021-03-25 21:11:01 +08:00
|
|
|
// fail on error
|
|
|
|
encrypt, err := common.Encrypt(content, q)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
content = string(encrypt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if ttl is valid
|
2021-03-24 23:33:28 +08:00
|
|
|
if (s.MinTTL != 0 && s.MinTTL > ttl) || (s.MaxTTL != 0 && s.MaxTTL < ttl) {
|
2021-03-24 23:11:23 +08:00
|
|
|
return ctx.Status(400).SendString("ERROR: TTL out of range")
|
|
|
|
}
|
|
|
|
|
2021-03-25 21:11:01 +08:00
|
|
|
// Set contents
|
2021-03-24 23:33:28 +08:00
|
|
|
if err := s.Backend.Set(path, content, ttl); err != nil {
|
2021-03-24 23:11:23 +08:00
|
|
|
return ctx.Status(400).SendString("ERROR: " + err.Error())
|
|
|
|
}
|
|
|
|
|
2021-03-25 21:11:01 +08:00
|
|
|
log.Debug(ctx.IP(), "updated", path)
|
|
|
|
|
2021-03-24 23:11:23 +08:00
|
|
|
return ctx.Status(200).SendString(content)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *yAxCServer) handleGetAnywhere(ctx *fiber.Ctx) (err error) {
|
|
|
|
path := ctx.Params("anywhere")
|
|
|
|
var res string
|
|
|
|
if res, err = s.Backend.Get(path); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-03-25 21:11:01 +08:00
|
|
|
|
|
|
|
// Encryption
|
|
|
|
if q := ctx.Query("secret"); q != "" {
|
2021-03-25 21:25:51 +08:00
|
|
|
if !s.EnableEncryption {
|
|
|
|
return errEncryptionNotEnabled
|
|
|
|
}
|
2021-03-25 21:11:01 +08:00
|
|
|
// do not fail on error
|
|
|
|
if encrypt, err := common.Decrypt(res, q); err == nil {
|
|
|
|
res = string(encrypt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug(ctx.IP(), "requested", path)
|
2021-03-24 23:11:23 +08:00
|
|
|
|
|
|
|
if res == "" {
|
|
|
|
ctx.Status(404)
|
|
|
|
} else {
|
|
|
|
ctx.Status(200)
|
|
|
|
}
|
|
|
|
return ctx.SendString(res)
|
|
|
|
}
|