Skip to content

Commit

Permalink
Unthrottle streams for all formats
Browse files Browse the repository at this point in the history
The n-param potentially requires processing even if there's no
deciphering needed.
Refactor decipherURL to reuse decryptNParam on any url.
  • Loading branch information
elProxy committed Apr 24, 2022
1 parent f363472 commit 7f71515
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (c *Client) GetStreamURL(video *Video, format *Format) (string, error) {
// GetStreamURLContext returns the url for a specific format with a context
func (c *Client) GetStreamURLContext(ctx context.Context, video *Video, format *Format) (string, error) {
if format.URL != "" {
return format.URL, nil
return c.unThrottle(ctx, video.ID, format.URL)
}

cipher := format.Cipher
Expand Down
42 changes: 39 additions & 3 deletions decipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,55 @@ func (c *Client) decipherURL(ctx context.Context, videoID string, cipher string)
}
query.Add(params.Get("sp"), string(bs))

query, err = c.decryptNParam(ctx, config, query)
if err != nil {
return "", err
}

uri.RawQuery = query.Encode()

return uri.String(), nil
}

func (c *Client) unThrottle(ctx context.Context, videoID string, urlString string) (string, error) {
config, err := c.getPlayerConfig(ctx, videoID)
if err != nil {
return "", err
}
uri, err := url.Parse(urlString)
if err != nil {
return "", err
}

query, err := c.decryptNParam(ctx, config, uri.Query())
if err != nil {
return "", err
}

uri.RawQuery = query.Encode()

return uri.String(), nil

}

func (c *Client) decryptNParam(ctx context.Context, config playerConfig, query url.Values) (url.Values, error) {

// decrypt n-parameter
nSig := query.Get("n")
if nSig != "" {
nDecoded, err := config.decodeNsig(nSig)
if err != nil {
return "", fmt.Errorf("unable to decode nSig: %w", err)
return nil, fmt.Errorf("unable to decode nSig: %w", err)
}
query.Set("n", nDecoded)
}

uri.RawQuery = query.Encode()
if c.Debug {
log.Printf("[nParam] n: %s; nDecoded: %s\nQuery: %v\n", nSig, query.Get("n"), query)
}

return query, nil

return uri.String(), nil
}

const (
Expand Down

0 comments on commit 7f71515

Please sign in to comment.