Repository navigation
Expand file tree
/
Copy pathfetch.go
More file actions
318 lines (303 loc) · 9.73 KB
/
Copy pathfetch.go
File metadata and controls
318 lines (303 loc) · 9.73 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
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
package lfm_api
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/cookiejar"
"net/url"
"path"
"regexp"
"strings"
"sync"
"time"
"golang.org/x/net/html"
"golang.org/x/net/publicsuffix"
)
const maxResponseBytes = 2 << 20
// A client keeps verification cookies across polls. Serialize handshakes so two
// concurrent callers cannot overwrite each other's challenge-start cookie.
type lastFMClient struct {
base string
http *http.Client
mu sync.Mutex
}
func newLastFMClient(base string) *lastFMClient {
jar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
origin, _ := url.Parse(base)
return &lastFMClient{base: base, http: &http.Client{
Jar: jar, Timeout: 10 * time.Second,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if len(via) >= 5 || req.URL.Scheme != origin.Scheme || req.URL.Host != origin.Host {
return fmt.Errorf("last.fm returned an unexpected redirect")
}
if len(via) > 0 && strings.Contains(via[0].URL.Path, "/partial/recenttracks") && req.URL.Path != via[0].URL.Path {
return fmt.Errorf("last.fm recent-tracks request redirected to %s", req.URL.Path)
}
return nil
},
}}
}
func (c *lastFMClient) request(ctx context.Context, method, target string, payload []byte) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, method, target, bytes.NewReader(payload))
if err != nil {
return nil, err
}
req.Header.Set("Accept-Language", "en")
if payload != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := c.http.Do(req)
if err != nil {
return nil, fmt.Errorf("fetch last.fm: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("last.fm returned status %d", resp.StatusCode)
}
body, err := io.ReadAll(io.LimitReader(resp.Body, maxResponseBytes+1))
if err != nil {
return nil, fmt.Errorf("read last.fm response: %w", err)
}
if len(body) > maxResponseBytes {
return nil, fmt.Errorf("last.fm response exceeds size limit")
}
return body, nil
}
func (c *lastFMClient) recentTracks(username string) (string, error) {
c.mu.Lock()
defer c.mu.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
target := c.base + "user/" + url.PathEscape(username) + "/partial/recenttracks?ajax=1"
body, err := c.request(ctx, http.MethodGet, target, nil)
if err != nil {
return "", err
}
if isChallenge(body) {
if err := c.verify(ctx, body); err != nil {
return "", err
}
body, err = c.request(ctx, http.MethodGet, target, nil)
if err != nil {
return "", err
}
if isChallenge(body) {
return "", fmt.Errorf("last.fm challenge persisted after verification")
}
}
if err := validateRecentTracks(body); err != nil {
return "", err
}
return string(body), nil
}
func isChallenge(body []byte) bool {
return bytes.Contains(body, []byte("/_fs-ch-")) || bytes.Contains(body, []byte("Client Challenge"))
}
var scriptAssignment = regexp.MustCompile(`\bsrc\s*=\s*["']([^"']+)["']`)
var challengeScriptPath = regexp.MustCompile(`^/_fs-ch-[A-Za-z0-9_-]+/script\.js$`)
var initCall = regexp.MustCompile(`(?:^|[;\n])\s*(?:window\.)?init\s*\(`)
type powData struct {
Base string `json:"base"`
Hash string `json:"hash"`
HMAC string `json:"hmac"`
Expires string `json:"expires"`
}
type challenge struct {
Type string `json:"ty"`
Data powData `json:"data"`
}
type challengeAnswer struct {
Type string `json:"ty"`
Base string `json:"base"`
Answer string `json:"answer"`
HMAC string `json:"hmac"`
Expires string `json:"expires"`
}
// Parse only the JSON arguments of the bootstrap call; never execute downloaded
// JavaScript. The currently supported puzzle is SHA256(base + two base62 chars).
func parseChallenge(script []byte) ([]challenge, string, string, error) {
matches := initCall.FindAllIndex(script, -1)
if len(matches) == 0 {
return nil, "", "", fmt.Errorf("last.fm challenge bootstrap not recognized")
}
rest := bytes.TrimSpace(script[matches[len(matches)-1][1]:])
var tasks []challenge
var token, prefix string
for i, dst := range []interface{}{&tasks, &token, &prefix} {
if i > 0 {
if len(rest) == 0 || rest[0] != ',' {
return nil, "", "", fmt.Errorf("invalid last.fm challenge arguments")
}
rest = bytes.TrimSpace(rest[1:])
}
dec := json.NewDecoder(bytes.NewReader(rest))
if err := dec.Decode(dst); err != nil {
return nil, "", "", fmt.Errorf("decode last.fm challenge: %w", err)
}
rest = bytes.TrimSpace(rest[dec.InputOffset():])
}
if token == "" || len(tasks) == 0 || len(tasks) > 4 {
return nil, "", "", fmt.Errorf("invalid last.fm challenge parameters")
}
return tasks, token, prefix, nil
}
func solvePOW(ctx context.Context, task challenge) (challengeAnswer, error) {
if task.Type != "pow" {
return challengeAnswer{}, fmt.Errorf("unsupported last.fm challenge type %q", task.Type)
}
d := task.Data
hash, err := hex.DecodeString(d.Hash)
if err != nil || len(hash) != sha256.Size || d.Base == "" || len(d.Base) > 256 || d.HMAC == "" || len(d.HMAC) > 256 || d.Expires == "" {
return challengeAnswer{}, fmt.Errorf("invalid last.fm proof-of-work parameters")
}
const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for _, a := range alphabet {
if err := ctx.Err(); err != nil {
return challengeAnswer{}, err
}
for _, b := range alphabet {
answer := string([]rune{a, b})
sum := sha256.Sum256([]byte(d.Base + answer))
if bytes.Equal(sum[:], hash) {
return challengeAnswer{"pow", d.Base, answer, d.HMAC, d.Expires}, nil
}
}
}
return challengeAnswer{}, fmt.Errorf("unsupported last.fm proof-of-work puzzle")
}
func (c *lastFMClient) verify(ctx context.Context, page []byte) error {
// Both external script tags and Last.fm's inline script.src assignment occur.
var candidates []string
z := html.NewTokenizer(bytes.NewReader(page))
for z.Next() != html.ErrorToken {
t := z.Token()
if t.Type == html.StartTagToken && t.Data == "script" {
candidates = append(candidates, searchHTMLAttribute(t.Attr, "src"))
}
if t.Type == html.TextToken {
for _, m := range scriptAssignment.FindAllStringSubmatch(t.Data, -1) {
candidates = append(candidates, m[1])
}
}
}
var scriptURL *url.URL
for _, candidate := range candidates {
u, err := url.Parse(candidate)
if err == nil && u.Scheme == "" && u.Host == "" && u.User == nil && u.Fragment == "" && challengeScriptPath.MatchString(u.Path) {
scriptURL = u
break
}
}
if scriptURL == nil {
return fmt.Errorf("last.fm challenge script not recognized")
}
origin, _ := url.Parse(c.base)
script, err := c.request(ctx, http.MethodGet, origin.ResolveReference(scriptURL).String(), nil)
if err != nil {
return err
}
tasks, token, prefix, err := parseChallenge(script)
if err != nil {
return err
}
if prefix != path.Dir(scriptURL.Path) {
return fmt.Errorf("unexpected last.fm challenge endpoint")
}
endpoint := origin.ResolveReference(&url.URL{Path: prefix + "/fst-post-back"}).String()
for round := 0; round < 2; round++ {
answers := make([]challengeAnswer, 0, len(tasks))
if len(tasks) == 0 || len(tasks) > 4 || token == "" {
return fmt.Errorf("invalid last.fm challenge parameters")
}
for _, task := range tasks {
answer, err := solvePOW(ctx, task)
if err != nil {
return err
}
answers = append(answers, answer)
}
payload, err := json.Marshal(struct {
Token string `json:"token"`
Data []challengeAnswer `json:"data"`
}{token, answers})
if err != nil {
return err
}
body, err := c.request(ctx, http.MethodPost, endpoint, payload)
if err != nil {
return err
}
var result struct {
Status string `json:"status"`
Challenges []challenge `json:"ch"`
Token string `json:"tok"`
}
if err := json.Unmarshal(body, &result); err != nil {
return fmt.Errorf("decode last.fm verification response: %w", err)
}
if result.Status == "success" {
return nil
}
if len(result.Challenges) == 0 {
return fmt.Errorf("last.fm challenge verification rejected")
}
tasks, token = result.Challenges, result.Token
}
return fmt.Errorf("last.fm challenge retry limit exceeded")
}
func validateRecentTracks(body []byte) error {
// Require explicit closing tags too: html.Parse repairs truncated documents.
z := html.NewTokenizer(bytes.NewReader(body))
var closedTable, closedBody bool
for z.Next() != html.ErrorToken {
t := z.Token()
if t.Type == html.EndTagToken {
closedTable = closedTable || t.Data == "table"
closedBody = closedBody || t.Data == "tbody"
}
}
if !closedTable || !closedBody {
return fmt.Errorf("incomplete last.fm recent-tracks response")
}
doc, err := html.Parse(bytes.NewReader(body))
if err != nil {
return fmt.Errorf("parse last.fm response: %w", err)
}
var table, tbody, fullPage bool
var walk func(*html.Node, bool)
walk = func(n *html.Node, inTable bool) {
// html.Parse inserts html/head/body nodes even for fragments. A title,
// however, identifies a full page rather than the requested partial.
if n.Type == html.ElementNode && n.Data == "title" && n.Parent != nil && n.Parent.Data == "head" {
fullPage = true
}
if n.Type == html.ElementNode && n.Data == "table" && hasClass(n, "chartlist") {
table = true
inTable = true
}
if inTable && n.Type == html.ElementNode && n.Data == "tbody" {
tbody = true
}
for child := n.FirstChild; child != nil; child = child.NextSibling {
walk(child, inTable)
}
}
walk(doc, false)
if fullPage || !table || !tbody {
return fmt.Errorf("unexpected last.fm recent-tracks response")
}
return nil
}
func hasClass(n *html.Node, class string) bool {
for _, value := range strings.Fields(searchHTMLAttribute(n.Attr, "class")) {
if value == class {
return true
}
}
return false
}