YouTubePubSubService.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Security.Cryptography;
  2. using System.Text;
  3. using System.Xml.Linq;
  4. using Application.Abstractions.YouTube;
  5. using Microsoft.Extensions.Logging;
  6. namespace Infrastructure.YouTube;
  7. /// <summary>
  8. /// YouTube PubSubHubbub(WebSub) 구독 관리
  9. /// - 구독 신청/해지: POST https://pubsubhubbub.appspot.com/subscribe
  10. /// - 콜백 검증: hub.challenge GET 요청 응답
  11. /// - Atom Feed 파싱: 새 영상/생방송 알림 수신
  12. /// </summary>
  13. internal sealed class YouTubePubSubService(
  14. IHttpClientFactory httpClientFactory,
  15. ILogger<YouTubePubSubService> logger
  16. ) : IYouTubePubSubService
  17. {
  18. private const string HubUrl = "https://pubsubhubbub.appspot.com/subscribe";
  19. private const string TopicBase = "https://www.youtube.com/feeds/videos.xml?channel_id=";
  20. // HMAC 서명 검증용 시크릿 — 실제 운영에서는 Config/환경변수에서 주입
  21. internal string HmacSecret { get; set; } = "dpot-youtube-pubsub-secret";
  22. // 콜백 URL — 서버 시작 시 설정
  23. internal string CallbackUrl { get; set; } = string.Empty;
  24. public async Task<bool> SubscribeAsync(string channelId, CancellationToken ct)
  25. {
  26. return await SendHubRequestAsync("subscribe", channelId, ct);
  27. }
  28. public async Task<bool> UnsubscribeAsync(string channelId, CancellationToken ct)
  29. {
  30. return await SendHubRequestAsync("unsubscribe", channelId, ct);
  31. }
  32. public YouTubePubSubNotification? ParseNotification(string atomXml)
  33. {
  34. try
  35. {
  36. var doc = XDocument.Parse(atomXml);
  37. XNamespace atom = "http://www.w3.org/2005/Atom";
  38. XNamespace yt = "http://www.youtube.com/xml/schemas/2015";
  39. var entry = doc.Descendants(atom + "entry").FirstOrDefault();
  40. if (entry is null)
  41. {
  42. return null;
  43. }
  44. var videoId = entry.Element(yt + "videoId")?.Value;
  45. var channelId = entry.Element(yt + "channelId")?.Value;
  46. var title = entry.Element(atom + "title")?.Value ?? "";
  47. var published = entry.Element(atom + "published")?.Value;
  48. var updated = entry.Element(atom + "updated")?.Value;
  49. if (string.IsNullOrEmpty(videoId) || string.IsNullOrEmpty(channelId))
  50. {
  51. logger.LogWarning("[PubSub] Atom feed missing videoId or channelId");
  52. return null;
  53. }
  54. return new YouTubePubSubNotification(
  55. videoId,
  56. channelId,
  57. title,
  58. DateTime.TryParse(published, out var pub) ? pub : DateTime.UtcNow,
  59. DateTime.TryParse(updated, out var upd) ? upd : DateTime.UtcNow
  60. );
  61. }
  62. catch (Exception ex)
  63. {
  64. logger.LogError(ex, "[PubSub] Failed to parse Atom feed");
  65. return null;
  66. }
  67. }
  68. public bool VerifySignature(string payload, string signature)
  69. {
  70. if (string.IsNullOrEmpty(signature))
  71. {
  72. return false;
  73. }
  74. // X-Hub-Signature 형식: "sha1=hex_digest" 또는 "sha256=hex_digest"
  75. var parts = signature.Split('=', 2);
  76. if (parts.Length != 2)
  77. {
  78. return false;
  79. }
  80. var algorithm = parts[0];
  81. var expectedHex = parts[1];
  82. byte[] hash;
  83. var keyBytes = Encoding.UTF8.GetBytes(HmacSecret);
  84. var payloadBytes = Encoding.UTF8.GetBytes(payload);
  85. if (algorithm is "sha1")
  86. {
  87. hash = HMACSHA1.HashData(keyBytes, payloadBytes);
  88. }
  89. else if (algorithm is "sha256")
  90. {
  91. hash = HMACSHA256.HashData(keyBytes, payloadBytes);
  92. }
  93. else
  94. {
  95. logger.LogWarning("[PubSub] Unsupported HMAC algorithm: {Algorithm}", algorithm);
  96. return false;
  97. }
  98. var computedHex = Convert.ToHexString(hash).ToLowerInvariant();
  99. return string.Equals(computedHex, expectedHex, StringComparison.OrdinalIgnoreCase);
  100. }
  101. // ── Private ──────────────────────────────────────────────────────
  102. private async Task<bool> SendHubRequestAsync(string mode, string channelId, CancellationToken ct)
  103. {
  104. try
  105. {
  106. var client = httpClientFactory.CreateClient("PubSubHub");
  107. var topicUrl = $"{TopicBase}{channelId}";
  108. var content = new FormUrlEncodedContent(new Dictionary<string, string>
  109. {
  110. ["hub.mode"] = mode,
  111. ["hub.topic"] = topicUrl,
  112. ["hub.callback"] = CallbackUrl,
  113. ["hub.secret"] = HmacSecret,
  114. ["hub.verify"] = "async"
  115. });
  116. var response = await client.PostAsync(HubUrl, content, ct);
  117. if (response.IsSuccessStatusCode || (int)response.StatusCode == 202)
  118. {
  119. logger.LogInformation("[PubSub] {Mode} request accepted for channelId={ChannelId}", mode, channelId);
  120. return true;
  121. }
  122. var body = await response.Content.ReadAsStringAsync(ct);
  123. logger.LogWarning("[PubSub] {Mode} failed: {StatusCode} {Body}", mode, response.StatusCode, body);
  124. return false;
  125. }
  126. catch (Exception ex)
  127. {
  128. logger.LogError(ex, "[PubSub] {Mode} error for channelId={ChannelId}", mode, channelId);
  129. return false;
  130. }
  131. }
  132. }