forked from minotar/imgd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skin.go
487 lines (432 loc) · 15.9 KB
/
skin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package main
import (
"bytes"
"errors"
"image/png"
"net/url"
"strings"
"time"
"github.com/minotar/imgd/storage"
"github.com/minotar/minecraft"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sync/singleflight"
)
// Todo: Config for these options?
const (
day = 24 * time.Hour
usernameTTL = 60 * day
usernameUnknownTTL = 14 * day
usernameRateLimitTTL = 2 * time.Hour
usernameErrorTTL = 1 * time.Hour
uuidTTL = 60 * day
uuidFreshTTL = 2 * time.Hour
uuidUnknownTTL = 7 * day
uuidRateLimitTTL = 1 * time.Hour
uuidErrorTTL = 30 * time.Minute
skinTTL = 1 * time.Hour
skinErrorTTL = 15 * time.Minute
metaUnknownCode = "204"
metaRateLimitCode = "429"
metaErrorCode = "0"
textureURL = "http://textures.minecraft.net"
)
var (
sfUsername singleflight.Group
sfUUID singleflight.Group
sfSkinPath singleflight.Group
)
type textures struct {
SkinPath string
//CapePath string
}
type mcUser struct {
minecraft.User
Textures textures
Timestamp time.Time
}
func getUUID(username string) (string, error) {
// Check the Username cache for whether we have a matching Username->UUID
retrieveTimer := prometheus.NewTimer(cacheDuration.WithLabelValues("cacheUUID", "retrieve"))
uuid, err := storage.RetrieveKV(cache["cacheUUID"], username)
retrieveTimer.ObserveDuration()
if err == nil && uuid != "" {
// UUID would be set to one of the meta Codes if an API error occured
log.Debugf("Found Username in cacheUUID (%s): %s", username, uuid)
stats.CacheUUID("hit")
} else if err != nil && err != storage.ErrNotFound {
// An error with the cache is considered fatal
log.Errorf("Failed Retrieve from cacheUUID (%s): %s", username, err.Error())
stats.CacheUUID("error")
return "", err
} else {
// If the UUID is still empty/not in cache, then we better request from the API :(
log.Debugf("Did not find Username in cacheUUID (%s)", username)
stats.CacheUUID("miss")
ttl := usernameTTL
stats.APIRequested("GetAPIProfile")
apiTimer := prometheus.NewTimer(getDuration.WithLabelValues("GetAPIProfile"))
apiProfile, err := mcClient.GetAPIProfile(username)
apiTimer.ObserveDuration()
uuid = apiProfile.UUID
if err != nil {
var code string
switch errMsg := err.Error(); errMsg {
// Based on returned error, we save a "meta" UUID to cache["cacheUUID"] for differing TTLs
case "unable to GetAPIProfile: user not found":
code = metaUnknownCode
ttl = usernameUnknownTTL
log.Infof("Failed UUID lookup (%s): %s", username, errMsg)
// Previously named "UnknownUsername"
stats.Errored("APIProfileUnknown")
case "unable to GetAPIProfile: rate limited":
code = metaRateLimitCode
ttl = usernameRateLimitTTL
log.Noticef("Failed UUID lookup (%s): %s", username, errMsg)
// Previously named "LookupUUIDRateLimit"
stats.Errored("APIProfileRateLimit")
default:
code = metaErrorCode
ttl = usernameErrorTTL
log.Warningf("Failed UUID lookup: (%s): %s", username, errMsg)
// Previously named "LookupUUID"
stats.Errored("APIProfileGeneric")
}
uuid = code
}
// If we make an API Request, we need to cache the response (pass or fail)
insertTimer := prometheus.NewTimer(cacheDuration.WithLabelValues("cacheUUID", "insert"))
err = storage.InsertKV(cache["cacheUUID"], username, uuid, ttl)
insertTimer.ObserveDuration()
if err != nil {
stats.CacheUUID("error")
log.Errorf("Failed Insert to cacheUUID (%s:%s): %s", username, uuid, err.Error())
}
}
// Before returning we strip the "meta" UUID (possibly from cache["cacheUUID"])
switch uuid {
case metaUnknownCode:
return "", errors.New("user not found")
case metaRateLimitCode:
return "", errors.New("rate limited")
case metaErrorCode:
return "", errors.New("UUID lookup failed")
case "":
// We shouldn't trigger here
return "", errors.New("empty UUID")
default:
return uuid, nil
}
}
// mcUser.Minecraft.UUID must be set
func (u *mcUser) pullSessionProfile() error {
if u.UUID == "" {
return errors.New("pullSessionProfile needs a UUID")
}
stats.APIRequested("GetSessionProfile")
spTimer := prometheus.NewTimer(getDuration.WithLabelValues("GetSessionProfile"))
sessionProfile, err := mcClient.GetSessionProfile(u.UUID)
spTimer.ObserveDuration()
if err != nil {
stats.Errored("SessionProfileGeneric")
return err
}
username := strings.ToLower(sessionProfile.Username)
if username == "" {
return errors.New("pullSessionProfile unable to grab Username")
}
u.Username = username
profileTextureProperty, err := minecraft.DecodeTextureProperty(sessionProfile)
if err != nil {
return err
}
skinURL, err := url.Parse(profileTextureProperty.Textures.Skin.URL)
if err != nil {
return errors.New("pullSessionProfile URL parsing failed: " + err.Error())
}
u.Textures.SkinPath = skinURL.Path
return nil
}
// When we have no useful data to return, we should error
func getUser(uuid string) (*mcUser, error) {
now := time.Now()
user := &mcUser{
User: minecraft.User{UUID: uuid},
}
retrieveTimer := prometheus.NewTimer(cacheDuration.WithLabelValues("cacheUserData", "retrieve"))
err := storage.RetrieveGob(cache["cacheUserData"], uuid, user)
retrieveTimer.ObserveDuration()
if err == nil && user.Textures.SkinPath != "" {
// SkinPath can be used to store meta Codes
// We must have now (at least partially) populated our User struct
// We will manually verify the time is Fresh, otherwise re-request
log.Debugf("Found UUID in cacheUserData (%s): %+v", uuid, user)
stats.CacheUserData("hit")
staleAt := user.Timestamp.Add(uuidFreshTTL)
if now.Before(staleAt) {
// If the Timestamp was "Zero", then this would never evaulate (and we only set Timestamp for good results)
// Todo: Potentially, we could perform a goroutine to execute the pull for a level of grace?
// Todo: Log time it's fresh from
log.Debugf("Data from cacheUserData was fresh (%s)", uuid)
stats.CacheUserData("fresh")
return user, nil
}
log.Debugf("Data from cacheUserData was stale, will try and freshen (%s)", uuid)
stats.CacheUserData("stale")
} else if err != nil && err != storage.ErrNotFound {
// An error with the cache is considered fatal
log.Errorf("Failed Retrieve from cacheUserData (%s): %s", uuid, err.Error())
stats.CacheUserData("error")
return &mcUser{}, err
} else {
// In a separate block to ensure we don't double count stale
log.Debugf("Did not find UUID in cacheUserData (%s)", uuid)
stats.CacheUserData("miss")
}
// EITHER: cache["cacheUserData"] hit but has old/stale data, it was actually a meta Code, or it was a miss
// Detect stale/fallback data (we only set Timestamp when valid)
canFallback := !user.Timestamp.IsZero()
if user.Textures.SkinPath == "" /* no entry in cache */ || canFallback {
ttl := uuidTTL
// Stats are counted within pullSessionProfile()
err = user.pullSessionProfile()
if err != nil /* API failed */ && canFallback {
log.Warningf("Stale SessionProfile used (%s): %s", uuid, err.Error())
stats.Errored("SessionProfileStale")
return user, nil
} else if err != nil {
var code string
switch errMsg := err.Error(); errMsg {
// Based on returned error, we save a "meta" SkinPath to cache["cacheUserData"] for differing TTLs
case "unable to GetSessionProfile: user not found":
code = metaUnknownCode
ttl = uuidUnknownTTL
log.Infof("Failed SessionProfile lookup (%s): %s", uuid, errMsg)
// Previously named "UnknownUsername"
stats.Errored("SessionProfileUnknown")
case "unable to GetSessionProfile: rate limited":
code = metaRateLimitCode
ttl = uuidRateLimitTTL
log.Noticef("Failed SessionProfile lookup (%s): %s", uuid, errMsg)
// Previously named "LookupUUIDRateLimit"
stats.Errored("SessionProfileRateLimit")
default:
code = metaErrorCode
ttl = uuidErrorTTL
log.Warningf("Failed SessionProfile lookup: (%s): %s", uuid, errMsg)
// Previously named "LookupUUID"
stats.Errored("SessionProfileGeneric")
}
user = &mcUser{
User: minecraft.User{UUID: uuid},
Textures: textures{SkinPath: code},
}
} else if user.Username != "" {
// We only set the Timestamp (used by the fresh check) if data is good.
user.Timestamp = now
// If we got (good) fresh data, we should update the cache["cacheUUID"] as well
// We do not need to block on this as it is not linked to the FlightGroup
go func() {
insertTimer := prometheus.NewTimer(cacheDuration.WithLabelValues("cacheUUID", "insert"))
err = storage.InsertKV(cache["cacheUUID"], user.Username, uuid, usernameTTL)
insertTimer.ObserveDuration()
if err != nil {
stats.CacheUUID("error")
log.Errorf("Failed Insert to cacheUUID (%s:%s)", user.Username, uuid)
}
}()
}
insertTimer := prometheus.NewTimer(cacheDuration.WithLabelValues("cacheUserData", "insert"))
err = storage.InsertGob(cache["cacheUserData"], uuid, user, ttl)
insertTimer.ObserveDuration()
if err != nil {
stats.CacheUserData("error")
log.Errorf("Failed Insert to cacheUserData (%s:%v)", uuid, user)
}
}
switch user.Textures.SkinPath {
case metaUnknownCode:
user.Textures.SkinPath = ""
return user, errors.New("user not found")
case metaRateLimitCode:
user.Textures.SkinPath = ""
return user, errors.New("rate limited")
case metaErrorCode:
user.Textures.SkinPath = ""
return user, errors.New("SessionProfile lookup failed")
case "":
// We shouldn't trigger here
return user, errors.New("no SkinPath")
default:
return user, nil
}
}
func getSkin(skinPath string) (minecraft.Skin, error) {
retrieveTimer := prometheus.NewTimer(cacheDuration.WithLabelValues("cacheSkin", "retrieve"))
skin, err := RetrieveSkin(cache["cacheSkin"], skinPath)
retrieveTimer.ObserveDuration()
if err == nil {
log.Debugf("Found SkinPath in cacheSkin (%s): %s", skinPath, skin.Hash)
stats.CacheSkin("hit")
return skin, nil
} else if err != nil && err != storage.ErrNotFound {
// An error with the cache is considered fatal
log.Errorf("Failed Retrieve from cacheSkin (%s): %s", skinPath, err.Error())
stats.CacheSkin("error")
return skin, err
}
log.Debugf("Did not find SkinPath in cacheSkin (%s)", skinPath)
stats.CacheSkin("miss")
// The cache["cacheSkin"] is not optimized for expiry based updates (eg. groupcache)
// We use cache["cacheSkinTransient"] for temporary results (eg. errors)
retrieveTimer = prometheus.NewTimer(cacheDuration.WithLabelValues("cacheSkinTransient", "retrieve"))
state, err := storage.RetrieveKV(cache["cacheSkinTransient"], skinPath)
retrieveTimer.ObserveDuration()
if err == nil {
// A hit means the Skin errored on last fetch
log.Warningf("Found SkinPath in cacheSkinTransient: " + state)
stats.CacheSkinTransient("hit")
return skin, errors.New("getSkin previously failed: " + state)
}
stats.CacheSkinTransient("miss")
skin.Mc = mcClient
skin.URL = textureURL + skinPath
stats.APIRequested("TextureFetch")
texTimer := prometheus.NewTimer(getDuration.WithLabelValues("TextureFetch"))
err = skin.Fetch()
texTimer.ObserveDuration()
if err != nil {
errMsg := err.Error()
log.Errorf("Failed TextureFetch (%s): %s", skinPath, errMsg)
stats.Errored("TextureFetch")
insertTimer := prometheus.NewTimer(cacheDuration.WithLabelValues("cacheSkinTransient", "insert"))
err = storage.InsertKV(cache["cacheSkinTransient"], skinPath, errMsg, skinErrorTTL)
insertTimer.ObserveDuration()
if err != nil {
stats.CacheSkinTransient("error")
log.Errorf("Failed Insert to cacheSkinTransient (%s)", skinPath)
}
return skin, errors.New("getSkin failed: " + errMsg)
}
insertTimer := prometheus.NewTimer(cacheDuration.WithLabelValues("cacheSkin", "insert"))
err = InsertSkin(cache["cacheSkin"], skinPath, skin)
insertTimer.ObserveDuration()
if err != nil {
stats.CacheSkin("error")
log.Errorf("Failed Insert to cacheSkin (%s)", skinPath)
}
return skin, nil
}
func InsertSkin(cache storage.Storage, key string, skin minecraft.Skin) error {
skinBuf := new(bytes.Buffer)
_ = png.Encode(skinBuf, skin.Image)
return cache.Insert(key, skinBuf.Bytes(), skinTTL)
}
func RetrieveSkin(cache storage.Storage, key string) (minecraft.Skin, error) {
skin := &minecraft.Skin{}
respBytes, err := cache.Retrieve(key)
if err != nil {
return minecraft.Skin{}, err
}
imgBuf := bytes.NewReader(respBytes)
skinErr := skin.Decode(imgBuf)
if skinErr != nil {
return minecraft.Skin{}, skinErr
}
return *skin, nil
}
func wrapUUIDLookup(uuid string) (*mcUser, error) {
// Singleflight ensures that lookups for same uuid will block
// crucial as the lookup could come from multiple places (not otherwise blocking)
user, err, _ := sfUUID.Do(uuid, func() (interface{}, error) {
// Todo: Could stat here and above to track usage
return getUser(uuid)
})
return user.(*mcUser), err
}
func wrapUsernameLookup(username string) (*mcUser, error) {
// Singleflight ensures that lookups for same username will block
// and then all return same data on completion
user, err, _ := sfUsername.Do(username, func() (interface{}, error) {
// Todo: Could stat here and above to track usage
user := &mcUser{}
var err error
// If the Username doesn't match, we will run through this twice (but at max twice)
for i := 0; i < 2; i++ {
uuid, err := getUUID(username)
if err != nil {
return user, err
}
// calls getUser(uuid) in a Singleflight
user, err = wrapUUIDLookup(uuid)
// Check that all errors returned are fatal?
if err != nil {
return user, err
}
// the cache["cacheUUID"] might return old data where Username pointed to the wrong UUID
// We then realise after looking up the UUID and this detects that inconsistency
if username != user.Username && i == 0 /* first iteration */ {
log.Debugf("Cached Username did not match UUID (%s:%s:%s)", username, uuid, user.Username)
stats.Errored("APIProfileStale")
// Re-setting the cached value to "" and re-running this function seems like the best method
// We could otherwise try to manually GetAPIProfile, but more complications
insertTimer := prometheus.NewTimer(cacheDuration.WithLabelValues("cacheUUID", "insert"))
err = storage.InsertKV(cache["cacheUUID"], username, "", uuidErrorTTL)
insertTimer.ObserveDuration()
if err != nil {
stats.CacheUUID("error")
log.Errorf("Failed Insert to cacheUUID (%s:%s): %s", username, uuid, err.Error())
}
// Once the wrong data is cleared from the cache, we can re-run this function
continue
}
if user.Textures.SkinPath == "" {
return user, errors.New("no Texture Path")
}
// Unless we got mismatched Usernames, we do not need to re-run
break
}
return user, err
})
return user.(*mcUser), err
}
func fetchUsernameSkin(username string) *mcSkin {
username = strings.ToLower(username)
if username == "char" || username == "mhf_steve" /* MHF_Steve */ {
skin, _ := minecraft.FetchSkinForSteve()
return &mcSkin{Skin: skin}
}
// We wrap the cache and API lookups in Singleflight to reduce outbound
// requests, reduce rate-limit chance and maybe speed up some requests
user, err := wrapUsernameLookup(username)
if err != nil {
skin, _ := minecraft.FetchSkinForSteve()
return &mcSkin{Skin: skin}
}
skinPath := user.Textures.SkinPath
skin, err, _ := sfSkinPath.Do(skinPath, func() (interface{}, error) {
return getSkin(skinPath)
})
if err != nil {
skin, _ := minecraft.FetchSkinForSteve()
return &mcSkin{Skin: skin}
}
return &mcSkin{Processed: nil, Skin: skin.(minecraft.Skin)}
}
func fetchUUIDSkin(uuid string) *mcSkin {
// We wrap the cache and API lookups in Singleflight to reduce outbound
// requests, reduce rate-limit chance and maybe speed up some requests
user, err := wrapUUIDLookup(uuid)
if err != nil {
skin, _ := minecraft.FetchSkinForSteve()
return &mcSkin{Skin: skin}
}
skinPath := user.Textures.SkinPath
skin, err, _ := sfSkinPath.Do(skinPath, func() (interface{}, error) {
return getSkin(skinPath)
})
if err != nil {
skin, _ := minecraft.FetchSkinForSteve()
return &mcSkin{Skin: skin}
}
return &mcSkin{Processed: nil, Skin: skin.(minecraft.Skin)}
}