Skip to content

Commit

Permalink
Merge branch 'master' into network-rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadowghost committed May 11, 2023
2 parents 6cc1203 + e1a30a4 commit c042f20
Show file tree
Hide file tree
Showing 18 changed files with 375 additions and 167 deletions.
2 changes: 1 addition & 1 deletion Emby.Dlna/Eventing/DlnaEventManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private async Task TriggerEvent(EventSubscription subscription, IDictionary<stri

try
{
using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
using var response = await _httpClientFactory.CreateClient(NamedClient.DirectIp)
.SendAsync(options, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
}
catch (OperationCanceledException)
Expand Down
13 changes: 0 additions & 13 deletions Emby.Server.Implementations/IO/ExtendedFileSystemInfo.cs

This file was deleted.

46 changes: 11 additions & 35 deletions Emby.Server.Implementations/IO/ManagedFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,25 +267,6 @@ private FileSystemMetadata GetFileSystemMetadata(FileSystemInfo info)
return result;
}

private static ExtendedFileSystemInfo GetExtendedFileSystemInfo(string path)
{
var result = new ExtendedFileSystemInfo();

var info = new FileInfo(path);

if (info.Exists)
{
result.Exists = true;

var attributes = info.Attributes;

result.IsHidden = (attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
result.IsReadOnly = (attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
}

return result;
}

/// <summary>
/// Takes a filename and removes invalid characters.
/// </summary>
Expand Down Expand Up @@ -403,19 +384,18 @@ public virtual void SetHidden(string path, bool isHidden)
return;
}

var info = GetExtendedFileSystemInfo(path);
var info = new FileInfo(path);

if (info.Exists && info.IsHidden != isHidden)
if (info.Exists &&
((info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) != isHidden)
{
if (isHidden)
{
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
File.SetAttributes(path, info.Attributes | FileAttributes.Hidden);
}
else
{
var attributes = File.GetAttributes(path);
attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
File.SetAttributes(path, attributes);
File.SetAttributes(path, info.Attributes & ~FileAttributes.Hidden);
}
}
}
Expand All @@ -428,27 +408,28 @@ public virtual void SetAttributes(string path, bool isHidden, bool readOnly)
return;
}

var info = GetExtendedFileSystemInfo(path);
var info = new FileInfo(path);

if (!info.Exists)
{
return;
}

if (info.IsReadOnly == readOnly && info.IsHidden == isHidden)
if (((info.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) == readOnly
&& ((info.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden) == isHidden)
{
return;
}

var attributes = File.GetAttributes(path);
var attributes = info.Attributes;

if (readOnly)
{
attributes |= FileAttributes.ReadOnly;
}
else
{
attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);
attributes &= ~FileAttributes.ReadOnly;
}

if (isHidden)
Expand All @@ -457,17 +438,12 @@ public virtual void SetAttributes(string path, bool isHidden, bool readOnly)
}
else
{
attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
attributes &= ~FileAttributes.Hidden;
}

File.SetAttributes(path, attributes);
}

private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
{
return attributes & ~attributesToRemove;
}

/// <summary>
/// Swaps the files.
/// </summary>
Expand Down
28 changes: 28 additions & 0 deletions Emby.Server.Implementations/Localization/Core/sn.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"HeaderAlbumArtists": "Vaimbi vemadambarefu",
"HeaderContinueWatching": "Simudzira kuona",
"HeaderFavoriteSongs": "Nziyo dzaunofarira",
"Albums": "Dambarefu",
"AppDeviceValues": "Apu: {0}, Dhivhaisi: {1}",
"Application": "Purogiramu",
"Artists": "Vaimbi",
"AuthenticationSucceededWithUserName": "apinda",
"Books": "Mabhuku",
"CameraImageUploadedFrom": "Mufananidzo mutsva vabva pakamera {0}",
"Channels": "Machanewo",
"ChapterNameValue": "Chikamu {0}",
"Collections": "Akafanana",
"Default": "Zvakasarudzwa Kare",
"DeviceOfflineWithName": "{0} haasisipo",
"DeviceOnlineWithName": "{0} aripo",
"External": "Zvekunze",
"FailedLoginAttemptWithUserName": "Vatadza kuloga chimboedza kushandisa {0}",
"Favorites": "Zvaunofarira",
"Folders": "Mafoodha",
"Forced": "Zvekumanikidzira",
"Genres": "Mhando",
"HeaderFavoriteAlbums": "Madambarefu aunofarira",
"HeaderFavoriteArtists": "Vaimbi vaunofarira",
"HeaderFavoriteEpisodes": "Maepisodhi aunofarira",
"HeaderFavoriteShows": "Masirisi aunofarira"
}
2 changes: 1 addition & 1 deletion Emby.Server.Implementations/Localization/Core/zh-HK.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"Favorites": "我的最愛",
"Folders": "資料夾",
"Genres": "風格",
"HeaderAlbumArtists": "專輯藝人",
"HeaderAlbumArtists": "專輯歌手",
"HeaderContinueWatching": "繼續觀看",
"HeaderFavoriteAlbums": "最愛的專輯",
"HeaderFavoriteArtists": "最愛的藝人",
Expand Down
12 changes: 10 additions & 2 deletions Jellyfin.Api/Auth/FirstTimeSetupPolicy/FirstTimeSetupHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ protected override Task HandleRequirementAsync(AuthorizationHandlerContext conte
return Task.CompletedTask;
}

