Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cipher #27

Merged
merged 6 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type PlayerResponseData struct {
ApproxDurationMs string `json:"approxDurationMs"`
AudioSampleRate string `json:"audioSampleRate"`
AudioChannels int `json:"audioChannels"`
Cipher string `json:"cipher"`
} `json:"formats"`
AdaptiveFormats []struct {
Itag int `json:"itag"`
Expand Down
217 changes: 217 additions & 0 deletions decipher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package youtube

import (
"fmt"
"io/ioutil"
"net/url"
"regexp"
"strconv"
"strings"
)

func (y *Youtube) parseDecipherOpsAndArgs() (operations []string, args []int, err error) {
/// try to get whole page
client, err := y.getHTTPClient()
if err != nil {
return nil, nil, fmt.Errorf("get http client error=%s", err)
}

if y.VideoID == "" {
return nil, nil, fmt.Errorf("video id is empty , err=%s", err)
}
embedUrl := fmt.Sprintf("https://youtube.com/embed/%s?hl=en", y.VideoID)

embeddedPageResp, err := client.Get(embedUrl)
if err != nil {
return nil, nil, err
}
defer embeddedPageResp.Body.Close()

if embeddedPageResp.StatusCode != 200 {
return nil, nil, err
}

embeddedPageBodyBytes, err := ioutil.ReadAll(embeddedPageResp.Body)
if err != nil {
return nil, nil, err
}
embeddedPage := string(embeddedPageBodyBytes)

playerConfigPattern := regexp.MustCompile("yt\\.setConfig\\({'PLAYER_CONFIG':(.*)}\\);")
playerConfig := playerConfigPattern.FindString(embeddedPage)

basejsPattern := regexp.MustCompile(`"js":"\\/s\\/player(.*)base\.js`)
//eg: "js":\"\/s\/player\/f676c671\/player_ias.vflset\/en_US\/base.js
escapedBasejsUrl := basejsPattern.FindString(playerConfig)
//eg: ["js", "\/s\/player\/f676c671\/player_ias.vflset\/en_US\/base.js]
arr := strings.Split(escapedBasejsUrl, ":\"")
basejsUrl := "https://youtube.com" + strings.ReplaceAll(arr[len(arr)-1], "\\", "")
basejsUrlResp, err := client.Get(basejsUrl)
if err != nil {
return nil, nil, err
}

defer basejsUrlResp.Body.Close()
if basejsUrlResp.StatusCode != 200 {
return nil, nil, err
}

basejsBodyBytes, err := ioutil.ReadAll(basejsUrlResp.Body)
if err != nil {
return nil, nil, err
}
basejs := string(basejsBodyBytes)

// regex to get name of decipher function
decipherFuncNamePattern := regexp.MustCompile(`(\w+)=function\(\w+\){(\w+)=(\w+)\.split\(\x22{2}\);.*?return\s+(\w+)\.join\(\x22{2}\)}`)

// Ft=function(a){a=a.split("");Et.vw(a,2);Et.Zm(a,4);Et.Zm(a,46);Et.vw(a,2);Et.Zm(a,34);Et.Zm(a,59);Et.cn(a,42);return a.join("")} => get Ft
arr = decipherFuncNamePattern.FindStringSubmatch(basejs)
funcName := arr[1]
decipherFuncBodyPattern := regexp.MustCompile(fmt.Sprintf(`[^h\.]%s=function\(\w+\)\{(.*?)\}`, funcName))

// eg: get a=a.split("");Et.vw(a,2);Et.Zm(a,4);Et.Zm(a,46);Et.vw(a,2);Et.Zm(a,34);Et.Zm(a,59);Et.cn(a,42);return a.join("")
arr = decipherFuncBodyPattern.FindStringSubmatch(basejs)
decipherFuncBody := arr[1]

// FuncName in Body => get Et
funcNameInBodyRegex := regexp.MustCompile(`(\w+).\w+\(\w+,\d+\);`)
arr = funcNameInBodyRegex.FindStringSubmatch(decipherFuncBody)
funcNameInBody := arr[1]
decipherDefBodyRegex := regexp.MustCompile(fmt.Sprintf(`var\s+%s=\{(\w+:function\(\w+(,\w+)?\)\{(.*?)\}),?\};`, funcNameInBody))
re := regexp.MustCompile(`\r?\n`)
basejs = re.ReplaceAllString(basejs, "")
arr1 := decipherDefBodyRegex.FindStringSubmatch(basejs)

// eg: vw:function(a,b){a.splice(0,b)},cn:function(a){a.reverse()},Zm:function(a,b){var c=a[0];a[0]=a[b%a.length];a[b%a.length]=c}
decipherDefBody := arr1[1]
// eq: [ a=a.split("") , Et.vw(a,2) , Et.Zm(a,4) , Et.Zm(a,46) , Et.vw(a,2) , Et.Zm(a,34), Et.Zm(a,59) , Et.cn(a,42) , return a.join("") ]
decipherFuncs := strings.Split(decipherFuncBody, ";")

var funcSeq []string
var funcArgs []int

for _, v := range decipherFuncs {
//calledFuncNameRegex \w+(?:.|\[)("?\w+(?:")?)\]?\(
//eg: Et.vw(a,2) => vw
calledFuncNameRegex, err := regexp.Compile(`\w+(?:.|\[)("?\w+(?:")?)\]?\(`)
if err != nil {
return nil, nil, err
}
arr := calledFuncNameRegex.FindStringSubmatch(v)
if len(arr) < 1 || arr[1] == "" {
continue
}
calledFuncName := arr[1]

// Et.vw(a,2) => [a, 2]
funcArgRegex := regexp.MustCompile(`\(\w+,(\d+)\)`)

// splice
spliceFuncPattern := fmt.Sprintf(`%s:\bfunction\b\([a],b\).(\breturn\b)?.?\w+\.splice`, calledFuncName)
if regexp.MustCompile(spliceFuncPattern).MatchString(decipherDefBody) {
arr := funcArgRegex.FindStringSubmatch(v)
arg, err := strconv.Atoi(arr[1])
if err != nil {
return nil, nil, err
}
funcSeq = append(funcSeq, "splice")
funcArgs = append(funcArgs, arg)
continue
}

// Swap
swapFuncPattern := fmt.Sprintf(`%s:\bfunction\b\(\w+\,\w\).\bvar\b.\bc=a\b`, calledFuncName)
if regexp.MustCompile(swapFuncPattern).MatchString(decipherDefBody) {
arr := funcArgRegex.FindStringSubmatch(v)
arg, err := strconv.Atoi(arr[1])
if err != nil {
return nil, nil, err
}
funcSeq = append(funcSeq, "swap")
funcArgs = append(funcArgs, arg)
continue
}

// Reverse
reverseFuncPattern := fmt.Sprintf(`%s:\bfunction\b\(\w+\)`, calledFuncName)
if regexp.MustCompile(reverseFuncPattern).MatchString(decipherDefBody) {
arr := funcArgRegex.FindStringSubmatch(v)
arg, err := strconv.Atoi(arr[1])
if err != nil {
return nil, nil, err
}
funcSeq = append(funcSeq, "reverse")
funcArgs = append(funcArgs, arg)
continue
}
}
return funcSeq, funcArgs, nil
}

func (y *Youtube) decipher(cipher string) (string, error) {
queryParams, err := url.ParseQuery(cipher)
if err != nil {
return "", err
}
cipherMap := make(map[string]string)
for key, value := range queryParams {
cipherMap[key] = strings.Join(value, "")
}
/* eg:
extract decipher from https://youtube.com/s/player/4fbb4d5b/player_ias.vflset/en_US/base.js

var Mt={
splice:function(a,b){a.splice(0,b)},
reverse:function(a){a.reverse()},
EQ:function(a,b){var c=a[0];a[0]=a[b%a.length];a[b%a.length]=c}};

a=a.split("");
Mt.splice(a,3);
Mt.EQ(a,39);
Mt.splice(a,2);
Mt.EQ(a,1);
Mt.splice(a,1);
Mt.EQ(a,35);
Mt.EQ(a,51);
Mt.splice(a,2);
Mt.reverse(a,52);
return a.join("")
*/

s := cipherMap["s"]
bs := []byte(s)
splice := func(b int) {
bs = bs[b:]
}
swap := func(b int) {
pos := b % len(bs)
bs[0], bs[pos] = bs[pos], bs[0]
}
reverse := func(options ...interface{}) {
l, r := 0, len(bs)-1
for l < r {
bs[l], bs[r] = bs[r], bs[l]
l++
r--
}
}
operations, args, err := y.parseDecipherOpsAndArgs()
if err != nil {
return "", err
}
for i, op := range operations {
switch op {
case "splice":
splice(args[i])
case "swap":
swap(args[i])
case "reverse":
reverse(args[i])
}
}
cipherMap["s"] = string(bs)

decipheredUrl := fmt.Sprintf("%s&%s=%s", cipherMap["url"], cipherMap["sp"], cipherMap["s"])
return decipheredUrl, nil
}
16 changes: 12 additions & 4 deletions youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,24 @@ func (y *Youtube) parseVideoInfo() error {
fmt.Println("<<<<<", status[0])
var streams []stream
for streamPos, streamRaw := range prData.StreamingData.Formats {

if streamRaw.MimeType == "" {
y.log(fmt.Sprintf("An error occured while decoding one of the video's stream's information: stream %d.\n", streamPos))
continue
}
streamUrl := streamRaw.URL
if streamUrl == "" {
cipher := streamRaw.Cipher
decipheredUrl, err := y.decipher(cipher)
if err != nil {
return err
}
streamUrl = decipheredUrl
}

streams = append(streams, stream{
"quality": streamRaw.Quality,
"type": streamRaw.MimeType,
"url": streamRaw.URL,
"url": streamUrl,

"title": title,
"author": author,
Expand All @@ -206,7 +214,6 @@ func (y *Youtube) parseVideoInfo() error {
}

func (y *Youtube) getHTTPClient() (*http.Client, error) {

// setup a http client
httpTransport := &http.Transport{}
httpClient := &http.Client{Transport: httpTransport}
Expand All @@ -230,7 +237,8 @@ func (y *Youtube) getHTTPClient() (*http.Client, error) {
}

func (y *Youtube) getVideoInfo() error {
url := "https://youtube.com/get_video_info?video_id=" + y.VideoID
eurl := "https://youtube.googleapis.com/v/" + y.VideoID
url := "https://youtube.com/get_video_info?video_id=" + y.VideoID + "&eurl=" + eurl
y.log(fmt.Sprintf("url: %s", url))

httpClient, err := y.getHTTPClient()
Expand Down