示例#1
0
        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
        }
示例#2
0
        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());
        }
示例#3
0
        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());
        }
示例#4
0
        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
        }