MigrateNetworkConfiguration.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #pragma warning disable CS0618 // obsolete
  2. using System;
  3. using System.IO;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6. using Emby.Server.Implementations;
  7. using MediaBrowser.Common.Net;
  8. using Microsoft.Extensions.Logging;
  9. namespace Jellyfin.Server.Migrations.PreStartupRoutines;
  10. /// <inheritdoc />
  11. [JellyfinMigration("2025-04-20T01:00:00", nameof(MigrateNetworkConfiguration), "4FB5C950-1991-11EE-9B4B-0800200C9A66", Stage = Stages.JellyfinMigrationStageTypes.PreInitialisation)]
  12. public class MigrateNetworkConfiguration : IMigrationRoutine
  13. {
  14. private readonly ServerApplicationPaths _applicationPaths;
  15. private readonly ILogger<MigrateNetworkConfiguration> _logger;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="MigrateNetworkConfiguration"/> class.
  18. /// </summary>
  19. /// <param name="applicationPaths">An instance of <see cref="ServerApplicationPaths"/>.</param>
  20. /// <param name="loggerFactory">An instance of the <see cref="ILoggerFactory"/> interface.</param>
  21. public MigrateNetworkConfiguration(ServerApplicationPaths applicationPaths, ILoggerFactory loggerFactory)
  22. {
  23. _applicationPaths = applicationPaths;
  24. _logger = loggerFactory.CreateLogger<MigrateNetworkConfiguration>();
  25. }
  26. /// <inheritdoc />
  27. public void Perform()
  28. {
  29. string path = Path.Combine(_applicationPaths.ConfigurationDirectoryPath, "network.xml");
  30. var oldNetworkConfigSerializer = new XmlSerializer(typeof(OldNetworkConfiguration), new XmlRootAttribute("NetworkConfiguration"));
  31. OldNetworkConfiguration? oldNetworkConfiguration = null;
  32. try
  33. {
  34. using var xmlReader = XmlReader.Create(path);
  35. oldNetworkConfiguration = (OldNetworkConfiguration?)oldNetworkConfigSerializer.Deserialize(xmlReader);
  36. }
  37. catch (InvalidOperationException ex)
  38. {
  39. _logger.LogError(ex, "Migrate NetworkConfiguration deserialize Invalid Operation error");
  40. }
  41. catch (Exception ex)
  42. {
  43. _logger.LogError(ex, "Migrate NetworkConfiguration deserialize error");
  44. }
  45. if (oldNetworkConfiguration is null)
  46. {
  47. return;
  48. }
  49. // Migrate network config values to new config schema
  50. var networkConfiguration = new NetworkConfiguration
  51. {
  52. AutoDiscovery = oldNetworkConfiguration.AutoDiscovery,
  53. BaseUrl = oldNetworkConfiguration.BaseUrl,
  54. CertificatePassword = oldNetworkConfiguration.CertificatePassword,
  55. CertificatePath = oldNetworkConfiguration.CertificatePath,
  56. EnableHttps = oldNetworkConfiguration.EnableHttps,
  57. EnableIPv4 = oldNetworkConfiguration.EnableIPV4,
  58. EnableIPv6 = oldNetworkConfiguration.EnableIPV6,
  59. EnablePublishedServerUriByRequest = oldNetworkConfiguration.EnablePublishedServerUriByRequest,
  60. EnableRemoteAccess = oldNetworkConfiguration.EnableRemoteAccess,
  61. EnableUPnP = oldNetworkConfiguration.EnableUPnP,
  62. IgnoreVirtualInterfaces = oldNetworkConfiguration.IgnoreVirtualInterfaces,
  63. InternalHttpPort = oldNetworkConfiguration.HttpServerPortNumber,
  64. InternalHttpsPort = oldNetworkConfiguration.HttpsPortNumber,
  65. IsRemoteIPFilterBlacklist = oldNetworkConfiguration.IsRemoteIPFilterBlacklist,
  66. KnownProxies = oldNetworkConfiguration.KnownProxies,
  67. LocalNetworkAddresses = oldNetworkConfiguration.LocalNetworkAddresses,
  68. LocalNetworkSubnets = oldNetworkConfiguration.LocalNetworkSubnets,
  69. PublicHttpPort = oldNetworkConfiguration.PublicPort,
  70. PublicHttpsPort = oldNetworkConfiguration.PublicHttpsPort,
  71. PublishedServerUriBySubnet = oldNetworkConfiguration.PublishedServerUriBySubnet,
  72. RemoteIPFilter = oldNetworkConfiguration.RemoteIPFilter,
  73. RequireHttps = oldNetworkConfiguration.RequireHttps
  74. };
  75. // Migrate old virtual interface name schema
  76. var oldVirtualInterfaceNames = oldNetworkConfiguration.VirtualInterfaceNames;
  77. if (oldVirtualInterfaceNames.Equals("vEthernet*", StringComparison.OrdinalIgnoreCase))
  78. {
  79. networkConfiguration.VirtualInterfaceNames = new string[] { "veth" };
  80. }
  81. else
  82. {
  83. networkConfiguration.VirtualInterfaceNames = oldVirtualInterfaceNames.Replace("*", string.Empty, StringComparison.OrdinalIgnoreCase).Split(',');
  84. }
  85. var networkConfigSerializer = new XmlSerializer(typeof(NetworkConfiguration));
  86. var xmlWriterSettings = new XmlWriterSettings { Indent = true };
  87. using var xmlWriter = XmlWriter.Create(path, xmlWriterSettings);
  88. networkConfigSerializer.Serialize(xmlWriter, networkConfiguration);
  89. }
  90. #pragma warning disable
  91. public sealed class OldNetworkConfiguration
  92. {
  93. public const int DefaultHttpPort = 8096;
  94. public const int DefaultHttpsPort = 8920;
  95. private string _baseUrl = string.Empty;
  96. public bool RequireHttps { get; set; }
  97. public string CertificatePath { get; set; } = string.Empty;
  98. public string CertificatePassword { get; set; } = string.Empty;
  99. public string BaseUrl
  100. {
  101. get => _baseUrl;
  102. set
  103. {
  104. // Normalize the start of the string
  105. if (string.IsNullOrWhiteSpace(value))
  106. {
  107. // If baseUrl is empty, set an empty prefix string
  108. _baseUrl = string.Empty;
  109. return;
  110. }
  111. if (value[0] != '/')
  112. {
  113. // If baseUrl was not configured with a leading slash, append one for consistency
  114. value = "/" + value;
  115. }
  116. // Normalize the end of the string
  117. if (value[^1] == '/')
  118. {
  119. // If baseUrl was configured with a trailing slash, remove it for consistency
  120. value = value.Remove(value.Length - 1);
  121. }
  122. _baseUrl = value;
  123. }
  124. }
  125. public int PublicHttpsPort { get; set; } = DefaultHttpsPort;
  126. public int HttpServerPortNumber { get; set; } = DefaultHttpPort;
  127. public int HttpsPortNumber { get; set; } = DefaultHttpsPort;
  128. public bool EnableHttps { get; set; }
  129. public int PublicPort { get; set; } = DefaultHttpPort;
  130. public bool UPnPCreateHttpPortMap { get; set; }
  131. public string UDPPortRange { get; set; } = string.Empty;
  132. public bool EnableIPV6 { get; set; }
  133. public bool EnableIPV4 { get; set; } = true;
  134. public bool EnableSSDPTracing { get; set; }
  135. public string SSDPTracingFilter { get; set; } = string.Empty;
  136. public int UDPSendCount { get; set; } = 2;
  137. public int UDPSendDelay { get; set; } = 100;
  138. public bool IgnoreVirtualInterfaces { get; set; } = true;
  139. public string VirtualInterfaceNames { get; set; } = "vEthernet*";
  140. public int GatewayMonitorPeriod { get; set; } = 60;
  141. public bool EnableMultiSocketBinding { get; } = true;
  142. public bool TrustAllIP6Interfaces { get; set; }
  143. public string HDHomerunPortRange { get; set; } = string.Empty;
  144. public string[] PublishedServerUriBySubnet { get; set; } = Array.Empty<string>();
  145. public bool AutoDiscoveryTracing { get; set; }
  146. public bool AutoDiscovery { get; set; } = true;
  147. public string[] RemoteIPFilter { get; set; } = Array.Empty<string>();
  148. public bool IsRemoteIPFilterBlacklist { get; set; }
  149. public bool EnableUPnP { get; set; }
  150. public bool EnableRemoteAccess { get; set; } = true;
  151. public string[] LocalNetworkSubnets { get; set; } = Array.Empty<string>();
  152. public string[] LocalNetworkAddresses { get; set; } = Array.Empty<string>();
  153. public string[] KnownProxies { get; set; } = Array.Empty<string>();
  154. public bool EnablePublishedServerUriByRequest { get; set; } = false;
  155. }
  156. }