diff --git a/go.mod b/go.mod index e39cc5c..df97507 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.19 require ( github.com/go-redsync/redsync/v4 v4.8.1 github.com/gofiber/fiber/v2 v2.48.0 - github.com/mcstatus-io/mcutil v1.3.0 + github.com/mcstatus-io/mcutil v1.3.1 github.com/redis/go-redis/v9 v9.0.5 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/go.sum b/go.sum index 4a845d7..699d1f6 100644 --- a/go.sum +++ b/go.sum @@ -66,6 +66,8 @@ 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/mcstatus-io/mcutil v1.3.0 h1:NjvwCQXPqBBuPKs5WLrfyg76y3lpNo/c1KwMWN2kB7A= github.com/mcstatus-io/mcutil v1.3.0/go.mod h1:VPWRCaYXfQheaTt4XJGYOGFzrp2E+B8g393SQiYO5mA= +github.com/mcstatus-io/mcutil v1.3.1 h1:S7k/3xpvFjSmFKNZ6QW260q11HRaPSQCSMCEBqmV5X4= +github.com/mcstatus-io/mcutil v1.3.1/go.mod h1:VPWRCaYXfQheaTt4XJGYOGFzrp2E+B8g393SQiYO5mA= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/src/routes.go b/src/routes.go index 249fe03..d302e3a 100644 --- a/src/routes.go +++ b/src/routes.go @@ -1,12 +1,14 @@ package main import ( + "encoding/json" "fmt" "main/src/assets" "net/http" "strconv" "github.com/gofiber/fiber/v2" + "github.com/mcstatus-io/mcutil" ) func init() { @@ -16,6 +18,11 @@ func init() { app.Get("/status/bedrock/:address", BedrockStatusHandler) app.Get("/icon", DefaultIconHandler) app.Get("/icon/:address", IconHandler) + app.Get("/debug/java/:address", DebugJavaStatusHandler) + app.Get("/debug/legacy/:address", DebugLegacyStatusHandler) + app.Get("/debug/bedrock/:address", DebugBedrockStatusHandler) + app.Get("/debug/query/basic/:address", DebugBasicQueryHandler) + app.Get("/debug/query/full/:address", DebugFullQueryHandler) app.Use(NotFoundHandler) } @@ -111,6 +118,141 @@ func DefaultIconHandler(ctx *fiber.Ctx) error { return ctx.Type("png").Send(assets.DefaultIcon) } +// DebugJavaStatusHandler returns the status of the Java edition Minecraft server specified in the address parameter. +func DebugJavaStatusHandler(ctx *fiber.Ctx) error { + host, port, err := ParseAddress(ctx.Params("address"), 25565) + + if err != nil { + return ctx.Status(http.StatusBadRequest).SendString("Invalid address value") + } + + if err = r.Increment(fmt.Sprintf("java-hits:%s-%d", host, port)); err != nil { + return err + } + + result, err := mcutil.StatusRaw(host, port) + + if err != nil { + return ctx.Status(http.StatusBadRequest).SendString(err.Error()) + } + + data, err := json.MarshalIndent(result, "", " ") + + if err != nil { + return err + } + + return ctx.Type("json").Send(data) +} + +// DebugJavaStatusHandler returns the legacy status of the Java edition Minecraft server specified in the address parameter. +func DebugLegacyStatusHandler(ctx *fiber.Ctx) error { + host, port, err := ParseAddress(ctx.Params("address"), 25565) + + if err != nil { + return ctx.Status(http.StatusBadRequest).SendString("Invalid address value") + } + + if err = r.Increment(fmt.Sprintf("java-hits:%s-%d", host, port)); err != nil { + return err + } + + result, err := mcutil.StatusLegacy(host, port) + + if err != nil { + return ctx.Status(http.StatusBadRequest).SendString(err.Error()) + } + + data, err := json.MarshalIndent(result, "", " ") + + if err != nil { + return err + } + + return ctx.Type("json").Send(data) +} + +// DebugBedrockStatusHandler returns the status of the Bedrock edition Minecraft server specified in the address parameter. +func DebugBedrockStatusHandler(ctx *fiber.Ctx) error { + host, port, err := ParseAddress(ctx.Params("address"), 19132) + + if err != nil { + return ctx.Status(http.StatusBadRequest).SendString("Invalid address value") + } + + if err = r.Increment(fmt.Sprintf("bedrock-hits:%s-%d", host, port)); err != nil { + return err + } + + result, err := mcutil.StatusBedrock(host, port) + + if err != nil { + return ctx.Status(http.StatusBadRequest).SendString(err.Error()) + } + + data, err := json.MarshalIndent(result, "", " ") + + if err != nil { + return err + } + + return ctx.Type("json").Send(data) +} + +// DebugBasicQueryHandler returns the basic query information of the Java edition Minecraft server specified in the address parameter. +func DebugBasicQueryHandler(ctx *fiber.Ctx) error { + host, port, err := ParseAddress(ctx.Params("address"), 25565) + + if err != nil { + return ctx.Status(http.StatusBadRequest).SendString("Invalid address value") + } + + if err = r.Increment(fmt.Sprintf("bedrock-hits:%s-%d", host, port)); err != nil { + return err + } + + result, err := mcutil.BasicQuery(host, port) + + if err != nil { + return ctx.Status(http.StatusBadRequest).SendString(err.Error()) + } + + data, err := json.MarshalIndent(result, "", " ") + + if err != nil { + return err + } + + return ctx.Type("json").Send(data) +} + +// DebugFullQueryHandler returns the full query information of the Java edition Minecraft server specified in the address parameter. +func DebugFullQueryHandler(ctx *fiber.Ctx) error { + host, port, err := ParseAddress(ctx.Params("address"), 25565) + + if err != nil { + return ctx.Status(http.StatusBadRequest).SendString("Invalid address value") + } + + if err = r.Increment(fmt.Sprintf("bedrock-hits:%s-%d", host, port)); err != nil { + return err + } + + result, err := mcutil.FullQuery(host, port) + + if err != nil { + return ctx.Status(http.StatusBadRequest).SendString(err.Error()) + } + + data, err := json.MarshalIndent(result, "", " ") + + if err != nil { + return err + } + + return ctx.Type("json").Send(data) +} + // NotFoundHandler handles requests to routes that do not exist and returns a 404 Not Found status. func NotFoundHandler(ctx *fiber.Ctx) error { return ctx.SendStatus(http.StatusNotFound)