2
0

MigrateNetworkConfiguration.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. using var xmlReader = XmlReader.Create(path);
  36. var oldNetworkConfiguration = (OldNetworkConfiguration?)oldNetworkConfigSerializer.Deserialize(xmlReader);
  37. if (oldNetworkConfiguration is not null)
  38. {
  39. // Migrate network config values to new config schema
  40. var networkConfiguration = new NetworkConfiguration();
  41. networkConfiguration.AutoDiscovery = oldNetworkConfiguration.AutoDiscovery;
  42. networkConfiguration.BaseUrl = oldNetworkConfiguration.BaseUrl;
  43. networkConfiguration.CertificatePassword = oldNetworkConfiguration.CertificatePassword;
  44. networkConfiguration.CertificatePath = oldNetworkConfiguration.CertificatePath;
  45. networkConfiguration.EnableHttps = oldNetworkConfiguration.EnableHttps;
  46. networkConfiguration.EnableIPv4 = oldNetworkConfiguration.EnableIPV4;
  47. networkConfiguration.EnableIPv6 = oldNetworkConfiguration.EnableIPV6;
  48. networkConfiguration.EnablePublishedServerUriByRequest = oldNetworkConfiguration.EnablePublishedServerUriByRequest;
  49. networkConfiguration.EnableRemoteAccess = oldNetworkConfiguration.EnableRemoteAccess;
  50. networkConfiguration.EnableUPnP = oldNetworkConfiguration.EnableUPnP;
  51. networkConfiguration.IgnoreVirtualInterfaces = oldNetworkConfiguration.IgnoreVirtualInterfaces;
  52. networkConfiguration.InternalHttpPort = oldNetworkConfiguration.HttpServerPortNumber;
  53. networkConfiguration.InternalHttpsPort = oldNetworkConfiguration.HttpsPortNumber;
  54. networkConfiguration.IsRemoteIPFilterBlacklist = oldNetworkConfiguration.IsRemoteIPFilterBlacklist;
  55. networkConfiguration.KnownProxies = oldNetworkConfiguration.KnownProxies;
  56. networkConfiguration.LocalNetworkAddresses = oldNetworkConfiguration.LocalNetworkAddresses;
  57. networkConfiguration.LocalNetworkSubnets = oldNetworkConfiguration.LocalNetworkSubnets;
  58. networkConfiguration.PublicHttpPort = oldNetworkConfiguration.PublicPort;
  59. networkConfiguration.PublicHttpsPort = oldNetworkConfiguration.PublicHttpsPort;
  60. networkConfiguration.PublishedServerUriBySubnet = oldNetworkConfiguration.PublishedServerUriBySubnet;
  61. networkConfiguration.RemoteIPFilter = oldNetworkConfiguration.RemoteIPFilter;
  62. networkConfiguration.RequireHttps = oldNetworkConfiguration.RequireHttps;
  63. // Migrate old virtual interface name schema
  64. var oldVirtualInterfaceNames = oldNetworkConfiguration.VirtualInterfaceNames;
  65. if (oldVirtualInterfaceNames.Equals("vEthernet*", StringComparison.OrdinalIgnoreCase))
  66. {
  67. networkConfiguration.VirtualInterfaceNames = new string[] { "veth" };
  68. }
  69. else
  70. {
  71. networkConfiguration.VirtualInterfaceNames = oldVirtualInterfaceNames.Replace("*", string.Empty, StringComparison.OrdinalIgnoreCase).Split(',');
  72. }
  73. var networkConfigSerializer = new XmlSerializer(typeof(NetworkConfiguration));
  74. var xmlWriterSettings = new XmlWriterSettings { Indent = true };
  75. using var xmlWriter = XmlWriter.Create(path, xmlWriterSettings);
  76. networkConfigSerializer.Serialize(xmlWriter, networkConfiguration);
  77. }
  78. }
  79. #pragma warning disable
  80. public sealed class OldNetworkConfiguration
  81. {
  82. public const int DefaultHttpPort = 8096;
  83. public const int DefaultHttpsPort = 8920;
  84. private string _baseUrl = string.Empty;
  85. public bool RequireHttps { get; set; }
  86. public string CertificatePath { get; set; } = string.Empty;
  87. public string CertificatePassword { get; set; } = string.Empty;
  88. public string BaseUrl
  89. {
  90. get => _baseUrl;
  91. set
  92. {
  93. // Normalize the start of the string
  94. if (string.IsNullOrWhiteSpace(value))
  95. {
  96. // If baseUrl is empty, set an empty prefix string
  97. _baseUrl = string.Empty;
  98. return;
  99. }
  100. if (value[0] != '/')
  101. {
  102. // If baseUrl was not configured with a leading slash, append one for consistency
  103. value = "/" + value;
  104. }
  105. // Normalize the end of the string
  106. if (value[^1] == '/')
  107. {
  108. // If baseUrl was configured with a trailing slash, remove it for consistency
  109. value = value.Remove(value.Length - 1);
  110. }
  111. _baseUrl = value;
  112. }
  113. }
  114. public int PublicHttpsPort { get; set; } = DefaultHttpsPort;
  115. public int HttpServerPortNumber { get; set; } = DefaultHttpPort;
  116. public int HttpsPortNumber { get; set; } = DefaultHttpsPort;
  117. public bool EnableHttps { get; set; }
  118. public int PublicPort { get; set; } = DefaultHttpPort;
  119. public bool UPnPCreateHttpPortMap { get; set; }
  120. public string UDPPortRange { get; set; } = string.Empty;
  121. public bool EnableIPV6 { get; set; }
  122. public bool EnableIPV4 { get; set; } = true;
  123. public bool EnableSSDPTracing { get; set; }
  124. public string SSDPTracingFilter { get; set; } = string.Empty;
  125. public int UDPSendCount { get; set; } = 2;
  126. public int UDPSendDelay { get; set; } = 100;
  127. public bool IgnoreVirtualInterfaces { get; set; } = true;
  128. public string VirtualInterfaceNames { get; set; } = "vEthernet*";
  129. public int GatewayMonitorPeriod { get; set; } = 60;
  130. public bool EnableMultiSocketBinding { get; } = true;
  131. public bool TrustAllIP6Interfaces { get; set; }
  132. public string HDHomerunPortRange { get; set; } = string.Empty;
  133. public string[] PublishedServerUriBySubnet { get; set; } = Array.Empty<string>();
  134. public bool AutoDiscoveryTracing { get; set; }
  135. public bool AutoDiscovery { get; set; } = true;
  136. public string[] RemoteIPFilter { get; set; } = Array.Empty<string>();
  137. public bool IsRemoteIPFilterBlacklist { get; set; }
  138. public bool EnableUPnP { get; set; }
  139. public bool EnableRemoteAccess { get; set; } = true;
  140. public string[] LocalNetworkSubnets { get; set; } = Array.Empty<string>();
  141. public string[] LocalNetworkAddresses { get; set; } = Array.Empty<string>();
  142. public string[] KnownProxies { get; set; } = Array.Empty<string>();
  143. public bool EnablePublishedServerUriByRequest { get; set; } = false;
  144. }
  145. #pragma warning restore
  146. }