Advertisement
MrBukkitHackYT

Query

Jun 8th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.83 KB | None | 0 0
  1. public class Query : IDisposable
  2. {
  3. Socket qSocket;
  4. IPAddress address;
  5. int _port = 0;
  6. string[] results;
  7. int _count = 0;
  8. DateTime[] timestamp = new DateTime[2];
  9.  
  10. public string passworded;
  11. public string players;
  12. public string max_players;
  13. public string hostname;
  14. public string gamemode;
  15. public string mapname;
  16. public string ping;
  17.  
  18. public enum PaketOpcode
  19. {
  20. Info = 'i',
  21. Rules = 'r',
  22. ClientList = 'c',
  23. DetailedClientList = 'd',
  24. Ping = 'p'
  25. }
  26.  
  27. public Query(string addr, int port, bool dns)
  28. {
  29. qSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  30.  
  31. qSocket.SendTimeout = 5000;
  32. qSocket.ReceiveTimeout = 5000;
  33. if (dns)
  34. {
  35. try
  36. {
  37. address = Dns.GetHostAddresses(addr)[0];
  38. }
  39. catch (Exception e)
  40. {
  41. MessageBox.Show("An error has occured in SAMP API!\n\n" + e.ToString());
  42. Environment.Exit(2);
  43. }
  44. }
  45. else
  46. {
  47. try
  48. {
  49. address = IPAddress.Parse(addr);
  50. }
  51. catch
  52. {
  53. }
  54. }
  55. _port = port;
  56. }
  57.  
  58. public bool Send(char opcode, string sign = "1337")
  59. {
  60. try
  61. {
  62. EndPoint endpoint = new IPEndPoint(address, _port);
  63. using (MemoryStream stream = new MemoryStream())
  64. {
  65. using (BinaryWriter writer = new BinaryWriter(stream))
  66. {
  67. writer.Write("SAMP".ToCharArray());
  68. string[] SplitIP = address.ToString().Split('.');
  69. writer.Write(Convert.ToByte(SplitIP[0]));
  70. writer.Write(Convert.ToByte(SplitIP[1]));
  71. writer.Write(Convert.ToByte(SplitIP[2]));
  72. writer.Write(Convert.ToByte(SplitIP[3]));
  73. writer.Write((ushort)_port);
  74. writer.Write(opcode);
  75. if (opcode == 'p') writer.Write(sign.ToCharArray());
  76. timestamp[0] = DateTime.Now;
  77. }
  78. if (qSocket.SendTo(stream.ToArray(), endpoint) > 0) return true;
  79. }
  80. }
  81. catch
  82. {
  83. return false;
  84. }
  85. return false;
  86. }
  87.  
  88. public int Recieve()
  89. {
  90. try
  91. {
  92. _count = 0;
  93. EndPoint endpoint = new IPEndPoint(address, _port);
  94. byte[] rBuffer = new byte[500];
  95. qSocket.ReceiveFrom(rBuffer, ref endpoint);
  96. timestamp[1] = DateTime.Now;
  97. using (MemoryStream stream = new MemoryStream(rBuffer))
  98. {
  99. using (BinaryReader reader = new BinaryReader(stream))
  100. {
  101. if (stream.Length <= 10) return _count;
  102. reader.ReadBytes(10);
  103. switch (reader.ReadChar())
  104. {
  105. case 'i':
  106. {
  107. results = new string[6];
  108. passworded = reader.ReadByte().ToString();
  109. //results[_count++] = reader.ReadByte().ToString(); // either 0 or 1, depending whether if the password has been set.
  110. players = reader.ReadInt16().ToString();
  111. //results[_count++] = reader.ReadInt16().ToString(); // current amount of players online on the server
  112. max_players = reader.ReadInt16().ToString();
  113. //results[_count++] = reader.ReadInt16().ToString(); // maximum amount of players that can join the server
  114. hostname = new string(reader.ReadChars(reader.ReadInt32()));
  115. //results[_count++] = new string(reader.ReadChars(reader.ReadInt32())); // hostname
  116. gamemode = new string(reader.ReadChars(reader.ReadInt32()));
  117. //results[_count++] = new string(reader.ReadChars(reader.ReadInt32())); // gamemode
  118. mapname = new string(reader.ReadChars(reader.ReadInt32()));
  119. //results[_count++] = new string(reader.ReadChars(reader.ReadInt32())); // mapname
  120. return _count;
  121. }
  122.  
  123. case 'r':
  124. {
  125. int rulecount = reader.ReadInt16();
  126. results = new string[rulecount * 2];
  127. for (int i = 0; i < rulecount; i++)
  128. {
  129. results[_count++] = new string(reader.ReadChars(reader.ReadByte())); // rule name (key)
  130. results[_count++] = new string(reader.ReadChars(reader.ReadByte())); // rule value (value)
  131. }
  132. return _count;
  133. }
  134.  
  135. case 'c':
  136. {
  137. int playercount = reader.ReadInt16();
  138. results = new string[playercount * 2];
  139. for (int i = 0; i < playercount; i++)
  140. {
  141. results[_count++] = new string(reader.ReadChars(reader.ReadByte())); // nickname
  142. results[_count++] = reader.ReadInt32().ToString(); // score
  143. }
  144. return _count;
  145. }
  146.  
  147. case 'd':
  148. {
  149. int playercount = reader.ReadInt16();
  150. results = new string[playercount * 4];
  151. for (int i = 0; i < playercount; i++)
  152. {
  153. results[_count++] = reader.ReadByte().ToString(); //playerid
  154. results[_count++] = new string(reader.ReadChars(reader.ReadByte())); //nick
  155. results[_count++] = reader.ReadInt32().ToString(); //score
  156. results[_count++] = reader.ReadInt32().ToString(); //ping
  157. }
  158. return _count;
  159. }
  160.  
  161. case 'p':
  162. {
  163. results = new string[1];
  164. results[_count++] = timestamp[1].Subtract(timestamp[0]).Milliseconds.ToString(); // time difference
  165. ping = reader.ReadByte().ToString();
  166. //results[_count++] = new string(reader.ReadChars(4)); // paket signature
  167. return _count;
  168. }
  169.  
  170. default: return _count;
  171. }
  172. }
  173. }
  174. }
  175. catch
  176. {
  177. return _count;
  178. }
  179. }
  180.  
  181.  
  182. // Form1
  183.  
  184. public void ServerStatus()
  185. {
  186.  
  187. ip = ("186.82.2.234");
  188. password = ("sanadreas");
  189. rq = new SAMP(ip, 7777, password, false);
  190. q = new Query(ip, 7777, false);
  191. q.Send('i');
  192. q.Recieve();
  193. ReloadStatus.Start();
  194. bool started = string.IsNullOrEmpty(q.hostname.ToString());
  195. if (started)
  196. {
  197. HostName.Text = q.hostname.ToString();
  198. PlayerStatus.Text = q.players.ToString() + " / " + q.max_players.ToString();
  199. ModeStatus.Text = q.gamemode.ToString();
  200. LenguajeStatus.Text = q.mapname.ToString();
  201. }
  202. else
  203. {
  204. MessageBox.Show("server is currently down, contact administration!");
  205. }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement