MigrateNetworkConfiguration.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 not null)
  50. {
  51. // Migrate network config values to new config schema
  52. var networkConfiguration = new NetworkConfiguration();
  53. networkConfiguration.AutoDiscovery = oldNetworkConfiguration.AutoDiscovery;
  54. networkConfiguration.BaseUrl = oldNetworkConfiguration.BaseUrl;
  55. networkConfiguration.CertificatePassword = oldNetworkConfiguration.CertificatePassword;
  56. networkConfiguration.CertificatePath = oldNetworkConfiguration.CertificatePath;
  57. networkConfiguration.EnableHttps = oldNetworkConfiguration.EnableHttps;
  58. networkConfiguration.EnableIPv4 = oldNetworkConfiguration.EnableIPV4;
  59. networkConfiguration.EnableIPv6 = oldNetworkConfiguration.EnableIPV6;
  60. networkConfiguration.EnablePublishedServerUriByRequest = oldNetworkConfiguration.EnablePublishedServerUriByRequest;
  61. networkConfiguration.EnableRemoteAccess = oldNetworkConfiguration.EnableRemoteAccess;
  62. networkConfiguration.EnableUPnP = oldNetworkConfiguration.EnableUPnP;
  63. networkConfiguration.IgnoreVirtualInterfaces = oldNetworkConfiguration.IgnoreVirtualInterfaces;
  64. networkConfiguration.InternalHttpPort = oldNetworkConfiguration.HttpServerPortNumber;
  65. networkConfiguration.InternalHttpsPort = oldNetworkConfiguration.HttpsPortNumber;
  66. networkConfiguration.IsRemoteIPFilterBlacklist = oldNetworkConfiguration.IsRemoteIPFilterBlacklist;
  67. networkConfiguration.KnownProxies = oldNetworkConfiguration.KnownProxies;
  68. networkConfiguration.LocalNetworkAddresses = oldNetworkConfiguration.LocalNetworkAddresses;
  69. networkConfiguration.LocalNetworkSubnets = oldNetworkConfiguration.LocalNetworkSubnets;
  70. networkConfiguration.PublicHttpPort = oldNetworkConfiguration.PublicPort;
  71. networkConfiguration.PublicHttpsPort = oldNetworkConfiguration.PublicHttpsPort;
  72. networkConfiguration.PublishedServerUriBySubnet = oldNetworkConfiguration.PublishedServerUriBySubnet;
  73. networkConfiguration.RemoteIPFilter = oldNetworkConfiguration.RemoteIPFilter;
  74. networkConfiguration.RequireHttps = oldNetworkConfiguration.RequireHttps;
  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. }
  91. #pragma warning disable
  92. public sealed class OldNetworkConfiguration
  93. {
  94. public const int DefaultHttpPort = 8096;
  95. public const int DefaultHttpsPort = 8920;
  96. private string _baseUrl = string.Empty;
  97. public bool RequireHttps { get; set; }
  98. public string CertificatePath { get; set; } = string.Empty;
  99. public string CertificatePassword { get; set; } = string.Empty;
  100. public string BaseUrl
  101. {
  102. get => _baseUrl;
  103. set
  104. {
  105. // Normalize the start of the string
  106. if (string.IsNullOrWhiteSpace(value))
  107. {
  108. // If baseUrl is empty, set an empty prefix string
  109. _baseUrl = string.Empty;
  110. return;
  111. }
  112. if (value[0] != '/')
  113. {
  114. // If baseUrl was not configured with a leading slash, append one for consistency
  115. value = "/" + value;
  116. }
  117. // Normalize the end of the string
  118. if (value[^1] == '/')
  119. {
  120. // If baseUrl was configured with a trailing slash, remove it for consistency
  121. value = value.Remove(value.Length - 1);
  122. }
  123. _baseUrl = value;
  124. }
  125. }
  126. public int PublicHttpsPort { get; set; } = DefaultHttpsPort;
  127. public int HttpServerPortNumber { get; set; } = DefaultHttpPort;
  128. public int HttpsPortNumber { get; set; } = DefaultHttpsPort;
  129. public bool EnableHttps { get; set; }
  130. public int PublicPort { get; set; } = DefaultHttpPort;
  131. public bool UPnPCreateHttpPortMap { get; set; }
  132. public string UDPPortRange { get; set; } = string.Empty;
  133. public bool EnableIPV6 { get; set; }
  134. public bool EnableIPV4 { get; set; } = true;
  135. public bool EnableSSDPTracing { get; set; }
  136. public string SSDPTracingFilter { get; set; } = string.Empty;
  137. public int UDPSendCount { get; set; } = 2;
  138. public int UDPSendDelay { get; set; } = 100;
  139. public bool IgnoreVirtualInterfaces { get; set; } = true;
  140. public string VirtualInterfaceNames { get; set; } = "vEthernet*";
  141. public int GatewayMonitorPeriod { get; set; } = 60;
  142. public bool EnableMultiSocketBinding { get; } = true;
  143. public bool TrustAllIP6Interfaces { get; set; }
  144. public string HDHomerunPortRange { get; set; } = string.Empty;
  145. public string[] PublishedServerUriBySubnet { get; set; } = Array.Empty<string>();
  146. public bool AutoDiscoveryTracing { get; set; }
  147. public bool AutoDiscovery { get; set; } = true;
  148. public string[] RemoteIPFilter { get; set; } = Array.Empty<string>();
  149. public bool IsRemoteIPFilterBlacklist { get; set; }
  150. public bool EnableUPnP { get; set; }
  151. public bool EnableRemoteAccess { get; set; } = true;
  152. public string[] LocalNetworkSubnets { get; set; } = Array.Empty<string>();
  153. public string[] LocalNetworkAddresses { get; set; } = Array.Empty<string>();
  154. public string[] KnownProxies { get; set; } = Array.Empty<string>();
  155. public bool EnablePublishedServerUriByRequest { get; set; } = false;
  156. }
  157. #pragma warning restore
  158. }