示例#1
0
文件: Program.cs 项目: creeperlv/LWMS
        static void Main(string[] args)
        {
            Console.InputEncoding  = Encoding.UTF8;
            Console.OutputEncoding = Encoding.UTF8;
            Console.WriteLine("Copyright (C) 2020-2021 Creeper Lv");
            Console.WriteLine("This software is licensed under the MIT License");
            var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            Console.WriteLine(path);
            var          Conf   = Path.Combine(path, "Settings.ini");
            SimpleConfig config = null;

            if (File.Exists(Conf))
            {
                config = SimpleConfig.LoadFromFile(Conf);
            }
            var conf = Path.Combine(Environment.CurrentDirectory, "Settings.ini");

            if (File.Exists(conf))
            {
                config = SimpleConfig.LoadFromFile(conf);
            }
            IPEndPoint endPoint;
            string     Address        = null;
            int        port           = -1;
            string     AcceptedPubKey = null;
            string     Username       = null;
            string     Password       = null;

            if (config is not null)
            {
                Address = config.GetValue("ServerAddress", null);
                {
                    _ = int.TryParse(config.GetValue("ServerPort", null), out port);
                }
                {
                    AcceptedPubKey = config.GetValue("ServerPubKey", null);
                }
                Username = config.GetValue("Username", null);
                Password = config.GetValue("Password", null);
            }
            if (Address is null)
            {
                Console.WriteLine("Please enter a target server:");
                Address = Console.ReadLine();
            }
            if (port is - 1)
            {
                Console.WriteLine("Please specify which port to connect:(22 by default)");

                if (int.TryParse(Console.ReadLine(), out port) == false)
                {
                    port = 22;
                }
            }

            endPoint = new IPEndPoint(Dns.GetHostAddresses(Address).First(), port);
            RSClient client = new RSClient(endPoint);

            client.RegisterOutput(new ConsoleOut());
            client.RegisterOnConnectionLost(() =>
            {
                Console.WriteLine("Connection Lost.");
            });
            var pubKey = client.Handshake00();

            Console.WriteLine("Received public key from server:");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(FingerPrint(pubKey));
            Console.ResetColor();
            Console.WriteLine("Please make sure if the key is from server.");
            bool isAccepted = false;

            if (AcceptedPubKey is not null)
            {
                Console.WriteLine("The key is automatically accpeted.");
                if (Convert.ToBase64String(pubKey) == AcceptedPubKey)
                {
                    isAccepted = true;
                }
            }
            else
            {
                Console.WriteLine("Enter \"Yes\" or \"Y\" to accept the key.");
                var r = Console.ReadLine().ToUpper();
                isAccepted = r == "YES" || r == "Y";
            }

            if (isAccepted == true)
            {
                Console.WriteLine("Please enter your user name:");
                Username = Console.ReadLine();
                Console.WriteLine("Please enter your password:"******"Logged in.");
                    while (true)
                    {
                        var cmd = Console.ReadLine();
                        if (cmd.ToUpper() == "EXIT" || cmd.ToUpper() == "/Q" || cmd.ToUpper() == "QUIT")
                        {
                            Environment.Exit(0);
                        }
                        else if (cmd.ToUpper() == "CLS" || cmd.ToUpper() == "CLEAR")
                        {
                            Console.Clear();
                        }
                        else
                        {
                            client.SendOut(cmd);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Rejected by the server.");
                }
            }