2019-11-21 04:03:13 +05:30
|
|
|
package mojangtextures
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/elyby/chrly/api/mojang"
|
2020-04-20 16:59:33 +05:30
|
|
|
"github.com/elyby/chrly/dispatcher"
|
2019-11-21 04:03:13 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type broadcastResult struct {
|
|
|
|
textures *mojang.SignedTexturesResponse
|
|
|
|
error error
|
|
|
|
}
|
|
|
|
|
|
|
|
type broadcaster struct {
|
|
|
|
lock sync.Mutex
|
|
|
|
listeners map[string][]chan *broadcastResult
|
|
|
|
}
|
|
|
|
|
|
|
|
func createBroadcaster() *broadcaster {
|
|
|
|
return &broadcaster{
|
|
|
|
listeners: make(map[string][]chan *broadcastResult),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a boolean value, which will be true if the passed username didn't exist before
|
|
|
|
func (c *broadcaster) AddListener(username string, resultChan chan *broadcastResult) bool {
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
|
|
val, alreadyHasSource := c.listeners[username]
|
|
|
|
if alreadyHasSource {
|
|
|
|
c.listeners[username] = append(val, resultChan)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
c.listeners[username] = []chan *broadcastResult{resultChan}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *broadcaster) BroadcastAndRemove(username string, result *broadcastResult) {
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
|
|
val, ok := c.listeners[username]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, channel := range val {
|
|
|
|
go func(channel chan *broadcastResult) {
|
|
|
|
channel <- result
|
|
|
|
close(channel)
|
|
|
|
}(channel)
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(c.listeners, username)
|
|
|
|
}
|
|
|
|
|
2023-12-22 06:27:59 +05:30
|
|
|
// https://help.minecraft.net/hc/en-us/articles/4408950195341#h_01GE5JX1Z0CZ833A7S54Y195KV
|
|
|
|
var allowedUsernamesRegex = regexp.MustCompile(`(?i)^[0-9a-z_]{3,16}$`)
|
2019-11-21 04:03:13 +05:30
|
|
|
|
2020-01-03 03:21:57 +05:30
|
|
|
type UUIDsProvider interface {
|
2019-11-21 04:03:13 +05:30
|
|
|
GetUuid(username string) (*mojang.ProfileInfo, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TexturesProvider interface {
|
|
|
|
GetTextures(uuid string) (*mojang.SignedTexturesResponse, error)
|
|
|
|
}
|
|
|
|
|
2020-02-08 15:58:10 +05:30
|
|
|
type Emitter interface {
|
2020-04-20 16:59:33 +05:30
|
|
|
dispatcher.Emitter
|
2020-02-08 15:58:10 +05:30
|
|
|
}
|
|
|
|
|
2019-11-21 04:03:13 +05:30
|
|
|
type Provider struct {
|
2020-02-08 15:58:10 +05:30
|
|
|
Emitter
|
2020-01-03 03:21:57 +05:30
|
|
|
UUIDsProvider
|
2019-11-21 04:03:13 +05:30
|
|
|
TexturesProvider
|
|
|
|
Storage
|
|
|
|
|
|
|
|
onFirstCall sync.Once
|
|
|
|
*broadcaster
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Provider) GetForUsername(username string) (*mojang.SignedTexturesResponse, error) {
|
|
|
|
ctx.onFirstCall.Do(func() {
|
|
|
|
ctx.broadcaster = createBroadcaster()
|
|
|
|
})
|
|
|
|
|
|
|
|
if !allowedUsernamesRegex.MatchString(username) {
|
2021-03-03 04:54:31 +05:30
|
|
|
return nil, nil
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
username = strings.ToLower(username)
|
2020-03-29 19:47:11 +05:30
|
|
|
ctx.Emit("mojang_textures:call", username)
|
2019-11-21 04:03:13 +05:30
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
uuid, found, err := ctx.getUuidFromCache(username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if found && uuid == "" {
|
2019-11-21 04:03:13 +05:30
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if uuid != "" {
|
2020-03-29 19:47:11 +05:30
|
|
|
textures, err := ctx.getTexturesFromCache(uuid)
|
2020-04-28 20:27:51 +05:30
|
|
|
if err == nil && textures != nil {
|
2019-11-21 04:03:13 +05:30
|
|
|
return textures, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resultChan := make(chan *broadcastResult)
|
|
|
|
isFirstListener := ctx.broadcaster.AddListener(username, resultChan)
|
|
|
|
if isFirstListener {
|
|
|
|
go ctx.getResultAndBroadcast(username, uuid)
|
|
|
|
} else {
|
2020-03-29 19:47:11 +05:30
|
|
|
ctx.Emit("mojang_textures:already_processing", username)
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
result := <-resultChan
|
|
|
|
|
|
|
|
return result.textures, result.error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Provider) getResultAndBroadcast(username string, uuid string) {
|
2020-03-29 19:47:11 +05:30
|
|
|
ctx.Emit("mojang_textures:before_result", username, uuid)
|
2019-11-21 04:03:13 +05:30
|
|
|
result := ctx.getResult(username, uuid)
|
2020-03-29 19:47:11 +05:30
|
|
|
ctx.Emit("mojang_textures:after_result", username, result.textures, result.error)
|
2020-04-04 02:58:50 +05:30
|
|
|
|
|
|
|
ctx.broadcaster.BroadcastAndRemove(username, result)
|
2019-11-21 04:03:13 +05:30
|
|
|
}
|
|
|
|
|
2021-02-08 01:49:01 +05:30
|
|
|
func (ctx *Provider) getResult(username string, cachedUuid string) *broadcastResult {
|
|
|
|
uuid := cachedUuid
|
2019-11-21 04:03:13 +05:30
|
|
|
if uuid == "" {
|
2020-03-29 19:47:11 +05:30
|
|
|
profile, err := ctx.getUuid(username)
|
2019-11-21 04:03:13 +05:30
|
|
|
if err != nil {
|
|
|
|
return &broadcastResult{nil, err}
|
|
|
|
}
|
|
|
|
|
|
|
|
uuid = ""
|
|
|
|
if profile != nil {
|
|
|
|
uuid = profile.Id
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = ctx.Storage.StoreUuid(username, uuid)
|
|
|
|
|
|
|
|
if uuid == "" {
|
|
|
|
return &broadcastResult{nil, nil}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 19:47:11 +05:30
|
|
|
textures, err := ctx.getTextures(uuid)
|
2019-11-21 04:03:13 +05:30
|
|
|
if err != nil {
|
2021-02-08 01:49:01 +05:30
|
|
|
// Previously cached UUIDs may disappear
|
|
|
|
// In this case we must invalidate UUID cache for given username
|
|
|
|
if _, ok := err.(*mojang.EmptyResponse); ok && cachedUuid != "" {
|
|
|
|
return ctx.getResult(username, "")
|
|
|
|
}
|
|
|
|
|
2019-11-21 04:03:13 +05:30
|
|
|
return &broadcastResult{nil, err}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mojang can respond with an error, but it will still count as a hit,
|
|
|
|
// therefore store the result even if textures is nil to prevent 429 error
|
|
|
|
ctx.Storage.StoreTextures(uuid, textures)
|
|
|
|
|
|
|
|
return &broadcastResult{textures, nil}
|
|
|
|
}
|
2020-03-29 19:47:11 +05:30
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
func (ctx *Provider) getUuidFromCache(username string) (string, bool, error) {
|
2020-03-29 19:47:11 +05:30
|
|
|
ctx.Emit("mojang_textures:usernames:before_cache", username)
|
2020-04-28 20:27:51 +05:30
|
|
|
uuid, found, err := ctx.Storage.GetUuid(username)
|
|
|
|
ctx.Emit("mojang_textures:usernames:after_cache", username, uuid, found, err)
|
2020-03-29 19:47:11 +05:30
|
|
|
|
2020-04-28 20:27:51 +05:30
|
|
|
return uuid, found, err
|
2020-03-29 19:47:11 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Provider) getTexturesFromCache(uuid string) (*mojang.SignedTexturesResponse, error) {
|
|
|
|
ctx.Emit("mojang_textures:textures:before_cache", uuid)
|
|
|
|
textures, err := ctx.Storage.GetTextures(uuid)
|
|
|
|
ctx.Emit("mojang_textures:textures:after_cache", uuid, textures, err)
|
|
|
|
|
|
|
|
return textures, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Provider) getUuid(username string) (*mojang.ProfileInfo, error) {
|
|
|
|
ctx.Emit("mojang_textures:usernames:before_call", username)
|
|
|
|
profile, err := ctx.UUIDsProvider.GetUuid(username)
|
|
|
|
ctx.Emit("mojang_textures:usernames:after_call", username, profile, err)
|
|
|
|
|
|
|
|
return profile, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctx *Provider) getTextures(uuid string) (*mojang.SignedTexturesResponse, error) {
|
|
|
|
ctx.Emit("mojang_textures:textures:before_call", uuid)
|
|
|
|
textures, err := ctx.TexturesProvider.GetTextures(uuid)
|
|
|
|
ctx.Emit("mojang_textures:textures:after_call", uuid, textures, err)
|
|
|
|
|
|
|
|
return textures, err
|
|
|
|
}
|