Added a webhook for hits with configurable URL to RL General Settings. For now it's just for custom made backedns, I will expand it later on with discord and telegram hooks.

This commit is contained in:
ruri
2019-09-07 17:13:01 +02:00
parent 31a349db22
commit cd7b3046aa
6 changed files with 83 additions and 0 deletions
@@ -1,5 +1,6 @@
using Extreme.Net;
using LiteDB;
using Newtonsoft.Json;
using OpenBullet.ViewModels;
using RuriLib;
using RuriLib.Models;
@@ -35,6 +35,10 @@
<ComboBox DockPanel.Dock="Top" x:Name="botsDisplayModeCombobox" SelectionChanged="botsDisplayModeCombobox_SelectionChanged"/>
<CheckBox Content="Enable Bot Log (slow, RAM-heavy)" IsChecked="{Binding EnableBotLog}" VerticalAlignment="Center" VerticalContentAlignment="Center" />
<CheckBox Content="Save Last Response Source (RAM-heavy)" IsChecked="{Binding SaveLastSource}" VerticalAlignment="Center" VerticalContentAlignment="Center" />
<Label FontWeight="Bold" Content="Hits Webhook" />
<CheckBox Content="Enabled" IsChecked="{Binding WebhookEnabled}" />
<Label Content="URL:" />
<TextBox Text="{Binding WebhookURL}" />
</StackPanel>
</Grid>
</Page>
+51
View File
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RuriLib.Models
{
/// <summary>
/// The class that gets JSON-encoded and gets sent to the Webhook URL when a hit is found (if enabled).
/// </summary>
public class WebhookFormat
{
/// <summary>
/// Initializes the WebhookFormat object.
/// </summary>
/// <param name="data">The input data</param>
/// <param name="type">The hit type</param>
/// <param name="capturedData">The string-encoded captured data</param>
/// <param name="timestamp">The unix timestamp</param>
/// <param name="configName">The config name</param>
/// <param name="configAuthor">The config author</param>
public WebhookFormat(string data, string type, string capturedData, DateTime timestamp, string configName, string configAuthor)
{
Data = data;
Type = type;
CapturedData = capturedData;
Timestamp = Math.Round((timestamp.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
ConfigName = configName;
ConfigAuthor = configAuthor;
}
/// <summary>The input data.</summary>
public string Data { get; set; }
/// <summary>The hit type.</summary>
public string Type { get; set; }
/// <summary>The string-encoded captured data.</summary>
public string CapturedData { get; set; }
/// <summary>The unix timestamp.</summary>
public double Timestamp { get; set; }
/// <summary>The config name.</summary>
public string ConfigName { get; set; }
/// <summary>The config author.</summary>
public string ConfigAuthor { get; set; }
}
}
+18
View File
@@ -1,4 +1,5 @@
using Extreme.Net;
using Newtonsoft.Json;
using RuriLib.LS;
using RuriLib.Models;
using RuriLib.ViewModels;
@@ -862,6 +863,23 @@ namespace RuriLib.Runner
RaiseFoundHit(hit);
}
// Call the webhook
if (Settings.General.WebhookEnabled)
{
HttpRequest request = new HttpRequest();
try
{
var toSend = new WebhookFormat(data, hitType, capturedData.ToCaptureString(), DateTime.Now, Config.Settings.Name, Config.Settings.Author);
var json = JsonConvert.SerializeObject(toSend);
//wc.UploadStringAsync(new Uri(Settings.General.WebhookURL), json);
request.PostAsync(Settings.General.WebhookURL, json, "application/json");
}
catch
{
RaiseMessageArrived(LogLevel.Error, $"Could not register the hit to webhook {Settings.General.WebhookURL}");
}
}
// Wait time
if (Settings.General.WaitTime > 0)
Thread.Sleep(Settings.General.WaitTime);
+1
View File
@@ -146,6 +146,7 @@
<Compile Include="Models\DataPool.cs" />
<Compile Include="Models\ProxyPool.cs" />
<Compile Include="Models\RemoteProxySource.cs" />
<Compile Include="Models\WebhookFormat.cs" />
<Compile Include="Runner\AbortableBackgroundWorker.cs">
<SubType>Component</SubType>
</Compile>
@@ -47,6 +47,14 @@ namespace RuriLib.ViewModels
/// <summary>Whether to store the last Response Source after a successful check.</summary>
public bool SaveLastSource { get { return saveLastSource; } set { saveLastSource = value; OnPropertyChanged(); } }
private bool webhookEnabled = false;
/// <summary>Whether to activate the hit webhook that gets called upon a SUCCESS or a custom result.</summary>
public bool WebhookEnabled { get { return webhookEnabled; } set { webhookEnabled = value; OnPropertyChanged(); } }
private string webhookURL = "";
/// <summary>The URL where the JSON-encoded hit gets sent.</summary>
public string WebhookURL { get { return webhookURL; } set { webhookURL = value; OnPropertyChanged(); } }
/// <summary>
/// Resets the properties to their default value.
/// </summary>