8
8
using System . Linq ;
9
9
using System . Net ;
10
10
using System . Net . Http ;
11
+ using System . Net . Security ;
11
12
using System . Threading ;
12
13
using System . Threading . Tasks ;
13
14
@@ -50,14 +51,18 @@ public SynologyService(IHostApplicationLifetime applicationLifetime, ILogger<Syn
50
51
_logger = logger ;
51
52
}
52
53
54
+
53
55
/// <summary>
54
56
/// Fetches all the end points, because they're dynamic between DSM versions.
55
57
/// </summary>
56
58
public async Task GetEndPointsAsync ( )
57
59
{
58
60
_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 ) )
61
66
{
62
67
httpClient . BaseAddress = new Uri ( Config . Url ) ;
63
68
@@ -71,7 +76,7 @@ public async Task GetEndPointsAsync()
71
76
if ( response . Data . TryGetValue ( API_LOGIN , out SynologyApiInfo loginInfo ) )
72
77
{
73
78
_logger . LogDebug ( $ "API: Found path '{ loginInfo . Path } ' for { API_LOGIN } ") ;
74
-
79
+
75
80
if ( loginInfo . MaxVersion < Config . ApiVersionAuth )
76
81
{
77
82
_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()
127
132
_logger . LogInformation ( "Login: Authenticating" ) ;
128
133
129
134
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 ) )
134
144
{
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 ) ;
137
146
138
- using ( HttpClient httpClient = new HttpClient ( httpClientHandler ) )
147
+ HttpResponseMessage result = await httpClient . GetAsync ( loginUri ) ;
148
+ if ( result . IsSuccessStatusCode )
139
149
{
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 )
144
152
{
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" ) ;
156
154
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 )
160
158
{
161
- _logger . LogError ( $ "Login: Failed due to error ' { response . Error . Code } '" ) ;
159
+ _applicationLifetime . StopApplication ( ) ;
162
160
}
161
+
162
+ return cookie ;
163
163
}
164
164
else
165
165
{
166
- _logger . LogError ( $ "Login: Failed due to HTTP status code ' { result . StatusCode } '") ;
166
+ _logger . LogError ( $ "Login: Failed due to error ' { response . Error . Code } '") ;
167
167
}
168
168
}
169
+ else
170
+ {
171
+ _logger . LogError ( $ "Login: Failed due to HTTP status code '{ result . StatusCode } '") ;
172
+ }
169
173
}
170
-
171
174
return null ;
172
175
}
173
176
@@ -182,16 +185,20 @@ public async Task<IEnumerable<SynologyCamera>> GetCamerasAsync()
182
185
Uri baseAddress = new Uri ( Config . Url ) ;
183
186
184
187
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 } )
187
194
{
188
195
cookieContainer . Add ( baseAddress , new Cookie ( "id" , Cookie . Value ) ) ;
189
-
196
+
190
197
string cameraInfoUri = string . Format ( URI_CAMERA_INFO , _cameraPath , Config . ApiVersionCamera ) ;
191
198
HttpResponseMessage result = await client . GetAsync ( cameraInfoUri ) ;
192
199
193
200
SynologyResponse < SynologyCameras > response = await GetResponse < SynologyCameras > ( result ) ;
194
- if ( response . Success )
201
+ if ( response . Success )
195
202
{
196
203
_logger . LogInformation ( $ "GetCameras: Successful. Found { response . Data . Cameras . Count ( ) } cameras.") ;
197
204
return response . Data . Cameras ;
@@ -214,15 +221,19 @@ public async Task<byte[]> TakeSnapshotAsync(string cameraName)
214
221
Uri baseAddress = new Uri ( Config . Url ) ;
215
222
216
223
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 } )
219
230
{
220
231
cookieContainer . Add ( baseAddress , new Cookie ( "id" , Cookie . Value ) ) ;
221
232
222
233
if ( Cameras . TryGetValue ( cameraName , out int id ) )
223
- {
234
+ {
224
235
_logger . LogDebug ( $ "{ cameraName } : Found with Synology ID '{ id } '.") ;
225
-
236
+
226
237
string resource = string . Format ( URI_CAMERA_SNAPSHOT + $ "&profileType={ ( int ) Config . Quality } ", _cameraPath , Config . ApiVersionCamera , id ) ;
227
238
_logger . LogDebug ( $ "{ cameraName } : Taking snapshot from '{ resource } '.") ;
228
239
0 commit comments