2014-07-04 02:22:57 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
2014-08-19 22:28:35 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-08-31 19:15:33 +00:00
|
|
|
|
using MediaBrowser.Common.Security;
|
2014-07-04 02:22:57 +00:00
|
|
|
|
using MediaBrowser.Controller;
|
|
|
|
|
using MediaBrowser.Controller.Net;
|
2014-08-31 19:15:33 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using MediaBrowser.Model.System;
|
2013-12-07 15:52:38 +00:00
|
|
|
|
using ServiceStack;
|
2014-08-10 22:13:17 +00:00
|
|
|
|
using System;
|
2014-07-04 02:22:57 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2014-08-10 22:13:17 +00:00
|
|
|
|
namespace MediaBrowser.Api.System
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-24 21:53:54 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class GetSystemInfo
|
|
|
|
|
/// </summary>
|
2014-03-23 19:36:25 +00:00
|
|
|
|
[Route("/System/Info", "GET", Summary = "Gets information about the server")]
|
2014-10-28 23:17:55 +00:00
|
|
|
|
[Authenticated(EscapeParentalControl = true)]
|
2013-02-21 01:33:05 +00:00
|
|
|
|
public class GetSystemInfo : IReturn<SystemInfo>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-27 22:01:29 +00:00
|
|
|
|
[Route("/System/Info/Public", "GET", Summary = "Gets public information about the server")]
|
|
|
|
|
public class GetPublicSystemInfo : IReturn<PublicSystemInfo>
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class RestartApplication
|
|
|
|
|
/// </summary>
|
2014-03-23 19:36:25 +00:00
|
|
|
|
[Route("/System/Restart", "POST", Summary = "Restarts the application, if needed")]
|
2014-11-15 02:31:03 +00:00
|
|
|
|
[Authenticated(Roles = "Admin")]
|
2013-02-21 01:33:05 +00:00
|
|
|
|
public class RestartApplication
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-25 03:54:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This is currently not authenticated because the uninstaller needs to be able to shutdown the server.
|
|
|
|
|
/// </summary>
|
2014-03-23 19:36:25 +00:00
|
|
|
|
[Route("/System/Shutdown", "POST", Summary = "Shuts down the application")]
|
2013-02-26 16:10:55 +00:00
|
|
|
|
public class ShutdownApplication
|
|
|
|
|
{
|
2014-11-09 03:18:14 +00:00
|
|
|
|
// TODO: This is not currently authenticated due to uninstaller
|
|
|
|
|
// Improve later
|
2013-02-26 16:10:55 +00:00
|
|
|
|
}
|
2014-03-23 19:36:25 +00:00
|
|
|
|
|
2014-07-04 02:22:57 +00:00
|
|
|
|
[Route("/System/Logs", "GET", Summary = "Gets a list of available server log files")]
|
2014-11-15 02:31:03 +00:00
|
|
|
|
[Authenticated(Roles = "Admin")]
|
2014-07-04 02:22:57 +00:00
|
|
|
|
public class GetServerLogs : IReturn<List<LogFile>>
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 22:28:35 +00:00
|
|
|
|
[Route("/System/Endpoint", "GET", Summary = "Gets information about the request endpoint")]
|
|
|
|
|
[Authenticated]
|
|
|
|
|
public class GetEndpointInfo : IReturn<EndpointInfo>
|
|
|
|
|
{
|
|
|
|
|
public string Endpoint { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-04 02:22:57 +00:00
|
|
|
|
[Route("/System/Logs/Log", "GET", Summary = "Gets a log file")]
|
2014-11-15 02:31:03 +00:00
|
|
|
|
[Authenticated(Roles = "Admin")]
|
2014-07-04 02:22:57 +00: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; }
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-31 19:15:33 +00:00
|
|
|
|
[Route("/System/SupporterInfo", "GET")]
|
|
|
|
|
[Authenticated]
|
|
|
|
|
public class GetSupporterInfo : IReturn<SupporterInfo>
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class SystemInfoService
|
|
|
|
|
/// </summary>
|
2013-03-16 05:52:33 +00:00
|
|
|
|
public class SystemService : BaseApiService
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-26 16:10:55 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _app host
|
|
|
|
|
/// </summary>
|
2013-03-07 05:34:00 +00:00
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
2014-07-04 02:22:57 +00:00
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
2013-10-31 14:03:23 +00:00
|
|
|
|
|
2014-08-19 22:28:35 +00:00
|
|
|
|
private readonly INetworkManager _network;
|
|
|
|
|
|
2014-08-31 19:15:33 +00:00
|
|
|
|
private readonly ISecurityManager _security;
|
|
|
|
|
|
2013-02-24 21:53:54 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="SystemService" /> class.
|
|
|
|
|
/// </summary>
|
2013-02-26 16:10:55 +00:00
|
|
|
|
/// <param name="appHost">The app host.</param>
|
2014-08-05 23:59:24 +00:00
|
|
|
|
/// <param name="appPaths">The application paths.</param>
|
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
2014-08-10 22:13:17 +00:00
|
|
|
|
/// <exception cref="ArgumentNullException">jsonSerializer</exception>
|
2014-08-31 19:15:33 +00:00
|
|
|
|
public SystemService(IServerApplicationHost appHost, IApplicationPaths appPaths, IFileSystem fileSystem, INetworkManager network, ISecurityManager security)
|
2013-02-24 21:53:54 +00:00
|
|
|
|
{
|
2013-02-26 16:10:55 +00:00
|
|
|
|
_appHost = appHost;
|
2014-07-04 02:22:57 +00:00
|
|
|
|
_appPaths = appPaths;
|
|
|
|
|
_fileSystem = fileSystem;
|
2014-08-19 22:28:35 +00:00
|
|
|
|
_network = network;
|
2014-08-31 19:15:33 +00:00
|
|
|
|
_security = security;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<object> Get(GetSupporterInfo request)
|
|
|
|
|
{
|
|
|
|
|
var result = await _security.GetSupporterInfo().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
2014-07-04 02:22:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object Get(GetServerLogs request)
|
|
|
|
|
{
|
|
|
|
|
List<FileInfo> files;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
files = new DirectoryInfo(_appPaths.LogDirectoryPath)
|
|
|
|
|
.EnumerateFiles("*", SearchOption.AllDirectories)
|
2014-11-05 19:28:41 +00:00
|
|
|
|
.Where(i => string.Equals(i.Extension, ".txt", StringComparison.OrdinalIgnoreCase))
|
2014-07-04 02:22:57 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
catch (DirectoryNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
files = new List<FileInfo>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object Get(GetLogFile request)
|
|
|
|
|
{
|
|
|
|
|
var file = new DirectoryInfo(_appPaths.LogDirectoryPath)
|
|
|
|
|
.EnumerateFiles("*", SearchOption.AllDirectories)
|
2014-11-05 19:28:41 +00:00
|
|
|
|
.First(i => string.Equals(i.Name, request.Name, StringComparison.OrdinalIgnoreCase));
|
2014-07-04 02:22:57 +00:00
|
|
|
|
|
|
|
|
|
return ResultFactory.GetStaticFileResult(Request, file.FullName, FileShare.ReadWrite);
|
2013-02-24 21:53:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
public object Get(GetSystemInfo request)
|
|
|
|
|
{
|
2013-03-07 05:34:00 +00:00
|
|
|
|
var result = _appHost.GetSystemInfo();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(result);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-27 22:01:29 +00:00
|
|
|
|
public object Get(GetPublicSystemInfo request)
|
|
|
|
|
{
|
|
|
|
|
var result = _appHost.GetSystemInfo();
|
|
|
|
|
|
|
|
|
|
var publicInfo = new PublicSystemInfo
|
|
|
|
|
{
|
|
|
|
|
Id = result.Id,
|
|
|
|
|
ServerName = result.ServerName,
|
2014-09-24 22:47:03 +00:00
|
|
|
|
Version = result.Version,
|
|
|
|
|
LocalAddress = result.LocalAddress,
|
|
|
|
|
WanAddress = result.WanAddress
|
2014-07-27 22:01:29 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ToOptimizedResult(publicInfo);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Posts the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
public void Post(RestartApplication request)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
2013-09-20 17:32:10 +00:00
|
|
|
|
await Task.Delay(100).ConfigureAwait(false);
|
|
|
|
|
await _appHost.Restart().ConfigureAwait(false);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-26 16:10:55 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Posts the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
public void Post(ShutdownApplication request)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
2013-09-20 17:32:10 +00:00
|
|
|
|
await Task.Delay(100).ConfigureAwait(false);
|
|
|
|
|
await _appHost.Shutdown().ConfigureAwait(false);
|
2013-02-26 16:10:55 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-19 22:28:35 +00:00
|
|
|
|
public object Get(GetEndpointInfo request)
|
|
|
|
|
{
|
|
|
|
|
return ToOptimizedResult(new EndpointInfo
|
|
|
|
|
{
|
|
|
|
|
IsLocal = Request.IsLocal,
|
|
|
|
|
IsInNetwork = _network.IsInLocalNetwork(request.Endpoint ?? Request.RemoteIp)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class EndpointInfo
|
|
|
|
|
{
|
|
|
|
|
public bool IsLocal { get; set; }
|
|
|
|
|
public bool IsInNetwork { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|