2021-06-29 08:40:20 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
2021-07-08 17:32:59 +00:00
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Security.Cryptography;
|
2021-06-29 08:40:20 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.Json;
|
2021-07-08 17:32:59 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2021-06-29 08:40:20 +00:00
|
|
|
|
|
|
|
|
|
using ASC.Common;
|
2021-07-08 17:32:59 +00:00
|
|
|
|
using ASC.Common.Logging;
|
2021-06-29 08:40:20 +00:00
|
|
|
|
using ASC.Core;
|
2021-07-08 17:32:59 +00:00
|
|
|
|
using ASC.Webhooks.Dao.Models;
|
|
|
|
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2021-06-29 08:40:20 +00:00
|
|
|
|
|
|
|
|
|
namespace ASC.Webhooks
|
|
|
|
|
{
|
|
|
|
|
[Scope]
|
|
|
|
|
public class WebhookSender
|
|
|
|
|
{
|
2021-07-08 17:32:59 +00:00
|
|
|
|
private const int repeatCount = 5;
|
|
|
|
|
|
|
|
|
|
private static readonly HttpClient httpClient = new HttpClient();
|
|
|
|
|
private ILog Log { get; }
|
2021-06-29 08:40:20 +00:00
|
|
|
|
private DbWorker DbWorker { get; }
|
2021-07-08 17:32:59 +00:00
|
|
|
|
public WebhookSender(IOptionsMonitor<ILog> option, DbWorker dbWorker)
|
2021-06-29 08:40:20 +00:00
|
|
|
|
{
|
|
|
|
|
DbWorker = dbWorker;
|
2021-07-08 17:32:59 +00:00
|
|
|
|
Log = option.Get("ASC.Webhooks");
|
2021-06-29 08:40:20 +00:00
|
|
|
|
}
|
2021-07-08 17:32:59 +00:00
|
|
|
|
public async Task Send(WebhooksQueueEntry webhooksQueueEntry)
|
2021-06-29 08:40:20 +00:00
|
|
|
|
{
|
2021-07-08 17:32:59 +00:00
|
|
|
|
var URI = webhooksQueueEntry.Uri;
|
|
|
|
|
var secretKey = webhooksQueueEntry.SecretKey;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < repeatCount; i++)
|
2021-06-29 08:40:20 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-07-08 17:32:59 +00:00
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, URI);
|
|
|
|
|
request.Headers.Add("Accept", "*/*");
|
|
|
|
|
request.Headers.Add("Secret","SHA256=" + GetSecretHash(secretKey, webhooksQueueEntry.Data));//*retry
|
2021-06-29 08:40:20 +00:00
|
|
|
|
|
2021-07-08 17:32:59 +00:00
|
|
|
|
request.Content = new StringContent(
|
|
|
|
|
webhooksQueueEntry.Data,
|
|
|
|
|
Encoding.UTF8,
|
|
|
|
|
"application/json");
|
2021-06-29 08:40:20 +00:00
|
|
|
|
|
2021-07-08 17:32:59 +00:00
|
|
|
|
var response = await httpClient.SendAsync(request);
|
2021-06-29 08:40:20 +00:00
|
|
|
|
|
2021-07-08 17:32:59 +00:00
|
|
|
|
if (response.IsSuccessStatusCode)
|
2021-06-29 08:40:20 +00:00
|
|
|
|
{
|
2021-07-08 17:32:59 +00:00
|
|
|
|
DbWorker.UpdateStatus(webhooksQueueEntry.Id, ProcessStatus.Success);
|
|
|
|
|
break;
|
2021-06-29 08:40:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2021-07-08 17:32:59 +00:00
|
|
|
|
if(i == repeatCount)
|
|
|
|
|
{
|
|
|
|
|
DbWorker.UpdateStatus(webhooksQueueEntry.Id, ProcessStatus.Failed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.Error("ERROR: " + ex.Message);
|
2021-06-29 08:40:20 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-08 17:32:59 +00:00
|
|
|
|
private string GetSecretHash(string secretKey, string body)
|
2021-06-29 08:40:20 +00:00
|
|
|
|
{
|
2021-07-08 17:32:59 +00:00
|
|
|
|
string computedSignature;
|
|
|
|
|
var secretBytes = Encoding.UTF8.GetBytes(secretKey);
|
|
|
|
|
|
|
|
|
|
using (var hasher = new HMACSHA256(secretBytes))
|
|
|
|
|
{
|
|
|
|
|
var data = Encoding.UTF8.GetBytes(body);
|
|
|
|
|
computedSignature = BitConverter.ToString(hasher.ComputeHash(data));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return computedSignature;
|
2021-06-29 08:40:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|