CreateNetworkConfiguration.cs 4.7 KB

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