-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtoken.go
More file actions
78 lines (65 loc) · 1.46 KB
/
token.go
File metadata and controls
78 lines (65 loc) · 1.46 KB
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
package main
import (
"fmt"
"github.com/hashicorp/golang-lru/v2/expirable"
"io"
"net/http"
"os"
"regexp"
"time"
)
var cache = expirable.NewLRU[string, string](1, nil, time.Hour*24)
func GetMusicToken(instance *WrapperInstance) (string, error) {
token, err := os.ReadFile(fmt.Sprintf("data/wrapper/rootfs/data/instances/%s/MUSIC_TOKEN", instance.Id))
if err != nil {
return "", err
}
return string(token), nil
}
func GetToken() (string, error) {
if token, ok := cache.Get("token"); ok {
return token, nil
}
req, err := http.NewRequest("GET", "https://music.apple.com", nil)
if err != nil {
return "", err
}
resp, err := GetHttpClient().Do(req)
if err != nil {
return "", err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
panic(err)
}
}(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
regex := regexp.MustCompile(`/assets/index~[^/]+\.js`)
indexJsUri := regex.FindString(string(body))
req, err = http.NewRequest("GET", "https://music.apple.com"+indexJsUri, nil)
if err != nil {
return "", err
}
resp, err = GetHttpClient().Do(req)
if err != nil {
return "", err
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
panic(err)
}
}(resp.Body)
body, err = io.ReadAll(resp.Body)
if err != nil {
return "", err
}
regex = regexp.MustCompile(`eyJh([^"]*)`)
token := regex.FindString(string(body))
cache.Add("token", token)
return token, nil
}