2015-07-19 13:22:50 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-10-29 10:19:09 +00:00
|
|
|
|
using Jackett.Services.Interfaces;
|
2015-07-19 13:22:50 +00:00
|
|
|
|
|
|
|
|
|
namespace Jackett.Services
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class ProcessService : IProcessService
|
|
|
|
|
{
|
|
|
|
|
private Logger logger;
|
|
|
|
|
|
|
|
|
|
public ProcessService(Logger l)
|
|
|
|
|
{
|
|
|
|
|
logger = l;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-22 22:00:52 +00:00
|
|
|
|
private void Run(string exe, string args, bool asAdmin, DataReceivedEventHandler d, DataReceivedEventHandler r)
|
2015-07-19 13:22:50 +00:00
|
|
|
|
{
|
|
|
|
|
var startInfo = new ProcessStartInfo()
|
|
|
|
|
{
|
|
|
|
|
CreateNoWindow = true,
|
|
|
|
|
UseShellExecute = false,
|
|
|
|
|
FileName = exe,
|
|
|
|
|
Arguments = args,
|
|
|
|
|
RedirectStandardError = true,
|
|
|
|
|
RedirectStandardOutput = true,
|
|
|
|
|
RedirectStandardInput = true
|
|
|
|
|
};
|
|
|
|
|
|
2015-07-22 22:00:52 +00:00
|
|
|
|
if (asAdmin)
|
|
|
|
|
{
|
|
|
|
|
startInfo.Verb = "runas";
|
|
|
|
|
startInfo.UseShellExecute = true;
|
|
|
|
|
startInfo.RedirectStandardError = false;
|
|
|
|
|
startInfo.RedirectStandardOutput = false;
|
|
|
|
|
startInfo.RedirectStandardInput = false;
|
|
|
|
|
}
|
2015-07-24 21:38:31 +00:00
|
|
|
|
logger.Debug("Running " + startInfo.FileName + " " + startInfo.Arguments);
|
2015-07-19 13:22:50 +00:00
|
|
|
|
var proc = Process.Start(startInfo);
|
2015-07-22 22:00:52 +00:00
|
|
|
|
|
|
|
|
|
if (!asAdmin)
|
|
|
|
|
{
|
|
|
|
|
proc.OutputDataReceived += d;
|
|
|
|
|
proc.ErrorDataReceived += r;
|
|
|
|
|
proc.BeginErrorReadLine();
|
|
|
|
|
proc.BeginOutputReadLine();
|
|
|
|
|
}
|
2015-07-19 13:22:50 +00:00
|
|
|
|
proc.WaitForExit();
|
2015-07-22 22:00:52 +00:00
|
|
|
|
if (!asAdmin)
|
|
|
|
|
{
|
|
|
|
|
proc.OutputDataReceived -= d;
|
|
|
|
|
proc.ErrorDataReceived -= r;
|
|
|
|
|
}
|
2015-07-19 13:22:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-22 22:00:52 +00:00
|
|
|
|
public string StartProcessAndGetOutput(string exe, string args, bool keepnewlines = false, bool asAdmin = false)
|
2015-07-19 13:22:50 +00:00
|
|
|
|
{
|
2015-07-22 22:00:52 +00:00
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
DataReceivedEventHandler rxData = (a, e) => {
|
|
|
|
|
if (keepnewlines || !string.IsNullOrWhiteSpace(e.Data))
|
|
|
|
|
{
|
|
|
|
|
sb.AppendLine(e.Data);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
DataReceivedEventHandler rxError = (s, e) => {
|
|
|
|
|
if (keepnewlines || !string.IsNullOrWhiteSpace(e.Data))
|
|
|
|
|
{
|
|
|
|
|
sb.AppendLine(e.Data);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Run(exe, args, asAdmin, rxData, rxError);
|
|
|
|
|
return sb.ToString();
|
2015-07-19 13:22:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-22 22:00:52 +00:00
|
|
|
|
public void StartProcessAndLog(string exe, string args, bool asAdmin = false)
|
2015-07-19 13:22:50 +00:00
|
|
|
|
{
|
2015-07-22 22:00:52 +00:00
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
DataReceivedEventHandler rxData = (a, e) =>
|
2015-07-19 13:22:50 +00:00
|
|
|
|
{
|
2015-07-22 22:00:52 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(e.Data))
|
|
|
|
|
{
|
|
|
|
|
logger.Debug(e.Data);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
DataReceivedEventHandler rxError = (s, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(e.Data))
|
|
|
|
|
{
|
|
|
|
|
logger.Error(e.Data);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Run(exe, args, asAdmin, rxData, rxError);
|
2015-07-19 13:22:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|