fix #24, Unhandled exception after issuing passkey with special characters in prompt
This commit is contained in:
parent
8c11d227d8
commit
a0ae3ac369
1 changed files with 13 additions and 48 deletions
61
Utils.cs
61
Utils.cs
|
@ -1,4 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace SteamGuard
|
namespace SteamGuard
|
||||||
{
|
{
|
||||||
|
@ -6,55 +7,19 @@ namespace SteamGuard
|
||||||
{
|
{
|
||||||
public static string ReadLineSecure()
|
public static string ReadLineSecure()
|
||||||
{
|
{
|
||||||
string text = "";
|
// Have bash handle the password input, because apparently it's impossible in C#,
|
||||||
ConsoleKeyInfo key;
|
// and we don't need to worry about windows compatibility.
|
||||||
int cursorIndex = 0;
|
string bash_cmd = @"read -s -p ""Password: "" password; echo $password";
|
||||||
do
|
Process p = new Process();
|
||||||
{
|
p.StartInfo.UseShellExecute = false;
|
||||||
key = Console.ReadKey(true);
|
p.StartInfo.FileName = "bash";
|
||||||
if (((int)key.Key) >= 65 && ((int)key.Key <= 90))
|
p.StartInfo.Arguments = string.Format("-c '{0}'", bash_cmd);
|
||||||
{
|
p.StartInfo.RedirectStandardOutput = true;
|
||||||
if (cursorIndex == 0 || cursorIndex == text.Length - 1)
|
p.Start();
|
||||||
{
|
|
||||||
text += key.KeyChar.ToString();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
text.Insert(cursorIndex, key.KeyChar.ToString());
|
|
||||||
}
|
|
||||||
cursorIndex++;
|
|
||||||
}
|
|
||||||
else if (key.Key == ConsoleKey.Backspace && cursorIndex > 0)
|
|
||||||
{
|
|
||||||
text.Remove(cursorIndex - 1, 1);
|
|
||||||
cursorIndex--;
|
|
||||||
}
|
|
||||||
else if (key.Key == ConsoleKey.RightArrow)
|
|
||||||
{
|
|
||||||
cursorIndex++;
|
|
||||||
}
|
|
||||||
else if (key.Key == ConsoleKey.LeftArrow)
|
|
||||||
{
|
|
||||||
cursorIndex--;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cursorIndex < 0)
|
p.WaitForExit();
|
||||||
{
|
Console.WriteLine();
|
||||||
cursorIndex = 0;
|
return p.StandardOutput.ReadToEnd().Trim();
|
||||||
}
|
|
||||||
else if (text.Length == 0 && cursorIndex > text.Length)
|
|
||||||
{
|
|
||||||
cursorIndex = 0;
|
|
||||||
}
|
|
||||||
else if (text.Length != 0 && cursorIndex >= text.Length)
|
|
||||||
{
|
|
||||||
cursorIndex = text.Length - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// For debugging:
|
|
||||||
// Console.Title = string.Format("{0}/{1} - {2} ({3}) - {4}", cursorIndex, text.Length, key.Key.ToString(), (int)key.Key, text);
|
|
||||||
} while (key.Key != ConsoleKey.Enter);
|
|
||||||
return text;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue