AutoDiscoveryHost.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.Text.Json;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using MediaBrowser.Common.Configuration;
  11. using MediaBrowser.Common.Net;
  12. using MediaBrowser.Controller;
  13. using MediaBrowser.Model.ApiClient;
  14. using Microsoft.Extensions.Hosting;
  15. using Microsoft.Extensions.Logging;
  16. namespace Jellyfin.Networking;
  17. /// <summary>
  18. /// <see cref="BackgroundService"/> responsible for responding to auto-discovery messages.
  19. /// </summary>
  20. public sealed class AutoDiscoveryHost : BackgroundService
  21. {
  22. /// <summary>
  23. /// The port to listen on for auto-discovery messages.
  24. /// </summary>
  25. private const int PortNumber = 7359;
  26. private readonly ILogger<AutoDiscoveryHost> _logger;
  27. private readonly IServerApplicationHost _appHost;
  28. private readonly IConfigurationManager _configurationManager;
  29. private readonly INetworkManager _networkManager;
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="AutoDiscoveryHost" /> class.
  32. /// </summary>
  33. /// <param name="logger">The <see cref="ILogger{AutoDiscoveryHost}"/>.</param>
  34. /// <param name="appHost">The <see cref="IServerApplicationHost"/>.</param>
  35. /// <param name="configurationManager">The <see cref="IConfigurationManager"/>.</param>
  36. /// <param name="networkManager">The <see cref="INetworkManager"/>.</param>
  37. public AutoDiscoveryHost(
  38. ILogger<AutoDiscoveryHost> logger,
  39. IServerApplicationHost appHost,
  40. IConfigurationManager configurationManager,
  41. INetworkManager networkManager)
  42. {
  43. _logger = logger;
  44. _appHost = appHost;
  45. _configurationManager = configurationManager;
  46. _networkManager = networkManager;
  47. }
  48. /// <inheritdoc />
  49. protected override async Task ExecuteAsync(CancellationToken stoppingToken)
  50. {
  51. var networkConfig = _configurationManager.GetNetworkConfiguration();
  52. if (!networkConfig.AutoDiscovery)
  53. {
  54. return;
  55. }
  56. var udpServers = new List<Task>();
  57. // Linux needs to bind to the broadcast addresses to receive broadcast traffic
  58. if (OperatingSystem.IsLinux() && networkConfig.EnableIPv4)
  59. {
  60. udpServers.Add(ListenForAutoDiscoveryMessage(IPAddress.Broadcast, stoppingToken));
  61. }
  62. udpServers.AddRange(_networkManager.GetInternalBindAddresses()
  63. .Select(intf => ListenForAutoDiscoveryMessage(
  64. OperatingSystem.IsLinux() && intf.AddressFamily == AddressFamily.InterNetwork
  65. ? NetworkUtils.GetBroadcastAddress(intf.Subnet)
  66. : intf.Address,
  67. stoppingToken)));
  68. await Task.WhenAll(udpServers).ConfigureAwait(false);
  69. }
  70. private async Task ListenForAutoDiscoveryMessage(IPAddress address, CancellationToken cancellationToken)
  71. {
  72. try
  73. {
  74. using var udpClient = new UdpClient(new IPEndPoint(address, PortNumber));
  75. udpClient.MulticastLoopback = false;
  76. while (!cancellationToken.IsCancellationRequested)
  77. {
  78. try
  79. {
  80. var result = await udpClient.ReceiveAsync(cancellationToken).ConfigureAwait(false);
  81. var text = Encoding.UTF8.GetString(result.Buffer);
  82. if (text.Contains("who is JellyfinServer?", StringComparison.OrdinalIgnoreCase))
  83. {
  84. await RespondToV2Message(udpClient, result.RemoteEndPoint, cancellationToken).ConfigureAwait(false);
  85. }
  86. }
  87. catch (SocketException ex)
  88. {
  89. _logger.LogError(ex, "Failed to receive data from socket");
  90. }
  91. }
  92. }
  93. catch (OperationCanceledException)
  94. {
  95. _logger.LogDebug("Broadcast socket operation cancelled");
  96. }
  97. catch (Exception ex)
  98. {
  99. // Exception in this function will prevent the background service from restarting in-process.
  100. _logger.LogError(ex, "Unable to bind to {Address}:{Port}", address, PortNumber);
  101. }
  102. }
  103. private async Task RespondToV2Message(UdpClient udpClient, IPEndPoint endpoint, CancellationToken cancellationToken)
  104. {
  105. var localUrl = _appHost.GetSmartApiUrl(endpoint.Address);
  106. if (string.IsNullOrEmpty(localUrl))
  107. {
  108. _logger.LogWarning("Unable to respond to server discovery request because the local ip address could not be determined.");
  109. return;
  110. }
  111. var response = new ServerDiscoveryInfo(localUrl, _appHost.SystemId, _appHost.FriendlyName);
  112. try
  113. {
  114. _logger.LogDebug("Sending AutoDiscovery response");
  115. await udpClient
  116. .SendAsync(JsonSerializer.SerializeToUtf8Bytes(response).AsMemory(), endpoint, cancellationToken)
  117. .ConfigureAwait(false);
  118. }
  119. catch (SocketException ex)
  120. {
  121. _logger.LogError(ex, "Error sending response message");
  122. }
  123. }
  124. }