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