Exemple #1
0
        public async Task HandleHello(string username, bool supports_upgrade)
        {
            WritePacket(new HelloProtocolPacket(username, Environment.MachineName, supports_upgrade));

            ProtocolPacket packet = await ReadPacket(3000);

            if (packet.CommandId == ProtocolCommandId.Goodbye)
            {
                throw new EndOfStreamException(((GoodbyeProtocolPacket)packet).Message);
            }
            else if (packet.CommandId != ProtocolCommandId.ReKey)
            {
                throw new EndOfStreamException("Unknown packet response");
            }
            else
            {
                ReKeyProtocolPacket p = (ReKeyProtocolPacket)packet;
                SetXorKey(p.XorKey);
            }
        }
Exemple #2
0
        // Return false if the connection should be closed.
        static bool HandlePacket(IClientEntry client, IEnumerable <IClientEntry> other_clients, ProtocolPacket packet)
        {
            bool           result       = true;
            ProtocolPacket write_packet = null;

            switch (packet.CommandId)
            {
            case ProtocolCommandId.Hello:
            {
                HelloProtocolPacket hello = (HelloProtocolPacket)packet;
                Console.WriteLine("Hello Packet for User: {0} HostName: {1}", hello.UserName, hello.HostName);
                client.UserName = hello.UserName;
                client.HostName = hello.HostName;
                ReKeyProtocolPacket rekey = new ReKeyProtocolPacket();
                if (hello.SupportsSecurityUpgrade)
                {
                    Random r = new Random();
                    rekey.XorKey = (byte)r.Next(256);
                }
                result = client.WritePacket(rekey);
                client.SetXorKey(rekey.XorKey);

                write_packet = new MessageProtocolPacket(hello.UserName,
                                                         String.Format("I've just joined from {0}", hello.HostName));
            }
            break;

            case ProtocolCommandId.Message:
                write_packet = packet;
                break;

            case ProtocolCommandId.GetUserList:
                result = client.WritePacket(new UserListProtocolPacket(other_clients.
                                                                       Where(c => c.UserName != null && c.HostName != null).Select(c => new UserListEntry(c.UserName, c.HostName))));
                break;

            case ProtocolCommandId.Target:
            {
                TargetProtocolPacket target        = (TargetProtocolPacket)packet;
                IClientEntry         target_client = other_clients.Where(c => c.UserName.Equals(target.UserName)).FirstOrDefault();
                if (target_client != null)
                {
                    result = target_client.WritePacket(target.Packet);
                }
            }
            break;

            case ProtocolCommandId.Goodbye:
                client.WritePacket(new GoodbyeProtocolPacket("Don't let the door hit you on the way out!"));
                if (!String.IsNullOrEmpty(client.UserName))
                {
                    GoodbyeProtocolPacket goodbye = (GoodbyeProtocolPacket)packet;
                    write_packet = new MessageProtocolPacket("Server", String.Format("'{0}' has quit, they said '{1}'", client.UserName, goodbye.Message));
                }
                result = false;
                break;

            case ProtocolCommandId.Ping:
                break;
            }

            if (write_packet != null)
            {
                foreach (IClientEntry entry in other_clients)
                {
                    entry.WritePacket(write_packet);
                }
            }

            return(result);
        }