2020-12-20 20:56:40 +08:00
|
|
|
|
using System;
|
|
|
|
|
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;
|
2020-12-20 20:56:40 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2021-02-23 18:03:37 +08:00
|
|
|
|
using Serilog;
|
2020-12-20 20:56:40 +08:00
|
|
|
|
|
2021-02-23 18:03:37 +08:00
|
|
|
|
namespace BililiveRecorder.Core.SimpleWebhook
|
2020-12-20 20:56:40 +08:00
|
|
|
|
{
|
2022-05-16 23:28:31 +08:00
|
|
|
|
internal class BasicWebhookV1
|
2020-12-20 20:56:40 +08:00
|
|
|
|
{
|
2021-02-23 18:03:37 +08:00
|
|
|
|
private static readonly ILogger logger = Log.ForContext<BasicWebhookV1>();
|
2020-12-20 20:56:40 +08:00
|
|
|
|
|
2022-05-02 23:26:43 +08:00
|
|
|
|
private readonly HttpClient client;
|
|
|
|
|
private readonly ConfigV3 config;
|
2020-12-20 20:56:40 +08:00
|
|
|
|
|
2021-12-19 21:10:34 +08:00
|
|
|
|
public BasicWebhookV1(ConfigV3 config)
|
2020-12-20 20:56:40 +08:00
|
|
|
|
{
|
2022-05-02 23:28:27 +08:00
|
|
|
|
this.config = config ?? throw new ArgumentNullException(nameof(config));
|
|
|
|
|
|
|
|
|
|
this.client = new HttpClient();
|
2022-05-02 23:26:43 +08:00
|
|
|
|
this.client.DefaultRequestHeaders.Add("User-Agent", $"BililiveRecorder/{GitVersionInformation.FullSemVer}");
|
2020-12-20 20:56:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-02-23 18:03:37 +08:00
|
|
|
|
public async Task SendAsync(RecordEndData data)
|
2020-12-20 20:56:40 +08:00
|
|
|
|
{
|
2022-05-02 23:26:43 +08:00
|
|
|
|
var urls = this.config.Global.WebHookUrls;
|
|
|
|
|
if (string.IsNullOrWhiteSpace(urls))
|
|
|
|
|
return;
|
2020-12-20 20:56:40 +08:00
|
|
|
|
|
|
|
|
|
var dataStr = JsonConvert.SerializeObject(data, Formatting.None);
|
2022-05-02 23:26:43 +08:00
|
|
|
|
|
|
|
|
|
logger.Debug("尝试发送 WebhookV1 数据 {WebhookData}, 地址 {Urls}", dataStr, urls);
|
|
|
|
|
|
|
|
|
|
var bytes = Encoding.UTF8.GetBytes(dataStr);
|
2020-12-20 20:56:40 +08:00
|
|
|
|
|
2021-01-01 14:46:27 +08:00
|
|
|
|
var tasks = urls!
|
2020-12-20 20:56:40 +08:00
|
|
|
|
.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));
|
2020-12-20 20:56:40 +08:00
|
|
|
|
|
|
|
|
|
await Task.WhenAll(tasks).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 23:26:43 +08:00
|
|
|
|
private async Task SendImplAsync(string url, byte[] data)
|
2020-12-20 20:56:40 +08:00
|
|
|
|
{
|
2023-11-05 20:54:52 +08:00
|
|
|
|
if (!BasicWebhookV2.IsUrlAllowed(url))
|
|
|
|
|
{
|
|
|
|
|
logger.Warning("不支持向 {Url} 发送 Webhook,已跳过", url);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-20 20:56:40 +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);
|
2020-12-20 20:56:40 +08:00
|
|
|
|
result.EnsureSuccessStatusCode();
|
2022-05-02 23:26:43 +08:00
|
|
|
|
logger.Debug("发送 WebhookV1 到 {Url} 成功", url);
|
2020-12-20 20:56:40 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2022-05-02 23:26:43 +08:00
|
|
|
|
logger.Warning(ex, "发送 WebhookV1 到 {Url} 失败", url);
|
2020-12-20 20:56:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|