skin.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package mc
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "log"
  6. "os"
  7. "regexp"
  8. )
  9. //DownloadSkin is exported function
  10. func (mc *Handler) DownloadSkin() {
  11. if _, err := os.Stat(mc.savePath + "/" + mc.UUID + ".png"); os.IsNotExist(err) {
  12. if IsValidUUID(mc.UUID) {
  13. resp := httpReq("https://sessionserver.mojang.com/session/minecraft/profile/" + mc.UUID)
  14. var profileArray profile
  15. json.Unmarshal(resp, &profileArray)
  16. if len(profileArray.Error) > 0 {
  17. log.Println("Error when reading profiles. Download aborted.")
  18. copy("default_skin.png", mc.savePath+"/"+mc.UUID+".png")
  19. } else {
  20. if len(profileArray.Properties) > 0 {
  21. decoded, _ := base64.URLEncoding.DecodeString(profileArray.Properties[0].Value)
  22. var textureArray texture
  23. json.Unmarshal([]byte(decoded), &textureArray)
  24. downloadFile(mc.savePath+"/"+mc.UUID+".png", textureArray.Textures.SKIN.URL)
  25. } else {
  26. copy("default_skin.png", mc.savePath+"/"+mc.UUID+".png")
  27. log.Println("Error when reading profiles. Download aborted.")
  28. }
  29. }
  30. } else {
  31. log.Println("Invaild UUID... Using fallback skin")
  32. copy("default_skin.png", mc.savePath+"/"+mc.UUID+".png")
  33. }
  34. } else {
  35. //log.Println("Skin exists, not downloading")
  36. }
  37. }
  38. //vaild UUID
  39. //https://stackoverflow.com/questions/25051675/how-to-validate-uuid-v4-in-go
  40. func IsValidUUID(uuid string) bool {
  41. r := regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
  42. return r.MatchString(uuid)
  43. }