2017-08-20 03:52:42 +05:30
|
|
|
package http
|
2017-06-30 21:10:25 +05:30
|
|
|
|
|
|
|
import (
|
2017-08-20 03:52:42 +05:30
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
2017-06-30 21:10:25 +05:30
|
|
|
"encoding/json"
|
2017-08-20 03:52:42 +05:30
|
|
|
"io"
|
2017-06-30 21:10:25 +05:30
|
|
|
"net/http"
|
2017-08-20 03:52:42 +05:30
|
|
|
"strconv"
|
|
|
|
"time"
|
2017-06-30 21:10:25 +05:30
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2018-02-16 02:43:57 +05:30
|
|
|
"github.com/elyby/chrly/model"
|
2017-06-30 21:10:25 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type texturesResponse struct {
|
|
|
|
Skin *Skin `json:"SKIN"`
|
|
|
|
Cape *Cape `json:"CAPE,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Skin struct {
|
|
|
|
Url string `json:"url"`
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
Metadata *skinMetadata `json:"metadata,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type skinMetadata struct {
|
|
|
|
Model string `json:"model"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Cape struct {
|
|
|
|
Url string `json:"url"`
|
|
|
|
Hash string `json:"hash"`
|
|
|
|
}
|
|
|
|
|
2017-08-20 03:52:42 +05:30
|
|
|
func (cfg *Config) Textures(response http.ResponseWriter, request *http.Request) {
|
|
|
|
cfg.Logger.IncCounter("textures.request", 1)
|
|
|
|
username := parseUsername(mux.Vars(request)["username"])
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2017-08-20 03:52:42 +05:30
|
|
|
skin, err := cfg.SkinsRepo.FindByUsername(username)
|
2017-06-30 21:10:25 +05:30
|
|
|
if err != nil || skin.SkinId == 0 {
|
2017-08-20 03:52:42 +05:30
|
|
|
if skin == nil {
|
|
|
|
skin = &model.Skin{}
|
|
|
|
}
|
|
|
|
|
2017-06-30 21:10:25 +05:30
|
|
|
skin.Url = "http://skins.minecraft.net/MinecraftSkins/" + username + ".png"
|
2017-08-20 03:52:42 +05:30
|
|
|
skin.Hash = string(buildNonElyTexturesHash(username))
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
textures := texturesResponse{
|
|
|
|
Skin: &Skin{
|
|
|
|
Url: skin.Url,
|
|
|
|
Hash: skin.Hash,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if skin.IsSlim {
|
|
|
|
textures.Skin.Metadata = &skinMetadata{
|
|
|
|
Model: "slim",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 03:52:42 +05:30
|
|
|
cape, err := cfg.CapesRepo.FindByUsername(username)
|
2017-06-30 21:10:25 +05:30
|
|
|
if err == nil {
|
|
|
|
var scheme string = "http://"
|
|
|
|
if request.TLS != nil {
|
|
|
|
scheme = "https://"
|
|
|
|
}
|
|
|
|
|
|
|
|
textures.Cape = &Cape{
|
2017-08-20 03:52:42 +05:30
|
|
|
Url: scheme + request.Host + "/cloaks/" + username,
|
2017-06-30 21:10:25 +05:30
|
|
|
Hash: calculateCapeHash(cape),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 03:52:42 +05:30
|
|
|
responseData, _ := json.Marshal(textures)
|
2017-06-30 21:10:25 +05:30
|
|
|
response.Header().Set("Content-Type", "application/json")
|
|
|
|
response.Write(responseData)
|
|
|
|
}
|
|
|
|
|
2017-08-20 03:52:42 +05:30
|
|
|
func calculateCapeHash(cape *model.Cape) string {
|
2017-06-30 21:10:25 +05:30
|
|
|
hasher := md5.New()
|
|
|
|
io.Copy(hasher, cape.File)
|
|
|
|
|
|
|
|
return hex.EncodeToString(hasher.Sum(nil))
|
|
|
|
}
|
2017-08-20 03:52:42 +05:30
|
|
|
|
|
|
|
func buildNonElyTexturesHash(username string) string {
|
|
|
|
hour := getCurrentHour()
|
|
|
|
hasher := md5.New()
|
|
|
|
hasher.Write([]byte("non-ely-" + strconv.FormatInt(hour, 10) + "-" + username))
|
|
|
|
|
|
|
|
return hex.EncodeToString(hasher.Sum(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
var timeNow = time.Now
|
|
|
|
|
|
|
|
func getCurrentHour() int64 {
|
|
|
|
n := timeNow()
|
|
|
|
return time.Date(n.Year(), n.Month(), n.Day(), n.Hour(), 0, 0, 0, time.UTC).Unix()
|
|
|
|
}
|