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