2013-02-23 17:31:51 -05:00
|
|
|
using System;
|
2019-07-07 15:03:26 -04:00
|
|
|
using System.Net;
|
2020-01-12 12:59:10 -05:00
|
|
|
using System.Net.Sockets;
|
2013-02-23 17:31:51 -05:00
|
|
|
using System.Text;
|
2020-01-12 12:59:10 -05:00
|
|
|
using System.Text.Json;
|
2017-02-05 15:44:08 -05:00
|
|
|
using System.Threading;
|
2013-02-23 17:31:51 -05:00
|
|
|
using System.Threading.Tasks;
|
2019-01-13 14:23:38 -05:00
|
|
|
using MediaBrowser.Controller;
|
|
|
|
using MediaBrowser.Model.ApiClient;
|
2020-05-02 12:56:09 -04:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2019-01-13 14:23:38 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2022-02-14 08:39:33 -05:00
|
|
|
using static MediaBrowser.Controller.Extensions.ConfigurationExtensions;
|
2013-02-23 17:31:51 -05:00
|
|
|
|
2016-11-04 14:56:47 -04:00
|
|
|
namespace Emby.Server.Implementations.Udp
|
2013-02-23 17:31:51 -05:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-01-12 12:59:10 -05:00
|
|
|
/// Provides a Udp Server.
|
2013-02-23 17:31:51 -05:00
|
|
|
/// </summary>
|
2020-01-12 12:59:10 -05:00
|
|
|
public sealed class UdpServer : IDisposable
|
2013-02-23 17:31:51 -05:00
|
|
|
{
|
|
|
|
/// <summary>
|
2020-06-15 18:37:52 -04:00
|
|
|
/// The _logger.
|
2013-02-23 17:31:51 -05:00
|
|
|
/// </summary>
|
2013-03-27 18:13:46 -04:00
|
|
|
private readonly ILogger _logger;
|
2014-07-27 18:01:29 -04:00
|
|
|
private readonly IServerApplicationHost _appHost;
|
2020-05-02 12:56:09 -04:00
|
|
|
private readonly IConfiguration _config;
|
|
|
|
|
2020-01-12 12:59:10 -05:00
|
|
|
private readonly byte[] _receiveBuffer = new byte[8192];
|
2014-07-27 18:01:29 -04:00
|
|
|
|
2021-10-02 13:36:56 -04:00
|
|
|
private Socket _udpSocket;
|
|
|
|
private IPEndPoint _endpoint;
|
2020-01-12 12:59:10 -05:00
|
|
|
private bool _disposed = false;
|
2013-02-26 11:10:55 -05:00
|
|
|
|
2013-02-23 17:31:51 -05:00
|
|
|
/// <summary>
|
2020-01-12 12:59:10 -05:00
|
|
|
/// Initializes a new instance of the <see cref="UdpServer" /> class.
|
2013-02-23 17:31:51 -05:00
|
|
|
/// </summary>
|
2021-05-28 08:33:54 -04:00
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
/// <param name="appHost">The application host.</param>
|
|
|
|
/// <param name="configuration">The configuration manager.</param>
|
2022-07-21 03:42:45 -04:00
|
|
|
/// <param name="bindAddress"> The bind address.</param>
|
2021-05-28 08:33:54 -04:00
|
|
|
/// <param name="port">The port.</param>
|
|
|
|
public UdpServer(
|
|
|
|
ILogger logger,
|
|
|
|
IServerApplicationHost appHost,
|
|
|
|
IConfiguration configuration,
|
2022-07-21 03:42:45 -04:00
|
|
|
IPAddress bindAddress,
|
2021-05-28 08:33:54 -04:00
|
|
|
int port)
|
2016-09-03 13:16:36 -04:00
|
|
|
{
|
2020-01-12 12:59:10 -05:00
|
|
|
_logger = logger;
|
|
|
|
_appHost = appHost;
|
2020-05-02 12:56:09 -04:00
|
|
|
_config = configuration;
|
2021-05-28 08:33:54 -04:00
|
|
|
|
2022-07-21 03:42:45 -04:00
|
|
|
_endpoint = new IPEndPoint(bindAddress, port);
|
2021-05-28 08:33:54 -04:00
|
|
|
|
|
|
|
_udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
|
|
|
_udpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
|
2016-09-03 13:16:36 -04:00
|
|
|
}
|
|
|
|
|
2021-11-16 06:24:17 -05:00
|
|
|
private async Task RespondToV2Message(EndPoint endpoint, CancellationToken cancellationToken)
|
2014-07-27 18:01:29 -04:00
|
|
|
{
|
2022-02-14 08:39:33 -05:00
|
|
|
string? localUrl = _config[AddressOverrideKey];
|
2021-05-28 08:33:54 -04:00
|
|
|
if (string.IsNullOrEmpty(localUrl))
|
|
|
|
{
|
|
|
|
localUrl = _appHost.GetSmartApiUrl(((IPEndPoint)endpoint).Address);
|
|
|
|
}
|
2013-03-27 18:13:46 -04:00
|
|
|
|
2021-05-28 08:33:54 -04:00
|
|
|
if (string.IsNullOrEmpty(localUrl))
|
2014-07-27 18:01:29 -04:00
|
|
|
{
|
2022-02-14 08:39:33 -05:00
|
|
|
_logger.LogWarning("Unable to respond to server discovery request because the local ip address could not be determined.");
|
2021-05-28 08:33:54 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-07-27 18:01:29 -04:00
|
|
|
|
2021-05-28 08:33:54 -04:00
|
|
|
var response = new ServerDiscoveryInfo(localUrl, _appHost.SystemId, _appHost.FriendlyName);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-11-16 06:24:17 -05:00
|
|
|
await _udpSocket.SendToAsync(JsonSerializer.SerializeToUtf8Bytes(response), SocketFlags.None, endpoint, cancellationToken).ConfigureAwait(false);
|
2014-07-27 18:01:29 -04:00
|
|
|
}
|
2021-05-28 08:33:54 -04:00
|
|
|
catch (SocketException ex)
|
2014-07-27 18:01:29 -04:00
|
|
|
{
|
2021-05-28 08:33:54 -04:00
|
|
|
_logger.LogError(ex, "Error sending response message");
|
2013-03-27 18:13:46 -04:00
|
|
|
}
|
2013-02-23 17:31:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Starts the specified port.
|
|
|
|
/// </summary>
|
2021-02-23 10:45:10 -05:00
|
|
|
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
|
2021-05-28 08:33:54 -04:00
|
|
|
public void Start(CancellationToken cancellationToken)
|
2013-12-07 10:52:38 -05:00
|
|
|
{
|
2020-01-12 12:59:10 -05:00
|
|
|
_udpSocket.Bind(_endpoint);
|
2017-05-24 15:12:55 -04:00
|
|
|
|
2020-01-12 12:59:10 -05:00
|
|
|
_ = Task.Run(async () => await BeginReceiveAsync(cancellationToken).ConfigureAwait(false), cancellationToken).ConfigureAwait(false);
|
2013-12-07 10:52:38 -05:00
|
|
|
}
|
|
|
|
|
2020-01-12 12:59:10 -05:00
|
|
|
private async Task BeginReceiveAsync(CancellationToken cancellationToken)
|
2013-02-23 17:31:51 -05:00
|
|
|
{
|
2020-01-12 12:59:10 -05:00
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
2013-02-26 11:10:55 -05:00
|
|
|
{
|
2020-01-12 12:59:10 -05:00
|
|
|
try
|
2015-05-16 23:17:23 -04:00
|
|
|
{
|
2021-11-18 08:13:43 -05:00
|
|
|
var result = await _udpSocket.ReceiveFromAsync(_receiveBuffer, SocketFlags.None, _endpoint, cancellationToken).ConfigureAwait(false);
|
2020-01-12 12:59:10 -05:00
|
|
|
var text = Encoding.UTF8.GetString(_receiveBuffer, 0, result.ReceivedBytes);
|
|
|
|
if (text.Contains("who is JellyfinServer?", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2021-11-16 06:24:17 -05:00
|
|
|
await RespondToV2Message(result.RemoteEndPoint, cancellationToken).ConfigureAwait(false);
|
2020-01-12 12:59:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (SocketException ex)
|
2017-03-26 15:00:35 -04:00
|
|
|
{
|
2020-05-02 12:56:09 -04:00
|
|
|
_logger.LogError(ex, "Failed to receive data from socket");
|
2020-01-12 12:59:10 -05:00
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
// Don't throw
|
2017-03-26 15:00:35 -04:00
|
|
|
}
|
2013-02-23 17:31:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 12:59:10 -05:00
|
|
|
/// <inheritdoc />
|
|
|
|
public void Dispose()
|
2013-02-23 17:31:51 -05:00
|
|
|
{
|
2020-01-12 12:59:10 -05:00
|
|
|
if (_disposed)
|
2016-12-28 01:08:18 -05:00
|
|
|
{
|
2020-01-12 12:59:10 -05:00
|
|
|
return;
|
2013-02-23 17:31:51 -05:00
|
|
|
}
|
|
|
|
|
2020-01-12 12:59:10 -05:00
|
|
|
_udpSocket?.Dispose();
|
2013-03-12 18:51:10 -04:00
|
|
|
|
2020-01-12 12:59:10 -05:00
|
|
|
GC.SuppressFinalize(this);
|
2013-02-23 17:31:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|