Skip to content

Commit

Permalink
Merge pull request #1577 from mregni/develop
Browse files Browse the repository at this point in the history
New release
  • Loading branch information
mregni authored Jul 29, 2021
2 parents c8407c6 + 166fd81 commit 214b51f
Show file tree
Hide file tree
Showing 413 changed files with 35,305 additions and 27,327 deletions.
115 changes: 115 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
version: 2
updates:
- package-ecosystem: nuget
directory: "/EmbyStat.Web"
schedule:
interval: daily
time: "04:00"
open-pull-requests-limit: 10
ignore:
- dependency-name: MediaBrowser.Common
versions:
- "> 3.3.10, < 4"
- dependency-name: Hangfire.AspNetCore
versions:
- 1.7.19
- 1.7.20
- 1.7.21
- dependency-name: Swashbuckle.AspNetCore
versions:
- 5.6.3
- 6.0.2
- 6.0.4
- 6.0.5
- 6.0.7
- 6.1.0
- 6.1.1
- 6.1.2
- dependency-name: Rollbar.PlugIns.NLog
versions:
- 3.14.1
- 4.0.0
- 4.0.1
- 4.0.2
- dependency-name: Rollbar.NetCore.AspNet
versions:
- 3.14.1
- 4.0.0
- 4.0.1
- 4.0.2
- dependency-name: DevExtreme.AspNet.Data
versions:
- 2.8.0
- 2.8.1
- dependency-name: Microsoft.AspNetCore.Authentication.JwtBearer
versions:
- 5.0.2
- 5.0.3
- 5.0.4
- dependency-name: Microsoft.AspNetCore.SpaServices.Extensions
versions:
- 5.0.2
- 5.0.3
- 5.0.4
- package-ecosystem: nuget
directory: "/Updater"
schedule:
interval: daily
time: "04:00"
open-pull-requests-limit: 10
ignore:
- dependency-name: NLog.Web.AspNetCore
versions:
- 4.10.0
- 4.11.0
- package-ecosystem: nuget
directory: "/Tests.Unit"
schedule:
interval: daily
time: "04:00"
open-pull-requests-limit: 10
ignore:
- dependency-name: MediaBrowser.Common
versions:
- "> 3.3.10, < 4"
- dependency-name: Hangfire.AspNetCore
versions:
- 1.7.19
- 1.7.20
- 1.7.21
- dependency-name: Swashbuckle.AspNetCore
versions:
- 5.6.3
- 6.0.2
- 6.0.4
- 6.0.5
- 6.0.7
- 6.1.0
- 6.1.1
- 6.1.2
- dependency-name: Rollbar.NetCore.AspNet
versions:
- 3.14.1
- 4.0.0
- 4.0.1
- 4.0.2
- dependency-name: Rollbar.PlugIns.NLog
versions:
- 3.14.1
- 4.0.0
- 4.0.1
- 4.0.2
- dependency-name: DevExtreme.AspNet.Data
versions:
- 2.8.0
- 2.8.1
- dependency-name: Microsoft.AspNetCore.Authentication.JwtBearer
versions:
- 5.0.2
- 5.0.3
- 5.0.4
- dependency-name: Microsoft.AspNetCore.SpaServices.Extensions
versions:
- 5.0.2
- 5.0.3
- 5.0.4
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,5 @@ usersettings.json
**/Embystat.Web/config/**
**/issues.json
**/coverage/**
**/ClientApp/build/**
**/ClientApp/build/**
EmbyStat.Web/ClientApp/.eslintcache
3 changes: 2 additions & 1 deletion EmbyStat.Clients.Base/Converters/ShowConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ public static Episode ConvertToEpisode(this VirtualEpisode episode, Show show, S
LocationType = LocationType.Virtual,
IndexNumber = episode.EpisodeNumber,
ParentId = season.Id,
PremiereDate = new DateTimeOffset(episode.FirstAired ?? new DateTime(), TimeSpan.Zero)
PremiereDate = episode.FirstAired ?? new DateTime(),
SeasonIndexNumber = season.IndexNumber
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion EmbyStat.Clients.Base/EmbyStat.Clients.Base.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
107 changes: 82 additions & 25 deletions EmbyStat.Clients.Base/Http/BaseHttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using EmbyStat.Clients.Base.Converters;
using EmbyStat.Common.Converters;
using EmbyStat.Common.Extensions;
Expand All @@ -14,6 +18,8 @@
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Querying;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
Expand All @@ -22,12 +28,12 @@ namespace EmbyStat.Clients.Base.Http
{
public abstract class BaseHttpClient
{
private readonly IHttpContextAccessor _accessor;
protected readonly Logger Logger;
protected string DeviceName { get; set; }
protected string ApplicationVersion { get; set; }
protected string DeviceId { get; set; }
protected string UserId { get; set; }

protected string apiKey { get; set; }
public string ApiKey
{
Expand All @@ -46,10 +52,11 @@ public string BaseUrl

protected readonly IRestClient RestClient;

protected BaseHttpClient(IRestClient client)
protected BaseHttpClient(IRestClient client, IHttpContextAccessor accessor)
{
Logger = LogFactory.CreateLoggerForType(typeof(BaseHttpClient), "BASE-HTTP-CLIENT");
_accessor = accessor;
RestClient = client.Initialize();
Logger = LogFactory.CreateLoggerForType(typeof(BaseHttpClient), "BASE-HTTP-CLIENT");
}

public void SetDeviceInfo(string deviceName, string authorizationScheme, string applicationVersion, string deviceId, string userId)
Expand Down Expand Up @@ -103,35 +110,85 @@ protected string ExecuteCall(IRestRequest request)
return ExecuteCall<T>(request);
}

protected MediaServerUdpBroadcast SearchServer(string message)

protected async Task<IEnumerable<MediaServerUdpBroadcast>> SearchServer(string message)
{
using var client = new UdpClient();
var requestData = Encoding.ASCII.GetBytes(message);
var serverEp = new IPEndPoint(IPAddress.Any, 7359);
var list = new List<MediaServerUdpBroadcast>();
Logger.Debug($"Checking following broadcast IPs (own IP: ");
foreach (var ip in GetBroadCastIps())
{
Logger.Debug($"\t{ip}");
await Task.Run(async () =>
{
var to = new IPEndPoint(ip, 7359);
using var client = new ServerSearcher(to);

client.EnableBroadcast = true;
client.Send(requestData, requestData.Length, new IPEndPoint(IPAddress.Broadcast, 7359));
client.MediaServerFound += (sender, broadcast) =>
{
list.Add(broadcast);
};

var timeToWait = TimeSpan.FromSeconds(3);
client.Send(message);
await Task.Run(() => Task.Delay(3000));
});
}

var asyncResult = client.BeginReceive(null, null);
asyncResult.AsyncWaitHandle.WaitOne(timeToWait);
if (asyncResult.IsCompleted)
return list;
}

private IEnumerable<IPAddress> GetBroadCastIps()
{
var ip = _accessor.HttpContext?.Connection.RemoteIpAddress;
Logger.Debug($"{ip})");
var interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (var adapter in interfaces)
{
try
foreach (var unicastInfo in adapter.GetIPProperties().UnicastAddresses)
{
var receivedData = client.EndReceive(asyncResult, ref serverEp);
var serverResponse = Encoding.ASCII.GetString(receivedData);
var udpBroadcastResult = JsonConvert.DeserializeObject<MediaServerUdpBroadcast>(serverResponse);
return udpBroadcastResult;
}
catch (Exception)
{
// No data received, swallow exception and return empty object
if (unicastInfo.Address.AddressFamily != AddressFamily.InterNetwork
|| unicastInfo.Address.MapToIPv4().Equals(IPAddress.Parse("127.0.0.1")))
{
continue;
}

if (CheckWhetherInSameNetwork(unicastInfo.Address, unicastInfo.IPv4Mask, ip))
yield return GetBroadcastAddress(unicastInfo);
}
}
}

private bool CheckWhetherInSameNetwork(IPAddress firstIp, IPAddress subNet, IPAddress secondIp)
{
var subnetmaskInInt = ConvertIpToUint(subNet);
var firstIpInInt = ConvertIpToUint(firstIp);
var secondIpInInt = ConvertIpToUint(secondIp);
var networkPortionofFirstIp = firstIpInInt & subnetmaskInInt;
var networkPortionofSecondIp = secondIpInInt & subnetmaskInInt;
return networkPortionofFirstIp == networkPortionofSecondIp;
}

private static uint ConvertIpToUint(IPAddress ipAddress)
{
var byteIp = ipAddress.GetAddressBytes();
var ipInUint = (uint)byteIp[3] << 24;
ipInUint += (uint)byteIp[2] << 16;
ipInUint += (uint)byteIp[1] << 8;
ipInUint += byteIp[0];
return ipInUint;
}

return null;
private IPAddress GetBroadcastAddress(UnicastIPAddressInformation unicastAddress)
{
return GetBroadcastAddress(unicastAddress.Address, unicastAddress.IPv4Mask);
}

private IPAddress GetBroadcastAddress(IPAddress address, IPAddress mask)
{
var ipAddress = BitConverter.ToUInt32(address.GetAddressBytes(), 0);
var ipMaskV4 = BitConverter.ToUInt32(mask.GetAddressBytes(), 0);
var broadCastIpAddress = ipAddress | ~ipMaskV4;

return new IPAddress(BitConverter.GetBytes(broadCastIpAddress));
}

public bool Ping(string message)
Expand Down Expand Up @@ -221,8 +278,8 @@ public List<Movie> GetMovies(string parentId, string collectionId, int startInde
var request = new RestRequest($"Items", Method.GET);
request.AddItemQueryAsParameters(query, UserId);
var baseItems = ExecuteAuthenticatedCall<QueryResult<BaseItemDto>>(request);
return baseItems?.Items != null
? baseItems.Items.Select(x => x.ConvertToMovie(collectionId, Logger)).ToList()
return baseItems?.Items != null
? baseItems.Items.Select(x => x.ConvertToMovie(collectionId, Logger)).ToList()
: new List<Movie>(0);
}

Expand Down
3 changes: 2 additions & 1 deletion EmbyStat.Clients.Base/Http/IHttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EmbyStat.Common.Models;
using EmbyStat.Common.Models.Entities;
using EmbyStat.Common.Models.Net;
Expand All @@ -19,7 +20,7 @@ public interface IHttpClient
JArray GetUsers();
JObject GetDevices();
bool Ping();
MediaServerUdpBroadcast SearchServer();
Task<IEnumerable<MediaServerUdpBroadcast>> SearchServer();
List<Movie> GetMovies(string parentId, string collectionId, int startIndex, int limit);
List<Show> GetShows(string libraryId);
List<Season> GetSeasons(string parentId);
Expand Down
63 changes: 63 additions & 0 deletions EmbyStat.Clients.Base/Http/ServerSearcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Text;
using EmbyStat.Common.Models;
using Newtonsoft.Json;

namespace EmbyStat.Clients.Base.Http
{
public class ServerSearcher : IDisposable
{
private readonly UdpClient _client;
public bool isDisposed { get; set; }
private IPEndPoint _to;
public event EventHandler<MediaServerUdpBroadcast> MediaServerFound;

public ServerSearcher(IPEndPoint to)
{
isDisposed = false;
_to = to;
var from = new IPEndPoint(0, 0);
_client = new UdpClient { EnableBroadcast = true, MulticastLoopback = true };
_client.Client.Bind(from);
_client.BeginReceive(OnMessageReceived, null);
}

public void Send(string message)
{
var requestData = Encoding.ASCII.GetBytes(message);
try
{
_client.Send(requestData, requestData.Length, _to);
}
catch (Exception)
{
//swallowing failed calls
}

}

private void OnMessageReceived(IAsyncResult ar)
{
if (!isDisposed)
{
var receiveBytes = _client.EndReceive(ar, ref _to);
var receiveString = Encoding.ASCII.GetString(receiveBytes);

var udpBroadcastResult = JsonConvert.DeserializeObject<MediaServerUdpBroadcast>(receiveString);

MediaServerFound?.Invoke(this, udpBroadcastResult);
_client.BeginReceive(OnMessageReceived, null);
}
}

public void Dispose()
{
isDisposed = true;
_client?.Dispose();
}
}
}
Loading

0 comments on commit 214b51f

Please sign in to comment.