Skip to content

Commit

Permalink
Fixed Youtube
Browse files Browse the repository at this point in the history
  • Loading branch information
schneidermanuel committed Jan 12, 2025
1 parent d0a2504 commit 07ddb3d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 29 deletions.
55 changes: 31 additions & 24 deletions DiscordBot.PubSub.Backend/DiscordBotPubSubBackendManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public DiscordBotPubSubBackendManager(DiscordSocketClient client,
IReactionRoleRefresher reactionRoleRefresher,
IYoutubeRefresher youtubeRefresher,
ITwitchRefresher twitchRefresher,
ITournamentCompletionDomain tournamentCompletionDomain
ITournamentCompletionDomain tournamentCompletionDomain
)
{
_client = client;
Expand Down Expand Up @@ -457,39 +457,46 @@ private async Task ProcessPost(HttpContext context)
{
var checksumHeader = context.Request.Headers["X-Hub-Signature"];
var signature = checksumHeader.ToString().Split('=')[1];
var stream = context.Request.Body;
string body;
using (var reader = new StreamReader(stream, Encoding.UTF8))
using (var stream = new MemoryStream())
{
body = await reader.ReadToEndAsync();
}
await context.Request.Body.CopyToAsync(stream);
stream.Position = 0;

var isValid = PubSubSecret.Check(body, signature);
var isValid = PubSubSecret.Check(stream, signature);

if (!isValid)
{
Console.WriteLine("Attention: Invalid signature");
}
if (!isValid)
{
await context.Response.CompleteAsync();
return;
}

await using (var newStream = GenerateStreamFromString(body))
{
var data = ConvertAtomToSyndication(newStream);
if (data != null)
string body;
stream.Position = 0;
using (var reader = new StreamReader(stream))
{
if (data.IsNewVideo)
body = await reader.ReadToEndAsync();
}

await using (var newStream = GenerateStreamFromString(body))
{
var data = ConvertAtomToSyndication(newStream);
if (data != null)
{
_ = _youtubeCallback.Invoke(data);
if (data.IsNewVideo)
{
_ = _youtubeCallback.Invoke(data);
}

context.Response.StatusCode = 200;
await context.Response.CompleteAsync();
return;
}

context.Response.StatusCode = 200;
await context.Response.CompleteAsync();
return;
}
}


context.Response.StatusCode = 400;
await context.Response.CompleteAsync();
context.Response.StatusCode = 400;
await context.Response.CompleteAsync();
}
}
catch (Exception e)
{
Expand Down
6 changes: 3 additions & 3 deletions DiscordBot.PubSub.Backend/PubSubSecret.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ public static string Secret
}
}

public static bool Check(string body, string signature)
public static bool Check(Stream body, string signature)
{
using (var hmac = new HMACSHA1(Encoding.UTF8.GetBytes(Secret)))
{
var hashBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(body));
var hashBytes = hmac.ComputeHash(body);
var hash = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
return signature.Equals(hash);
}
}

public static bool Check256(string body, string signature)
{
using (var hmac = new HMACSHA256(Encoding.UTF8.GetBytes(Secret)))
Expand All @@ -38,5 +39,4 @@ public static bool Check256(string body, string signature)
return signature.Remove(0, 7).Equals(hash);
}
}

}
5 changes: 3 additions & 2 deletions DiscordBot.PubSub/Youtube/YoutubePubSubRegistrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class YoutubePubSubRegistrator : IYoutubePubSubRegistrator

public async Task SubscribeAsync(string channelId)
{
var topic = $"https://www.youtube.com/feeds/videos.xml?channel_id={channelId}";
var topic = $"https://www.youtube.com/xml/feeds/videos.xml?channel_id={channelId}";

var client = new HttpClient();
client.DefaultRequestHeaders.Add("ContentType", "application/x-www-form-urlencoded");
Expand All @@ -20,7 +20,8 @@ public async Task SubscribeAsync(string channelId)
new("hub.callback", $"https://{BotClientConstants.Hostname}:{BotClientConstants.Port}"),
new("hub.topic", topic),
new("hub.verify", "async"),
new("hub.secret", "hello")
new("hub.verify_token", Guid.NewGuid().ToString()),
new("hub.secret", PubSubSecret.Secret)
}));


Expand Down

0 comments on commit 07ddb3d

Please sign in to comment.