Skip to content

Commit

Permalink
add setting to enable or disable remote access
Browse files Browse the repository at this point in the history
  • Loading branch information
LukePulverenti committed Jan 11, 2018
1 parent ba2cf9e commit 209749d
Show file tree
Hide file tree
Showing 25 changed files with 108 additions and 177 deletions.
2 changes: 1 addition & 1 deletion Emby.Server.Implementations/ApplicationHost.cs
Expand Up @@ -992,7 +992,7 @@ protected async Task RegisterResources(IProgress<double> progress)
ChannelManager = new ChannelManager(UserManager, DtoService, LibraryManager, LogManager.GetLogger("ChannelManager"), ServerConfigurationManager, FileSystemManager, UserDataManager, JsonSerializer, LocalizationManager, HttpClient, ProviderManager);
RegisterSingleInstance(ChannelManager);

MediaSourceManager = new MediaSourceManager(ItemRepository, UserManager, LibraryManager, LogManager.GetLogger("MediaSourceManager"), JsonSerializer, FileSystemManager, UserDataManager, TimerFactory);
MediaSourceManager = new MediaSourceManager(ItemRepository, UserManager, LibraryManager, LogManager.GetLogger("MediaSourceManager"), JsonSerializer, FileSystemManager, UserDataManager, TimerFactory, () => MediaEncoder);
RegisterSingleInstance(MediaSourceManager);

SessionManager = new SessionManager(UserDataManager, LogManager.GetLogger("SessionManager"), LibraryManager, UserManager, musicManager, DtoService, ImageProcessor, JsonSerializer, this, HttpClient, AuthenticationRepository, DeviceManager, MediaSourceManager, TimerFactory);
Expand Down
Expand Up @@ -38,6 +38,12 @@ public ExternalPortForwarding(ILogManager logmanager, IServerApplicationHost app
_deviceDiscovery = deviceDiscovery;
_httpClient = httpClient;
_timerFactory = timerFactory;
_config.ConfigurationUpdated += _config_ConfigurationUpdated1;
}

private void _config_ConfigurationUpdated1(object sender, EventArgs e)
{
_config_ConfigurationUpdated(sender, e);
}

private string _lastConfigIdentifier;
Expand All @@ -52,6 +58,7 @@ private string GetConfigIdentifier()
values.Add(_appHost.HttpsPort.ToString(CultureInfo.InvariantCulture));
values.Add((config.EnableHttps || config.RequireHttps).ToString());
values.Add(_appHost.EnableHttps.ToString());
values.Add((config.EnableRemoteAccess).ToString());

return string.Join("|", values.ToArray(values.Count));
}
Expand All @@ -68,7 +75,7 @@ void _config_ConfigurationUpdated(object sender, EventArgs e)

public void Run()
{
if (_config.Configuration.EnableUPnP)
if (_config.Configuration.EnableUPnP && _config.Configuration.EnableRemoteAccess)
{
Start();
}
Expand Down
20 changes: 17 additions & 3 deletions Emby.Server.Implementations/HttpServer/HttpListenerHost.cs
Expand Up @@ -422,6 +422,21 @@ private bool ValidateHost(string host)
return true;
}

private bool ValidateRequest(string remoteIp, bool isLocal)
{
if (isLocal)
{
return true;
}

if (_config.Configuration.EnableRemoteAccess)
{
return true;
}

return _networkManager.IsInLocalNetwork(remoteIp);
}

