2016-04-24 05:03:49 +02:00
using System ;
using MediaBrowser.Common.Net ;
2014-11-09 19:24:57 +01:00
using MediaBrowser.Model.Logging ;
using MediaBrowser.Server.Startup.Common ;
using MediaBrowser.ServerApplication.Networking ;
using System.Collections.Generic ;
2016-04-24 05:03:49 +02:00
using System.Diagnostics ;
2016-04-03 19:34:52 +02:00
using System.IO ;
2014-11-09 19:24:57 +01:00
using System.Reflection ;
2016-09-06 08:50:07 +02:00
using System.Windows.Forms ;
2015-10-04 06:23:11 +02:00
using CommonIO ;
2016-01-21 18:29:14 +01:00
using MediaBrowser.Controller.Power ;
2016-06-23 19:04:18 +02:00
using MediaBrowser.Model.System ;
2016-05-01 23:48:37 +02:00
using MediaBrowser.Server.Implementations.Persistence ;
2016-04-02 06:29:48 +02:00
using MediaBrowser.Server.Startup.Common.FFMpeg ;
2016-04-24 05:03:49 +02:00
using OperatingSystem = MediaBrowser . Server . Startup . Common . OperatingSystem ;
2014-11-09 19:24:57 +01:00
namespace MediaBrowser.ServerApplication.Native
{
public class WindowsApp : INativeApp
{
2015-01-13 04:46:44 +01:00
private readonly IFileSystem _fileSystem ;
2016-01-21 18:45:42 +01:00
private readonly ILogger _logger ;
2015-01-13 04:46:44 +01:00
2016-01-21 18:45:42 +01:00
public WindowsApp ( IFileSystem fileSystem , ILogger logger )
2015-01-13 04:46:44 +01:00
{
_fileSystem = fileSystem ;
2016-01-21 18:45:42 +01:00
_logger = logger ;
2015-01-13 04:46:44 +01:00
}
2014-11-09 19:24:57 +01:00
public List < Assembly > GetAssembliesWithParts ( )
{
var list = new List < Assembly > ( ) ;
2015-10-12 21:09:56 +02:00
if ( ! System . Environment . Is64BitProcess )
{
2016-01-06 17:57:15 +01:00
//list.Add(typeof(PismoIsoManager).Assembly);
2015-10-12 21:09:56 +02:00
}
2014-11-09 19:24:57 +01:00
list . Add ( GetType ( ) . Assembly ) ;
2016-04-02 06:29:48 +02:00
2014-11-09 19:24:57 +01:00
return list ;
}
2016-03-26 18:51:27 +01:00
public void AuthorizeServer ( int udpPort , int httpServerPort , int httpsPort , string applicationPath , string tempDirectory )
2014-11-09 19:24:57 +01:00
{
2016-03-26 18:51:27 +01:00
ServerAuthorization . AuthorizeServer ( udpPort , httpServerPort , httpsPort , applicationPath , tempDirectory ) ;
2014-11-09 19:24:57 +01:00
}
public NativeEnvironment Environment
{
get
{
return new NativeEnvironment
{
OperatingSystem = OperatingSystem . Windows ,
2016-06-23 19:04:18 +02:00
SystemArchitecture = System . Environment . Is64BitOperatingSystem ? Architecture . X64 : Architecture . X86 ,
2014-11-24 00:10:41 +01:00
OperatingSystemVersionString = System . Environment . OSVersion . VersionString
2014-11-09 19:24:57 +01:00
} ;
}
}
2015-10-03 20:13:53 +02:00
public bool SupportsLibraryMonitor
{
get { return true ; }
}
2014-11-09 19:24:57 +01:00
public bool SupportsRunningAsService
{
get
{
return true ;
}
}
public bool IsRunningAsService
{
get ;
set ;
}
public bool CanSelfRestart
{
get
{
return MainStartup . CanSelfRestart ;
}
}
public bool SupportsAutoRunAtStartup
{
get
{
return true ;
}
}
public bool CanSelfUpdate
{
get
{
return MainStartup . CanSelfUpdate ;
}
}
public void Shutdown ( )
{
MainStartup . Shutdown ( ) ;
}
2015-06-05 16:27:01 +02:00
public void Restart ( StartupOptions startupOptions )
2014-11-09 19:24:57 +01:00
{
MainStartup . Restart ( ) ;
}
public void ConfigureAutoRun ( bool autorun )
{
2016-04-03 19:34:52 +02:00
var shortcutPath = Path . Combine ( System . Environment . GetFolderPath ( System . Environment . SpecialFolder . StartMenu ) , "Emby" , "Emby Server.lnk" ) ;
var startupPath = System . Environment . GetFolderPath ( System . Environment . SpecialFolder . Startup ) ;
if ( autorun )
{
//Copy our shortut into the startup folder for this user
var targetPath = Path . Combine ( startupPath , Path . GetFileName ( shortcutPath ) ? ? "Emby Server.lnk" ) ;
_fileSystem . CreateDirectory ( Path . GetDirectoryName ( targetPath ) ) ;
File . Copy ( shortcutPath , targetPath , true ) ;
}
else
{
//Remove our shortcut from the startup folder for this user
_fileSystem . DeleteFile ( Path . Combine ( startupPath , Path . GetFileName ( shortcutPath ) ? ? "Emby Server.lnk" ) ) ;
}
2014-11-09 19:24:57 +01:00
}
public INetworkManager CreateNetworkManager ( ILogger logger )
{
return new NetworkManager ( logger ) ;
}
public void PreventSystemStandby ( )
{
2016-04-23 20:38:36 +02:00
MainStartup . Invoke ( Standby . PreventSleep ) ;
}
public void AllowSystemStandby ( )
{
MainStartup . Invoke ( Standby . AllowSleep ) ;
2014-11-09 19:24:57 +01:00
}
2016-01-21 18:29:14 +01:00
public IPowerManagement GetPowerManagement ( )
{
2016-01-21 18:45:42 +01:00
return new WindowsPowerManagement ( _logger ) ;
2016-01-21 18:29:14 +01:00
}
2016-04-02 06:29:48 +02:00
public FFMpegInstallInfo GetFfmpegInstallInfo ( )
{
var info = new FFMpegInstallInfo ( ) ;
info . FFMpegFilename = "ffmpeg.exe" ;
info . FFProbeFilename = "ffprobe.exe" ;
2016-06-23 19:04:18 +02:00
info . Version = "0" ;
2016-04-02 06:29:48 +02:00
return info ;
}
2016-04-24 05:03:49 +02:00
public void LaunchUrl ( string url )
{
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = url
} ,
EnableRaisingEvents = true ,
} ;
process . Exited + = ProcessExited ;
try
{
process . Start ( ) ;
}
catch ( Exception ex )
{
_logger . ErrorException ( "Error launching url: {0}" , ex , url ) ;
throw ;
}
}
2016-05-01 23:48:37 +02:00
public IDbConnector GetDbConnector ( )
{
return new DbConnector ( _logger ) ;
}
2016-04-24 05:03:49 +02:00
/// <summary>
/// Processes the exited.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
private static void ProcessExited ( object sender , EventArgs e )
{
( ( Process ) sender ) . Dispose ( ) ;
}
2016-09-03 19:16:36 +02:00
public void EnableLoopback ( string appName )
{
LoopUtil . Run ( appName ) ;
}
2016-09-06 07:02:05 +02:00
2016-09-06 08:50:07 +02:00
private bool Confirm ( )
{
return MessageBox . Show ( "Emby has detected that Windows Firewall has been configured in a way that may prevent your other devices from accessing Emby Server. Click OK to remove this rule, or cancel to proceed anyway." , "Windows Firewall" , MessageBoxButtons . OKCancel ) = = DialogResult . OK ;
}
2016-09-06 07:02:05 +02:00
public bool PortsRequireAuthorization ( string applicationPath )
{
var appNameSrch = Path . GetFileName ( applicationPath ) ;
var startInfo = new ProcessStartInfo
{
FileName = "netsh" ,
Arguments = "advfirewall firewall show rule \"" + appNameSrch + "\"" ,
CreateNoWindow = true ,
UseShellExecute = false ,
WindowStyle = ProcessWindowStyle . Hidden ,
ErrorDialog = false ,
RedirectStandardOutput = true
} ;
using ( var process = Process . Start ( startInfo ) )
{
process . Start ( ) ;
try
{
var data = process . StandardOutput . ReadToEnd ( ) ? ? string . Empty ;
if ( data . IndexOf ( "Block" , StringComparison . OrdinalIgnoreCase ) ! = - 1 )
{
2016-09-06 19:59:10 +02:00
_logger . Info ( "Found windows firewall rule: " + data ) ;
2016-09-06 08:50:07 +02:00
return Confirm ( ) ;
2016-09-06 07:02:05 +02:00
}
//var parts = data.Split('\n');
//return parts.Length > 4;
2016-09-06 08:50:07 +02:00
//return Confirm();
2016-09-06 07:02:05 +02:00
return false ;
}
catch ( Exception ex )
{
_logger . ErrorException ( "Error querying windows firewall" , ex ) ;
// Hate having to do this
try
{
process . Kill ( ) ;
}
catch ( Exception ex1 )
{
_logger . ErrorException ( "Error killing process" , ex1 ) ;
}
throw ;
}
}
}
2014-11-09 19:24:57 +01:00
}
2016-06-17 15:21:34 +02:00
}