From afec7fe2fd6cd69dbd298528b713474d648f1de8 Mon Sep 17 00:00:00 2001 From: NeT32 Date: Tue, 26 Apr 2022 19:44:05 -0300 Subject: [PATCH] feat: hasJoined and hasPaid routes --- server/api.go | 12 ++++++++++++ server/cache.go | 49 +++++++++++++++++++++++++++++++++++------------- server/mojang.go | 21 +++++++++++++-------- server/server.go | 16 +++++++++++++--- 4 files changed, 74 insertions(+), 24 deletions(-) diff --git a/server/api.go b/server/api.go index 2682a78..8b08c4e 100644 --- a/server/api.go +++ b/server/api.go @@ -36,3 +36,15 @@ func FetchProfileByName(userName string) (model.Profile, MojangResponse) { } return profile, data } + +func HasPaid(userName string) (string, MojangResponse) { + result := "failed" + data := UsernameToUUID(userName) + if data.Code == 200 { + result = "true" + } + if data.Code == 204 { + result = "false" + } + return result, data +} diff --git a/server/cache.go b/server/cache.go index b5bd0c3..d95b148 100644 --- a/server/cache.go +++ b/server/cache.go @@ -1,6 +1,8 @@ package server import ( + "encoding/json" + "log" "time" "github.com/go-redis/redis/v8" @@ -21,26 +23,47 @@ func redisConn() *redis.Client { } type CacheResponse struct { - hasCache bool - response MojangResponse + HasCache bool `json:"hascache"` + Response MojangResponse `json:"response"` } -func HasCache(cacheKey string) CacheResponse { +func ExistsKey(cacheKey string) int64 { + rdb := redisConn() + return rdb.Exists(rdb.Context(), cacheKey).Val() +} + +func SaveValue(cacheKey string, value string) bool { rdb := redisConn() - hasCache := rdb.Exists(rdb.Context(), cacheKey).Val() - return CacheResponse{ - hasCache == 1, - MojangResponse{ - 200, - rdb.Get(rdb.Context(), cacheKey).Val()}} + rdb.Set(rdb.Context(), cacheKey, value, 24*time.Hour).Val() + return true +} + +func GetValue(cacheKey string) string { + rdb := redisConn() + return rdb.Get(rdb.Context(), cacheKey).Val() +} + +func HasCache(cacheKey string) CacheResponse { + hasCache := ExistsKey(cacheKey) + value := GetValue(cacheKey) + var response MojangResponse + if hasCache == 1 { + err := json.Unmarshal([]byte(value), &response) + if err != nil { + log.Println(err, "HasCache:", cacheKey) + } + } + return CacheResponse{hasCache == 1, response} } func SaveCache(cacheKey string, response MojangResponse) CacheResponse { saved := false - if response.Code == 200 { - rdb := redisConn() - rdb.Set(rdb.Context(), cacheKey, response.Json, time.Hour).Val() - saved = true + if response.Code < 500 { + data, err := json.Marshal(response) + if err != nil { + log.Println(err, "SaveCache:", cacheKey, response) + } + saved = SaveValue(cacheKey, string(data)) } return CacheResponse{saved, response} } diff --git a/server/mojang.go b/server/mojang.go index c6d0b1b..b26a10f 100644 --- a/server/mojang.go +++ b/server/mojang.go @@ -36,35 +36,40 @@ func UuidToProfile(uuid string, unsigned string) MojangResponse { return mojangGet(URL) } +func HasJoined(userName string, serverId string) MojangResponse { + URL := SESSION_URL + fmt.Sprintf("session/minecraft/hasJoined?username=%s&serverId=%s", userName, serverId) + return mojangGet(URL) +} + func BlockedServers() MojangResponse { URL := SESSION_URL + "blockedservers" return mojangGet(URL) } type MojangResponse struct { - Code int - Json string + Code int `json:"code"` + Json string `json:"json"` } func mojangGet(URL string) MojangResponse { key := URL cache := HasCache(key) - if cache.hasCache { - return cache.response + if cache.HasCache { + return cache.Response } resp, err := http.Get(URL) - return SaveCache(key, mojangResponse(resp, err)).response + return SaveCache(key, mojangResponse(resp, err)).Response } func mojangPost(URL string, jsonData []byte) MojangResponse { hashData := hex.EncodeToString(md5.New().Sum(jsonData)) key := URL + hashData cache := HasCache(key) - if cache.hasCache { - return cache.response + if cache.HasCache { + return cache.Response } resp, err := http.Post(URL, "application/json", bytes.NewBuffer(jsonData)) - return SaveCache(key, mojangResponse(resp, err)).response + return SaveCache(key, mojangResponse(resp, err)).Response } func mojangResponse(resp *http.Response, err error) MojangResponse { diff --git a/server/server.go b/server/server.go index f641165..7150226 100644 --- a/server/server.go +++ b/server/server.go @@ -13,9 +13,9 @@ const VERSION = "1.0.2" var name, author, url = "mojang-redis", "NeT32", "https://github.com/net32/mojang-redis" func InitServer() { - log.Println("Starting", name, "developed by", author) - log.Println("API Version", VERSION) - log.Println("GitHub >", url) + log.Printf("Starting %s developed by %s.", name, author) + log.Printf("API Version: %s", VERSION) + log.Printf("GitHub: %s", url) gin.SetMode(gin.ReleaseMode) r := gin.Default() r.GET("/", func(c *gin.Context) { @@ -54,10 +54,20 @@ func InitServer() { } writeJsonResponse(c, UuidToProfile(uuid, unsigned)) }) + r.GET("/session/minecraft/hasJoined", func(c *gin.Context) { + userName, _ := c.GetQuery("username") + serverId, _ := c.GetQuery("serverId") + writeJsonResponse(c, HasJoined(userName, serverId)) + }) r.GET("/blockedservers", func(c *gin.Context) { response := BlockedServers() c.String(response.Code, response.Json) }) + r.GET("/auth/haspaid/:name", func(c *gin.Context) { + userName := c.Params.ByName("name") + response, _ := HasPaid(userName) + c.String(200, response) + }) addr := ":" + GetEnv("PORT", "8080") log.Println("Listen: " + addr) r.Run(addr)