MigrateNetworkConfiguration.cs 8.0 KB

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