MigrateNetworkConfiguration.cs 8.0 KB

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