< 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: 65
Coverable lines: 65
Total lines: 211
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%
get_Id()100%210%
get_Name()100%210%
get_PerformOnNewInstall()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
 1using System;
 2using System.IO;
 3using System.Xml;
 4using System.Xml.Serialization;
 5using Emby.Server.Implementations;
 6using MediaBrowser.Common.Net;
 7using Microsoft.Extensions.Logging;
 8
 9namespace Jellyfin.Server.Migrations.PreStartupRoutines;
 10
 11/// <inheritdoc />
 12public class MigrateNetworkConfiguration : IMigrationRoutine
 13{
 14    private readonly ServerApplicationPaths _applicationPaths;
 15    private readonly ILogger<MigrateNetworkConfiguration> _logger;
 16
 17    /// <summary>
 18    /// Initializes a new instance of the <see cref="MigrateNetworkConfiguration"/> class.
 19    /// </summary>
 20    /// <param name="applicationPaths">An instance of <see cref="ServerApplicationPaths"/>.</param>
 21    /// <param name="loggerFactory">An instance of the <see cref="ILoggerFactory"/> interface.</param>
 22    public MigrateNetworkConfiguration(ServerApplicationPaths applicationPaths, ILoggerFactory loggerFactory)
 23    {
 024        _applicationPaths = applicationPaths;
 025        _logger = loggerFactory.CreateLogger<MigrateNetworkConfiguration>();
 026    }
 27
 28    /// <inheritdoc />
 029    public Guid Id => Guid.Parse("4FB5C950-1991-11EE-9B4B-0800200C9A66");
 30
 31    /// <inheritdoc />
 032    public string Name => nameof(MigrateNetworkConfiguration);
 33
 34    /// <inheritdoc />
 035    public bool PerformOnNewInstall => false;
 36
 37    /// <inheritdoc />
 38    public void Perform()
 39    {
 040        string path = Path.Combine(_applicationPaths.ConfigurationDirectoryPath, "network.xml");
 041        var oldNetworkConfigSerializer = new XmlSerializer(typeof(OldNetworkConfiguration), new XmlRootAttribute("Networ
 042        OldNetworkConfiguration? oldNetworkConfiguration = null;
 43
 44        try
 45        {
 046            using var xmlReader = XmlReader.Create(path);
 047            oldNetworkConfiguration = (OldNetworkConfiguration?)oldNetworkConfigSerializer.Deserialize(xmlReader);
 048        }
 049        catch (InvalidOperationException ex)
 50        {
 051            _logger.LogError(ex, "Migrate NetworkConfiguration deserialize Invalid Operation error");
 052        }
 053        catch (Exception ex)
 54        {
 055            _logger.LogError(ex, "Migrate NetworkConfiguration deserialize error");
 056        }
 57
 058        if (oldNetworkConfiguration is null)
 59        {
 060            return;
 61        }
 62
 63        // Migrate network config values to new config schema
 064        var networkConfiguration = new NetworkConfiguration
 065        {
 066            AutoDiscovery = oldNetworkConfiguration.AutoDiscovery,
 067            BaseUrl = oldNetworkConfiguration.BaseUrl,
 068            CertificatePassword = oldNetworkConfiguration.CertificatePassword,
 069            CertificatePath = oldNetworkConfiguration.CertificatePath,
 070            EnableHttps = oldNetworkConfiguration.EnableHttps,
 071            EnableIPv4 = oldNetworkConfiguration.EnableIPV4,
 072            EnableIPv6 = oldNetworkConfiguration.EnableIPV6,
 073            EnablePublishedServerUriByRequest = oldNetworkConfiguration.EnablePublishedServerUriByRequest,
 074            EnableRemoteAccess = oldNetworkConfiguration.EnableRemoteAccess,
 075            EnableUPnP = oldNetworkConfiguration.EnableUPnP,
 076            IgnoreVirtualInterfaces = oldNetworkConfiguration.IgnoreVirtualInterfaces,
 077            InternalHttpPort = oldNetworkConfiguration.HttpServerPortNumber,
 078            InternalHttpsPort = oldNetworkConfiguration.HttpsPortNumber,
 079            IsRemoteIPFilterBlacklist = oldNetworkConfiguration.IsRemoteIPFilterBlacklist,
 080            KnownProxies = oldNetworkConfiguration.KnownProxies,
 081            LocalNetworkAddresses = oldNetworkConfiguration.LocalNetworkAddresses,
 082            LocalNetworkSubnets = oldNetworkConfiguration.LocalNetworkSubnets,
 083            PublicHttpPort = oldNetworkConfiguration.PublicPort,
 084            PublicHttpsPort = oldNetworkConfiguration.PublicHttpsPort,
 085            PublishedServerUriBySubnet = oldNetworkConfiguration.PublishedServerUriBySubnet,
 086            RemoteIPFilter = oldNetworkConfiguration.RemoteIPFilter,
 087            RequireHttps = oldNetworkConfiguration.RequireHttps
 088        };
 89
 90        // Migrate old virtual interface name schema
 091        var oldVirtualInterfaceNames = oldNetworkConfiguration.VirtualInterfaceNames;
 092        if (oldVirtualInterfaceNames.Equals("vEthernet*", StringComparison.OrdinalIgnoreCase))
 93        {
 094            networkConfiguration.VirtualInterfaceNames = new string[] { "veth" };
 95        }
 96        else
 97        {
 098            networkConfiguration.VirtualInterfaceNames = oldVirtualInterfaceNames.Replace("*", string.Empty, StringCompa
 99        }
 100
 0101        var networkConfigSerializer = new XmlSerializer(typeof(NetworkConfiguration));
 0102        var xmlWriterSettings = new XmlWriterSettings { Indent = true };
 0103        using var xmlWriter = XmlWriter.Create(path, xmlWriterSettings);
 0104        networkConfigSerializer.Serialize(xmlWriter, networkConfiguration);
 0105    }
 106
 107#pragma warning disable
 108    public sealed class OldNetworkConfiguration
 109    {
 110        public const int DefaultHttpPort = 8096;
 111
 112        public const int DefaultHttpsPort = 8920;
 113
 0114        private string _baseUrl = string.Empty;
 115
 116        public bool RequireHttps { get; set; }
 117
 118        public string CertificatePath { get; set; } = string.Empty;
 119
 120        public string CertificatePassword { get; set; } = string.Empty;
 121
 122        public string BaseUrl
 123        {
 0124            get => _baseUrl;
 125            set
 126            {
 127                // Normalize the start of the string
 0128                if (string.IsNullOrWhiteSpace(value))
 129                {
 130                    // If baseUrl is empty, set an empty prefix string
 0131                    _baseUrl = string.Empty;
 0132                    return;
 133                }
 134
 0135                if (value[0] != '/')
 136                {
 137                    // If baseUrl was not configured with a leading slash, append one for consistency
 0138                    value = "/" + value;
 139                }
 140
 141                // Normalize the end of the string
 0142                if (value[^1] == '/')
 143                {
 144                    // If baseUrl was configured with a trailing slash, remove it for consistency
 0145                    value = value.Remove(value.Length - 1);
 146                }
 147
 0148                _baseUrl = value;
 0149            }
 150        }
 151
 152        public int PublicHttpsPort { get; set; } = DefaultHttpsPort;
 153
 154        public int HttpServerPortNumber { get; set; } = DefaultHttpPort;
 155
 156        public int HttpsPortNumber { get; set; } = DefaultHttpsPort;
 157
 158        public bool EnableHttps { get; set; }
 159
 160        public int PublicPort { get; set; } = DefaultHttpPort;
 161
 162        public bool UPnPCreateHttpPortMap { get; set; }
 163
 164        public string UDPPortRange { get; set; } = string.Empty;
 165
 166        public bool EnableIPV6 { get; set; }
 167
 168        public bool EnableIPV4 { get; set; } = true;
 169
 170        public bool EnableSSDPTracing { get; set; }
 171
 172        public string SSDPTracingFilter { get; set; } = string.Empty;
 173
 174        public int UDPSendCount { get; set; } = 2;
 175
 176        public int UDPSendDelay { get; set; } = 100;
 177
 178        public bool IgnoreVirtualInterfaces { get; set; } = true;
 179
 180        public string VirtualInterfaceNames { get; set; } = "vEthernet*";
 181
 182        public int GatewayMonitorPeriod { get; set; } = 60;
 183
 184        public bool EnableMultiSocketBinding { get; } = true;
 185
 186        public bool TrustAllIP6Interfaces { get; set; }
 187
 188        public string HDHomerunPortRange { get; set; } = string.Empty;
 189
 190        public string[] PublishedServerUriBySubnet { get; set; } = Array.Empty<string>();
 191
 192        public bool AutoDiscoveryTracing { get; set; }
 193
 194        public bool AutoDiscovery { get; set; } = true;
 195
 196        public string[] RemoteIPFilter { get; set; } = Array.Empty<string>();
 197
 198        public bool IsRemoteIPFilterBlacklist { get; set; }
 199
 200        public bool EnableUPnP { get; set; }
 201
 202        public bool EnableRemoteAccess { get; set; } = true;
 203
 204        public string[] LocalNetworkSubnets { get; set; } = Array.Empty<string>();
 205
 206        public string[] LocalNetworkAddresses { get; set; } = Array.Empty<string>();
 207        public string[] KnownProxies { get; set; } = Array.Empty<string>();
 208
 209        public bool EnablePublishedServerUriByRequest { get; set; } = false;
 210    }
 211}