Esempio n. 1
0
 internal static Rcon Authorize(ConnectionInfo conInfo, string msg)
 {
     return(new QueryMasterBase().Invoke <Rcon>(() =>
     {
         RconSource obj = new RconSource(conInfo);
         obj.socket = new TcpQuery(conInfo);
         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 == null ? new byte[1] : recvData);
             throw;
         }
         if (header != -1)
         {
             return obj;
         }
         return obj;
     }, conInfo.Retries + 1, null, conInfo.ThrowExceptions));
 }
Esempio n. 2
0
        private string sendCommand(string command, bool isMultipacketResponse)
        {
            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());
        }
Esempio n. 3
0
        internal async Task <RconSrcPacket> GetResponseAsync(RconSrcPacket senPacket)
        {
            if (_procTask == null)
            {
                throw new QueryMasterException($"{nameof(TcpQuery)} must be initialized before calling {nameof(GetResponseAsync)}");
            }
            if (_procTask.IsCompleted)
            {
                throw new QueryMasterException("Receive thread is in a failed state");
            }

            var tcs     = new TaskCompletionSource <RconSrcPacket>();
            var handler = new OnPacketEventHandler((data, exception) =>
            {
                if (data != null)
                {
                    var packet = RconUtil.ProcessPacket(data);
                    if (packet.Id == senPacket.Id)
                    {
                        tcs.SetResult(packet);
                    }
                }

                if (exception != null)
                {
                    tcs.SetException(exception);
                }
            });

            try
            {
                await _ss.WaitAsync();

                OnPacket += handler;
                try
                {
                    SendData(RconUtil.GetBytes(senPacket));
                }
                catch (Exception ex) { throw new QueryMasterException("Failed to send packet", ex); }

                if (await Task.WhenAny(tcs.Task, Task.Delay(_conInfo.ReceiveTimeout)) == tcs.Task)
                {
                    return(tcs.Task.Result);
                }
                else
                {
                    return(null);
                }
            }
            finally
            {
                OnPacket -= handler;
                _ss.Release();
            }
        }
Esempio n. 4
0
        private void _proc_callback()
        {
            try
            {
                var    buffer   = new List <byte>();
                byte[] recvData = new byte[BufferSize];
                int    size     = 0;
                while (true)
                {
                    int       count         = 0;
                    Exception lastException = null;
                    try
                    {
                        count = Receive(recvData);
                    } catch (Exception ex) { lastException = ex; }

                    if (lastException != null)
                    {
                        OnPacket?.Invoke(null, new QueryMasterException("Receive failed", lastException));
                        break;
                    }

                    if (count > 0)
                    {
                        buffer.AddRange(recvData.Take(count));
                        while (RconUtil.IsPacket(buffer.ToArray(), out size))
                        {
                            var packet = buffer.Take(size).ToArray();
                            OnPacket?.Invoke(packet, null);
                            buffer.RemoveRange(0, size);
                        }
                    }
                    else
                    {
                        OnPacket?.Invoke(null, new QueryMasterException("Receive failed because socket was shutdown by remote host"));
                        break;
                    }
                }
            }
            catch (Exception) { }
        }
Esempio n. 5
0
        private string sendCommand(string command, bool isMultipacketResponse)
        {
            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();
            bool          lastPacket  = false;
            bool          firstPacket = false;

            try
            {
                for (int i = 0; i < recvData.Count; i++)
                {
                    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
                    {
                        lastPacket  = (i == recvData.Count - 1);
                        firstPacket = i == 0;
                        str.Append(RconUtil.ProcessPacket(recvData[i], firstPacket, lastPacket).Body);
                    }
                }
            }
            catch (Exception e)
            {
                e.Data.Add("ReceivedData", recvData.SelectMany(x => x).ToArray());
                throw;
            }
            return(str.ToString());
        }
Esempio n. 6
0
        private bool Reconnect()
        {
            if (_socket != null)
            {
                try
                {
                    _socket.Dispose();
                    _socket = null;
                }
                catch { }
            }

            _socket = new TcpQuery(_connInfo);

            //attempt to authorize this conenction
            var packet = new RconSrcPacket()
            {
                Body = _password, Id = (int)PacketId.ExecCmd, Type = (int)PacketType.Auth
            };
            var buffer = _socket.GetResponse(RconUtil.GetBytes(packet));

            if (buffer == null || buffer.Length < 4)
            {
                return(false);
            }

            var header = BitConverter.ToInt32(buffer, 4);

            if (header == -1)
            {
                return(false);
            }

            _socket.Init();
            return(true);
        }