2014-07-03 19:22:57 -07:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2014-08-19 15:28:35 -07:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-08-31 12:15:33 -07:00
|
|
|
|
using MediaBrowser.Common.Security;
|
2014-07-03 19:22:57 -07:00
|
|
|
|
using MediaBrowser.Controller;
|
|
|
|
|
using MediaBrowser.Controller.Net;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
using MediaBrowser.Model.System;
|
2014-08-10 15:13:17 -07:00
|
|
|
|
using System;
|
2014-07-03 19:22:57 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
using System.Threading.Tasks;
|
2017-05-25 23:48:54 -07:00
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
|
|
|
|
using MediaBrowser.Model.IO;
|
2016-01-09 14:27:30 -07:00
|
|
|
|
using MediaBrowser.Model.Net;
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Model.Services;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2014-08-10 15:13:17 -07:00
|
|
|
|
namespace MediaBrowser.Api.System
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2013-02-24 14:53:54 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class GetSystemInfo
|
|
|
|
|
/// </summary>
|
2014-03-23 12:36:25 -07:00
|
|
|
|
[Route("/System/Info", "GET", Summary = "Gets information about the server")]
|
2016-06-23 10:04:18 -07:00
|
|
|
|
[Authenticated(EscapeParentalControl = true, AllowBeforeStartupWizard = true)]
|
2013-02-20 18:33:05 -07:00
|
|
|
|
public class GetSystemInfo : IReturn<SystemInfo>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-27 15:01:29 -07:00
|
|
|
|
[Route("/System/Info/Public", "GET", Summary = "Gets public information about the server")]
|
|
|
|
|
public class GetPublicSystemInfo : IReturn<PublicSystemInfo>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-02 16:45:21 -07:00
|
|
|
|
[Route("/System/Ping", "POST")]
|
|
|
|
|
public class PingSystem : IReturnVoid
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class RestartApplication
|
|
|
|
|
/// </summary>
|
2014-03-23 12:36:25 -07:00
|
|
|
|
[Route("/System/Restart", "POST", Summary = "Restarts the application, if needed")]
|
2017-09-03 11:38:26 -07:00
|
|
|
|
[Authenticated(Roles = "Admin", AllowLocal = true)]
|
2013-02-20 18:33:05 -07:00
|
|
|
|
public class RestartApplication
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-24 20:54:45 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is currently not authenticated because the uninstaller needs to be able to shutdown the server.
|
|
|
|
|
/// </summary>
|
2014-03-23 12:36:25 -07:00
|
|
|
|
[Route("/System/Shutdown", "POST", Summary = "Shuts down the application")]
|
2017-09-03 11:38:26 -07:00
|
|
|
|
[Authenticated(Roles = "Admin", AllowLocal = true)]
|
2013-02-26 09:10:55 -07:00
|
|
|
|
public class ShutdownApplication
|
|
|
|
|
{
|
|
|
|
|
}
|
2014-03-23 12:36:25 -07:00
|
|
|
|
|
2014-07-03 19:22:57 -07:00
|
|
|
|
[Route("/System/Logs", "GET", Summary = "Gets a list of available server log files")]
|
2014-11-14 19:31:03 -07:00
|
|
|
|
[Authenticated(Roles = "Admin")]
|
2017-08-19 12:43:35 -07:00
|
|
|
|
public class GetServerLogs : IReturn<LogFile[]>
|
2014-07-03 19:22:57 -07:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:28:35 -07:00
|
|
|
|
[Route("/System/Endpoint", "GET", Summary = "Gets information about the request endpoint")]
|
|
|
|
|
[Authenticated]
|
2016-01-09 14:27:30 -07:00
|
|
|
|
public class GetEndpointInfo : IReturn<EndPointInfo>
|
2014-08-19 15:28:35 -07:00
|
|
|
|
{
|
|
|
|
|
public string Endpoint { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-03 19:22:57 -07:00
|
|
|
|
[Route("/System/Logs/Log", "GET", Summary = "Gets a log file")]
|
2014-11-14 19:31:03 -07:00
|
|
|
|
[Authenticated(Roles = "Admin")]
|
2014-07-03 19:22:57 -07:00
|
|
|
|
public class GetLogFile
|
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "Name", Description = "The log file name.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class SystemInfoService
|
|
|
|
|
/// </summary>
|
2013-03-15 22:52:33 -07:00
|
|
|
|
public class SystemService : BaseApiService
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2013-02-26 09:10:55 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _app host
|
|
|
|
|
/// </summary>
|
2013-03-06 22:34:00 -07:00
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
2014-07-03 19:22:57 -07:00
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-10-31 07:03:23 -07:00
|
|
|
|
|
2014-08-19 15:28:35 -07:00
|
|
|
|
private readonly INetworkManager _network;
|
|
|
|
|
|
2014-08-31 12:15:33 -07:00
|
|
|
|
private readonly ISecurityManager _security;
|
|
|
|
|
|
2013-02-24 14:53:54 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="SystemService" /> class.
|
|
|
|
|
/// </summary>
|
2013-02-26 09:10:55 -07:00
|
|
|
|
/// <param name="appHost">The app host.</param>
|
2014-08-05 16:59:24 -07:00
|
|
|
|
/// <param name="appPaths">The application paths.</param>
|
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
2014-08-10 15:13:17 -07:00
|
|
|
|
/// <exception cref="ArgumentNullException">jsonSerializer</exception>
|
2014-08-31 12:15:33 -07:00
|
|
|
|
public SystemService(IServerApplicationHost appHost, IApplicationPaths appPaths, IFileSystem fileSystem, INetworkManager network, ISecurityManager security)
|
2013-02-24 14:53:54 -07:00
|
|
|
|
{
|
2013-02-26 09:10:55 -07:00
|
|
|
|
_appHost = appHost;
|
2014-07-03 19:22:57 -07:00
|
|
|
|
_appPaths = appPaths;
|
|
|
|
|
_fileSystem = fileSystem;
|
2014-08-19 15:28:35 -07:00
|
|
|
|
_network = network;
|
2014-08-31 12:15:33 -07:00
|
|
|
|
_security = security;
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-02 16:45:21 -07:00
|
|
|
|
public object Post(PingSystem request)
|
|
|
|
|
{
|
|
|
|
|
return _appHost.Name;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-03 19:22:57 -07:00
|
|
|
|
public object Get(GetServerLogs request)
|
|
|
|
|
{
|
2017-08-09 12:56:38 -07:00
|
|
|
|
IEnumerable<FileSystemMetadata> files;
|
2014-07-03 19:22:57 -07:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-08-09 12:56:38 -07:00
|
|
|
|
files = _fileSystem.GetFiles(_appPaths.LogDirectoryPath, new[] { ".txt" }, true, false);
|
2014-07-03 19:22:57 -07:00
|
|
|
|
}
|
2016-10-31 20:07:45 -07:00
|
|
|
|
catch (IOException)
|
2014-07-03 19:22:57 -07:00
|
|
|
|
{
|
2017-09-03 11:38:26 -07:00
|
|
|
|
files = new FileSystemMetadata[] { };
|
2014-07-03 19:22:57 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = files.Select(i => new LogFile
|
|
|
|
|
{
|
|
|
|
|
DateCreated = _fileSystem.GetCreationTimeUtc(i),
|
|
|
|
|
DateModified = _fileSystem.GetLastWriteTimeUtc(i),
|
|
|
|
|
Name = i.Name,
|
|
|
|
|
Size = i.Length
|
|
|
|
|
|
|
|
|
|
}).OrderByDescending(i => i.DateModified)
|
|
|
|
|
.ThenByDescending(i => i.DateCreated)
|
|
|
|
|
.ThenBy(i => i.Name)
|
2017-08-19 12:43:35 -07:00
|
|
|
|
.ToArray();
|
2014-07-03 19:22:57 -07:00
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-18 23:18:29 -07:00
|
|
|
|
public Task<object> Get(GetLogFile request)
|
2014-07-03 19:22:57 -07:00
|
|
|
|
{
|
2016-06-23 10:04:18 -07:00
|
|
|
|
var file = _fileSystem.GetFiles(_appPaths.LogDirectoryPath)
|
2014-11-05 12:28:41 -07:00
|
|
|
|
.First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
|
2014-07-03 19:22:57 -07:00
|
|
|
|
|
2017-09-03 18:24:20 -07:00
|
|
|
|
// For older files, assume fully static
|
|
|
|
|
if (file.LastWriteTimeUtc < DateTime.UtcNow.AddHours(-1))
|
|
|
|
|
{
|
|
|
|
|
return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShareMode.Read);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
|
return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShareMode.ReadWrite);
|
2013-02-24 14:53:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
2016-06-19 09:53:43 -07:00
|
|
|
|
public async Task<object> Get(GetSystemInfo request)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2016-06-19 09:53:43 -07:00
|
|
|
|
var result = await _appHost.GetSystemInfo().ConfigureAwait(false);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-19 09:53:43 -07:00
|
|
|
|
public async Task<object> Get(GetPublicSystemInfo request)
|
2014-07-27 15:01:29 -07:00
|
|
|
|
{
|
2016-06-19 09:53:43 -07:00
|
|
|
|
var result = await _appHost.GetSystemInfo().ConfigureAwait(false);
|
2014-07-27 15:01:29 -07:00
|
|
|
|
|
|
|
|
|
var publicInfo = new PublicSystemInfo
|
|
|
|
|
{
|
|
|
|
|
Id = result.Id,
|
|
|
|
|
ServerName = result.ServerName,
|
2014-09-24 15:47:03 -07:00
|
|
|
|
Version = result.Version,
|
|
|
|
|
LocalAddress = result.LocalAddress,
|
2015-09-05 10:19:13 -07:00
|
|
|
|
WanAddress = result.WanAddress,
|
|
|
|
|
OperatingSystem = result.OperatingSystem
|
2014-07-27 15:01:29 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(publicInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Posts the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
public void Post(RestartApplication request)
|
|
|
|
|
{
|
2017-09-09 11:51:24 -07:00
|
|
|
|
_appHost.Restart();
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-26 09:10:55 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Posts the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
public void Post(ShutdownApplication request)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
2013-09-20 10:32:10 -07:00
|
|
|
|
await Task.Delay(100).ConfigureAwait(false);
|
|
|
|
|
await _appHost.Shutdown().ConfigureAwait(false);
|
2013-02-26 09:10:55 -07:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 15:28:35 -07:00
|
|
|
|
public object Get(GetEndpointInfo request)
|
|
|
|
|
{
|
2016-01-09 14:27:30 -07:00
|
|
|
|
return ToOptimizedResult(new EndPointInfo
|
2014-08-19 15:28:35 -07:00
|
|
|
|
{
|
|
|
|
|
IsLocal = Request.IsLocal,
|
|
|
|
|
IsInNetwork = _network.IsInLocalNetwork(request.Endpoint ?? Request.RemoteIp)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|