|
3 | 3 | // </copyright> |
4 | 4 |
|
5 | 5 | using System; |
6 | | -using System.Collections.Generic; |
7 | 6 | using System.IO; |
8 | | -using System.Linq; |
9 | | -using System.Security.Cryptography.X509Certificates; |
10 | 7 | using System.Text; |
11 | 8 | using System.Threading; |
12 | | -using System.Threading.Tasks; |
13 | 9 |
|
14 | 10 | using FubarDev.FtpServer; |
15 | 11 | using FubarDev.FtpServer.AccountManagement.Directories.RootPerUser; |
16 | 12 | using FubarDev.FtpServer.AccountManagement.Directories.SingleRootWithoutHome; |
17 | | -using FubarDev.FtpServer.Authentication; |
18 | 13 | using FubarDev.FtpServer.CommandExtensions; |
19 | 14 | using FubarDev.FtpServer.Commands; |
20 | 15 | using FubarDev.FtpServer.FileSystem; |
@@ -226,33 +221,6 @@ public static IServiceCollection AddFtpServices( |
226 | 221 | break; |
227 | 222 | } |
228 | 223 |
|
229 | | - if (options.Ftps.Implicit) |
230 | | - { |
231 | | - var implicitFtpsCertificate = options.GetCertificate(); |
232 | | - if (implicitFtpsCertificate != null) |
233 | | - { |
234 | | - services |
235 | | - .AddSingleton(new ImplicitFtpsControlConnectionStreamAdapterOptions(implicitFtpsCertificate)) |
236 | | - .AddSingleton<IFtpControlStreamAdapter, ImplicitFtpsControlConnectionStreamAdapter>(); |
237 | | - |
238 | | - // Ensure that PROT and PBSZ commands are working. |
239 | | - services.Decorate<IFtpServer>( |
240 | | - (ftpServer, _) => |
241 | | - { |
242 | | - ftpServer.ConfigureConnection += (s, e) => |
243 | | - { |
244 | | - var serviceProvider = e.Connection.ConnectionServices; |
245 | | - var stateMachine = serviceProvider.GetRequiredService<IFtpLoginStateMachine>(); |
246 | | - var authTlsMechanism = serviceProvider.GetRequiredService<IEnumerable<IAuthenticationMechanism>>() |
247 | | - .Single(x => x.CanHandle("TLS")); |
248 | | - stateMachine.Activate(authTlsMechanism); |
249 | | - }; |
250 | | - |
251 | | - return ftpServer; |
252 | | - }); |
253 | | - } |
254 | | - } |
255 | | - |
256 | 224 | #if NETCOREAPP |
257 | 225 | services.Decorate<IFtpServer>( |
258 | 226 | (ftpServer, serviceProvider) => |
@@ -302,6 +270,15 @@ private static IFtpServerBuilder ConfigureServer(this IFtpServerBuilder builder, |
302 | 270 | .EnableConnectionCheck(); |
303 | 271 | } |
304 | 272 |
|
| 273 | + if (options.Ftps.Implicit) |
| 274 | + { |
| 275 | + var implicitFtpsCertificate = options.GetCertificate(); |
| 276 | + if (implicitFtpsCertificate != null) |
| 277 | + { |
| 278 | + builder = builder.UseImplicitTls(implicitFtpsCertificate); |
| 279 | + } |
| 280 | + } |
| 281 | + |
305 | 282 | return builder; |
306 | 283 | } |
307 | 284 |
|
@@ -329,41 +306,11 @@ private static UserCredential GetUserCredential( |
329 | 306 | return credential; |
330 | 307 | } |
331 | 308 |
|
332 | | - private class ImplicitFtpsControlConnectionStreamAdapterOptions |
333 | | - { |
334 | | - public ImplicitFtpsControlConnectionStreamAdapterOptions(X509Certificate2 certificate) |
335 | | - { |
336 | | - Certificate = certificate; |
337 | | - } |
338 | | - |
339 | | - public X509Certificate2 Certificate { get; } |
340 | | - } |
341 | | - |
342 | 309 | private static TimeSpan? ToTimeSpan(int? seconds) |
343 | 310 | { |
344 | 311 | return seconds == null |
345 | 312 | ? (TimeSpan?)null |
346 | 313 | : TimeSpan.FromSeconds(seconds.Value); |
347 | 314 | } |
348 | | - |
349 | | - private class ImplicitFtpsControlConnectionStreamAdapter : IFtpControlStreamAdapter |
350 | | - { |
351 | | - private readonly ImplicitFtpsControlConnectionStreamAdapterOptions _options; |
352 | | - private readonly ISslStreamWrapperFactory _sslStreamWrapperFactory; |
353 | | - |
354 | | - public ImplicitFtpsControlConnectionStreamAdapter( |
355 | | - ImplicitFtpsControlConnectionStreamAdapterOptions options, |
356 | | - ISslStreamWrapperFactory sslStreamWrapperFactory) |
357 | | - { |
358 | | - _options = options; |
359 | | - _sslStreamWrapperFactory = sslStreamWrapperFactory; |
360 | | - } |
361 | | - |
362 | | - /// <inheritdoc /> |
363 | | - public Task<Stream> WrapAsync(Stream stream, CancellationToken cancellationToken) |
364 | | - { |
365 | | - return _sslStreamWrapperFactory.WrapStreamAsync(stream, false, _options.Certificate, cancellationToken); |
366 | | - } |
367 | | - } |
368 | 315 | } |
369 | 316 | } |
0 commit comments