-
-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update iOS client and add random visitor data to requests (#865)
- Loading branch information
Showing
3 changed files
with
137 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace YoutubeExplode.Utils | ||
{ | ||
internal class ProtoBuilder | ||
{ | ||
private MemoryStream _byteBuffer; | ||
|
||
public ProtoBuilder() | ||
{ | ||
_byteBuffer = new MemoryStream(); | ||
} | ||
|
||
public byte[] ToBytes() | ||
{ | ||
return _byteBuffer.ToArray(); | ||
} | ||
|
||
public string ToUrlencodedBase64() | ||
{ | ||
var b64 = Convert | ||
.ToBase64String(ToBytes()) | ||
.Replace('+', '-') | ||
.Replace('/', '_') | ||
.TrimEnd('='); | ||
return Uri.EscapeDataString(b64); | ||
} | ||
|
||
private void WriteVarint(long val) | ||
{ | ||
if (val == 0) | ||
{ | ||
_byteBuffer.WriteByte(0); | ||
} | ||
else | ||
{ | ||
long v = val; | ||
while (v != 0) | ||
{ | ||
byte b = (byte)(v & 0x7F); | ||
v >>= 7; | ||
|
||
if (v != 0) | ||
{ | ||
b |= 0x80; | ||
} | ||
_byteBuffer.WriteByte(b); | ||
} | ||
} | ||
} | ||
|
||
private void Field(int field, byte wire) | ||
{ | ||
long fbits = ((long)field) << 3; | ||
long wbits = ((long)wire) & 0x07; | ||
long val = fbits | wbits; | ||
WriteVarint(val); | ||
} | ||
|
||
public void Varint(int field, long val) | ||
{ | ||
Field(field, 0); | ||
WriteVarint(val); | ||
} | ||
|
||
public void String(int field, string str) | ||
{ | ||
var strBytes = Encoding.UTF8.GetBytes(str); | ||
Bytes(field, strBytes); | ||
} | ||
|
||
public void Bytes(int field, byte[] bytes) | ||
{ | ||
Field(field, 2); | ||
WriteVarint(bytes.Length); | ||
_byteBuffer.Write(bytes, 0, bytes.Length); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace YoutubeExplode.Utils | ||
{ | ||
internal static class YoutubeParsingHelper | ||
{ | ||
private static string CONTENT_PLAYBACK_NONCE_ALPHABET = | ||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; | ||
|
||
public static string GetRandomVisitorData() | ||
{ | ||
Random r = new Random(); | ||
ProtoBuilder pbE2 = new ProtoBuilder(); | ||
pbE2.String(2, ""); | ||
pbE2.Varint(4, r.Next(255) + 1); | ||
|
||
ProtoBuilder pbE = new ProtoBuilder(); | ||
pbE.String(1, "US"); | ||
pbE.Bytes(2, pbE2.ToBytes()); | ||
|
||
ProtoBuilder pb = new ProtoBuilder(); | ||
pb.String(1, GenerateRandomStringFromAlphabet(CONTENT_PLAYBACK_NONCE_ALPHABET, 11, r)); | ||
pb.Varint(5, DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000 - r.Next(600000)); | ||
pb.Bytes(6, pbE.ToBytes()); | ||
return pb.ToUrlencodedBase64(); | ||
} | ||
|
||
private static string GenerateRandomStringFromAlphabet( | ||
string alphabet, | ||
int length, | ||
Random random | ||
) | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < length; i++) | ||
{ | ||
sb.Append(alphabet.ElementAt(random.Next(alphabet.Length))); | ||
} | ||
return sb.ToString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters