aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Startup.cs
blob: f069c8180bf92dfbbefa23b23169c02c7340c7f8 (plain) (blame)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// <copyright file="Startup.cs" company="slskd Team">
//     Copyright (c) slskd Team. All rights reserved.
//
//     This program is free software: you can redistribute it and/or modify
//     it under the terms of the GNU Affero General Public License as published
//     by the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
//
//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU Affero General Public License for more details.
//
//     You should have received a copy of the GNU Affero General Public License
//     along with this program.  If not, see https://www.gnu.org/licenses/.
// </copyright>

namespace slskd
{
    using System;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Text.Json.Serialization;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Authentication.JwtBearer;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Diagnostics;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc.ApiExplorer;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.FileProviders;
    using Microsoft.IdentityModel.Tokens;
    using Microsoft.OpenApi.Models;
    using Prometheus;
    using Prometheus.SystemMetrics;
    using Serilog;
    using slskd.Authentication;
    using slskd.Core.API;
    using slskd.Integrations.FTP;
    using slskd.Integrations.Pushbullet;
    using slskd.Messaging;
    using slskd.Search;
    using slskd.Search.API;
    using slskd.Shares;
    using slskd.Transfers;
    using slskd.Users;
    using slskd.Validation;
    using Soulseek;

    /// <summary>
    ///     ASP.NET Startup.
    /// </summary>
    public class Startup
    {
        /// <summary>
        ///     Initializes a new instance of the <see cref="Startup"/> class.
        /// </summary>
        /// <param name="configuration">The application configuration root.</param>
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            OptionsAtStartup = new OptionsAtStartup();
            Configuration.GetSection(Program.AppName).Bind(OptionsAtStartup, (o) =>
            {
                o.BindNonPublicProperties = true;
            });

            UrlBase = OptionsAtStartup.Web.UrlBase;
            UrlBase = UrlBase.StartsWith("/") ? UrlBase : "/" + UrlBase;

            ContentPath = Path.GetFullPath(OptionsAtStartup.Web.ContentPath);

            JwtSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(OptionsAtStartup.Web.Authentication.Jwt.Key));
        }

        private IConfiguration Configuration { get; }
        private string ContentPath { get; set; }
        private SymmetricSecurityKey JwtSigningKey { get; set; }
        private OptionsAtStartup OptionsAtStartup { get; }
        private string UrlBase { get; set; }

        /// <summary>
        ///     Configure services.
        /// </summary>
        /// <param name="services">The service collection.</param>
        public void ConfigureServices(IServiceCollection services)
        {
            var logger = Log.ForContext<Startup>();

            // add the instance of OptionsAtStartup to DI as they were at startup. use when Options might change, but
            // the values at startup are to be used.
            services.AddSingleton(OptionsAtStartup);

            // add IOptionsMonitor and IOptionsSnapshot to DI.  use when the current Options are to be used.
            services.AddOptions<Options>()
                .Bind(Configuration.GetSection(Program.AppName), o => { o.BindNonPublicProperties = true; })
                .Validate(options =>
                {
                    if (!options.TryValidate(out var result))
                    {
                        logger.Warning("Options (re)configuration rejected.");
                        logger.Warning(result.GetResultView());
                        return false;
                    }

                    return true;
                });

            services.AddManagedState<State>();

            services.AddCors(options => options.AddPolicy("AllowAll", builder => builder
                .AllowAnyHeader()
                .AllowAnyMethod()
                .SetIsOriginAllowed((host) => true)
                .AllowCredentials()));

            services.AddSingleton(JwtSigningKey);

            if (!OptionsAtStartup.Web.Authentication.Disabled)
            {
                services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                    .AddJwtBearer(options =>
                    {
                        options.TokenValidationParameters = new TokenValidationParameters
                        {
                            ClockSkew = TimeSpan.FromMinutes(5),
                            RequireSignedTokens = true,
                            RequireExpirationTime = true,
                            ValidateLifetime = true,
                            ValidIssuer = Program.AppName,
                            ValidateIssuer = true,
                            ValidateAudience = false,
                            IssuerSigningKey = JwtSigningKey,
                            ValidateIssuerSigningKey = true,
                        };

                        options.Events = new JwtBearerEvents
                        {
                            OnMessageReceived = context =>
                            {
                                // assign the request token from the access_token query parameter
                                // but only if the destination is a SignalR hub
                                // https://docs.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view=aspnetcore-5.0
                                if (context.HttpContext.Request.Path.StartsWithSegments("/hub"))
                                {
                                    context.Token = context.Request.Query["access_token"];
                                }

                                return Task.CompletedTask;
                            },
                        };
                    });
            }
            else
            {
                logger.Warning("Authentication of web requests is DISABLED");

                services.AddAuthentication(PassthroughAuthentication.AuthenticationScheme)
                    .AddScheme<PassthroughAuthenticationOptions, PassthroughAuthenticationHandler>(PassthroughAuthentication.AuthenticationScheme, options =>
                    {
                        options.Username = "Anonymous";
                        options.Role = Role.Administrator;
                    });
            }

            services.AddRouting(options => options.LowercaseUrls = true);
            services.AddControllers().AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.Converters.Add(new IPAddressConverter());
                options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
                options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
            });

            services.AddSignalR().AddJsonProtocol(options =>
            {
                options.PayloadSerializerOptions.Converters.Add(new IPAddressConverter());
                options.PayloadSerializerOptions.Converters.Add(new JsonStringEnumConverter());
            });

            services.AddHealthChecks();

            services.AddApiVersioning(options => options.ReportApiVersions = true);
            services.AddVersionedApiExplorer(options =>
            {
                options.GroupNameFormat = "'v'VVV";
                options.SubstituteApiVersionInUrl = true;
            });

            if (OptionsAtStartup.Feature.Swagger)
            {
                services.AddSwaggerGen(options =>
                {
                    options.DescribeAllParametersInCamelCase();
                    options.SwaggerDoc(
                        "v0",
                        new OpenApiInfo
                        {
                            Title = Program.AppName,
                            Version = "v0",
                        });

                    if (System.IO.File.Exists(Program.XmlDocumentationFile))
                    {
                        options.IncludeXmlComments(Program.XmlDocumentationFile);
                    }
                    else
                    {
                        logger.Warning($"Unable to find XML documentation in {Program.XmlDocumentationFile}, Swagger will not include metadata");
                    }
                });
            }

            if (OptionsAtStartup.Feature.Prometheus)
            {
                services.AddSystemMetrics();
            }

            services.AddDbContextFactory<SearchDbContext>(options =>
            {
                options.UseSqlite($"Data Source={Path.Combine(Program.AppDirectory, "data", "search.db")}");
            });

            services.AddDbContextFactory<UserDbContext>(options =>
            {
                options.UseSqlite($"Data Source={Path.Combine(Program.AppDirectory, "data", "users.db")}");
            });

            services.AddHttpClient();

            // add a partially configured instance of SoulseekClient. the Application instance will
            // complete configuration at startup.
            services.AddSingleton<ISoulseekClient, SoulseekClient>(_ =>
                new SoulseekClient(options: new SoulseekClientOptions(minimumDiagnosticLevel: OptionsAtStartup.Soulseek.DiagnosticLevel)));

            // add the core application service to DI as well as a hosted service so that other services can
            // access instance methods
            services.AddSingleton<IApplication, Application>();
            services.AddHostedService(p => p.GetRequiredService<IApplication>());

            services.AddSingleton<ITransferTracker, TransferTracker>();
            services.AddSingleton<IBrowseTracker, BrowseTracker>();
            services.AddSingleton<IConversationTracker, ConversationTracker>();
            services.AddSingleton<IRoomTracker, RoomTracker>(_ => new RoomTracker(messageLimit: 250));

            services.AddSingleton<ISharedFileCache, SharedFileCache>();

            services.AddSingleton<ISearchService, SearchService>();
            services.AddSingleton<IUserService, UserService>();
            services.AddSingleton<IRoomService, RoomService>();

            services.AddSingleton<IFTPClientFactory, FTPClientFactory>();
            services.AddSingleton<IFTPService, FTPService>();

            services.AddSingleton<IPushbulletService, PushbulletService>();
        }

        /// <summary>
        ///     Configure middleware.
        /// </summary>
        /// <param name="app">The application builder.</param>
        /// <param name="apiVersionDescriptionProvider">The api version description provider.</param>
        public void Configure(
            IApplicationBuilder app,
            IApiVersionDescriptionProvider apiVersionDescriptionProvider)
        {
            var logger = Log.ForContext<Startup>();

            // initialize DbContext instances to avoid race conditions when a db needs to be
            // created from scratch. note that this method needs to be updated to include each
            // DbContext explicitly.
            InitializeDbContexts(app);

            app.UseExceptionHandler(a => a.Run(async context =>
            {
                await context.Response.WriteAsJsonAsync(context.Features.Get<IExceptionHandlerPathFeature>().Error.Message);
            }));

            app.UseCors("AllowAll");

            if (OptionsAtStartup.Web.Https.Force)
            {
                app.UseHttpsRedirection();
                app.UseHsts();

                logger.Information($"Forcing HTTP requests to HTTPS");
            }

            // allow users to specify a custom path base, for use behind a reverse proxy
            app.UsePathBase(UrlBase);
            logger.Information("Using base url {UrlBase}", UrlBase);

            // remove any errant double forward slashes which may have been introduced by manipulating the path base
            app.Use(async (context, next) =>
            {
                var path = context.Request.Path.ToString();

                if (path.StartsWith("//"))
                {
                    context.Request.Path = new string(path.Skip(1).ToArray());
                }

                await next();
            });

            FileServerOptions fileServerOptions = default;

            if (!System.IO.Directory.Exists(ContentPath))
            {
                logger.Warning($"Static content disabled; cannot find content path '{ContentPath}'");
            }
            else
            {
                fileServerOptions = new FileServerOptions
                {
                    FileProvider = new PhysicalFileProvider(ContentPath),
                    RequestPath = string.Empty,
                    EnableDirectoryBrowsing = false,
                    EnableDefaultFiles = true,
                };

                app.UseFileServer(fileServerOptions);
                logger.Information("Serving static content from {ContentPath}", ContentPath);
            }

            if (OptionsAtStartup.Feature.Prometheus)
            {
                app.UseHttpMetrics();
                logger.Information("Publishing Prometheus metrics to /metrics");
            }

            if (OptionsAtStartup.Web.Logging)
            {
                app.UseSerilogRequestLogging();
            }

            app.UseAuthentication();

            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<ApplicationHub>("/hub/application");
                endpoints.MapHub<LogsHub>("/hub/logs");
                endpoints.MapHub<SearchHub>("/hub/search");

                endpoints.MapControllers();
                endpoints.MapHealthChecks("/health");

                if (OptionsAtStartup.Feature.Prometheus)
                {
                    endpoints.MapMetrics("/metrics");
                }
            });

            if (OptionsAtStartup.Feature.Swagger)
            {
                app.UseSwagger();
                app.UseSwaggerUI(options => apiVersionDescriptionProvider.ApiVersionDescriptions.ToList()
                    .ForEach(description => options.SwaggerEndpoint($"{(UrlBase == "/" ? string.Empty : UrlBase)}/swagger/{description.GroupName}/swagger.json", description.GroupName)));

                logger.Information("Publishing Swagger documentation to /swagger");
            }

            // if we made it this far and the route still wasn't matched, return the index unless it's an api route. this is
            // required so that SPA routing (React Router, etc) can work properly
            app.Use(async (context, next) =>
            {
                // exclude API routes which are not matched or return a 404
                if (!context.Request.Path.StartsWithSegments("/api"))
                {
                    context.Request.Path = "/";
                }

                await next();
            });

            // finally, hit the fileserver again. if the path was modified to return the index above, the index document will be
            // returned. otherwise it will throw a final 404 back to the client.
            if (System.IO.Directory.Exists(ContentPath))
            {
                app.UseFileServer(fileServerOptions);
            }
        }

        private void InitializeDbContexts(IApplicationBuilder app)
        {
            var logger = Log.ForContext<Startup>();

            IDbContextFactory<T> GetFactory<T>()
                where T : DbContext
                => app.ApplicationServices.GetRequiredService<IDbContextFactory<T>>();

            logger.Debug("Initializing database contexts");

            try
            {
                using var search = GetFactory<SearchDbContext>().CreateDbContext();
                using var peer = GetFactory<UserDbContext>().CreateDbContext();
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Failed to initialize one or more application databases");
                throw;
            }
        }
    }
}