forked from TripwireTeam/tripwire
152 lines
3.5 KiB
Go
152 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"log"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
func profileEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
uuid := mux.Vars(r)["uuid"]
|
|
|
|
exists, err := playerExistsByUUID(uuid)
|
|
if err != nil {
|
|
handleError(w, err)
|
|
return
|
|
}
|
|
if !exists {
|
|
sendError(w, YggError{
|
|
Code: 400,
|
|
Error: "Bad Request",
|
|
ErrorMessage: "The user does not exist.",
|
|
})
|
|
return
|
|
}
|
|
|
|
player, err := getPlayerByUUID(uuid)
|
|
if err != nil {
|
|
handleError(w, err)
|
|
return
|
|
}
|
|
|
|
response, err := generateProfileResponse(uuid, player.Username)
|
|
if err != nil {
|
|
handleError(w, err)
|
|
return
|
|
}
|
|
|
|
sendJSON(w, response)
|
|
}
|
|
|
|
func hasJoinedEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
params := r.URL.Query()
|
|
if !params.Has("username") || !params.Has("serverId") {
|
|
sendJSON(w, &Empty{})
|
|
return
|
|
}
|
|
|
|
player, err := getPlayerByUsername(params.Get("username"))
|
|
if err != nil {
|
|
handleError(w, err)
|
|
return
|
|
}
|
|
|
|
response, err := generateProfileResponse(params.Get("username"), player.UUID)
|
|
if err != nil {
|
|
handleError(w, err)
|
|
return
|
|
}
|
|
|
|
sendJSON(w, response)
|
|
}
|
|
|
|
func joinEndpoint(w http.ResponseWriter, r *http.Request) {
|
|
var payload JoinPayload
|
|
unmarshalTo(r, &payload)
|
|
|
|
if payload.AccessToken == "" || payload.SelectedProfile == "" || payload.ServerId == "" {
|
|
sendError(w, YggError{
|
|
Code: 400,
|
|
Error: "IllegalArgumentException",
|
|
ErrorMessage: "A required field is not present.",
|
|
})
|
|
return
|
|
}
|
|
player, err := getPlayerByAuthToken(payload.AccessToken)
|
|
if err != nil {
|
|
handleError(w, err)
|
|
return
|
|
}
|
|
log.Println(payload.SelectedProfile, ",", player.UUID)
|
|
if payload.SelectedProfile != shrinkUUID(player.UUID) {
|
|
sendError(w, YggError{
|
|
Code: 400,
|
|
Error: "Bad Request",
|
|
ErrorMessage: "The request data is malformed.",
|
|
})
|
|
return
|
|
}
|
|
sendEmpty(w)
|
|
}
|
|
|
|
func registerSessionEndpoints(r *mux.Router) {
|
|
prefix := "/sessionserver"
|
|
r.HandleFunc(prefix+"/session/minecraft/profile/{uuid}", profileEndpoint).Methods("GET")
|
|
r.HandleFunc(prefix+"/session/minecraft/join", joinEndpoint).Methods("POST")
|
|
r.HandleFunc(prefix+"/session/minecraft/hasJoined", hasJoinedEndpoint).Methods("GET")
|
|
}
|
|
|
|
func generateProfileResponse(uuid string, username string) (ProfileResponse, error) {
|
|
// todo: make this more visually appealing if possible
|
|
clearUUID := strings.Join(strings.Split(uuid, "-"), "")
|
|
skin := SkinTexture{}
|
|
skin.Url = config.BaseUrl + "/getTexture/" + uuid + "?type=skin"
|
|
skin.Metadata = SkinMetadata{}
|
|
skin.Metadata.Model = "default"
|
|
|
|
textures := ProfileTextureMetadata{}
|
|
textures.Id = clearUUID
|
|
textures.Name = username
|
|
textures.Textures = ProfileTextures{}
|
|
textures.Textures.Skin = skin
|
|
|
|
_, err := os.Stat("capes/" + uuid + ".png")
|
|
if err == nil {
|
|
cape := &Texture{}
|
|
cape.Url = config.BaseUrl + "/getTexture/" + uuid + "?type=cape"
|
|
textures.Textures.Cape = cape
|
|
}
|
|
|
|
marshalledTextures, err := json.Marshal(textures)
|
|
if err != nil {
|
|
return ProfileResponse{}, err
|
|
}
|
|
encodedTextures := base64.StdEncoding.EncodeToString(marshalledTextures)
|
|
|
|
response := ProfileResponse{}
|
|
response.Id = clearUUID
|
|
response.Name = username
|
|
response.Properties = []Property{
|
|
{
|
|
Name: "textures",
|
|
Value: encodedTextures,
|
|
},
|
|
}
|
|
return response, nil
|
|
}
|
|
|
|
func shrinkUUID(uuid string) string {
|
|
return strings.Join(strings.Split(uuid, "-"), "")
|
|
}
|
|
|
|
func growUUID(uuid string) string {
|
|
if len(uuid) == 32 {
|
|
return uuid[0:7] + "-" + uuid[8:11] + "-" + uuid[12:15] + "-" + uuid[16:19] + "-" + uuid[20:31]
|
|
}
|
|
return ""
|
|
}
|