jellyfin/Jellyfin.Api/Controllers/StartupController.cs
BaronGreenback 124bd4c2c0
Networking: 1 - Network Manager (#4124)
* NetworkManager

* Config file with additional options.

* Update Jellyfin.Networking/Manager/INetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/INetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Model/Configuration/ServerConfiguration.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Model/Configuration/ServerConfiguration.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Model/Configuration/ServerConfiguration.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Split function.

* Update Jellyfin.Networking/Manager/INetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* fixed iterations

* Update Jellyfin.Networking.csproj

* Update NetworkManager.cs

* Updated to NetCollection 1.03.

* Update ServerConfiguration.cs

* Update StartupController.cs

* Update INetworkManager.cs

Removed public

* Update INetworkManager.cs

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Updated comment

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Update Jellyfin.Networking/Manager/INetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Remove mono code.
Removed forced chromecast option

* Inverted if

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Moved config into a separate container

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Changed sortedList to dictionary.

* Update INetworkManager.cs

Changed UpdateSettings param type

* Update NetworkManager.cs

* Update NetworkManager.cs

* Update NetworkManager.cs

* Update NetworkConfiguration.cs

* Update INetworkManager.cs

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Update MediaBrowser.Model/Configuration/ServerConfiguration.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Model/Configuration/ServerConfiguration.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Updated changes github didn't update.

* Fixed compilation.

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Removed read locking.

* Update NetworkManager.cs

Changed configuration event to NamedConfigurationUpdated

* updated comment

* removed whitespace

* Updated NetCollection to 1.0.6
Updated DXCopAnalyser to 3.3.1

* removed NetCollection

* Update NetworkManager.cs

* Update NetworkExtensions.cs

* Update NetworkExtensions.cs

Removed function.

* Update NetworkExtensions.cs

* Update NetworkManager.cs

Changed ToString() to AsString() as native collection formats incorrectly.

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update NetworkExtensions.cs

* Update Jellyfin.Networking/Configuration/NetworkConfiguration.cs

Co-authored-by: h1dden-da3m0n <33120068+h1dden-da3m0n@users.noreply.github.com>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: h1dden-da3m0n <33120068+h1dden-da3m0n@users.noreply.github.com>

* Update Jellyfin.Networking/Configuration/NetworkConfiguration.cs

Co-authored-by: h1dden-da3m0n <33120068+h1dden-da3m0n@users.noreply.github.com>

* Update Jellyfin.Networking/Configuration/NetworkConfiguration.cs

Co-authored-by: h1dden-da3m0n <33120068+h1dden-da3m0n@users.noreply.github.com>

* Update MediaBrowser.Common/Net/IPObject.cs

Co-authored-by: h1dden-da3m0n <33120068+h1dden-da3m0n@users.noreply.github.com>

* updated

* Replaced NetCollection with Collection<IPObject>

* Update MediaBrowser.Common/Net/NetworkExtensions.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Model/Configuration/PathSubstitution.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/NetworkExtensions.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPObject.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPObject.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPObject.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPHost.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPHost.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPHost.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPHost.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPHost.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPHost.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPHost.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPObject.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* Update MediaBrowser.Common/Net/IPObject.cs

Co-authored-by: Cody Robibero <cody@robibe.ro>

* updated comments.

* Updated comments / changes as suggested by @crobibero.

* Split function as suggested

* Fixed null ref.

* Updated comment.

* Updated cs to .net5

* Fixed issue with PublishedServerUrl

* Fixes

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Restored locking

* optimisation

* Added comment

* updates.

* updated.

* updates

* updated.

* Update IPHost.cs

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

* Update NetworkManager.cs

* Removed whitespace.

* Added debug logging

* Added debug.

* Update Jellyfin.Networking/Manager/NetworkManager.cs

Co-authored-by: Bond-009 <bond.009@outlook.com>

* Replaced GetAddressBytes

Co-authored-by: Cody Robibero <cody@robibe.ro>
Co-authored-by: Claus Vium <cvium@users.noreply.github.com>
Co-authored-by: h1dden-da3m0n <33120068+h1dden-da3m0n@users.noreply.github.com>
Co-authored-by: Bond-009 <bond.009@outlook.com>
2020-11-21 13:59:14 +01:00

148 lines
6.2 KiB
C#

using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using Jellyfin.Api.Constants;
using Jellyfin.Api.Models.StartupDtos;
using Jellyfin.Networking.Configuration;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Library;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Jellyfin.Api.Controllers
{
/// <summary>
/// The startup wizard controller.
/// </summary>
[Authorize(Policy = Policies.FirstTimeSetupOrElevated)]
public class StartupController : BaseJellyfinApiController
{
private readonly IServerConfigurationManager _config;
private readonly IUserManager _userManager;
/// <summary>
/// Initializes a new instance of the <see cref="StartupController" /> class.
/// </summary>
/// <param name="config">The server configuration manager.</param>
/// <param name="userManager">The user manager.</param>
public StartupController(IServerConfigurationManager config, IUserManager userManager)
{
_config = config;
_userManager = userManager;
}
/// <summary>
/// Completes the startup wizard.
/// </summary>
/// <response code="204">Startup wizard completed.</response>
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Complete")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public ActionResult CompleteWizard()
{
_config.Configuration.IsStartupWizardCompleted = true;
_config.SaveConfiguration();
return NoContent();
}
/// <summary>
/// Gets the initial startup wizard configuration.
/// </summary>
/// <response code="200">Initial startup wizard configuration retrieved.</response>
/// <returns>An <see cref="OkResult"/> containing the initial startup wizard configuration.</returns>
[HttpGet("Configuration")]
[ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<StartupConfigurationDto> GetStartupConfiguration()
{
return new StartupConfigurationDto
{
UICulture = _config.Configuration.UICulture,
MetadataCountryCode = _config.Configuration.MetadataCountryCode,
PreferredMetadataLanguage = _config.Configuration.PreferredMetadataLanguage
};
}
/// <summary>
/// Sets the initial startup wizard configuration.
/// </summary>
/// <param name="startupConfiguration">The updated startup configuration.</param>
/// <response code="204">Configuration saved.</response>
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("Configuration")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public ActionResult UpdateInitialConfiguration([FromBody, Required] StartupConfigurationDto startupConfiguration)
{
_config.Configuration.UICulture = startupConfiguration.UICulture ?? string.Empty;
_config.Configuration.MetadataCountryCode = startupConfiguration.MetadataCountryCode ?? string.Empty;
_config.Configuration.PreferredMetadataLanguage = startupConfiguration.PreferredMetadataLanguage ?? string.Empty;
_config.SaveConfiguration();
return NoContent();
}
/// <summary>
/// Sets remote access and UPnP.
/// </summary>
/// <param name="startupRemoteAccessDto">The startup remote access dto.</param>
/// <response code="204">Configuration saved.</response>
/// <returns>A <see cref="NoContentResult"/> indicating success.</returns>
[HttpPost("RemoteAccess")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public ActionResult SetRemoteAccess([FromBody, Required] StartupRemoteAccessDto startupRemoteAccessDto)
{
NetworkConfiguration settings = _config.GetNetworkConfiguration();
settings.EnableRemoteAccess = startupRemoteAccessDto.EnableRemoteAccess;
settings.EnableUPnP = startupRemoteAccessDto.EnableAutomaticPortMapping;
_config.SaveConfiguration("network", settings);
return NoContent();
}
/// <summary>
/// Gets the first user.
/// </summary>
/// <response code="200">Initial user retrieved.</response>
/// <returns>The first user.</returns>
[HttpGet("User")]
[HttpGet("FirstUser", Name = "GetFirstUser_2")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<StartupUserDto> GetFirstUser()
{
// TODO: Remove this method when startup wizard no longer requires an existing user.
await _userManager.InitializeAsync().ConfigureAwait(false);
var user = _userManager.Users.First();
return new StartupUserDto
{
Name = user.Username,
Password = user.Password
};
}
/// <summary>
/// Sets the user name and password.
/// </summary>
/// <param name="startupUserDto">The DTO containing username and password.</param>
/// <response code="204">Updated user name and password.</response>
/// <returns>
/// A <see cref="Task" /> that represents the asynchronous update operation.
/// The task result contains a <see cref="NoContentResult"/> indicating success.
/// </returns>
[HttpPost("User")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> UpdateStartupUser([FromBody] StartupUserDto startupUserDto)
{
var user = _userManager.Users.First();
user.Username = startupUserDto.Name;
await _userManager.UpdateUserAsync(user).ConfigureAwait(false);
if (!string.IsNullOrEmpty(startupUserDto.Password))
{
await _userManager.ChangePassword(user, startupUserDto.Password).ConfigureAwait(false);
}
return NoContent();
}
}
}