-
-
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.
- Loading branch information
Showing
4 changed files
with
97 additions
and
113 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,44 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using YoutubeExplode.Utils; | ||
|
||
namespace YoutubeExplode.Bridge; | ||
|
||
internal static class VisitorData | ||
{ | ||
private static readonly Random Random = new(); | ||
|
||
private static string GenerateRandomString(int length) | ||
{ | ||
const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; | ||
|
||
var buffer = new StringBuilder(); | ||
for (var i = 0; i < length; i++) | ||
buffer.Append(alphabet.ElementAt(Random.Next(alphabet.Length))); | ||
|
||
return buffer.ToString(); | ||
} | ||
|
||
public static string Generate() => | ||
new ProtoBuilder() | ||
.AddString(1, GenerateRandomString(11)) | ||
.AddNumber( | ||
5, | ||
DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() / 1000 - Random.Next(600000) | ||
) | ||
.AddBytes( | ||
6, | ||
new ProtoBuilder() | ||
.AddString(1, "US") | ||
.AddBytes( | ||
2, | ||
new ProtoBuilder() | ||
.AddString(2, "") | ||
.AddNumber(4, Random.Next(255) + 1) | ||
.ToBytes() | ||
) | ||
.ToBytes() | ||
) | ||
.ToUrlEncodedBase64(); | ||
} |
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 |
---|---|---|
@@ -1,81 +1,71 @@ | ||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace YoutubeExplode.Utils | ||
namespace YoutubeExplode.Utils; | ||
|
||
internal class ProtoBuilder | ||
{ | ||
internal class ProtoBuilder | ||
{ | ||
private MemoryStream _byteBuffer; | ||
private readonly MemoryStream _byteBuffer = new(); | ||
|
||
public ProtoBuilder() | ||
private void WriteNumber(long val) | ||
{ | ||
if (val == 0) | ||
{ | ||
_byteBuffer = new MemoryStream(); | ||
_byteBuffer.WriteByte(0); | ||
return; | ||
} | ||
|
||
public byte[] ToBytes() | ||
var v = val; | ||
while (v != 0) | ||
{ | ||
return _byteBuffer.ToArray(); | ||
} | ||
var b = (byte)(v & 0x7F); | ||
v >>= 7; | ||
|
||
public string ToUrlencodedBase64() | ||
{ | ||
var b64 = Convert | ||
.ToBase64String(ToBytes()) | ||
.Replace('+', '-') | ||
.Replace('/', '_') | ||
.TrimEnd('='); | ||
return Uri.EscapeDataString(b64); | ||
} | ||
if (v != 0) | ||
b |= 0x80; | ||
|
||
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); | ||
} | ||
} | ||
_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); | ||
} | ||
private void WriteField(int field, byte wire) | ||
{ | ||
var fBits = (long)field << 3; | ||
var wBits = (long)wire & 0x07; | ||
var val = fBits | wBits; | ||
|
||
public void Varint(int field, long val) | ||
{ | ||
Field(field, 0); | ||
WriteVarint(val); | ||
} | ||
WriteNumber(val); | ||
} | ||
|
||
public void String(int field, string str) | ||
{ | ||
var strBytes = Encoding.UTF8.GetBytes(str); | ||
Bytes(field, strBytes); | ||
} | ||
public ProtoBuilder AddNumber(int field, long val) | ||
{ | ||
WriteField(field, 0); | ||
WriteNumber(val); | ||
|
||
public void Bytes(int field, byte[] bytes) | ||
{ | ||
Field(field, 2); | ||
WriteVarint(bytes.Length); | ||
_byteBuffer.Write(bytes, 0, bytes.Length); | ||
} | ||
return this; | ||
} | ||
|
||
public ProtoBuilder AddString(int field, string str) => | ||
AddBytes(field, Encoding.UTF8.GetBytes(str)); | ||
|
||
public ProtoBuilder AddBytes(int field, byte[] bytes) | ||
{ | ||
WriteField(field, 2); | ||
WriteNumber(bytes.Length); | ||
_byteBuffer.Write(bytes, 0, bytes.Length); | ||
|
||
return this; | ||
} | ||
|
||
public byte[] ToBytes() => _byteBuffer.ToArray(); | ||
|
||
public string ToUrlEncodedBase64() => | ||
Uri.EscapeDataString( | ||
Convert.ToBase64String(ToBytes()).Replace('+', '-').Replace('/', '_').TrimEnd('=') | ||
); | ||
|
||
[ExcludeFromCodeCoverage] | ||
public override string ToString() => ToUrlEncodedBase64(); | ||
} |
This file was deleted.
Oops, something went wrong.
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