Skip to content

Commit d6aa4d0

Browse files
committed
Allowing invalid HTTPS certificates
1 parent 0871892 commit d6aa4d0

File tree

2 files changed

+55
-39
lines changed

2 files changed

+55
-39
lines changed

SynoAI/Config.cs

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public static class Config
3030
/// The password to login to the API with.
3131
/// </summary>
3232
public static string Password { get; private set; }
33+
/// <summary>
34+
/// Allow insecure URL Access to the Synology API
35+
/// </summary>
36+
public static bool AllowInsecureUrl {get;private set;}
3337

3438
/// <summary>
3539
/// The version of the SYNO.API.Auth API to use.
@@ -115,6 +119,7 @@ public static void Generate(ILogger logger, IConfiguration configuration)
115119
Url = configuration.GetValue<string>("Url");
116120
Username = configuration.GetValue<string>("User");
117121
Password = configuration.GetValue<string>("Password");
122+
AllowInsecureUrl = configuration.GetValue<bool>("AllowInsecureUrl");
118123

119124
ApiVersionAuth = configuration.GetValue<int>("ApiVersionInfo", 6); // DSM 6.0 beta2
120125
ApiVersionCamera = configuration.GetValue<int>("ApiVersionCamera", 9); // Surveillance Station 8.0

SynoAI/Services/SynologyService.cs

+50-39
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System.Net;
1010
using System.Net.Http;
11+
using System.Net.Security;
1112
using System.Threading;
1213
using System.Threading.Tasks;
1314

@@ -50,14 +51,18 @@ public SynologyService(IHostApplicationLifetime applicationLifetime, ILogger<Syn
5051
_logger = logger;
5152
}
5253