private bool ValidateSsl(string remoteIp, string urlString)
{
if (_config.Configuration.RequireHttps && _appHost.EnableHttps)
Expand All @@ -448,7 +463,7 @@ protected async Task RequestHandler(IHttpRequest httpReq, string urlString, stri
bool enableLog = false;
bool logHeaders = false;
string urlToLog = null;
string remoteIp = null;
string remoteIp = httpReq.RemoteIp;

try
{
Expand All @@ -460,7 +475,7 @@ protected async Task RequestHandler(IHttpRequest httpReq, string urlString, stri
return;
}

if (!ValidateHost(host))
if (!ValidateHost(host) || !ValidateRequest(remoteIp, httpReq.IsLocal))
{
httpRes.StatusCode = 400;
httpRes.ContentType = "text/plain";
Expand Down Expand Up @@ -498,7 +513,6 @@ protected async Task RequestHandler(IHttpRequest httpReq, string urlString, stri
if (enableLog)
{
urlToLog = GetUrlToLog(urlString);
remoteIp = httpReq.RemoteIp;

LoggerUtils.LogRequest(_logger, urlToLog, httpReq.HttpMethod, httpReq.UserAgent, logHeaders ? httpReq.Headers : null);
}
Expand Down
39 changes: 23 additions & 16 deletions Emby.Server.Implementations/Library/MediaSourceManager.cs
Expand Up @@ -16,6 +16,7 @@
using MediaBrowser.Model.IO;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Threading;
using MediaBrowser.Model.Dlna;

namespace Emby.Server.Implementations.Library
{
Expand All @@ -31,8 +32,9 @@ public class MediaSourceManager : IMediaSourceManager, IDisposable
private readonly ILogger _logger;
private readonly IUserDataManager _userDataManager;
private readonly ITimerFactory _timerFactory;
private readonly Func<IMediaEncoder> _mediaEncoder;

public MediaSourceManager(IItemRepository itemRepo, IUserManager userManager, ILibraryManager libraryManager, ILogger logger, IJsonSerializer jsonSerializer, IFileSystem fileSystem, IUserDataManager userDataManager, ITimerFactory timerFactory)
public MediaSourceManager(IItemRepository itemRepo, IUserManager userManager, ILibraryManager libraryManager, ILogger logger, IJsonSerializer jsonSerializer, IFileSystem fileSystem, IUserDataManager userDataManager, ITimerFactory timerFactory, Func<IMediaEncoder> mediaEncoder)
{
_itemRepo = itemRepo;
_userManager = userManager;
Expand All @@ -42,6 +44,7 @@ public MediaSourceManager(IItemRepository itemRepo, IUserManager userManager, IL
_fileSystem = fileSystem;
_userDataManager = userDataManager;
_timerFactory = timerFactory;
_mediaEncoder = mediaEncoder;
}

public void AddParts(IEnumerable<IMediaSourceProvider> providers)
Expand Down Expand Up @@ -215,21 +218,6 @@ public async Task<MediaSourceInfo> GetMediaSource(IHasMediaSources item, string
{
return await GetLiveStream(liveStreamId, cancellationToken).ConfigureAwait(false);
}
//await _liveStreamSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);

//try
//{
// var stream = _openStreams.Values.FirstOrDefault(i => string.Equals(i.MediaSource.Id, mediaSourceId, StringComparison.OrdinalIgnoreCase));

// if (stream != null)
// {
// return stream.MediaSource;
// }
//}
//finally
//{
// _liveStreamSemaphore.Release();
//}

var sources = await GetPlayackMediaSources(item.Id.ToString("N"), null, enablePathSubstitution, new[] { MediaType.Audio, MediaType.Video },
CancellationToken.None).ConfigureAwait(false);
Expand Down Expand Up @@ -399,6 +387,25 @@ public async Task<LiveStreamResponse> OpenLiveStream(LiveStreamRequest request,
}
}

public async Task<MediaSourceInfo> GetLiveStreamMediaInfo(string id, CancellationToken cancellationToken)
{
var mediaSource = await GetLiveStream(id, cancellationToken).ConfigureAwait(false);

var info = await _mediaEncoder().GetMediaInfo(new MediaInfoRequest
{
MediaSource = mediaSource,
ExtractChapters = false,
MediaType = DlnaProfileType.Video

}, cancellationToken).ConfigureAwait(false);

mediaSource.MediaStreams = info.MediaStreams;
mediaSource.Container = info.Container;
mediaSource.Bitrate = info.Bitrate;

return mediaSource;
}

public async Task<Tuple<MediaSourceInfo, IDirectStreamProvider>> GetLiveStreamWithDirectStreamProvider(string id, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(id))
Expand Down
11 changes: 0 additions & 11 deletions Emby.Server.Implementations/Library/ResolverHelper.cs
Expand Up @@ -107,17 +107,6 @@ private static string GetDisplayName(string path, bool isDirectory)
return isDirectory ? Path.GetFileName(path) : Path.GetFileNameWithoutExtension(path);
}

/// <summary>
/// The MB name regex
/// </summary>
private static readonly Regex MbNameRegex = new Regex(@"(\[.*?\])");

internal static string StripBrackets(string inputString)
{
var output = MbNameRegex.Replace(inputString, string.Empty).Trim();
return Regex.Replace(output, @"\s+", " ");
}

/// <summary>
/// Ensures DateCreated and DateModified have values
/// </summary>
Expand Down
Expand Up @@ -4,6 +4,7 @@
using MediaBrowser.Model.Entities;
using System;
using System.IO;
using MediaBrowser.Model.Extensions;

namespace Emby.Server.Implementations.Library.Resolvers.Movies
{
Expand All @@ -30,14 +31,13 @@ protected override BoxSet Resolve(ItemResolveArgs args)
{
return null;
}

if (filename.IndexOf("[boxset]", StringComparison.OrdinalIgnoreCase) != -1 ||
args.ContainsFileSystemEntryByName("collection.xml"))

if (filename.IndexOf("[boxset]", StringComparison.OrdinalIgnoreCase) != -1 || args.ContainsFileSystemEntryByName("collection.xml"))
{
return new BoxSet
{
Path = args.Path,
Name = ResolverHelper.StripBrackets(Path.GetFileName(args.Path))
Name = Path.GetFileName(args.Path).Replace("[boxset]", string.Empty, StringComparison.OrdinalIgnoreCase).Trim()
};
}
}
Expand Down
Expand Up @@ -2,6 +2,7 @@
using MediaBrowser.Controller.Playlists;
using System;
using System.IO;
using MediaBrowser.Model.Extensions;

namespace Emby.Server.Implementations.Library.Resolvers
{
Expand Down Expand Up @@ -31,7 +32,7 @@ protected override Playlist Resolve(ItemResolveArgs args)
return new Playlist
{
Path = args.Path,
Name = ResolverHelper.StripBrackets(Path.GetFileName(args.Path))
Name = Path.GetFileName(args.Path).Replace("[playlist]", string.Empty, StringComparison.OrdinalIgnoreCase).Trim()
};
}
}
Expand Down
23 changes: 6 additions & 17 deletions Emby.Server.Implementations/LiveTv/LiveStreamHelper.cs
Expand Up @@ -22,8 +22,6 @@ public class LiveStreamHelper
private readonly IMediaEncoder _mediaEncoder;
private readonly ILogger _logger;

const int ProbeAnalyzeDurationMs = 3000;
const int PlaybackAnalyzeDurationMs = 3000;
private IJsonSerializer _json;
private IApplicationPaths _appPaths;

Expand Down Expand Up @@ -59,27 +57,18 @@ public async Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudi

if (mediaInfo == null)
{
var path = mediaSource.Path;
var protocol = mediaSource.Protocol;

if (!string.IsNullOrWhiteSpace(mediaSource.EncoderPath) && mediaSource.EncoderProtocol.HasValue)
{
path = mediaSource.EncoderPath;
protocol = mediaSource.EncoderProtocol.Value;
}

if (probeDelayMs > 0)
{
await Task.Delay(probeDelayMs, cancellationToken).ConfigureAwait(false);
}

mediaSource.AnalyzeDurationMs = 3000;

mediaInfo = await _mediaEncoder.GetMediaInfo(new MediaInfoRequest
{
InputPath = path,
Protocol = protocol,
MediaSource = mediaSource,
MediaType = isAudio ? DlnaProfileType.Audio : DlnaProfileType.Video,
ExtractChapters = false,
AnalyzeDurationMs = ProbeAnalyzeDurationMs
ExtractChapters = false

}, cancellationToken).ConfigureAwait(false);

Expand Down Expand Up @@ -172,10 +161,10 @@ public async Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudi
videoStream.IsAVC = null;
}

mediaSource.AnalyzeDurationMs = 3000;

// Try to estimate this
mediaSource.InferTotalBitrate(true);

mediaSource.AnalyzeDurationMs = PlaybackAnalyzeDurationMs;
}

public Task AddMediaInfoWithProbe(MediaSourceInfo mediaSource, bool isAudio, int probeDelayMs, CancellationToken cancellationToken)
Expand Down
Expand Up @@ -521,7 +521,6 @@ private MediaSourceInfo GetMediaSource(TunerHostInfo info, string channelId, Cha
SupportsTranscoding = true,
IsInfiniteStream = true,
IgnoreDts = true,
//AnalyzeDurationMs = 2000000
//IgnoreIndex = true,
//ReadAtNativeFramerate = true
};
Expand Down
2 changes: 2 additions & 0 deletions MediaBrowser.Controller/Library/IMediaSourceManager.cs
Expand Up @@ -88,6 +88,8 @@ public interface IMediaSourceManager
/// <param name="id">The live stream identifier.</param>
/// <returns>Task.</returns>
Task CloseLiveStream(string id);

Task<MediaSourceInfo> GetLiveStreamMediaInfo(string id, CancellationToken cancellationToken);
}

public interface IDirectStreamProvider
Expand Down
6 changes: 2 additions & 4 deletions MediaBrowser.Controller/MediaEncoding/MediaInfoRequest.cs
Expand Up @@ -3,19 +3,17 @@
using MediaBrowser.Model.IO;
using MediaBrowser.Model.MediaInfo;
using System.Collections.Generic;
using MediaBrowser.Model.Dto;

namespace MediaBrowser.Controller.MediaEncoding
{
public class MediaInfoRequest
{
public string InputPath { get; set; }
public MediaProtocol Protocol { get; set; }
public MediaSourceInfo MediaSource { get; set; }
public bool ExtractChapters { get; set; }
public DlnaProfileType MediaType { get; set; }
public IIsoMount MountedIso { get; set; }
public VideoType VideoType { get; set; }
public string[] PlayableStreamFileNames { get; set; }
public int AnalyzeDurationMs { get; set; }

public MediaInfoRequest()
{
Expand Down
3 changes: 3 additions & 0 deletions MediaBrowser.Model/Configuration/ServerConfiguration.cs
Expand Up @@ -62,6 +62,7 @@ public class ServerConfiguration : BaseApplicationConfiguration
public bool IsPortAuthorized { get; set; }

public bool AutoRunWebApp { get; set; }
public bool EnableRemoteAccess { get; set; }

/// <summary>
/// Gets or sets a value indicating whether [enable case sensitive item ids].
Expand Down Expand Up @@ -214,6 +215,8 @@ public ServerConfiguration()
EnableCaseSensitiveItemIds = true;

EnableAutomaticRestart = true;
AutoRunWebApp = true;
EnableRemoteAccess = true;

EnableUPnP = true;
MinResumePct = 5;
Expand Down
8 changes: 6 additions & 2 deletions MediaBrowser.Providers/MediaInfo/FFProbeAudioInfo.cs
Expand Up @@ -14,6 +14,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using MediaBrowser.Model.Dto;

namespace MediaBrowser.Providers.MediaInfo
{
Expand Down Expand Up @@ -41,9 +42,12 @@ public async Task<ItemUpdateType> Probe<T>(T item, CancellationToken cancellatio
{
var result = await _mediaEncoder.GetMediaInfo(new MediaInfoRequest
{
InputPath = item.Path,
MediaType = DlnaProfileType.Audio,
Protocol = MediaProtocol.File
MediaSource = new MediaSourceInfo
{
Path = item.Path,
Protocol = MediaProtocol.File
}

}, cancellationToken).ConfigureAwait(false);

Expand Down
10 changes: 7 additions & 3 deletions MediaBrowser.Providers/MediaInfo/FFProbeVideoInfo.cs
Expand Up @@ -26,6 +26,7 @@
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Globalization;
using MediaBrowser.Model.Dto;

namespace MediaBrowser.Providers.MediaInfo
{
Expand Down Expand Up @@ -132,10 +133,13 @@ public FFProbeVideoInfo(ILogger logger, IIsoManager isoManager, IMediaEncoder me
{
PlayableStreamFileNames = streamFileNames,
ExtractChapters = true,
VideoType = item.VideoType,
MediaType = DlnaProfileType.Video,
InputPath = item.Path,
Protocol = protocol
MediaSource = new MediaSourceInfo
{
Path = item.Path,
Protocol = protocol,
VideoType = item.VideoType
}

}, cancellationToken);
}
Expand Down
1 change: 0 additions & 1 deletion MediaBrowser.Providers/Movies/GenericMovieDbInfo.cs
Expand Up @@ -174,7 +174,6 @@ private void ProcessMainInfo(MetadataResult<T> resultItem, TmdbSettingsResult se

//movie.VoteCount = movieData.vote_count;

//release date and certification are retrieved based on configured country and we fall back on US if not there and to minimun release date if still no match
if (movieData.releases != null && movieData.releases.countries != null)
{
var releases = movieData.releases.countries.Where(i => !string.IsNullOrWhiteSpace(i.certification)).ToList();
Expand Down

0 comments on commit 209749d

Please sign in to comment.