From 9fc1be0ec17517c6b69e681533c981bd65a490f5 Mon Sep 17 00:00:00 2001 From: lan Date: Thu, 9 Jul 2026 21:06:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(yggdrasil):=20=E5=AE=9E=E7=8E=B0=20MC=201.?= =?UTF-8?q?21.7=20=E5=8D=95=E7=94=A8=E6=88=B7=E5=90=8D=E6=9F=A5=20UUID=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 对应官方 api.minecraftservices.com/minecraft/profile/lookup/name/{name}, 1.21.7 的 /whitelist add 等命令会请求该接口(authlib-injector issue #277)。 authlib-injector 把 api.minecraftservices.com override 到 {apiRoot}/minecraftservices, 故路由注册在 mcServices 组下: GET /api/yggdrasil/minecraftservices/minecraft/profile/lookup/name/:name 复用 ProfileSearchByNameSingle,返回 {id,name},未找到返回 404。 go build ./... && go test ./... 全部通过。 --- internal/handler/routes.go | 2 ++ internal/handler/yggdrasil_handler.go | 35 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/internal/handler/routes.go b/internal/handler/routes.go index 24427f0..18fc977 100644 --- a/internal/handler/routes.go +++ b/internal/handler/routes.go @@ -186,6 +186,8 @@ func registerYggdrasilRoutesWithDI(v1 *gin.RouterGroup, h *Handlers) { mcServices.POST("/player/attributes", h.Friends.UpdateAttributes) mcServices.POST("/presence", h.Friends.UpdatePresence) mcServices.GET("/privacy/blocklist", h.Friends.GetBlocklist) + // MC 1.21.7 /whitelist add 查询单用户名 UUID(authlib-injector issue #277) + mcServices.GET("/minecraft/profile/lookup/name/:name", h.Yggdrasil.LookupProfileByName) } authserver := ygg.Group("/authserver") diff --git a/internal/handler/yggdrasil_handler.go b/internal/handler/yggdrasil_handler.go index 4376fc9..b613927 100644 --- a/internal/handler/yggdrasil_handler.go +++ b/internal/handler/yggdrasil_handler.go @@ -682,6 +682,41 @@ func (h *YggdrasilHandler) GetProfileByName(c *gin.Context) { c.JSON(http.StatusOK, result) } +// LookupProfileByName 单用户名查 UUID(authlib-injector issue #277 / MC 1.21.7) +// @Summary MinecraftServices 按用户名查 UUID +// @Description 对应官方 api.minecraftservices.com/minecraft/profile/lookup/name/{name}, +// @Description 1.21.7 的 /whitelist add 等命令会请求该接口。authlib-injector 把 +// @Description api.minecraftservices.com override 到 {apiRoot}/minecraftservices。 +// @Description 返回 {id,name},未找到返回 404。 +// @Tags Yggdrasil +// @Produce json +// @Param name path string true "用户名" +// @Success 200 {object} model.NameAndId "档案标识" +// @Failure 404 {object} map[string]string "档案不存在" +// @Router /api/yggdrasil/minecraftservices/minecraft/profile/lookup/name/{name} [get] +func (h *YggdrasilHandler) LookupProfileByName(c *gin.Context) { + name := c.Param("name") + if name == "" { + c.JSON(http.StatusNotFound, gin.H{"error": "用户不存在"}) + return + } + + result, err := h.container.ProfileService.ProfileSearchByNameSingle(c.Request.Context(), name) + if err != nil { + h.logger.Warn("按用户名查档失败", zap.String("name", name), zap.Error(err)) + c.JSON(http.StatusNotFound, gin.H{"error": "用户不存在"}) + return + } + if result == nil { + h.logger.Info("用户名未找到", zap.String("name", name)) + c.JSON(http.StatusNotFound, gin.H{"error": "用户不存在"}) + return + } + + h.logger.Info("成功获取档案", zap.String("name", name), zap.String("uuid", result.ID)) + c.JSON(http.StatusOK, result) +} + // GetMetaData 获取Yggdrasil元数据 // @Summary Yggdrasil元数据 // @Description Yggdrasil协议: 获取服务器元数据