diff --git a/TGCommandLine/Program.cs b/TGCommandLine/Program.cs index 6c1e5f9aef..2f5967f724 100644 --- a/TGCommandLine/Program.cs +++ b/TGCommandLine/Program.cs @@ -113,11 +113,34 @@ namespace TGCommandLine return result; } + static bool AcceptedBadCert = false; + static bool BadCertificateInteractive(string message) + { + if (AcceptedBadCert) + return true; + Console.WriteLine(message); + Console.Write("Do you wish to continue? NOT RECCOMENDED! (y/N): "); + var result = Console.ReadLine().Trim().ToLower(); + AcceptedBadCert = result == "y" || result == "yes"; + return AcceptedBadCert; + } + static int Main(string[] args) { Command.OutputProcVar.Value = Console.WriteLine; if (args.Length != 0) + { + Server.SetBadCertificateHandler((message) => { + foreach (var I in args) + if (I.ToLower() == "--disable-ssl-verification") //im just not even going to document this because i hate it so much + return true; + return false; + }); + //allow self signed certs in debug mode return (int)RunCommandLine(new List(args)); + } + + Server.SetBadCertificateHandler(BadCertificateInteractive); Console.WriteLine("Type 'remote' to connect to a remote service"); //interactive mode @@ -149,12 +172,14 @@ namespace TGCommandLine if (res != null) { Console.WriteLine("Unable to connect: " + res); - Server.SetRemoteLoginInformation(null, 0, null, null); + Server.MakeLocalConnection(); + AcceptedBadCert = false; } else if (!Server.Authenticate()) { Console.WriteLine("Authentication error: Username/password/windows identity is not authorized! Returning to local mode..."); - Server.SetRemoteLoginInformation(null, 0, null, null); + Server.MakeLocalConnection(); + AcceptedBadCert = false; } else { @@ -163,7 +188,7 @@ namespace TGCommandLine } break; case "disconnect": - Server.SetRemoteLoginInformation(null, 0, null, null); + Server.MakeLocalConnection(); Console.WriteLine("Switch to local mode"); break; case "quit": diff --git a/TGControlPanel/Login.cs b/TGControlPanel/Login.cs index d37e5a6c8b..b6292425df 100644 --- a/TGControlPanel/Login.cs +++ b/TGControlPanel/Login.cs @@ -28,7 +28,7 @@ namespace TGControlPanel private void LocalLoginButton_Click(object sender, EventArgs e) { - Server.SetRemoteLoginInformation(null, 0, null, null); + Server.MakeLocalConnection(); VerifyAndConnect(); } diff --git a/TGControlPanel/Program.cs b/TGControlPanel/Program.cs index 55de52accf..280b4f0d54 100644 --- a/TGControlPanel/Program.cs +++ b/TGControlPanel/Program.cs @@ -8,8 +8,9 @@ namespace TGControlPanel static class Program { [STAThread] - static void Main(string [] args) + static void Main(string[] args) { + Server.SetBadCertificateHandler(BadCertificateHandler); try { if (Properties.Settings.Default.UpgradeRequired) @@ -31,6 +32,17 @@ namespace TGControlPanel Properties.Settings.Default.Save(); } } + static bool SSLErrorPromptResult = false; + static bool BadCertificateHandler(string message) + { + if (!SSLErrorPromptResult) + { + var result = MessageBox.Show(message + " IT IS HIGHLY RECCOMENDED YOU DO NOT PROCEED! Continue?", "SSL Error", MessageBoxButtons.YesNo) == DialogResult.Yes; + SSLErrorPromptResult = result; + return result; + } + return true; + } public static void ServiceDisconnectException(Exception e) { diff --git a/TGServiceInterface/Server.cs b/TGServiceInterface/Server.cs index 31360754ff..5b8b73ed8e 100644 --- a/TGServiceInterface/Server.cs +++ b/TGServiceInterface/Server.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Net; +using System.Net.Security; using System.ServiceModel; namespace TGServiceInterface { @@ -37,12 +38,41 @@ namespace TGServiceInterface /// static string HTTPSPassword; + + public static void SetBadCertificateHandler(Func handler) + { + ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, error) => + { + string ErrorMessage; + switch (error) + { + case SslPolicyErrors.None: + return true; + case SslPolicyErrors.RemoteCertificateChainErrors: + ErrorMessage = "There are certificate chain errors."; + break; + case SslPolicyErrors.RemoteCertificateNameMismatch: + ErrorMessage = "The certificate name does not match."; + break; + case SslPolicyErrors.RemoteCertificateNotAvailable: + ErrorMessage = "The certificate doesn't exist in the trust store."; + break; + default: + ErrorMessage = "An unknown error occurred."; + break; + } + ErrorMessage = String.Format("The certificate failed to verify for {0}:{1}. {2} {3}", HTTPSURL, HTTPSPort, ErrorMessage, cert.ToString()); + return handler(ErrorMessage); + }; + } + /// /// Set the interface to look for services on the current computer /// public static void MakeLocalConnection() { HTTPSURL = null; + HTTPSPassword = null; } /// @@ -92,10 +122,6 @@ namespace TGServiceInterface outChannel.Credentials.UserName.UserName = HTTPSUsername; outChannel.Credentials.UserName.Password = HTTPSPassword; } -#if DEBUG - //allow self signed certs in debug mode - ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, error) => true; -#endif return outChannel.CreateChannel(); }