BililiveRecorder/BililiveRecorder.Core/SimpleWebhook/BasicWebhookV2.cs

136 lines
4.9 KiB
C#
Raw Normal View History

2021-02-23 18:03:37 +08:00
using System;
2023-11-05 20:54:52 +08:00
using System.Collections.Generic;
2021-02-23 18:03:37 +08:00
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
2021-12-19 21:10:34 +08:00
using BililiveRecorder.Core.Config.V3;
2021-02-23 18:03:37 +08:00
using BililiveRecorder.Core.Event;
using Newtonsoft.Json;
using Serilog;
namespace BililiveRecorder.Core.SimpleWebhook
{
internal class BasicWebhookV2
2021-02-23 18:03:37 +08:00
{
private static readonly ILogger logger = Log.ForContext<BasicWebhookV2>();
private readonly HttpClient client;
private readonly GlobalConfig config;
public BasicWebhookV2(GlobalConfig config)
{
this.config = config ?? throw new ArgumentNullException(nameof(config));
this.client = new HttpClient();
2021-02-26 21:57:10 +08:00
this.client.DefaultRequestHeaders.Add("User-Agent", $"BililiveRecorder/{GitVersionInformation.FullSemVer}");
2021-02-23 18:03:37 +08:00
}
public Task SendSessionStartedAsync(RecordSessionStartedEventArgs args) =>
this.SendAsync(new EventWrapper<RecordSessionStartedEventArgs>(args) { EventType = EventType.SessionStarted });
public Task SendSessionEndedAsync(RecordSessionEndedEventArgs args) =>
this.SendAsync(new EventWrapper<RecordSessionEndedEventArgs>(args) { EventType = EventType.SessionEnded });
public Task SendFileOpeningAsync(RecordFileOpeningEventArgs args) =>
this.SendAsync(new EventWrapper<RecordFileOpeningEventArgs>(args) { EventType = EventType.FileOpening });
public Task SendFileClosedAsync(RecordFileClosedEventArgs args) =>
this.SendAsync(new EventWrapper<RecordFileClosedEventArgs>(args) { EventType = EventType.FileClosed });
public Task SendStreamStartedAsync(StreamStartedEventArgs args) =>
this.SendAsync(new EventWrapper<StreamStartedEventArgs>(args) { EventType = EventType.StreamStarted });
public Task SendStreamEndedAsync(StreamEndedEventArgs args) =>
this.SendAsync(new EventWrapper<StreamEndedEventArgs>(args) { EventType = EventType.StreamEnded });
2021-02-23 18:03:37 +08:00
private async Task SendAsync(object data)
{
var urls = this.config.WebHookUrlsV2;
2022-05-02 23:26:43 +08:00
if (string.IsNullOrWhiteSpace(urls))
return;
2021-02-23 18:03:37 +08:00
var dataStr = JsonConvert.SerializeObject(data, Formatting.None);
2022-05-02 23:26:43 +08:00
logger.Debug("尝试发送 WebhookV2 到 {Urls}, 数据 {WebhookData}", urls, dataStr);
2022-05-02 23:26:43 +08:00
var bytes = Encoding.UTF8.GetBytes(dataStr);
2021-02-23 18:03:37 +08:00
var tasks = urls!
.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim())
.Where(x => !string.IsNullOrWhiteSpace(x))
2022-05-02 23:26:43 +08:00
.Select(x => this.SendImplAsync(x, bytes));
2021-02-23 18:03:37 +08:00
await Task.WhenAll(tasks).ConfigureAwait(false);
}
2022-05-02 23:26:43 +08:00
private async Task SendImplAsync(string url, byte[] data)
2021-02-23 18:03:37 +08:00
{
2023-11-05 20:54:52 +08:00
if (!IsUrlAllowed(url))
{
logger.Warning("不支持向 {Url} 发送 Webhook已跳过", url);
return;
}
2021-02-23 18:03:37 +08:00
for (var i = 0; i < 3; i++)
try
{
2022-05-02 23:26:43 +08:00
if (i > 0)
await Task.Delay(i * 1000);
using var body = new ByteArrayContent(data);
body.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var result = await this.client.PostAsync(url, body).ConfigureAwait(false);
2021-02-23 18:03:37 +08:00
result.EnsureSuccessStatusCode();
2022-05-02 23:26:43 +08:00
logger.Debug("发送 WebhookV2 到 {Url} 成功", url);
2021-02-23 18:03:37 +08:00
return;
}
catch (Exception ex)
{
2022-05-02 23:26:43 +08:00
logger.Warning(ex, "发送 WebhookV2 到 {Url} 失败", url);
2021-02-23 18:03:37 +08:00
}
}
2023-11-05 20:54:52 +08:00
private static readonly IReadOnlyList<string> DisallowedDomains = new[]
{
"test.example.com",
"baidu" + ".com",
"qq" + ".com",
"google" + ".com",
"b23" + ".tv",
"bilibili" + ".com",
"bilibili" + ".cn",
"bilibili" + ".tv",
"bilicomic" + ".com",
"bilicomics" + ".com",
"bilivideo" + ".com",
"bilivideo" + ".cn",
"biligame" + ".com",
"biligame" + ".net",
"biliapi" + ".com",
"biliapi" + ".net",
"hdslb" + ".com",
};
internal static bool IsUrlAllowed(string url)
{
if (string.IsNullOrWhiteSpace(url))
return false;
if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
return false;
if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)
return false;
foreach (var domain in DisallowedDomains)
if (uri.Host.EndsWith(domain, StringComparison.OrdinalIgnoreCase))
return false;
return true;
}
2021-02-23 18:03:37 +08:00
}
}