Skip to content

Commit

Permalink
#185 - Adding config to allow insecure HTTPS connections for webhooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Done committed Mar 22, 2023
1 parent 5a5f533 commit b6fdadc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ The webhook notification can be used to make web requests (e.g. API calls) eithe
"Method": "POST",
"Authentication": "Bearer",
"Token": "XYZ.123456",
"SendImage": "false"
"SendImage": false,
"AllowInsecureUrl": false
}
```
* Url [required]: The URL to send the image to
Expand All @@ -254,7 +255,8 @@ The webhook notification can be used to make web requests (e.g. API calls) eithe
* Bearer
* Token [optional]: The token to use when using Basic Authorization
* ImageField [optional] (Default: ```image```): The field name of the image in the POST data
* SendImage [optional] (Default: ```true```): The image will be sent to the webhook when the method is POST, PATCH or PUT.
* SendImage [optional] (Default: ```true```): The image will be sent to the webhook when the method is POST, PATCH or PUT
* AllowInsecureUrl [optional] (Default: ```false```): Whether to allow an insecure HTTPS connection to the Webhook.

#### Example POST data
The following is example data for when ```SendImage``` is ```false``` and ```SynoAIUrl``` is ```"http://192.168.1.2"```.
Expand Down
25 changes: 24 additions & 1 deletion SynoAI/Notifiers/Webhook/Webhook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
Expand Down Expand Up @@ -52,6 +53,10 @@ public class Webhook : NotifierBase
/// content-type of multipart/form-data.
/// </summary>
public bool SendImage { get; set; }
/// <summary>
/// Allow insecure URL Access to the API.
/// </summary>
public bool AllowInsecureUrl { get; set; }

/// <summary>
/// Sends a notification to the Webhook.
Expand All @@ -62,7 +67,7 @@ public class Webhook : NotifierBase
public override async Task SendAsync(Camera camera, Notification notification, ILogger logger)
{
logger.LogInformation($"{camera.Name}: Webhook: Processing");
using (HttpClient client = new())
using (HttpClient client = GetHttpClient())
{
FileStream fileStream = null;
client.DefaultRequestHeaders.Authorization = GetAuthenticationHeader();
Expand Down Expand Up @@ -152,6 +157,24 @@ public override async Task SendAsync(Camera camera, Notification notification, I
}
}

/// <summary>
/// Gets a <see cref="HttpClient"/> object for the Webhook request.
/// </summary>
/// <returns>A <see cref="HttpClient"/>.</returns>
private HttpClient GetHttpClient()
{
if (!Config.AllowInsecureUrl)
{
return new();
}

HttpClientHandler httpClientHandler = new()
{
ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) => true
};
return new(httpClientHandler);
}

/// <summary>
/// Generates an authentication header for the client.
/// </summary>
Expand Down
6 changes: 4 additions & 2 deletions SynoAI/Notifiers/Webhook/WebhookFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ public override INotifier Create(ILogger logger, IConfigurationSection section)
string method = section.GetValue<string>("Method", "POST");
bool sendImage = section.GetValue<bool>("SendImage", true);
bool sendTypes = section.GetValue<bool>("SendTypes", false);
bool allowInsecureUrl = section.GetValue("AllowInsecureUrl", false);

Webhook webhook = new Webhook()
Webhook webhook = new()
{
Url = url,
Authentication = authentication,
Username = username,
Password = password,
Token = token,
SendImage = sendImage
SendImage = sendImage,
AllowInsecureUrl = allowInsecureUrl
};

if (!string.IsNullOrWhiteSpace(imageField))
Expand Down

0 comments on commit b6fdadc

Please sign in to comment.