-
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathServiceCollectionExtensions.cs
More file actions
158 lines (128 loc) · 7.66 KB
/
ServiceCollectionExtensions.cs
File metadata and controls
158 lines (128 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// <copyright file="ServiceCollectionExtensions.cs" company="Fubar Development Junker">
// Copyright (c) Fubar Development Junker. All rights reserved.
// </copyright>
using System;
using System.Reflection;
using FubarDev.FtpServer;
using FubarDev.FtpServer.AccountManagement.Directories.SingleRootWithoutHome;
using FubarDev.FtpServer.Authentication;
using FubarDev.FtpServer.Authorization;
using FubarDev.FtpServer.BackgroundTransfer;
using FubarDev.FtpServer.CommandExtensions;
using FubarDev.FtpServer.CommandHandlers;
using FubarDev.FtpServer.Commands;
using FubarDev.FtpServer.DataConnection;
using FubarDev.FtpServer.FileSystem;
using FubarDev.FtpServer.Localization;
using FubarDev.FtpServer.Networking;
using FubarDev.FtpServer.ServerCommandHandlers;
using FubarDev.FtpServer.ServerCommands;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods for <see cref="IServiceCollection"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds the FTP server services to the collection.
/// </summary>
/// <param name="services">The service collection to add the FTP server services to.</param>
/// <param name="configure">Configuration of the FTP server services.</param>
/// <returns>The service collection.</returns>
public static IServiceCollection AddFtpServer(
this IServiceCollection services,
Action<IFtpServerBuilder> configure)
{
services.AddOptions();
services.AddSingleton<IFtpServer, FtpServer>();
services.AddSingleton<IFtpListenerService, DefaultFtpListenerService>();
services.AddSingleton<ITemporaryDataFactory, TemporaryDataFactory>();
services.AddSingleton<IPasvListenerFactory, PasvListenerFactory>();
services.AddSingleton<IPasvAddressResolver, SimplePasvAddressResolver>();
services.AddSingleton<IFtpConnectionAccessor, FtpConnectionAccessor>();
var commandAssembly = typeof(PassCommandHandler).GetTypeInfo().Assembly;
// Command handlers
services.AddSingleton<IFtpCommandHandlerScanner>(
_ => new AssemblyFtpCommandHandlerScanner(commandAssembly));
#pragma warning disable CS0612 // Typ oder Element ist veraltet
services.AddScoped<IFtpCommandHandlerScanner, ServiceFtpCommandHandlerScanner>();
#pragma warning restore CS0612 // Typ oder Element ist veraltet
services.TryAddScoped<IFtpCommandHandlerProvider, DefaultFtpCommandHandlerProvider>();
// Command handler extensions
services.AddScoped<IFtpCommandHandlerExtensionScanner>(
sp => new AssemblyFtpCommandHandlerExtensionScanner(
sp.GetRequiredService<IFtpCommandHandlerProvider>(),
sp.GetService<ILogger<AssemblyFtpCommandHandlerExtensionScanner>>(),
commandAssembly));
#pragma warning disable CS0612 // Typ oder Element ist veraltet
services.AddScoped<IFtpCommandHandlerExtensionScanner, ServiceFtpCommandHandlerExtensionScanner>();
#pragma warning restore CS0612 // Typ oder Element ist veraltet
services.TryAddScoped<IFtpCommandHandlerExtensionProvider, DefaultFtpCommandHandlerExtensionProvider>();
// Activator for FTP commands (and extensions)
services.AddScoped<IFtpCommandActivator, DefaultFtpCommandActivator>();
// Feature provider
services.AddScoped<IFeatureInfoProvider, DefaultFeatureInfoProvider>();
services.AddScoped<TcpSocketClientAccessor>();
services.AddScoped(sp => sp.GetRequiredService<TcpSocketClientAccessor>().TcpSocketClient ?? throw new NullReferenceException("TCP socket client null"));
services.AddScoped<IFtpConnection, FtpConnection>();
services.AddScoped<IFtpLoginStateMachine, FtpLoginStateMachine>();
#pragma warning disable 618
services.AddScoped<IBackgroundCommandHandler>(sp => new BackgroundCommandHandler(sp.GetRequiredService<IFtpConnection>()));
#pragma warning restore 618
services.AddScoped<IFtpCommandDispatcher, DefaultFtpCommandDispatcher>();
services.AddScoped<IFtpHostSelector, SingleFtpHostSelector>();
services.AddSingleton<IFtpCatalogLoader, DefaultFtpCatalogLoader>();
services.TryAddSingleton<IFtpServerMessages, DefaultFtpServerMessages>();
services.AddSingleton<IBackgroundTransferWorker, BackgroundTransferWorker>();
services.AddSingleton(sp => (IFtpService)sp.GetRequiredService<IFtpServer>());
services.AddSingleton(sp => (IFtpService)sp.GetRequiredService<IBackgroundTransferWorker>());
services.AddSingleton<IFtpServerHost, FtpServerHost>();
services.AddSingleton<ISslStreamWrapperFactory, DefaultSslStreamWrapperFactory>();
services.TryAddSingleton<IAccountDirectoryQuery, SingleRootWithoutHomeAccountDirectoryQuery>();
services.Scan(
sel => sel.FromAssemblyOf<IAuthorizationAction>()
.AddClasses(filter => filter.AssignableTo<IAuthorizationAction>()).As<IAuthorizationAction>().WithSingletonLifetime());
services.Scan(
sel => sel.FromAssemblyOf<PasswordAuthorization>()
.AddClasses(filter => filter.AssignableTo<IAuthorizationMechanism>()).As<IAuthorizationMechanism>().WithScopedLifetime());
services.Scan(
sel => sel.FromAssemblyOf<TlsAuthenticationMechanism>()
.AddClasses(filter => filter.AssignableTo<IAuthenticationMechanism>()).As<IAuthenticationMechanism>().WithScopedLifetime());
services
.AddScoped<IServerCommandExecutor, ReflectionServerCommandExecutor>();
services
.AddScoped<IServerCommandHandler<SendResponseServerCommand>, SendResponseServerCommandHandler>()
.AddScoped<IServerCommandHandler<CloseConnectionServerCommand>, CloseConnectionServerCommandHandler>()
.AddScoped<IServerCommandHandler<TlsEnableServerCommand>, TlsEnableServerCommandHandler>()
.AddScoped<IServerCommandHandler<PauseConnectionServerCommand>, PauseConnectionServerCommandHandler>()
.AddScoped<IServerCommandHandler<ResumeConnectionServerCommand>, ResumeConnectionServerCommandHandler>()
.AddScoped<IServerCommandHandler<DataConnectionServerCommand>, DataConnectionServerCommandHandler>()
.AddScoped<IServerCommandHandler<CloseDataConnectionServerCommand>, CloseDataConnectionServerCommandHandler>();
services
.AddSingleton<ActiveDataConnectionFeatureFactory>()
.AddSingleton<PassiveDataConnectionFeatureFactory>()
.AddSingleton<SecureDataConnectionWrapper>();
services
.AddSingleton<IFtpDataConnectionValidator, PromiscuousPasvDataConnectionValidator>();
configure(new FtpServerBuilder(services).EnableDefaultChecks());
return services;
}
private class FtpServerBuilder : IFtpServerBuilder
{
/// <summary>
/// Initializes a new instance of the <see cref="FtpServerBuilder"/> class.
/// </summary>
/// <param name="services">The service collection.</param>
public FtpServerBuilder(IServiceCollection services)
{
Services = services;
}
/// <inheritdoc />
public IServiceCollection Services { get; }
}
}
}