if (requirement.RequireAdmin && !context.User.IsInRole(UserRoles.Administrator))
var contextUser = context.User;
if (requirement.RequireAdmin && !contextUser.IsInRole(UserRoles.Administrator))
{
context.Fail();
return Task.CompletedTask;
}

var userId = contextUser.GetUserId();
if (userId.Equals(default))
{
context.Fail();
return Task.CompletedTask;
Expand All @@ -50,7 +58,7 @@ protected override Task HandleRequirementAsync(AuthorizationHandlerContext conte
return Task.CompletedTask;
}

var user = _userManager.GetUserById(context.User.GetUserId());
var user = _userManager.GetUserById(userId);
if (user is null)
{
throw new ResourceNotFoundException();
Expand Down
12 changes: 12 additions & 0 deletions Jellyfin.Api/Controllers/SystemController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ public SystemController(
/// Gets information about the server.
/// </summary>
/// <response code="200">Information retrieved.</response>
/// <response code="403">User does not have permission to retrieve information.</response>
/// <returns>A <see cref="SystemInfo"/> with info about the system.</returns>
[HttpGet("Info")]
[Authorize(Policy = Policies.FirstTimeSetupOrIgnoreParentalControl)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public ActionResult<SystemInfo> GetSystemInfo()
{
return _appHost.GetSystemInfo(Request);
Expand Down Expand Up @@ -97,10 +99,12 @@ public ActionResult<string> PingSystem()
/// Restarts the application.
/// </summary>
/// <response code="204">Server restarted.</response>
/// <response code="403">User does not have permission to restart server.</response>
/// <returns>No content. Server restarted.</returns>
[HttpPost("Restart")]
[Authorize(Policy = Policies.LocalAccessOrRequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public ActionResult RestartApplication()
{
Task.Run(async () =>
Expand All @@ -115,10 +119,12 @@ public ActionResult RestartApplication()
/// Shuts down the application.
/// </summary>
/// <response code="204">Server shut down.</response>
/// <response code="403">User does not have permission to shutdown server.</response>
/// <returns>No content. Server shut down.</returns>
[HttpPost("Shutdown")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public ActionResult ShutdownApplication()
{
Task.Run(async () =>
Expand All @@ -133,10 +139,12 @@ public ActionResult ShutdownApplication()
/// Gets a list of available server log files.
/// </summary>
/// <response code="200">Information retrieved.</response>
/// <response code="403">User does not have permission to get server logs.</response>
/// <returns>An array of <see cref="LogFile"/> with the available log files.</returns>
[HttpGet("Logs")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public ActionResult<LogFile[]> GetServerLogs()
{
IEnumerable<FileSystemMetadata> files;
Expand Down Expand Up @@ -170,10 +178,12 @@ public ActionResult<LogFile[]> GetServerLogs()
/// Gets information about the request endpoint.
/// </summary>
/// <response code="200">Information retrieved.</response>
/// <response code="403">User does not have permission to get endpoint information.</response>
/// <returns><see cref="EndPointInfo"/> with information about the endpoint.</returns>
[HttpGet("Endpoint")]
[Authorize]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
public ActionResult<EndPointInfo> GetEndpointInfo()
{
return new EndPointInfo
Expand All @@ -188,10 +198,12 @@ public ActionResult<EndPointInfo> GetEndpointInfo()
/// </summary>
/// <param name="name">The name of the log file to get.</param>
/// <response code="200">Log file retrieved.</response>
/// <response code="403">User does not have permission to get log files.</response>
/// <returns>The log file.</returns>
[HttpGet("Logs/Log")]
[Authorize(Policy = Policies.RequiresElevation)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesFile(MediaTypeNames.Text.Plain)]
public ActionResult GetLogFile([FromQuery, Required] string name)
{
Expand Down
120 changes: 120 additions & 0 deletions Jellyfin.Networking/HappyEyeballs/HttpClientExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
The MIT License (MIT)
Copyright (c) .NET Foundation and Contributors
All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

using System.IO;
using System.Net.Http;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;

namespace Jellyfin.Networking.HappyEyeballs
{
/// <summary>
/// Defines the <see cref="HttpClientExtension"/> class.
///
/// Implementation taken from https://github.com/ppy/osu-framework/pull/4191 .
/// </summary>
public static class HttpClientExtension
{
/// <summary>
/// Gets or sets a value indicating whether the client should use IPv6.
/// </summary>
public static bool UseIPv6 { get; set; } = true;

/// <summary>
/// Implements the httpclient callback method.
/// </summary>
/// <param name="context">The <see cref="SocketsHttpConnectionContext"/> instance.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> instance.</param>
/// <returns>The http steam.</returns>
public static async ValueTask<Stream> OnConnect(SocketsHttpConnectionContext context, CancellationToken cancellationToken)
{
if (!UseIPv6)
{
return await AttemptConnection(AddressFamily.InterNetwork, context, cancellationToken).ConfigureAwait(false);
}

using var cancelIPv6 = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
var tryConnectAsyncIPv6 = AttemptConnection(AddressFamily.InterNetworkV6, context, cancelIPv6.Token);

// GetAwaiter().GetResult() is used instead of .Result as this results in improved exception handling.
// The tasks have already been completed.
// See https://github.com/dotnet/corefx/pull/29792/files#r189415885 for more details.
if (await Task.WhenAny(tryConnectAsyncIPv6, Task.Delay(200, cancelIPv6.Token)).ConfigureAwait(false) == tryConnectAsyncIPv6 && tryConnectAsyncIPv6.IsCompletedSuccessfully)
{
cancelIPv6.Cancel();
return tryConnectAsyncIPv6.GetAwaiter().GetResult();
}

using var cancelIPv4 = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
var tryConnectAsyncIPv4 = AttemptConnection(AddressFamily.InterNetwork, context, cancelIPv4.Token);

if (await Task.WhenAny(tryConnectAsyncIPv6, tryConnectAsyncIPv4).ConfigureAwait(false) == tryConnectAsyncIPv6)
{
if (tryConnectAsyncIPv6.IsCompletedSuccessfully)
{
cancelIPv4.Cancel();
return tryConnectAsyncIPv6.GetAwaiter().GetResult();
}

return tryConnectAsyncIPv4.GetAwaiter().GetResult();
}
else
{
if (tryConnectAsyncIPv4.IsCompletedSuccessfully)
{
cancelIPv6.Cancel();
return tryConnectAsyncIPv4.GetAwaiter().GetResult();
}

return tryConnectAsyncIPv6.GetAwaiter().GetResult();
}
}

private static async Task<Stream> AttemptConnection(AddressFamily addressFamily, SocketsHttpConnectionContext context, CancellationToken cancellationToken)
{
// The following socket constructor will create a dual-mode socket on systems where IPV6 is available.
var socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp)
{
// Turn off Nagle's algorithm since it degrades performance in most HttpClient scenarios.
NoDelay = true
};

try
{
await socket.ConnectAsync(context.DnsEndPoint, cancellationToken).ConfigureAwait(false);
// The stream should take the ownership of the underlying socket,
// closing it when it's disposed.
return new NetworkStream(socket, ownsSocket: true);
}
catch
{
socket.Dispose();
throw;
}
}
}
}
Loading

0 comments on commit c042f20

Please sign in to comment.