2019-04-14 20:06:27 +05:30
|
|
|
package mojang
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2020-04-02 04:59:14 +05:30
|
|
|
"fmt"
|
2019-04-14 20:06:27 +05:30
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-04-21 05:34:03 +05:30
|
|
|
"time"
|
2019-04-14 20:06:27 +05:30
|
|
|
)
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
var HttpClient = &http.Client{
|
|
|
|
Timeout: 3 * time.Second,
|
2019-05-06 01:36:29 +05:30
|
|
|
Transport: &http.Transport{
|
|
|
|
MaxIdleConnsPerHost: 1024,
|
|
|
|
},
|
2019-04-21 05:34:03 +05:30
|
|
|
}
|
2019-04-14 20:06:27 +05:30
|
|
|
|
|
|
|
type SignedTexturesResponse struct {
|
2019-04-27 04:16:15 +05:30
|
|
|
Id string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Props []*Property `json:"properties"`
|
|
|
|
decodedTextures *TexturesProp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *SignedTexturesResponse) DecodeTextures() *TexturesProp {
|
|
|
|
if t.decodedTextures == nil {
|
|
|
|
var texturesProp string
|
|
|
|
for _, prop := range t.Props {
|
|
|
|
if prop.Name == "textures" {
|
|
|
|
texturesProp = prop.Value
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if texturesProp == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedTextures, _ := DecodeTextures(texturesProp)
|
|
|
|
t.decodedTextures = decodedTextures
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.decodedTextures
|
2019-04-14 20:06:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type Property struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Signature string `json:"signature,omitempty"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProfileInfo struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
IsLegacy bool `json:"legacy,omitempty"`
|
|
|
|
IsDemo bool `json:"demo,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:09:17 +05:30
|
|
|
// Exchanges usernames array to array of uuids
|
|
|
|
// See https://wiki.vg/Mojang_API#Playernames_-.3E_UUIDs
|
2019-04-14 20:06:27 +05:30
|
|
|
func UsernamesToUuids(usernames []string) ([]*ProfileInfo, error) {
|
|
|
|
requestBody, _ := json.Marshal(usernames)
|
2019-04-21 05:34:03 +05:30
|
|
|
request, _ := http.NewRequest("POST", "https://api.mojang.com/profiles/minecraft", bytes.NewBuffer(requestBody))
|
2019-04-14 20:06:27 +05:30
|
|
|
|
|
|
|
request.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
response, err := HttpClient.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
2019-04-21 01:09:17 +05:30
|
|
|
if responseErr := validateResponse(response); responseErr != nil {
|
|
|
|
return nil, responseErr
|
2019-04-14 20:06:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var result []*ProfileInfo
|
|
|
|
|
|
|
|
body, _ := ioutil.ReadAll(response.Body)
|
|
|
|
_ = json.Unmarshal(body, &result)
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:09:17 +05:30
|
|
|
// Obtains textures information for provided uuid
|
|
|
|
// See https://wiki.vg/Mojang_API#UUID_-.3E_Profile_.2B_Skin.2FCape
|
2019-04-15 03:01:09 +05:30
|
|
|
func UuidToTextures(uuid string, signed bool) (*SignedTexturesResponse, error) {
|
|
|
|
url := "https://sessionserver.mojang.com/session/minecraft/profile/" + uuid
|
|
|
|
if signed {
|
|
|
|
url += "?unsigned=false"
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
request, _ := http.NewRequest("GET", url, nil)
|
2019-04-14 20:06:27 +05:30
|
|
|
|
|
|
|
response, err := HttpClient.Do(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
2019-04-21 01:09:17 +05:30
|
|
|
if responseErr := validateResponse(response); responseErr != nil {
|
|
|
|
return nil, responseErr
|
2019-04-14 20:06:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var result *SignedTexturesResponse
|
|
|
|
|
|
|
|
body, _ := ioutil.ReadAll(response.Body)
|
|
|
|
_ = json.Unmarshal(body, &result)
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:09:17 +05:30
|
|
|
func validateResponse(response *http.Response) error {
|
2019-04-21 01:34:29 +05:30
|
|
|
switch {
|
|
|
|
case response.StatusCode == 204:
|
2019-04-21 01:09:17 +05:30
|
|
|
return &EmptyResponse{}
|
2019-04-21 05:34:03 +05:30
|
|
|
case response.StatusCode == 400:
|
|
|
|
type errorResponse struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
Message string `json:"errorMessage"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var decodedError *errorResponse
|
|
|
|
body, _ := ioutil.ReadAll(response.Body)
|
|
|
|
_ = json.Unmarshal(body, &decodedError)
|
|
|
|
|
|
|
|
return &BadRequestError{ErrorType: decodedError.Error, Message: decodedError.Message}
|
2019-11-08 04:02:26 +05:30
|
|
|
case response.StatusCode == 403:
|
|
|
|
return &ForbiddenError{}
|
2019-04-21 01:34:29 +05:30
|
|
|
case response.StatusCode == 429:
|
2019-04-21 01:09:17 +05:30
|
|
|
return &TooManyRequestsError{}
|
2019-04-21 01:34:29 +05:30
|
|
|
case response.StatusCode >= 500:
|
2019-04-21 05:34:03 +05:30
|
|
|
return &ServerError{Status: response.StatusCode}
|
2019-04-21 01:09:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
type ResponseError interface {
|
|
|
|
IsMojangError() bool
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:09:17 +05:30
|
|
|
// Mojang API doesn't return a 404 Not Found error for non-existent data identifiers
|
|
|
|
// Instead, they return 204 with an empty body
|
|
|
|
type EmptyResponse struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*EmptyResponse) Error() string {
|
2020-04-02 04:59:14 +05:30
|
|
|
return "200: Empty Response"
|
2019-04-21 01:09:17 +05:30
|
|
|
}
|
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (*EmptyResponse) IsMojangError() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// When passed request params are invalid, Mojang returns 400 Bad Request error
|
|
|
|
type BadRequestError struct {
|
|
|
|
ResponseError
|
|
|
|
ErrorType string
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *BadRequestError) Error() string {
|
2020-04-02 04:59:14 +05:30
|
|
|
return fmt.Sprintf("400 %s: %s", e.ErrorType, e.Message)
|
2019-04-21 05:34:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (*BadRequestError) IsMojangError() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-11-08 04:02:26 +05:30
|
|
|
// When Mojang decides you're such a bad guy, this error appears (even if the request has no authorization)
|
|
|
|
type ForbiddenError struct {
|
|
|
|
ResponseError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*ForbiddenError) Error() string {
|
2020-04-02 04:59:14 +05:30
|
|
|
return "403: Forbidden"
|
2019-11-08 04:02:26 +05:30
|
|
|
}
|
|
|
|
|
2019-04-21 01:09:17 +05:30
|
|
|
// When you exceed the set limit of requests, this error will be returned
|
2019-04-14 20:06:27 +05:30
|
|
|
type TooManyRequestsError struct {
|
2019-04-21 05:34:03 +05:30
|
|
|
ResponseError
|
2019-04-14 20:06:27 +05:30
|
|
|
}
|
|
|
|
|
2019-04-21 01:09:17 +05:30
|
|
|
func (*TooManyRequestsError) Error() string {
|
2020-04-02 04:59:14 +05:30
|
|
|
return "429: Too Many Requests"
|
2019-04-14 20:06:27 +05:30
|
|
|
}
|
2019-04-21 01:34:29 +05:30
|
|
|
|
2019-04-21 05:34:03 +05:30
|
|
|
func (*TooManyRequestsError) IsMojangError() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-04-21 01:34:29 +05:30
|
|
|
// ServerError happens when Mojang's API returns any response with 50* status
|
|
|
|
type ServerError struct {
|
2019-04-21 05:34:03 +05:30
|
|
|
ResponseError
|
2019-04-21 01:34:29 +05:30
|
|
|
Status int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ServerError) Error() string {
|
2020-04-02 04:59:14 +05:30
|
|
|
return fmt.Sprintf("%d: %s", e.Status, "Server error")
|
2019-04-21 01:34:29 +05:30
|
|
|
}
|
2019-04-21 05:34:03 +05:30
|
|
|
|
|
|
|
func (*ServerError) IsMojangError() bool {
|
|
|
|
return true
|
|
|
|
}
|