mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Initial commit
This commit is contained in:
commit
5f1d811c66
23 changed files with 3612 additions and 0 deletions
57
utils.go
Normal file
57
utils.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package crunchyroll
|
||||
|
||||
import (
|
||||
"crypto/cipher"
|
||||
"encoding/json"
|
||||
"github.com/grafov/m3u8"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func decodeMapToStruct(m interface{}, s interface{}) error {
|
||||
jsonBody, err := json.Marshal(m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.Unmarshal(jsonBody, s)
|
||||
}
|
||||
|
||||
// https://github.com/oopsguy/m3u8/blob/4150e93ec8f4f8718875a02973f5d792648ecb97/tool/crypt.go#L25
|
||||
func decryptSegment(client *http.Client, segment *m3u8.MediaSegment, block cipher.Block, iv []byte) ([]byte, error) {
|
||||
resp, err := client.Get(segment.URI)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
raw, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
blockMode := cipher.NewCBCDecrypter(block, iv[:block.BlockSize()])
|
||||
decrypted := make([]byte, len(raw))
|
||||
blockMode.CryptBlocks(decrypted, raw)
|
||||
raw = pkcs5UnPadding(decrypted)
|
||||
|
||||
return raw, nil
|
||||
}
|
||||
|
||||
// https://github.com/oopsguy/m3u8/blob/4150e93ec8f4f8718875a02973f5d792648ecb97/tool/crypt.go#L47
|
||||
func pkcs5UnPadding(origData []byte) []byte {
|
||||
length := len(origData)
|
||||
unPadding := int(origData[length-1])
|
||||
return origData[:(length - unPadding)]
|
||||
}
|
||||
|
||||
func regexGroups(parsed [][]string, subexpNames ...string) map[string]string {
|
||||
groups := map[string]string{}
|
||||
for _, match := range parsed {
|
||||
for i, content := range match {
|
||||
if subexpName := subexpNames[i]; subexpName != "" {
|
||||
groups[subexpName] = content
|
||||
}
|
||||
}
|
||||
}
|
||||
return groups
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue