internal static Rcon Authorize(IPEndPoint address, string msg)
 {
     RconSource obj = new RconSource(address);
     byte[] recvData = new byte[50];
     RconSrcPacket packet = new RconSrcPacket() { Body = msg, Id = (int)PacketId.ExecCmd, Type = (int)PacketType.Auth };
     recvData = obj.socket.GetResponse(RconUtil.GetBytes(packet));
     int header;
     try
     {
         header = BitConverter.ToInt32(recvData, 4);
     }
     catch (Exception e)
     {
         e.Data.Add("ReceivedData", recvData);
         throw;
     }
     if (header != -1)
     {
         return obj;
     }
     else
     {
         throw new System.Exception();
     }
     obj.socket.Dispose();
     return obj;
 }
 public override string SendCommand(string command)
 {
     RconSrcPacket senPacket = new RconSrcPacket() { Body = command, Id = (int)PacketId.ExecCmd, Type = (int)PacketType.Exec };
     List<byte[]> recvData = socket.GetMultiPacketResponse(RconUtil.GetBytes(senPacket));
     StringBuilder str = new StringBuilder();
     try
     {
         for (int i = 0; i < recvData.Count; i++)
         {
             //consecutive rcon command replies start with an empty packet
             if (BitConverter.ToInt32(recvData[i], 4) == (int)PacketId.Empty)
                 continue;
             if (recvData[i].Length - BitConverter.ToInt32(recvData[i], 0) == 4)
             {
                 str.Append(RconUtil.ProcessPacket(recvData[i]).Body);
             }
             else
             {
                 str.Append(RconUtil.ProcessPacket(recvData[i]).Body + Util.BytesToString(recvData[++i].Take(recvData[i].Length - 2).ToArray()));
             }
         }
     }
     catch (Exception e)
     {
         e.Data.Add("ReceivedData", recvData.SelectMany(x => x).ToArray());
         //throw;
     }
     return str.ToString();
 }
Exemple #3
0
        public override string SendCommand(string command)
        {
            RconSrcPacket senPacket = new RconSrcPacket()
            {
                Body = command, Id = (int)PacketId.ExecCmd, Type = (int)PacketType.Exec
            };
            List <byte[]> recvData = socket.GetMultiPacketResponse(RconUtil.GetBytes(senPacket));
            StringBuilder str      = new StringBuilder();

            try
            {
                for (int i = 0; i < recvData.Count; i++)
                {
                    //consecutive rcon command replies start with an empty packet
                    if (BitConverter.ToInt32(recvData[i], 4) == (int)PacketId.Empty)
                    {
                        continue;
                    }
                    if (recvData[i].Length - BitConverter.ToInt32(recvData[i], 0) == 4)
                    {
                        str.Append(RconUtil.ProcessPacket(recvData[i]).Body);
                    }
                    else
                    {
                        str.Append(RconUtil.ProcessPacket(recvData[i]).Body + Util.BytesToString(recvData[++i].Take(recvData[i].Length - 2).ToArray()));
                    }
                }
            }
            catch (Exception e)
            {
                e.Data.Add("ReceivedData", recvData.SelectMany(x => x).ToArray());
                throw;
            }
            return(str.ToString());
        }
Exemple #4
0
        internal static Rcon Authorize(IPEndPoint address, string msg)
        {
            RconSource obj = new RconSource(address);

            byte[]        recvData = new byte[50];
            RconSrcPacket packet   = new RconSrcPacket()
            {
                Body = msg, Id = (int)PacketId.ExecCmd, Type = (int)PacketType.Auth
            };

            recvData = obj.socket.GetResponse(RconUtil.GetBytes(packet));
            int header;

            try
            {
                header = BitConverter.ToInt32(recvData, 4);
            }
            catch (Exception e)
            {
                e.Data.Add("ReceivedData", recvData);
                throw;
            }
            if (header != -1)
            {
                return(obj);
            }
            obj.socket.Dispose();
            return(obj);
        }
Exemple #5
0
        internal static RconSrcPacket ProcessPacket(byte[] data)
        {
            RconSrcPacket packet = new RconSrcPacket();

            try
            {
                Parser parser = new Parser(data);
                packet.Size = parser.ReadInt();
                packet.Id   = parser.ReadInt();
                packet.Type = parser.ReadInt();
                byte[] body = parser.GetUnParsedData();
                if (body.Length == 2)
                {
                    packet.Body = string.Empty;
                }
                else
                {
                    packet.Body = Util.BytesToString(body).TrimEnd('\0', ' ');
                }
            }
            catch (Exception e)
            {
                e.Data.Add("ReceivedData", data);
                throw;
            }
            return(packet);
        }
 internal static byte[] GetBytes(RconSrcPacket packet)
 {
     byte[] command = Util.StringToBytes(packet.Body);
     packet.Size = 10 + command.Length;
     List<byte> y = new List<byte>(packet.Size + 4);
     y.AddRange(BitConverter.GetBytes(packet.Size));
     y.AddRange(BitConverter.GetBytes(packet.Id));
     y.AddRange(BitConverter.GetBytes(packet.Type));
     y.AddRange(command);
     //part of string
     y.Add(0x00);
     //end terminater
     y.Add(0x00);
     return y.ToArray();
 }
Exemple #7
0
        internal static byte[] GetBytes(RconSrcPacket packet)
        {
            byte[] command = Util.StringToBytes(packet.Body);
            packet.Size = 10 + command.Length;
            List <byte> y = new List <byte>(packet.Size + 4);

            y.AddRange(BitConverter.GetBytes(packet.Size));
            y.AddRange(BitConverter.GetBytes(packet.Id));
            y.AddRange(BitConverter.GetBytes(packet.Type));
            y.AddRange(command);
            //part of string
            y.Add(0x00);
            //end terminater
            y.Add(0x00);
            return(y.ToArray());
        }
 internal static RconSrcPacket ProcessPacket(byte[] data)
 {
     RconSrcPacket packet = new RconSrcPacket();
     try
     {
         Parser parser = new Parser(data);
         packet.Size = parser.ReadInt();
         packet.Id = parser.ReadInt();
         packet.Type = parser.ReadInt();
         byte[] body = parser.GetUnParsedData();
         if (body.Length == 2)
             packet.Body = string.Empty;
         else
             packet.Body = Util.BytesToString(body, 0, body.Length - 3);
     }
     catch (Exception e)
     {
         e.Data.Add("ReceivedData", data);
         throw;
     }
     return packet;
 }