Пример #1
0
 public void OnSockMgrShutdownBegin(object sender, SockMgrShutdownBeginEventArgs e)
 {
     _sockController.RemoveSockMgr(e.Handler);
     // TODO: Dispose protocol stack
     _sockMgr.GetProtocolStack()?.Dispose();
     try
     {
         // print: [Shutdown] local -> remote
         if (e.Handler.GetSockBase().Role == SocketRole.Client)
         {
             Console.WriteLine(string.Format("[Shutdown] {0} -> {1}",
                                             e.Handler.GetSockBase().GetSocket().LocalEndPoint.ToString(),
                                             e.Handler.GetSockBase().GetSocket().RemoteEndPoint.ToString()));
         }
         if (e.Handler.GetSockBase().Role == SocketRole.Listener)
         {
             Console.WriteLine(string.Format("[Shutdown] {0} <- *",
                                             e.Handler.GetSockBase().GetSocket().LocalEndPoint.ToString()));
         }
         Console.Write("> ");
     }
     catch (ObjectDisposedException) { }
 }
Пример #2
0
        static void InterfaceAesConsole(SockMgr sockMgr)
        {
            byte[] key;
            string input;

            Protocol.AESProtocolState   state;
            List <Protocol.AESProtocol> aesProtocols = new List <Protocol.AESProtocol>();
            int i = 0;

            foreach (var proto in sockMgr.GetProtocolStack().GetState().MiddleProtocols)
            {
                if (proto.GetType() == typeof(Protocol.AESProtocol))
                {
                    aesProtocols.Add((Protocol.AESProtocol)proto);
                    Console.WriteLine($"{i}\t{proto.ToString()}");
                    ++i;
                }
            }
            if (aesProtocols.Count == 0)
            {
                return;
            }

            Console.WriteLine("[Interface-AES] Select one by index");
            Console.Write("> ");
            input = Console.ReadLine();
            int index;

            try
            {
                index = int.Parse(input);
            }
            catch (FormatException)
            {
                Console.WriteLine("[Error] Invalid Input");
                return;
            }

            try
            {
                state = aesProtocols[index].GetState();
            }
            catch (ArgumentOutOfRangeException)
            {
                Console.WriteLine("[Error] Index Out Of Range");
                return;
            }

            Console.WriteLine("[Interface-AES]");
            Console.WriteLine("1. Set Key");
            Console.WriteLine("2. Disable Key");
            Console.WriteLine("3. Exit");
            Console.Write("> ");
            input = Console.ReadLine();
            switch (input)
            {
            case "1":
                key = SetKeyConsole();
                if (key != null)
                {
                    state.Key     = key;
                    state.Enabled = true;
                    aesProtocols[index].SetState(state);
                }
                break;

            case "2":
                state.Enabled = false;
                aesProtocols[index].SetState(state);
                break;

            case "3":
                return;
            }
        }
Пример #3
0
        static void InterfaceMenu(SockMgr sockMgr)
        {
            bool isExit = false;

            while (!isExit && !sockMgr.IsShutdown)
            {
                Console.WriteLine(string.Format("[Interface Menu] {0} -> {1}",
                                                sockMgr.GetSockBase().GetSocket().LocalEndPoint.ToString(),
                                                sockMgr.GetSockBase().GetSocket().RemoteEndPoint.ToString()));
                Console.WriteLine("1. Send Text");
                Console.WriteLine("2. Close");
                Console.WriteLine("3. Is Host?");
                Console.WriteLine("4. Exit");
                Console.WriteLine("5. Config AES");
                Console.WriteLine("6. Send Small File");
                foreach (var proto in sockMgr.GetProtocolStack().GetState().MiddleProtocols)
                {
                    if (proto.GetType() == typeof(Protocol.AESProtocol) && ((Protocol.AESProtocol)proto).GetState().Enabled == false)
                    {
                        Console.WriteLine("[Warning] There exists an AES layer that is not enabled");
                    }
                }

                Console.Write("> ");
                string sel = Console.ReadLine();
                if (sockMgr.IsShutdown)
                {
                    break;
                }
                try
                {
                    switch (sel)
                    {
                    case "1":
                        SendTextConsole(sockMgr);
                        break;

                    case "2":
                        sockMgr.Shutdown();
                        break;

                    case "3":
                        Console.WriteLine(sockMgr.GetSockBase().IsHost.ToString());
                        break;

                    case "4":
                        isExit = true;
                        break;

                    case "5":
                        InterfaceAesConsole(sockMgr);
                        break;

                    case "6":
                        SendSmallFileConsole(sockMgr);
                        break;

                    default:
                        break;
                    }
                }
                catch (NullReferenceException) { }  // in case the remote has shutdown
            }
        }