54+
5355
/// <summary>
5456
/// Fetches all the end points, because they're dynamic between DSM versions.
5557
/// </summary>
5658
public async Task GetEndPointsAsync()
5759
{
5860
_logger.LogInformation("API: Querying end points");
59-
60-
using (HttpClient httpClient = new HttpClient())
61+
using var httpClientHandler = new HttpClientHandler();
62+
if (Config.AllowInsecureUrl)
63+
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) => true;
64+
65+
using (HttpClient httpClient = new HttpClient(httpClientHandler))
6166
{
6267
httpClient.BaseAddress = new Uri(Config.Url);
6368

@@ -71,7 +76,7 @@ public async Task GetEndPointsAsync()
7176
if (response.Data.TryGetValue(API_LOGIN, out SynologyApiInfo loginInfo))
7277
{
7378
_logger.LogDebug($"API: Found path '{loginInfo.Path}' for {API_LOGIN}");
74-
79+
7580
if (loginInfo.MaxVersion < Config.ApiVersionAuth)
7681
{
7782
_logger.LogError($"API: {API_CAMERA} only supports a max version of {loginInfo.MaxVersion}, but the system is set to use version {Config.ApiVersionAuth}.");
@@ -127,47 +132,45 @@ public async Task<Cookie> LoginAsync()
127132
_logger.LogInformation("Login: Authenticating");
128133

129134
CookieContainer cookieContainer = new CookieContainer();
130-
using (HttpClientHandler httpClientHandler = new HttpClientHandler
131-
{
132-
CookieContainer = cookieContainer
133-
})
135+
using var httpClientHandler = new HttpClientHandler();
136+
httpClientHandler.CookieContainer = cookieContainer;
137+
if (Config.AllowInsecureUrl)
138+
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) => true;
139+
140+
string loginUri = string.Format(URI_LOGIN, _loginPath, Config.ApiVersionAuth, Config.Username, Config.Password);
141+
_logger.LogDebug($"Login: Logging in ({loginUri})");
142+
143+
using (HttpClient httpClient = new HttpClient(httpClientHandler))
134144
{
135-
string loginUri = string.Format(URI_LOGIN, _loginPath, Config.ApiVersionAuth, Config.Username, Config.Password);
136-
_logger.LogDebug($"Login: Logging in ({loginUri})");
145+
httpClient.BaseAddress = new Uri(Config.Url);
137146

138-
using (HttpClient httpClient = new HttpClient(httpClientHandler))
147+
HttpResponseMessage result = await httpClient.GetAsync(loginUri);
148+
if (result.IsSuccessStatusCode)
139149
{
140-
httpClient.BaseAddress = new Uri(Config.Url);
141-
142-
HttpResponseMessage result = await httpClient.GetAsync(loginUri);
143-
if (result.IsSuccessStatusCode)
150+
SynologyResponse<SynologyLogin> response = await GetResponse<SynologyLogin>(result);
151+
if (response.Success)
144152
{
145-
SynologyResponse<SynologyLogin> response = await GetResponse<SynologyLogin>(result);
146-
if (response.Success)
147-
{
148-
_logger.LogInformation("Login: Successful");
149-
150-
IEnumerable<Cookie> cookies = cookieContainer.GetCookies(httpClient.BaseAddress).Cast<Cookie>().ToList();
151-
Cookie cookie = cookies.FirstOrDefault(x => x.Name == "id");
152-
if (cookie == null)
153-
{
154-
_applicationLifetime.StopApplication();
155-
}
153+
_logger.LogInformation("Login: Successful");
156154

157-
return cookie;
158-
}
159-
else
155+
IEnumerable<Cookie> cookies = cookieContainer.GetCookies(httpClient.BaseAddress).Cast<Cookie>().ToList();
156+
Cookie cookie = cookies.FirstOrDefault(x => x.Name == "id");
157+
if (cookie == null)
160158
{
161-
_logger.LogError($"Login: Failed due to error '{response.Error.Code}'");
159+
_applicationLifetime.StopApplication();
162160
}
161+
162+
return cookie;
163163
}
164164
else
165165
{
166-
_logger.LogError($"Login: Failed due to HTTP status code '{result.StatusCode}'");
166+
_logger.LogError($"Login: Failed due to error '{response.Error.Code}'");
167167
}
168168
}
169+
else
170+
{
171+
_logger.LogError($"Login: Failed due to HTTP status code '{result.StatusCode}'");
172+
}
169173
}
170-
171174
return null;
172175
}
173176

@@ -182,16 +185,20 @@ public async Task<IEnumerable<SynologyCamera>> GetCamerasAsync()
182185
Uri baseAddress = new Uri(Config.Url);
183186

184187
CookieContainer cookieContainer = new CookieContainer();
185-
using (HttpClientHandler handler = new HttpClientHandler() { CookieContainer = cookieContainer })
186-
using (HttpClient client = new HttpClient(handler) { BaseAddress = baseAddress })
188+
using var httpClientHandler = new HttpClientHandler();
189+
httpClientHandler.CookieContainer = cookieContainer;
190+
if (Config.AllowInsecureUrl)
191+
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) => true;
192+
193+
using (HttpClient client = new HttpClient(httpClientHandler) { BaseAddress = baseAddress })
187194
{
188195
cookieContainer.Add(baseAddress, new Cookie("id", Cookie.Value));
189-
196+
190197
string cameraInfoUri = string.Format(URI_CAMERA_INFO, _cameraPath, Config.ApiVersionCamera);
191198
HttpResponseMessage result = await client.GetAsync(cameraInfoUri);
192199

193200
SynologyResponse<SynologyCameras> response = await GetResponse<SynologyCameras>(result);
194-
if (response.Success)
201+
if (response.Success)
195202
{
196203
_logger.LogInformation($"GetCameras: Successful. Found {response.Data.Cameras.Count()} cameras.");
197204
return response.Data.Cameras;
@@ -214,15 +221,19 @@ public async Task<byte[]> TakeSnapshotAsync(string cameraName)
214221
Uri baseAddress = new Uri(Config.Url);
215222

216223
CookieContainer cookieContainer = new CookieContainer();
217-
using (HttpClientHandler handler = new HttpClientHandler() { CookieContainer = cookieContainer })
218-
using (HttpClient client = new HttpClient(handler) { BaseAddress = baseAddress })
224+
using var httpClientHandler = new HttpClientHandler();
225+
httpClientHandler.CookieContainer = cookieContainer;
226+
if (Config.AllowInsecureUrl)
227+
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) => true;
228+
229+
using (HttpClient client = new HttpClient(httpClientHandler) { BaseAddress = baseAddress })
219230
{
220231
cookieContainer.Add(baseAddress, new Cookie("id", Cookie.Value));
221232

222233
if (Cameras.TryGetValue(cameraName, out int id))
223-
{
234+
{
224235
_logger.LogDebug($"{cameraName}: Found with Synology ID '{id}'.");
225-
236+
226237
string resource = string.Format(URI_CAMERA_SNAPSHOT + $"&profileType={(int)Config.Quality}", _cameraPath, Config.ApiVersionCamera, id);
227238
_logger.LogDebug($"{cameraName}: Taking snapshot from '{resource}'.");
228239

0 commit comments

Comments
 (0)