MigrateNetworkConfiguration.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Xml.Serialization;
  5. using Emby.Server.Implementations;
  6. using Jellyfin.Networking.Configuration;
  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. {
  40. oldNetworkConfiguration = (OldNetworkConfiguration?)oldNetworkConfigSerializer.Deserialize(xmlReader);
  41. }
  42. }
  43. catch (InvalidOperationException ex)
  44. {
  45. _logger.LogError(ex, "Migrate NetworkConfiguration deserialize Invalid Operation error");
  46. }
  47. catch (Exception ex)
  48. {
  49. _logger.LogError(ex, "Migrate NetworkConfiguration deserialize error");
  50. }
  51. if (oldNetworkConfiguration is not null)
  52. {
  53. // Migrate network config values to new config schema
  54. var networkConfiguration = new NetworkConfiguration();
  55. networkConfiguration.AutoDiscovery = oldNetworkConfiguration.AutoDiscovery;
  56. networkConfiguration.BaseUrl = oldNetworkConfiguration.BaseUrl;
  57. networkConfiguration.CertificatePassword = oldNetworkConfiguration.CertificatePassword;
  58. networkConfiguration.CertificatePath = oldNetworkConfiguration.CertificatePath;
  59. networkConfiguration.EnableHttps = oldNetworkConfiguration.EnableHttps;
  60. networkConfiguration.EnableIPv4 = oldNetworkConfiguration.EnableIPV4;
  61. networkConfiguration.EnableIPv6 = oldNetworkConfiguration.EnableIPV6;
  62. networkConfiguration.EnablePublishedServerUriByRequest = oldNetworkConfiguration.EnablePublishedServerUriByRequest;
  63. networkConfiguration.EnableRemoteAccess = oldNetworkConfiguration.EnableRemoteAccess;
  64. networkConfiguration.EnableUPnP = oldNetworkConfiguration.EnableUPnP;
  65. networkConfiguration.IgnoreVirtualInterfaces = oldNetworkConfiguration.IgnoreVirtualInterfaces;
  66. networkConfiguration.InternalHttpPort = oldNetworkConfiguration.HttpServerPortNumber;
  67. networkConfiguration.InternalHttpsPort = oldNetworkConfiguration.HttpsPortNumber;
  68. networkConfiguration.IsRemoteIPFilterBlacklist = oldNetworkConfiguration.IsRemoteIPFilterBlacklist;
  69. networkConfiguration.KnownProxies = oldNetworkConfiguration.KnownProxies;
  70. networkConfiguration.LocalNetworkAddresses = oldNetworkConfiguration.LocalNetworkAddresses;
  71. networkConfiguration.LocalNetworkSubnets = oldNetworkConfiguration.LocalNetworkSubnets;
  72. networkConfiguration.PublicHttpPort = oldNetworkConfiguration.PublicPort;
  73. networkConfiguration.PublicHttpsPort = oldNetworkConfiguration.PublicHttpsPort;
  74. networkConfiguration.PublishedServerUriBySubnet = oldNetworkConfiguration.PublishedServerUriBySubnet;
  75. networkConfiguration.RemoteIPFilter = oldNetworkConfiguration.RemoteIPFilter;
  76. networkConfiguration.RequireHttps = oldNetworkConfiguration.RequireHttps;
  77. // Migrate old virtual interface name schema
  78. var oldVirtualInterfaceNames = oldNetworkConfiguration.VirtualInterfaceNames;
  79. if (oldVirtualInterfaceNames.Equals("vEthernet*", StringComparison.OrdinalIgnoreCase))
  80. {
  81. networkConfiguration.VirtualInterfaceNames = new string[] { "veth" };
  82. }
  83. else
  84. {
  85. networkConfiguration.VirtualInterfaceNames = oldVirtualInterfaceNames.Replace("*", string.Empty, StringComparison.OrdinalIgnoreCase).Split(',');
  86. }
  87. var networkConfigSerializer = new XmlSerializer(typeof(NetworkConfiguration));
  88. var xmlWriterSettings = new XmlWriterSettings { Indent = true };
  89. using (var xmlWriter = XmlWriter.Create(path, xmlWriterSettings))
  90. {
  91. networkConfigSerializer.Serialize(xmlWriter, networkConfiguration);
  92. }
  93. }
  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. #pragma warning restore
  162. }