-
Notifications
You must be signed in to change notification settings - Fork 25
/
cipher.go
55 lines (43 loc) · 1.22 KB
/
cipher.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
package youtube
import (
"fmt"
"github.com/lithdew/bytesutil"
"github.com/lithdew/youtube/sig"
"github.com/valyala/fasthttp"
"github.com/valyala/fastjson"
)
type Cipher struct {
URL string `json:"url"`
Signature string `json:"s"`
SignaturePolicy string `json:"sp"`
}
func ParseCipherJSON(v *fastjson.Value) Cipher {
a := fasthttp.AcquireArgs()
defer fasthttp.ReleaseArgs(a)
a.ParseBytes(v.GetStringBytes())
return Cipher{
URL: string(a.Peek("url")),
Signature: string(a.Peek("s")),
SignaturePolicy: string(a.Peek("sp")),
}
}
func (c Cipher) DecodeURL(script string) (string, error) {
factory, err := sig.LookupCipherFactory(script)
if err != nil {
return "", fmt.Errorf("failed to lookup cipher factory in script: %w", err)
}
cipher, err := sig.LookupCipher(factory, script)
if err != nil {
return "", fmt.Errorf("failed to lookup cipher steps in script: %w", err)
}
decoded := cipher.Decode(c.Signature)
uri := fasthttp.AcquireURI()
defer fasthttp.ReleaseURI(uri)
uri.Parse(nil, bytesutil.Slice(c.URL))
if c.SignaturePolicy == "" {
uri.QueryArgs().Add("signature", decoded)
} else {
uri.QueryArgs().Add(c.SignaturePolicy, decoded)
}
return uri.String(), nil
}