From 953eb044761b9b039b42a269c5b2d6355ae4c6bc Mon Sep 17 00:00:00 2001 From: Jacob Gunther Date: Tue, 2 May 2023 19:21:07 -0500 Subject: [PATCH] Update dependencies and add unique UUID route --- config.example.yml | 1 + go.mod | 2 +- go.sum | 4 ++++ src/config.go | 2 ++ src/redis.go | 17 +++++++++++++++++ src/routes.go | 35 +++++++++++++++++++++++++++++++++++ src/util.go | 4 ++++ 7 files changed, 64 insertions(+), 1 deletion(-) diff --git a/config.example.yml b/config.example.yml index 0504a93..9292d51 100644 --- a/config.example.yml +++ b/config.example.yml @@ -1,6 +1,7 @@ environment: development host: 127.0.0.1 port: 3000 +auth_key: "" redis: host: 127.0.0.1 port: 6379 diff --git a/go.mod b/go.mod index 0315fd4..41b01d3 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.18 require ( github.com/go-redis/redis/v8 v8.11.5 github.com/gofiber/fiber/v2 v2.44.0 - github.com/mineatar-io/skin-render v1.0.6 + github.com/mineatar-io/skin-render v1.0.8 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 083ec36..50fdb49 100644 --- a/go.sum +++ b/go.sum @@ -30,6 +30,10 @@ github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWV github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mineatar-io/skin-render v1.0.6 h1:tvuS/L1w4Awx4qxZDbrnvmHG8C4UGUFPaw9IDD/r1jg= github.com/mineatar-io/skin-render v1.0.6/go.mod h1:KkgHwrhTIqD73dkmeQwh5k2aHuWS8cDahVmd64gM128= +github.com/mineatar-io/skin-render v1.0.7 h1:mJ7KIcekZFFKFffjF6swYTchTl2tjD86/4OJdprdTso= +github.com/mineatar-io/skin-render v1.0.7/go.mod h1:KkgHwrhTIqD73dkmeQwh5k2aHuWS8cDahVmd64gM128= +github.com/mineatar-io/skin-render v1.0.8 h1:5HJGqYJ2t2UxQtfN00v1+VWelG32d0TQZip7Htz4cR4= +github.com/mineatar-io/skin-render v1.0.8/go.mod h1:KkgHwrhTIqD73dkmeQwh5k2aHuWS8cDahVmd64gM128= github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= diff --git a/src/config.go b/src/config.go index c37b799..b87b3af 100644 --- a/src/config.go +++ b/src/config.go @@ -13,6 +13,7 @@ var ( Environment: "development", Host: "127.0.0.1", Port: 3001, + AuthKey: "", Redis: RedisConfig{ Host: "127.0.0.1", Port: 6379, @@ -95,6 +96,7 @@ type Config struct { Environment string `yaml:"environment"` Host string `yaml:"host"` Port uint16 `yaml:"port"` + AuthKey string `yaml:"auth_key"` Redis RedisConfig `yaml:"redis"` Routes RoutesConfig `yaml:"routes"` Cache CacheConfig `yaml:"cache"` diff --git a/src/redis.go b/src/redis.go index def618c..64bcf23 100644 --- a/src/redis.go +++ b/src/redis.go @@ -34,6 +34,23 @@ func (r *Redis) Connect(conf RedisConfig) error { return c.Ping(ctx).Err() } +// Scan returns all keys by the pattern in the Redis database. +func (r *Redis) Scan(cursor uint64, pattern string) ([]string, uint64, error) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + + defer cancel() + + res := r.conn.Scan(ctx, cursor, pattern, 25) + + if err := res.Err(); err != nil { + return nil, 0, err + } + + keys, newCursor := res.Val() + + return keys, newCursor, nil +} + // GetString gets the value from Redis by the key and returns the value as a string. func (r *Redis) GetString(key string) (string, bool, error) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) diff --git a/src/routes.go b/src/routes.go index ed1bf1d..1637993 100644 --- a/src/routes.go +++ b/src/routes.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "strconv" + "strings" "github.com/gofiber/fiber/v2" "github.com/mineatar-io/skin-render" @@ -11,6 +12,7 @@ import ( func init() { app.Get("/ping", PingHandler) + app.Get("/list", ListHandler) app.Get("/skin/:uuid", SkinHandler) app.Get("/face/:uuid", FaceHandler) app.Get("/head/:uuid", HeadHandler) @@ -27,6 +29,39 @@ func PingHandler(ctx *fiber.Ctx) error { return ctx.SendStatus(http.StatusOK) } +// ListHandler is the API handler used for the `/list` route. +func ListHandler(ctx *fiber.Ctx) error { + authKey := ctx.Get("Authorization") + + if len(authKey) < 1 { + return ctx.SendStatus(http.StatusUnauthorized) + } + + if authKey != conf.AuthKey { + return ctx.SendStatus(http.StatusForbidden) + } + + result := make([]string, 0) + + for { + keys, cursor, err := r.Scan(0, "unique:*") + + if err != nil { + return err + } + + for _, uuid := range keys { + result = append(result, strings.TrimPrefix(uuid, "unique:")) + } + + if cursor == 0 || len(keys) < 1 { + break + } + } + + return ctx.JSON(result) +} + // FullBodyHandler is the API handler used for the `/body/full/:uuid` route. func FullBodyHandler(ctx *fiber.Ctx) error { opts := ParseQueryParams(ctx, conf.Routes.FullBody) diff --git a/src/util.go b/src/util.go index d941218..3aea367 100644 --- a/src/util.go +++ b/src/util.go @@ -96,6 +96,10 @@ func GetPlayerSkin(uuid string) (*image.NRGBA, bool, error) { return skin.GetDefaultSkin(slim), slim, nil } + if err = r.Set(fmt.Sprintf("unique:%s", textures.UUID), "0", 0); err != nil { + return nil, false, err + } + value := "" for _, property := range textures.Properties {