< Summary - Jellyfin

Information
Class: Jellyfin.Server.Migrations.PreStartupRoutines.MigrateNetworkConfiguration
Assembly: jellyfin
File(s): /srv/git/jellyfin/Jellyfin.Server/Migrations/PreStartupRoutines/MigrateNetworkConfiguration.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 62
Coverable lines: 62
Total lines: 205
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 10
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
Perform()0%2040%
.ctor()100%210%
get_BaseUrl()100%210%
set_BaseUrl(...)0%4260%

File(s)

/srv/git/jellyfin/Jellyfin.Server/Migrations/PreStartupRoutines/MigrateNetworkConfiguration.cs

#LineLine coverage
 1#pragma warning disable CS0618 // obsolete
 2
 3using System;
 4using System.IO;
 5using System.Xml;
 6using System.Xml.Serialization;
 7using Emby.Server.Implementations;
 8using MediaBrowser.Common.Net;
 9using Microsoft.Extensions.Logging;
 10
 11namespace Jellyfin.Server.Migrations.PreStartupRoutines;
 12
 13/// <inheritdoc />
 14[JellyfinMigration("2025-04-20T01:00:00", nameof(MigrateNetworkConfiguration), "4FB5C950-1991-11EE-9B4B-0800200C9A66", S
 15public class MigrateNetworkConfiguration : IMigrationRoutine
 16{
 17    private readonly ServerApplicationPaths _applicationPaths;
 18    private readonly ILogger<MigrateNetworkConfiguration> _logger;
 19
 20    /// <summary>
 21    /// Initializes a new instance of the <see cref="MigrateNetworkConfiguration"/> class.
 22    /// </summary>
 23    /// <param name="applicationPaths">An instance of <see cref="ServerApplicationPaths"/>.</param>
 24    /// <param name="loggerFactory">An instance of the <see cref="ILoggerFactory"/> interface.</param>
 25    public MigrateNetworkConfiguration(ServerApplicationPaths applicationPaths, ILoggerFactory loggerFactory)
 26    {
 027        _applicationPaths = applicationPaths;
 028        _logger = loggerFactory.CreateLogger<MigrateNetworkConfiguration>();
 029    }
 30
 31    /// <inheritdoc />
 32    public void Perform()
 33    {
 034        string path = Path.Combine(_applicationPaths.ConfigurationDirectoryPath, "network.xml");
 035        var oldNetworkConfigSerializer = new XmlSerializer(typeof(OldNetworkConfiguration), new XmlRootAttribute("Networ
 036        OldNetworkConfiguration? oldNetworkConfiguration = null;
 37
 38        try
 39        {
 040            using var xmlReader = XmlReader.Create(path);
 041            oldNetworkConfiguration = (OldNetworkConfiguration?)oldNetworkConfigSerializer.Deserialize(xmlReader);
 042        }
 043        catch (InvalidOperationException ex)
 44        {
 045            _logger.LogError(ex, "Migrate NetworkConfiguration deserialize Invalid Operation error");
 046        }
 047        catch (Exception ex)
 48        {
 049            _logger.LogError(ex, "Migrate NetworkConfiguration deserialize error");
 050        }
 51
 052        if (oldNetworkConfiguration is null)
 53        {
 054            return;
 55        }
 56
 57        // Migrate network config values to new config schema
 058        var networkConfiguration = new NetworkConfiguration
 059        {
 060            AutoDiscovery = oldNetworkConfiguration.AutoDiscovery,
 061            BaseUrl = oldNetworkConfiguration.BaseUrl,
 062            CertificatePassword = oldNetworkConfiguration.CertificatePassword,
 063            CertificatePath = oldNetworkConfiguration.CertificatePath,
 064            EnableHttps = oldNetworkConfiguration.EnableHttps,
 065            EnableIPv4 = oldNetworkConfiguration.EnableIPV4,
 066            EnableIPv6 = oldNetworkConfiguration.EnableIPV6,
 067            EnablePublishedServerUriByRequest = oldNetworkConfiguration.EnablePublishedServerUriByRequest,
 068            EnableRemoteAccess = oldNetworkConfiguration.EnableRemoteAccess,
 069            EnableUPnP = oldNetworkConfiguration.EnableUPnP,
 070            IgnoreVirtualInterfaces = oldNetworkConfiguration.IgnoreVirtualInterfaces,
 071            InternalHttpPort = oldNetworkConfiguration.HttpServerPortNumber,
 072            InternalHttpsPort = oldNetworkConfiguration.HttpsPortNumber,
 073            IsRemoteIPFilterBlacklist = oldNetworkConfiguration.IsRemoteIPFilterBlacklist,
 074            KnownProxies = oldNetworkConfiguration.KnownProxies,
 075            LocalNetworkAddresses = oldNetworkConfiguration.LocalNetworkAddresses,
 076            LocalNetworkSubnets = oldNetworkConfiguration.LocalNetworkSubnets,
 077            PublicHttpPort = oldNetworkConfiguration.PublicPort,
 078            PublicHttpsPort = oldNetworkConfiguration.PublicHttpsPort,
 079            PublishedServerUriBySubnet = oldNetworkConfiguration.PublishedServerUriBySubnet,
 080            RemoteIPFilter = oldNetworkConfiguration.RemoteIPFilter,
 081            RequireHttps = oldNetworkConfiguration.RequireHttps
 082        };
 83
 84        // Migrate old virtual interface name schema
 085        var oldVirtualInterfaceNames = oldNetworkConfiguration.VirtualInterfaceNames;
 086        if (oldVirtualInterfaceNames.Equals("vEthernet*", StringComparison.OrdinalIgnoreCase))
 87        {
 088            networkConfiguration.VirtualInterfaceNames = new string[] { "veth" };
 89        }
 90        else
 91        {
 092            networkConfiguration.VirtualInterfaceNames = oldVirtualInterfaceNames.Replace("*", string.Empty, StringCompa
 93        }
 94
 095        var networkConfigSerializer = new XmlSerializer(typeof(NetworkConfiguration));
 096        var xmlWriterSettings = new XmlWriterSettings { Indent = true };
 097        using var xmlWriter = XmlWriter.Create(path, xmlWriterSettings);
 098        networkConfigSerializer.Serialize(xmlWriter, networkConfiguration);
 099    }
 100
 101#pragma warning disable
 102    public sealed class OldNetworkConfiguration
 103    {
 104        public const int DefaultHttpPort = 8096;
 105
 106        public const int DefaultHttpsPort = 8920;
 107
 0108        private string _baseUrl = string.Empty;
 109
 110        public bool RequireHttps { get; set; }
 111
 112        public string CertificatePath { get; set; } = string.Empty;
 113
 114        public string CertificatePassword { get; set; } = string.Empty;
 115
 116        public string BaseUrl
 117        {
 0118            get => _baseUrl;
 119            set
 120            {
 121                // Normalize the start of the string
 0122                if (string.IsNullOrWhiteSpace(value))
 123                {
 124                    // If baseUrl is empty, set an empty prefix string
 0125                    _baseUrl = string.Empty;
 0126                    return;
 127                }
 128
 0129                if (value[0] != '/')
 130                {
 131                    // If baseUrl was not configured with a leading slash, append one for consistency
 0132                    value = "/" + value;
 133                }
 134
 135                // Normalize the end of the string
 0136                if (value[^1] == '/')
 137                {
 138                    // If baseUrl was configured with a trailing slash, remove it for consistency
 0139                    value = value.Remove(value.Length - 1);
 140                }
 141
 0142                _baseUrl = value;
 0143            }
 144        }
 145
 146        public int PublicHttpsPort { get; set; } = DefaultHttpsPort;
 147
 148        public int HttpServerPortNumber { get; set; } = DefaultHttpPort;
 149
 150        public int HttpsPortNumber { get; set; } = DefaultHttpsPort;
 151
 152        public bool EnableHttps { get; set; }
 153
 154        public int PublicPort { get; set; } = DefaultHttpPort;
 155
 156        public bool UPnPCreateHttpPortMap { get; set; }
 157
 158        public string UDPPortRange { get; set; } = string.Empty;
 159
 160        public bool EnableIPV6 { get; set; }
 161
 162        public bool EnableIPV4 { get; set; } = true;
 163
 164        public bool EnableSSDPTracing { get; set; }
 165
 166        public string SSDPTracingFilter { get; set; } = string.Empty;
 167
 168        public int UDPSendCount { get; set; } = 2;
 169
 170        public int UDPSendDelay { get; set; } = 100;
 171
 172        public bool IgnoreVirtualInterfaces { get; set; } = true;
 173
 174        public string VirtualInterfaceNames { get; set; } = "vEthernet*";
 175
 176        public int GatewayMonitorPeriod { get; set; } = 60;
 177
 178        public bool EnableMultiSocketBinding { get; } = true;
 179
 180        public bool TrustAllIP6Interfaces { get; set; }
 181
 182        public string HDHomerunPortRange { get; set; } = string.Empty;
 183
 184        public string[] PublishedServerUriBySubnet { get; set; } = Array.Empty<string>();
 185
 186        public bool AutoDiscoveryTracing { get; set; }
 187
 188        public bool AutoDiscovery { get; set; } = true;
 189
 190        public string[] RemoteIPFilter { get; set; } = Array.Empty<string>();
 191
 192        public bool IsRemoteIPFilterBlacklist { get; set; }
 193
 194        public bool EnableUPnP { get; set; }
 195
 196        public bool EnableRemoteAccess { get; set; } = true;
 197
 198        public string[] LocalNetworkSubnets { get; set; } = Array.Empty<string>();
 199
 200        public string[] LocalNetworkAddresses { get; set; } = Array.Empty<string>();
 201        public string[] KnownProxies { get; set; } = Array.Empty<string>();
 202
 203        public bool EnablePublishedServerUriByRequest { get; set; } = false;
 204    }
 205}