CreateNetworkConfiguration.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Xml.Serialization;
  5. using Emby.Server.Implementations;
  6. using Microsoft.Extensions.Logging;
  7. namespace Jellyfin.Server.Migrations.PreStartupRoutines;
  8. /// <inheritdoc />
  9. #pragma warning disable CS0618 // Type or member is obsolete
  10. [JellyfinMigration("2025-04-20T00:00:00", nameof(CreateNetworkConfiguration), "9B354818-94D5-4B68-AC49-E35CB85F9D84", Stage = Stages.JellyfinMigrationStageTypes.PreInitialisation)]
  11. public class CreateNetworkConfiguration : IMigrationRoutine
  12. #pragma warning restore CS0618 // Type or member is obsolete
  13. {
  14. private readonly ServerApplicationPaths _applicationPaths;
  15. private readonly ILogger<CreateNetworkConfiguration> _logger;
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="CreateNetworkConfiguration"/> class.
  18. /// </summary>
  19. /// <param name="applicationPaths">An instance of <see cref="ServerApplicationPaths"/>.</param>
  20. /// <param name="loggerFactory">An instance of the <see cref="ILoggerFactory"/> interface.</param>
  21. public CreateNetworkConfiguration(ServerApplicationPaths applicationPaths, ILoggerFactory loggerFactory)
  22. {
  23. _applicationPaths = applicationPaths;
  24. _logger = loggerFactory.CreateLogger<CreateNetworkConfiguration>();
  25. }
  26. /// <inheritdoc />
  27. public void Perform()
  28. {
  29. string path = Path.Combine(_applicationPaths.ConfigurationDirectoryPath, "network.xml");
  30. if (File.Exists(path))
  31. {
  32. _logger.LogDebug("Network configuration file already exists, skipping");
  33. return;
  34. }
  35. var serverConfigSerializer = new XmlSerializer(typeof(OldNetworkConfiguration), new XmlRootAttribute("ServerConfiguration"));
  36. using var xmlReader = XmlReader.Create(_applicationPaths.SystemConfigurationFilePath);
  37. var networkSettings = serverConfigSerializer.Deserialize(xmlReader);
  38. var networkConfigSerializer = new XmlSerializer(typeof(OldNetworkConfiguration), new XmlRootAttribute("NetworkConfiguration"));
  39. var xmlWriterSettings = new XmlWriterSettings { Indent = true };
  40. using var xmlWriter = XmlWriter.Create(path, xmlWriterSettings);
  41. networkConfigSerializer.Serialize(xmlWriter, networkSettings);
  42. }
  43. #pragma warning disable
  44. public sealed class OldNetworkConfiguration
  45. {
  46. public const int DefaultHttpPort = 8096;
  47. public const int DefaultHttpsPort = 8920;
  48. private string _baseUrl = string.Empty;
  49. public bool RequireHttps { get; set; }
  50. public string CertificatePath { get; set; } = string.Empty;
  51. public string CertificatePassword { get; set; } = string.Empty;
  52. public string BaseUrl
  53. {
  54. get => _baseUrl;
  55. set
  56. {
  57. // Normalize the start of the string
  58. if (string.IsNullOrWhiteSpace(value))
  59. {
  60. // If baseUrl is empty, set an empty prefix string
  61. _baseUrl = string.Empty;
  62. return;
  63. }
  64. if (value[0] != '/')
  65. {
  66. // If baseUrl was not configured with a leading slash, append one for consistency
  67. value = "/" + value;
  68. }
  69. // Normalize the end of the string
  70. if (value[^1] == '/')
  71. {
  72. // If baseUrl was configured with a trailing slash, remove it for consistency
  73. value = value.Remove(value.Length - 1);
  74. }
  75. _baseUrl = value;
  76. }
  77. }
  78. public int PublicHttpsPort { get; set; } = DefaultHttpsPort;
  79. public int HttpServerPortNumber { get; set; } = DefaultHttpPort;
  80. public int HttpsPortNumber { get; set; } = DefaultHttpsPort;
  81. public bool EnableHttps { get; set; }
  82. public int PublicPort { get; set; } = DefaultHttpPort;
  83. public bool EnableIPV6 { get; set; }
  84. public bool EnableIPV4 { get; set; } = true;
  85. public bool IgnoreVirtualInterfaces { get; set; } = true;
  86. public string[] VirtualInterfaceNames { get; set; } = new string[] { "veth" };
  87. public string[] PublishedServerUriBySubnet { get; set; } = Array.Empty<string>();
  88. public string[] RemoteIPFilter { get; set; } = Array.Empty<string>();
  89. public bool IsRemoteIPFilterBlacklist { get; set; }
  90. public bool EnableUPnP { get; set; }
  91. public bool EnableRemoteAccess { get; set; } = true;
  92. public string[] LocalNetworkSubnets { get; set; } = Array.Empty<string>();
  93. public string[] LocalNetworkAddresses { get; set; } = Array.Empty<string>();
  94. public string[] KnownProxies { get; set; } = Array.Empty<string>();
  95. }
  96. }