aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server/Extensions/WebHostBuilderExtensions.cs
blob: 58d3e1b2d99bf7885ac8a5e142e1a5022774be23 (plain)
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
using System;
using System.IO;
using System.Net;
using Jellyfin.Server.Helpers;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Extensions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Server.Extensions;

/// <summary>
/// Extensions for configuring the web host builder.
/// </summary>
public static class WebHostBuilderExtensions
{
    /// <summary>
    /// Configure the web host builder.
    /// </summary>
    /// <param name="builder">The builder to configure.</param>
    /// <param name="appHost">The application host.</param>
    /// <param name="startupConfig">The application configuration.</param>
    /// <param name="appPaths">The application paths.</param>
    /// <param name="logger">The logger.</param>
    /// <returns>The configured web host builder.</returns>
    public static IWebHostBuilder ConfigureWebHostBuilder(
        this IWebHostBuilder builder,
        CoreAppHost appHost,
        IConfiguration startupConfig,
        IApplicationPaths appPaths,
        ILogger logger)
    {
        return builder
            .UseKestrel((builderContext, options) =>
            {
                var addresses = appHost.NetManager.GetAllBindInterfaces();

                bool flagged = false;
                foreach (IPObject netAdd in addresses)
                {
                    logger.LogInformation("Kestrel listening on {Address}", IPAddress.IPv6Any.Equals(netAdd.Address) ? "All Addresses" : netAdd);
                    options.Listen(netAdd.Address, appHost.HttpPort);
                    if (appHost.ListenWithHttps)
                    {
                        options.Listen(
                            netAdd.Address,
                            appHost.HttpsPort,
                            listenOptions => listenOptions.UseHttps(appHost.Certificate));
                    }
                    else if (builderContext.HostingEnvironment.IsDevelopment())
                    {
                        try
                        {
                            options.Listen(
                                netAdd.Address,
                                appHost.HttpsPort,
                                listenOptions => listenOptions.UseHttps());
                        }
                        catch (InvalidOperationException)
                        {
                            if (!flagged)
                            {
                                logger.LogWarning("Failed to listen to HTTPS using the ASP.NET Core HTTPS development certificate. Please ensure it has been installed and set as trusted");
                                flagged = true;
                            }
                        }
                    }
                }

                // Bind to unix socket (only on unix systems)
                if (startupConfig.UseUnixSocket() && Environment.OSVersion.Platform == PlatformID.Unix)
                {
                    var socketPath = StartupHelpers.GetUnixSocketPath(startupConfig, appPaths);

                    // Workaround for https://github.com/aspnet/AspNetCore/issues/14134
                    if (File.Exists(socketPath))
                    {
                        File.Delete(socketPath);
                    }

                    options.ListenUnixSocket(socketPath);
                    logger.LogInformation("Kestrel listening to unix socket {SocketPath}", socketPath);
                }
            })
            .UseStartup(_ => new Startup(appHost));
    }
}