Exemple #1
0
        public static RconPacket ParseData(byte[] data)
        {
            RconPacket rconPacket = new RconPacket();

            rconPacket.receivedDate = DateTime.Now;
            try
            {
                List <byte> datalist = new List <byte>(data);

                datalist.RemoveRange(0, 2); //Remove BE-header form packet.

                int crc = BitConverter.ToInt32(datalist.Take(4).ToArray(), 0);
                datalist.RemoveRange(0, 4); //Remove crc from packet.

                int correctCrc = (int)Crc32.Compute(datalist.ToArray());
                datalist.RemoveAt(0); //Remove last byte of header(0xff).

                rconPacket.type = (PacketType)datalist[0];
                datalist.RemoveAt(0);                           //Remove packet type.

                if (rconPacket.type == PacketType.Login_Packet) //If packet is type of login, then there should be only 1 byte (login state) left.
                {
                    rconPacket.data    = datalist.ToArray();
                    rconPacket.isValid = (crc == correctCrc);
                }

                else
                {
                    rconPacket.sequenceNumber = datalist[0];
                    datalist.RemoveAt(0);     //Remove sequence number.

                    if (datalist[0] == 0x00)  //is multipacket.
                    {
                        datalist.RemoveAt(0); //Remove multipacket header.
                        rconPacket.isMultipacket    = true;
                        rconPacket.multiPacketCount = datalist[0];
                        datalist.RemoveAt(0); //Remove multipacket count.
                        rconPacket.multiPacketIndex = datalist[0];
                        datalist.RemoveAt(0); //Remove multipacket index.
                        rconPacket.data    = datalist.ToArray();
                        rconPacket.isValid = (crc == correctCrc);
                    }

                    else
                    {
                        rconPacket.isValid = (crc == correctCrc);
                        rconPacket.data    = datalist.ToArray();
                    }
                }
            }
            catch
            {
                rconPacket.isValid = false;
            }

            return(rconPacket);
        }
Exemple #2
0
        private bool Authenticate(string password)
        {
            SendPacket(password, PacketType.Login_Packet);
            var        response = Client.Receive(ref RemoteEndpoint);
            RconPacket p        = Rcon.ParseData(response);

            if (p.type == PacketType.Login_Packet && p.isValid && p.data[0] == 1)
            {
                return(true);
            }
            return(false);
        }
Exemple #3
0
        private void UdpReceiveCallback(IAsyncResult ar)
        {
            byte[] received = null;
            try
            {
                received   = Client.EndReceive(ar, ref RemoteEndpoint);
                errorCount = 0;
            }
            catch
            {
                if (errorCount > 5)
                {
                    SetRconEnabled(false);
                }
                errorCount++;

                return;
            }

            RconPacket p = ParseData(received);

            if (p.isValid)
            {
                p.Debug();
                if (p.isMultipacket)
                {
                    Handler.PushMultiPacket(p);
                }
                else if (p.type == PacketType.ServerMessage_Packet)
                {
                    Handler.PushNormalPacket(p);
                    SendPacket("", PacketType.ServerMessage_Packet, p.sequenceNumber);
                }
                else if (p.type == PacketType.Command_Packet)
                {
                    Handler.PushNormalPacket(p);
                }
            }

            if (RconEnabled)
            {
                ReceiveUdp();
            }
        }
Exemple #4
0
 public void PushNormalPacket(RconPacket packet)
 {
     ReadyPackets.Add(packet);
 }
Exemple #5
0
 public void PushMultiPacket(RconPacket packet)
 {
     multiPackets.Add(packet);
 }