internal static void HandleClient(NetStream stream, IPEndPoint ip) { try { var ssl = new SslStream(stream); ssl.AuthenticateAsServer(Certificate); stream = ssl; } catch (Exception e) { stream.Write("ERR_SSL\r\n"); stream.Write(e.ToString()); return; } stream.Encoding = Encoding.ASCII; var encdodingstring = stream.Read(); int codepage; if (int.TryParse(encdodingstring, out codepage)) { try { var cpenc = Encoding.GetEncoding(codepage); stream.Encoding = cpenc; stream.Write("success"); } catch { stream.Write("ERR_ENCODING_NOT_FOUND"); stream.Close(); return; } } else { try { var encoding = Encoding.GetEncoding(encdodingstring); stream.Encoding = encoding; stream.Write("success"); } catch { stream.Write("ERR_ENCODING_NOT_FOUND"); stream.Close(); return; } } try { var cred = Authenticate(stream); if (!cred.Successful) { stream.Close(); return; } stream.Write("LOGIN_SUCCESS"); ClientLoop.BeginClient(new Client(stream, cred.Username, cred.SecurityLevel, ip)); } catch (Exception ex) { stream.Write("ERR\r\n"); stream.Write(ex.ToString()); } }
private static void DoWork() { #region Initial Formatting // ReSharper disable ImplicitlyCapturedClosure // ReSharper disable AccessToModifiedClosure // ReSharper disable UnusedAnonymousMethodSignature try { Console.BufferWidth = 120; Console.WindowWidth = 120; Console.WindowHeight = 40; } catch { Console.WriteLine("Can't change width D:"); } #endregion #region Connect gethost: Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("Host: "); Console.ForegroundColor = ConsoleColor.White; var host = Console.ReadLine(); getport: Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("Port: "); Console.ForegroundColor = ConsoleColor.White; var portstr = Console.ReadLine(); ushort port; if (!ushort.TryParse(portstr, out port)) { Console.WriteLine("That is not a valid port."); goto getport; } var tc = false; connect: NetStream stream; try { stream = SimpleStream.ConnectToHost(host, port); } catch { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Could not connect to host."); goto gethost; } var ssl = new SslStream(stream); try { ssl.AuthenticateAsClient(host, (sender, certificate, chain, errors) => { if (errors != SslPolicyErrors.None) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("There are problems with security certificate"); Console.ForegroundColor = ConsoleColor.Yellow; if (errors == SslPolicyErrors.RemoteCertificateNameMismatch) { Console.WriteLine("The name on the certificate (" + GetCN(certificate.Subject) + ") does not match the hostname entered (" + host + ")"); } else { Console.WriteLine(errors); } doproceed: Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("Proceed? [y/n] "); Console.ForegroundColor = ConsoleColor.White; var key = Console.ReadKey(); Console.WriteLine(); switch (key.Key) { case ConsoleKey.Y: return(true); case ConsoleKey.N: return(false); default: Console.WriteLine("Please enter Y or N"); goto doproceed; } } return(true); }); } catch (AuthenticationException e) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Could not authenticate. {0}", e.Message); return; } stream = ssl; // ReSharper disable HeuristicUnreachableCode // ReSharper disable ConditionIsAlwaysTrueOrFalse if (!tc) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("-----SSL Security info-----"); Console.ForegroundColor = ConsoleColor.Yellow; DisplaySecurityLevel((System.Net.Security.SslStream)stream.UnderlyingStream); DisplaySecurityServices((System.Net.Security.SslStream)stream.UnderlyingStream); } // ReSharper restore HeuristicUnreachableCode // ReSharper restore ConditionIsAlwaysTrueOrFalse tc = true; stream.Encoding = Encoding.ASCII; stream.Write("utf-8"); stream.Encoding = Encoding.UTF8; stream.Read(); bool[] mancon = { true }; #endregion #region Enter User Info var streamclosemre = new ManualResetEvent(false); Thread readthread; Thread writethread = null; Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("User: "******"Pass: "******"ERR")) { if (cred_result == "ERR_AUTH_INVALID_CREDENTIALS") { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Incorrect username or password."); goto connect; } if (cred_result == "ERR_ACCOUNT_NOT_AUTHORIZED") { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("That account is not authorized to connect."); goto connect; } goto cleanup; } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Login successful!"); Console.ForegroundColor = ConsoleColor.Gray; #endregion #region Loop (readthread = new Thread( delegate() { while (mancon[0] && stream.Connected) { var msg = stream.Read(); if (msg == null) { mancon[0] = false; streamclosemre.Set(); if (writethread != null && writethread.IsAlive) { writethread.Abort(); } return; } if (msg.StartsWith("$")) { msg = msg.Substring(1); } WriteAnsiString(msg); } })).Start(); (writethread = new Thread( delegate() { Console.TreatControlCAsInput = false; Console.CancelKeyPress += (sender, args) => { mancon[0] = false; streamclosemre.Set(); if (readthread != null && readthread.IsAlive) { readthread.Abort(); } writethread.Abort(); args.Cancel = true; }; while (stream.Connected && mancon[0]) { var m2S = Console.ReadLine(); if (string.IsNullOrWhiteSpace(m2S)) { continue; } if (stream.Write(m2S)) { continue; } mancon[0] = false; streamclosemre.Set(); if (readthread != null && readthread.IsAlive) { readthread.Abort(); } } })).Start(); // ReSharper restore ImplicitlyCapturedClosure // ReSharper restore AccessToModifiedClosure // ReSharper restore UnusedAnonymousMethodSignature #endregion #region Cleanup streamclosemre.WaitOne(); cleanup: Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("\r\nCONNECTION LOST\r\n"); #endregion }
private void ButtonClick1(object sender, RoutedEventArgs e) { var hsplit = hostbox.Text.Split(':'); if (hsplit.Length < 2) { MessageBox.Show("Please specify the host in the form of address:port", "Bukkit Service Client", MessageBoxButton.OK, MessageBoxImage.Error); return; } int port; if (!int.TryParse(hsplit[1], out port)) { MessageBox.Show("Port must be a number", "Bukkit Service Client", MessageBoxButton.OK, MessageBoxImage.Error); return; } var tcp = new TcpClient(hsplit[0], port); var ssl = new SslStream(tcp); ssl.AuthenticateAsClient (hsplit[0], delegate(object o, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { if (errors == SslPolicyErrors.None) { return(true); } if (errors == SslPolicyErrors.RemoteCertificateNameMismatch) { return(MessageBox.Show("The name on the remote certificate (" + GetCN(certificate.Subject) + ") does not match the host entered (" + hsplit[0] + ")\r\nWould you like to connect anyways?", "Bukkit Service Client", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.Yes); } return(MessageBox.Show("An unknown error has occured validating the certificate " + "(Does your machine not recognize the root CA?)\r\n" + "Would you like to proceed anyways?", "Bukkit Service Client", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.Yes); }); if (!ssl.Write(userbox.Text + ":" + passbox.Password)) { LostConnection(); return; } var cred_result = ssl.Read(); if (cred_result == null) { LostConnection(); return; } if (cred_result.StartsWith("ERR")) { ssl.Close(); if (cred_result == "ERR_AUTH_INVALID_CREDENTIALS") { MessageBox.Show("Incorrect username or password"); return; } if (cred_result == "ERR_ACCOUNT_NOT_AUTHORIZED") { MessageBox.Show("This account is not authorized to connect"); return; } MessageBox.Show("An unknown error has occured during authentication"); return; } if (cred_result != "LOGIN_SUCCESS") { MessageBox.Show("An unknown error has occured during authentication"); return; } Debug.Assert(NavigationService != null, "NavigationService != null"); NavigationService.Navigate(new ConsolePage()); }
private void ButtonClick1(object sender, RoutedEventArgs e) { var hsplit = hostbox.Text.Split(':'); if (hsplit.Length < 2) { MessageBox.Show("Please specify the host in the form of address:port", "Bukkit Service Client", MessageBoxButton.OK, MessageBoxImage.Error); return; } int port; if (!int.TryParse(hsplit[1], out port)) { MessageBox.Show("Port must be a number", "Bukkit Service Client", MessageBoxButton.OK, MessageBoxImage.Error); return; } var tcp = new TcpClient(hsplit[0], port); var ssl = new SslStream(tcp); ssl.AuthenticateAsClient (hsplit[0], delegate(object o, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { if (errors == SslPolicyErrors.None) { return true; } if (errors == SslPolicyErrors.RemoteCertificateNameMismatch) { return MessageBox.Show("The name on the remote certificate (" + GetCN(certificate.Subject) + ") does not match the host entered (" + hsplit[0] + ")\r\nWould you like to connect anyways?", "Bukkit Service Client", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.Yes; } return MessageBox.Show("An unknown error has occured validating the certificate " + "(Does your machine not recognize the root CA?)\r\n" + "Would you like to proceed anyways?", "Bukkit Service Client", MessageBoxButton.YesNo, MessageBoxImage.Exclamation) == MessageBoxResult.Yes; }); if (!ssl.Write(userbox.Text + ":" + passbox.Password)) { LostConnection(); return; } var cred_result = ssl.Read(); if (cred_result == null) { LostConnection(); return; } if (cred_result.StartsWith("ERR")) { ssl.Close(); if (cred_result == "ERR_AUTH_INVALID_CREDENTIALS") { MessageBox.Show("Incorrect username or password"); return; } if (cred_result == "ERR_ACCOUNT_NOT_AUTHORIZED") { MessageBox.Show("This account is not authorized to connect"); return; } MessageBox.Show("An unknown error has occured during authentication"); return; } if (cred_result != "LOGIN_SUCCESS") { MessageBox.Show("An unknown error has occured during authentication"); return; } Debug.Assert(NavigationService != null, "NavigationService != null"); NavigationService.Navigate(new ConsolePage()); }
private async void ConnectClick(object sender, EventArgs e) { hostbox.Enabled = false; userbox.Enabled = false; passbox.Enabled = false; var hsplit = hostbox.Text.Split(':'); var host = hsplit[0]; if (string.IsNullOrWhiteSpace(host)) { Invoke(new Action(() => MessageBox.Show(this, "Please enter a host"))); hostbox.Enabled = true; userbox.Enabled = true; passbox.Enabled = true; return; } var port = 3000; if (hsplit.Length > 1) { if (!int.TryParse(hsplit[1], out port)) { Invoke(new Action(() => MessageBox.Show(this, "Port must be a number"))); hostbox.Enabled = true; userbox.Enabled = true; passbox.Enabled = true; return; } } SslStream ssl; try { var tcp = new TcpClient(); await tcp.ConnectAsync(host, port); ssl = new SslStream(tcp); } catch { Invoke(new Action(() => MessageBox.Show(this, "Error connecting"))); hostbox.Enabled = true; userbox.Enabled = true; passbox.Enabled = true; return; } try { await ssl.AuthenticateAsClientAsync(host, ValidationCallback); } catch { Invoke(new Action(() => MessageBox.Show(this, "Error securing connection"))); hostbox.Enabled = true; userbox.Enabled = true; passbox.Enabled = true; return; } Stream = ssl; Stream.Encoding = Encoding.UTF8; Stream.Write("utf-8"); Stream.Read(); Stream.Write(userbox.Text + ":" + passbox.Text); var res = Stream.Read(); if (res.StartsWith("ERR")) { if (res == "ERR_AUTH_INVALID_CREDENTIALS") { Invoke(new Action(() => MessageBox.Show(this, "Bad username or password"))); } else if (res == "ERR_ACCOUNT_NOT_AUTHORIZED") { Invoke(new Action(() => MessageBox.Show(this, "Account not authorized"))); } else { Invoke(new Action(() => MessageBox.Show(this, "An unkown error occured during authentication"))); } hostbox.Enabled = true; userbox.Enabled = true; passbox.Enabled = true; return; } Properties.Settings.Default.hostname = hostbox.Text; Properties.Settings.Default.username = userbox.Text; #pragma warning disable 665 if (Properties.Settings.Default.remeberpass = rempass.Checked) { #pragma warning restore 665 Properties.Settings.Default.password = passbox.Text; } else { Properties.Settings.Default.password = ""; } Properties.Settings.Default.Save(); Hide(); new ConsoleForm().ShowDialog(); Close(); }
private static void DoWork() { #region Initial Formatting // ReSharper disable ImplicitlyCapturedClosure // ReSharper disable AccessToModifiedClosure // ReSharper disable UnusedAnonymousMethodSignature try { Console.BufferWidth = 120; Console.WindowWidth = 120; Console.WindowHeight = 40; } catch { Console.WriteLine("Can't change width D:"); } #endregion #region Connect gethost: Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("Host: "); Console.ForegroundColor = ConsoleColor.White; var host = Console.ReadLine(); getport: Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("Port: "); Console.ForegroundColor = ConsoleColor.White; var portstr = Console.ReadLine(); ushort port; if (!ushort.TryParse(portstr, out port)) { Console.WriteLine("That is not a valid port."); goto getport; } var tc = false; connect: NetStream stream; try { stream = SimpleStream.ConnectToHost(host, port); } catch { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Could not connect to host."); goto gethost; } var ssl = new SslStream(stream); try { ssl.AuthenticateAsClient(host, (sender, certificate, chain, errors) => { if (errors != SslPolicyErrors.None) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("There are problems with security certificate"); Console.ForegroundColor = ConsoleColor.Yellow; if (errors == SslPolicyErrors.RemoteCertificateNameMismatch) { Console.WriteLine("The name on the certificate (" + GetCN(certificate.Subject) + ") does not match the hostname entered (" + host + ")"); } else { Console.WriteLine(errors); } doproceed: Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("Proceed? [y/n] "); Console.ForegroundColor = ConsoleColor.White; var key = Console.ReadKey(); Console.WriteLine(); switch (key.Key) { case ConsoleKey.Y: return true; case ConsoleKey.N: return false; default: Console.WriteLine("Please enter Y or N"); goto doproceed; } } return true; }); } catch (AuthenticationException e) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Could not authenticate. {0}", e.Message); return; } stream = ssl; // ReSharper disable HeuristicUnreachableCode // ReSharper disable ConditionIsAlwaysTrueOrFalse if (!tc) { Console.ForegroundColor = ConsoleColor.DarkGreen; Console.WriteLine("-----SSL Security info-----"); Console.ForegroundColor = ConsoleColor.Yellow; DisplaySecurityLevel((System.Net.Security.SslStream)stream.UnderlyingStream); DisplaySecurityServices((System.Net.Security.SslStream)stream.UnderlyingStream); } // ReSharper restore HeuristicUnreachableCode // ReSharper restore ConditionIsAlwaysTrueOrFalse tc = true; stream.Encoding = Encoding.ASCII; stream.Write("utf-8"); stream.Encoding = Encoding.UTF8; stream.Read(); bool[] mancon = { true }; #endregion #region Enter User Info var streamclosemre = new ManualResetEvent(false); Thread readthread; Thread writethread = null; Console.ForegroundColor = ConsoleColor.Cyan; Console.Write("User: "******"Pass: "******"ERR")) { if (cred_result == "ERR_AUTH_INVALID_CREDENTIALS") { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Incorrect username or password."); goto connect; } if (cred_result == "ERR_ACCOUNT_NOT_AUTHORIZED") { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("That account is not authorized to connect."); goto connect; } goto cleanup; } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Login successful!"); Console.ForegroundColor = ConsoleColor.Gray; #endregion #region Loop (readthread = new Thread( delegate() { while (mancon[0] && stream.Connected) { var msg = stream.Read(); if (msg == null) { mancon[0] = false; streamclosemre.Set(); if (writethread != null && writethread.IsAlive) { writethread.Abort(); } return; } if (msg.StartsWith("$")) msg = msg.Substring(1); WriteAnsiString(msg); } })).Start(); (writethread = new Thread( delegate() { Console.TreatControlCAsInput = false; Console.CancelKeyPress += (sender, args) => { mancon[0] = false; streamclosemre.Set(); if (readthread != null && readthread.IsAlive) { readthread.Abort(); } writethread.Abort(); args.Cancel = true; }; while (stream.Connected && mancon[0]) { var m2S = Console.ReadLine(); if (string.IsNullOrWhiteSpace(m2S)) continue; if (stream.Write(m2S)) continue; mancon[0] = false; streamclosemre.Set(); if (readthread != null && readthread.IsAlive) { readthread.Abort(); } } })).Start(); // ReSharper restore ImplicitlyCapturedClosure // ReSharper restore AccessToModifiedClosure // ReSharper restore UnusedAnonymousMethodSignature #endregion #region Cleanup streamclosemre.WaitOne(); cleanup: Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("\r\nCONNECTION LOST\r\n"); #endregion }