原始提交

This commit is contained in:
2022-02-22 18:04:33 +08:00
commit 014dbe4982
32 changed files with 4057 additions and 0 deletions

62
router/home_router.go Normal file
View File

@@ -0,0 +1,62 @@
/*
* Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package router
import (
"github.com/gin-gonic/gin"
"net/http"
)
type MetaInfo struct {
ImplementationName string `json:"implementationName,omitempty"`
ImplementationVersion string `json:"implementationVersion,omitempty"`
ServerName string `json:"serverName,omitempty"`
Links struct {
Homepage string `json:"homepage,omitempty"`
Register string `json:"register,omitempty"`
} `json:"links"`
FeatureNonEmailLogin bool `json:"feature.non_email_login,omitempty"`
FeatureLegacySkinApi bool `json:"feature.legacy_skin_api,omitempty"`
FeatureNoMojangNamespace bool `json:"feature.no_mojang_namespace,omitempty"`
}
type ServerMeta struct {
Meta MetaInfo `json:"meta"`
SkinDomains []string `json:"skinDomains"`
SignaturePublickey string `json:"signaturePublickey"`
}
type HomeRouter interface {
Home(c *gin.Context)
}
type homeRouterImpl struct {
serverMeta ServerMeta
}
func NewHomeRouter(meta *ServerMeta) HomeRouter {
homeRouter := homeRouterImpl{
serverMeta: *meta,
}
return &homeRouter
}
// Home 首页路由
func (h *homeRouterImpl) Home(c *gin.Context) {
c.JSON(http.StatusOK, h.serverMeta)
}

68
router/init.go Normal file
View File

@@ -0,0 +1,68 @@
/*
* Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package router
import (
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"yggdrasil-go/service"
)
func InitRouters(router *gin.Engine, db *gorm.DB, meta *ServerMeta, skinRootUrl string) {
err := router.SetTrustedProxies([]string{"127.0.0.1"})
if err != nil {
panic(err)
}
tokenService := service.NewTokenService()
userService := service.NewUserService(tokenService, db)
sessionService := service.NewSessionService(tokenService)
textureService := service.NewTextureService(tokenService, db)
homeRouter := NewHomeRouter(meta)
userRouter := NewUserRouter(userService, skinRootUrl)
sessionRouter := NewSessionRouter(sessionService, skinRootUrl)
textureRouter := NewTextureRouter(textureService)
router.GET("/", homeRouter.Home)
authserver := router.Group("/authserver")
{
authserver.POST("/register", userRouter.Register)
authserver.POST("/authenticate", userRouter.Login)
authserver.POST("/change", userRouter.ChangeProfile)
authserver.POST("/refresh", userRouter.Refresh)
authserver.POST("/validate", userRouter.Validate)
authserver.POST("/invalidate", userRouter.Invalidate)
authserver.POST("/signout", userRouter.Signout)
}
router.GET("/users/profiles/minecraft/:username", userRouter.UsernameToUUID)
sessionserver := router.Group("/sessionserver/session/minecraft")
{
sessionserver.GET("/profile/:uuid", userRouter.QueryProfile)
sessionserver.POST("/join", sessionRouter.JoinServer)
sessionserver.GET("/hasJoined", sessionRouter.HasJoinedServer)
}
router.GET("/textures/:hash", textureRouter.GetTexture)
api := router.Group("/api")
{
api.POST("/profiles/minecraft", userRouter.QueryUUIDs)
api.POST("/user/profile/:uuid/:textureType", textureRouter.SetTexture)
api.PUT("/user/profile/:uuid/:textureType", textureRouter.UploadTexture)
api.DELETE("/user/profile/:uuid/:textureType", textureRouter.DeleteTexture)
api.GET("/users/profiles/minecraft/:username", userRouter.UsernameToUUID)
}
}

85
router/session_router.go Normal file
View File

@@ -0,0 +1,85 @@
/*
* Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package router
import (
"github.com/gin-gonic/gin"
"net/http"
"strings"
"yggdrasil-go/service"
"yggdrasil-go/util"
)
type SessionRouter interface {
JoinServer(c *gin.Context)
HasJoinedServer(c *gin.Context)
}
type sessionRouterImpl struct {
sessionService service.SessionService
skinRootUrl string
}
func NewSessionRouter(sessionService service.SessionService, skinRootUrl string) SessionRouter {
sessionRouter := sessionRouterImpl{
sessionService: sessionService,
skinRootUrl: skinRootUrl,
}
return &sessionRouter
}
type JoinServerRequest struct {
AccessToken string `json:"accessToken" binding:"required"`
SelectedProfile string `json:"selectedProfile" binding:"required"`
ServerId string `json:"serverId" binding:"required"`
}
func (s *sessionRouterImpl) JoinServer(c *gin.Context) {
request := JoinServerRequest{}
err := c.ShouldBindJSON(&request)
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, util.NewForbiddenOperationError(err.Error()))
return
}
ip := c.Request.RemoteAddr[:strings.LastIndexByte(c.Request.RemoteAddr, ':')]
err = s.sessionService.JoinServer(request.AccessToken, request.ServerId, request.SelectedProfile, ip)
if err != nil {
util.HandleError(c, err)
return
}
c.Status(http.StatusNoContent)
}
func (s *sessionRouterImpl) HasJoinedServer(c *gin.Context) {
username := c.Query("username")
serverId := c.Query("serverId")
ip := c.DefaultQuery("ip",
c.Request.RemoteAddr[:strings.LastIndexByte(c.Request.RemoteAddr, ':')])
var textureBaseUrl string
if len(s.skinRootUrl) > 0 {
textureBaseUrl = strings.TrimRight(s.skinRootUrl, "/") + "/textures"
} else {
textureBaseUrl = c.Request.URL.Scheme + "://" + c.Request.URL.Hostname() + "/textures"
}
response, err := s.sessionService.HasJoinedServer(serverId, username, ip, textureBaseUrl)
if err != nil {
util.HandleError(c, err)
return
}
c.JSON(http.StatusOK, response)
}

166
router/texture_router.go Normal file
View File

@@ -0,0 +1,166 @@
/*
* Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package router
import (
"github.com/gin-gonic/gin"
"net/http"
"yggdrasil-go/model"
"yggdrasil-go/service"
"yggdrasil-go/util"
)
type TextureRouter interface {
GetTexture(c *gin.Context)
SetTexture(c *gin.Context)
UploadTexture(c *gin.Context)
DeleteTexture(c *gin.Context)
}
type textureRouterImpl struct {
textureService service.TextureService
}
func NewTextureRouter(textureService service.TextureService) TextureRouter {
textureRouter := textureRouterImpl{textureService: textureService}
return &textureRouter
}
type SetTextureRequest struct {
Url string `json:"url" binding:"required,url"`
Model string `json:"model" binding:"oneof=slim default"`
}
func (t *textureRouterImpl) GetTexture(c *gin.Context) {
hash := c.Param("hash")
response, err := t.textureService.GetTexture(hash)
if err != nil {
util.HandleError(c, err)
return
}
c.Header("Cache-Control", "public, max-age=31536000")
c.Data(http.StatusOK, "image/png", response)
}
func (t *textureRouterImpl) SetTexture(c *gin.Context) {
request := SetTextureRequest{Model: string(model.STEVE)}
err := c.ShouldBindJSON(&request)
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, util.NewForbiddenOperationError(err.Error()))
return
}
bearerToken := c.GetHeader("Authorization")
if len(bearerToken) < 8 {
c.AbortWithStatusJSON(http.StatusUnauthorized, util.NewForbiddenOperationError(util.MessageInvalidToken))
return
}
accessToken := bearerToken[7:]
profileId, err := util.ToUUID(c.Param("uuid"))
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, util.NewIllegalArgumentError(err.Error()))
return
}
textureType := c.Param("textureType")
if textureType != "skin" && textureType != "cape" {
c.AbortWithStatusJSON(http.StatusBadRequest, util.NewIllegalArgumentError("Invalid texture type."))
return
}
if request.Model == "" {
request.Model = string(model.STEVE)
}
modelType := model.ModelType(request.Model)
err = t.textureService.SetTexture(accessToken, profileId, request.Url, textureType, &modelType)
if err != nil {
util.HandleError(c, err)
return
}
c.Status(http.StatusNoContent)
}
func (t *textureRouterImpl) UploadTexture(c *gin.Context) {
bearerToken := c.GetHeader("Authorization")
if len(bearerToken) < 8 {
c.AbortWithStatusJSON(http.StatusUnauthorized, util.NewForbiddenOperationError(util.MessageInvalidToken))
return
}
accessToken := bearerToken[7:]
profileId, err := util.ToUUID(c.Param("uuid"))
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, util.NewIllegalArgumentError(err.Error()))
return
}
textureType := c.Param("textureType")
if textureType != "skin" && textureType != "cape" {
c.AbortWithStatusJSON(http.StatusBadRequest, util.NewIllegalArgumentError("Invalid texture type."))
return
}
modelStr := c.PostForm("model")
modelType := model.STEVE
if modelStr == "ALEX" {
modelType = model.ALEX
}
file, err := c.FormFile("file")
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, util.NewIllegalArgumentError(err.Error()))
return
}
if file.Size > (1 << 20) {
c.AbortWithStatusJSON(http.StatusBadRequest, util.NewIllegalArgumentError("File too large(more than 1MiB)"))
return
}
fileReader, err := file.Open()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, util.YggdrasilError{
ErrorCode: "Internal Server Error",
ErrorMessage: "Can not open file.",
})
return
}
defer fileReader.Close()
err = t.textureService.UploadTexture(accessToken, profileId, fileReader, textureType, &modelType)
if err != nil {
util.HandleError(c, err)
return
}
c.Status(http.StatusNoContent)
}
func (t *textureRouterImpl) DeleteTexture(c *gin.Context) {
bearerToken := c.GetHeader("Authorization")
if len(bearerToken) < 8 {
c.AbortWithStatusJSON(http.StatusUnauthorized, util.NewForbiddenOperationError(util.MessageInvalidToken))
return
}
accessToken := bearerToken[7:]
profileId, err := util.ToUUID(c.Param("uuid"))
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, util.NewIllegalArgumentError(err.Error()))
return
}
textureType := c.Param("textureType")
if textureType != "skin" && textureType != "cape" {
c.AbortWithStatusJSON(http.StatusBadRequest, util.NewIllegalArgumentError("Invalid texture type."))
return
}
err = t.textureService.DeleteTexture(accessToken, profileId, textureType)
if err != nil {
util.HandleError(c, err)
return
}
c.Status(http.StatusNoContent)
}

265
router/user_router.go Normal file
View File

@@ -0,0 +1,265 @@
/*
* Copyright (C) 2022. Gardel <sunxinao@hotmail.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package router
import (
"github.com/gin-gonic/gin"
"net/http"
"strings"
"yggdrasil-go/model"
"yggdrasil-go/service"
"yggdrasil-go/util"
)
type UserRouter interface {
Register(c *gin.Context)
Login(c *gin.Context)
ChangeProfile(c *gin.Context)
Refresh(c *gin.Context)
Validate(c *gin.Context)
Invalidate(c *gin.Context)
Signout(c *gin.Context)
UsernameToUUID(c *gin.Context)
QueryUUIDs(c *gin.Context)
QueryProfile(c *gin.Context)
}
type userRouterImpl struct {
userService service.UserService
skinRootUrl string
}
func NewUserRouter(userService service.UserService, skinRootUrl string) UserRouter {
userRouter := userRouterImpl{
userService: userService,
skinRootUrl: skinRootUrl,
}
return &userRouter
}
type RegRequest struct {
Username string `json:"username" binding:"required,email"`
Password string `json:"password" binding:"required"`
ProfileName string `json:"profileName" binding:"required"`
}
type MinecraftAgent struct {
Name string `json:"name"`
Version int `json:"version"`
}
type ClientTokenBase struct {
ClientToken *string `json:"clientToken,omitempty"`
}
type AccessTokenBase struct {
AccessToken string `json:"accessToken" binding:"required"`
}
type DualTokenBase struct {
AccessTokenBase
ClientTokenBase
}
type LoginRequest struct {
ClientTokenBase
Username string `json:"username" binding:"required,email"`
Password string `json:"password" binding:"required"`
RequestUser bool `json:"requestUser"`
Agent *MinecraftAgent `json:"agent,omitempty"`
}
type RefreshRequest struct {
DualTokenBase
RequestUser bool `json:"requestUser"`
SelectedProfile *model.ProfileResponse `json:"selectedProfile,omitempty"`
}
type ValidateRequest struct {
DualTokenBase
}
type ChangeProfileRequest struct {
DualTokenBase
ChangeTo string `json:"changeTo" binding:"required"`
}
type InvalidateRequest struct {
AccessTokenBase
}
type SignoutRequest struct {
Username string `json:"username" binding:"required,email"`
Password string `json:"password" binding:"required"`
}
func (u *userRouterImpl) Register(c *gin.Context) {
request := RegRequest{}
err := c.ShouldBindJSON(&request)
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, util.NewForbiddenOperationError(err.Error()))
return
}
response, err := u.userService.Register(request.Username, request.Password, request.ProfileName)
if err != nil {
util.HandleError(c, err)
return
}
c.JSON(http.StatusOK, response)
}
func (u *userRouterImpl) Login(c *gin.Context) {
request := LoginRequest{}
err := c.ShouldBindJSON(&request)
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, util.NewForbiddenOperationError(err.Error()))
return
}
response, err := u.userService.Login(request.Username, request.Password, request.ClientToken, request.RequestUser)
if err != nil {
util.HandleError(c, err)
return
}
c.JSON(http.StatusOK, response)
}
func (u *userRouterImpl) ChangeProfile(c *gin.Context) {
request := ChangeProfileRequest{}
err := c.ShouldBindJSON(&request)
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, util.NewForbiddenOperationError(err.Error()))
return
}
err = u.userService.ChangeProfile(request.AccessToken, request.ClientToken, request.ChangeTo)
if err != nil {
util.HandleError(c, err)
return
}
c.Status(http.StatusNoContent)
}
func (u *userRouterImpl) Refresh(c *gin.Context) {
request := RefreshRequest{}
err := c.ShouldBindJSON(&request)
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, util.NewForbiddenOperationError(err.Error()))
return
}
response, err := u.userService.Refresh(request.AccessToken, request.ClientToken, request.RequestUser, request.SelectedProfile)
if err != nil {
util.HandleError(c, err)
return
}
c.JSON(http.StatusOK, response)
}
func (u *userRouterImpl) Validate(c *gin.Context) {
request := ValidateRequest{}
err := c.ShouldBindJSON(&request)
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, util.NewForbiddenOperationError(err.Error()))
return
}
err = u.userService.Validate(request.AccessToken, request.ClientToken)
if err != nil {
util.HandleError(c, err)
return
}
c.Status(http.StatusNoContent)
}
func (u *userRouterImpl) Invalidate(c *gin.Context) {
request := InvalidateRequest{}
err := c.ShouldBindJSON(&request)
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, util.NewForbiddenOperationError(err.Error()))
return
}
err = u.userService.Invalidate(request.AccessToken)
if err != nil {
util.HandleError(c, err)
return
}
c.Status(http.StatusNoContent)
}
func (u *userRouterImpl) Signout(c *gin.Context) {
request := SignoutRequest{}
err := c.ShouldBindJSON(&request)
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, util.NewForbiddenOperationError(err.Error()))
return
}
err = u.userService.Signout(request.Username, request.Password)
if err != nil {
util.HandleError(c, err)
return
}
c.Status(http.StatusNoContent)
}
func (u *userRouterImpl) UsernameToUUID(c *gin.Context) {
username := c.Param("username")
response, err := u.userService.UsernameToUUID(username)
if err != nil {
util.HandleError(c, err)
return
}
if response != nil {
c.JSON(http.StatusOK, response)
} else {
c.Status(http.StatusNoContent)
}
}
func (u *userRouterImpl) QueryUUIDs(c *gin.Context) {
var request []string
err := c.ShouldBindJSON(&request)
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, util.NewForbiddenOperationError(err.Error()))
return
}
response, err := u.userService.QueryUUIDs(request)
if err != nil {
util.HandleError(c, err)
return
}
c.JSON(http.StatusOK, response)
}
func (u *userRouterImpl) QueryProfile(c *gin.Context) {
profileIdStr := c.Param("uuid")
profileId, err := util.ToUUID(profileIdStr)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, util.NewIllegalArgumentError(err.Error()))
return
}
unsigned := "true" == c.DefaultQuery("unsigned", "false")
var textureBaseUrl string
if len(u.skinRootUrl) > 0 {
textureBaseUrl = strings.TrimRight(u.skinRootUrl, "/") + "/textures"
} else {
textureBaseUrl = c.Request.URL.Scheme + "://" + c.Request.URL.Hostname() + "/textures"
}
response, err := u.userService.QueryProfile(profileId, unsigned, textureBaseUrl)
if err != nil {
util.HandleError(c, err)
return
}
c.JSON(http.StatusOK, response)
}