Advertisement
MrBukkitHackYT

SAMPQUERY

Jun 8th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.55 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Windows.Forms;
  6.  
  7. namespace SAMPQ
  8. {
  9.     public class SAMP : IDisposable
  10.     {
  11.         Socket qSocket;
  12.         IPAddress address;
  13.         int _port = 0;
  14.         string _password = null;
  15.         string[] results = new string[50];
  16.         int _count = 0;
  17.  
  18.         public SAMP(string addr, int port, string password, bool dns)
  19.         {
  20.             qSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  21.             qSocket.SendTimeout = 5000;
  22.             qSocket.ReceiveTimeout = 5000;
  23.             if (dns)
  24.             {
  25.                 try
  26.                 {
  27.                     address = Dns.GetHostAddresses(addr)[0];
  28.                 }
  29.                 catch
  30.                 {
  31.                 }
  32.             }
  33.             else
  34.             {
  35.                 try
  36.                 {
  37.                     address = IPAddress.Parse(addr);
  38.                 }
  39.                 catch
  40.                 {
  41.                 }
  42.             }
  43.  
  44.             _port = port;
  45.             _password = password;
  46.         }
  47.  
  48.         public bool Send(string command)
  49.         {
  50.             try
  51.             {
  52.                 IPEndPoint endpoint = new IPEndPoint(address, _port);
  53.                 using (MemoryStream stream = new MemoryStream())
  54.                 {
  55.                     using (BinaryWriter writer = new BinaryWriter(stream))
  56.                     {
  57.                         writer.Write("SAMP".ToCharArray());
  58.                         string[] SplitIP = address.ToString().Split('.');
  59.                         writer.Write(Convert.ToByte(SplitIP[0]));
  60.                         writer.Write(Convert.ToByte(SplitIP[1]));
  61.                         writer.Write(Convert.ToByte(SplitIP[2]));
  62.                         writer.Write(Convert.ToByte(SplitIP[3]));
  63.                         writer.Write((ushort)_port);
  64.                         writer.Write('x');
  65.                         writer.Write((ushort)_password.Length);
  66.                         writer.Write(_password.ToCharArray());
  67.                         writer.Write((ushort)command.Length);
  68.                         writer.Write(command.ToCharArray());
  69.                     }
  70.                     if (qSocket.SendTo(stream.ToArray(), endpoint) > 0) return true;
  71.                 }
  72.             }
  73.             catch
  74.             {
  75.                 return false;
  76.             }
  77.             return false;
  78.         }
  79.  
  80.         public int Recieve()
  81.         {
  82.             try
  83.             {
  84.                 for (int i = 0; i < results.GetLength(0); i++) results.SetValue(null, i);
  85.                 _count = 0;
  86.                 EndPoint endpoint = new IPEndPoint(address, _port);
  87.                 byte[] rBuffer = new byte[500];
  88.                 int count = qSocket.ReceiveFrom(rBuffer, ref endpoint);
  89.                 using (MemoryStream stream = new MemoryStream(rBuffer))
  90.                 {
  91.                     using (BinaryReader reader = new BinaryReader(stream))
  92.                     {
  93.                         if (stream.Length <= 11) return _count;
  94.                         reader.ReadBytes(11);
  95.                         short len;
  96.                         try
  97.                         {
  98.                             while ((len = reader.ReadInt16()) != 0) results[_count++] = new string(reader.ReadChars((int)len));
  99.                         }
  100.                         catch
  101.                         {
  102.                             return _count;
  103.                         }
  104.                     }
  105.                 }
  106.             }
  107.             catch
  108.             {
  109.                 return _count;
  110.             }
  111.             return _count;
  112.         }
  113.  
  114.         public string[] Store(int count = -1)
  115.         {
  116.             string[] rString = new string[count != -1 ? count : _count];
  117.             for (int i = 0; (i < count || count == -1) && i < _count; i++) rString[i] = results[i];
  118.             _count = 0;
  119.             return rString;
  120.         }
  121.  
  122.         public void Dispose()
  123.         {
  124.             try
  125.             {
  126.                 qSocket.Dispose();
  127.             }
  128.             catch
  129.             {
  130.             }
  131.         }
  132.     }
  133.  
  134.     public class Query : IDisposable
  135.     {
  136.         Socket qSocket;
  137.         IPAddress address;
  138.         int _port = 0;
  139.         string[] results;
  140.         int _count = 0;
  141.         DateTime[] timestamp = new DateTime[2];
  142.  
  143.         public string passworded;
  144.         public string players;
  145.         public string max_players;
  146.         public string hostname;
  147.         public string gamemode;
  148.         public string mapname;
  149.         public string ping;
  150.  
  151.         public enum PaketOpcode
  152.         {
  153.             Info = 'i',
  154.             Rules = 'r',
  155.             ClientList = 'c',
  156.             DetailedClientList = 'd',
  157.             Ping = 'p'
  158.         }
  159.  
  160.         public Query(string addr, int port, bool dns)
  161.         {
  162.             qSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  163.  
  164.             qSocket.SendTimeout = 5000;
  165.             qSocket.ReceiveTimeout = 5000;
  166.             if (dns)
  167.             {
  168.                 try
  169.                 {
  170.                     address = Dns.GetHostAddresses(addr)[0];
  171.                 }
  172.                 catch (Exception e)
  173.                 {
  174.                     MessageBox.Show("An error has occured in SAMP API!\n\n" + e.ToString());
  175.                     Environment.Exit(2);
  176.                 }
  177.             }
  178.             else
  179.             {
  180.                 try
  181.                 {
  182.                     address = IPAddress.Parse(addr);
  183.                 }
  184.                 catch
  185.                 {
  186.                 }
  187.             }
  188.             _port = port;
  189.         }
  190.  
  191.         public bool Send(char opcode, string sign = "1337")
  192.         {
  193.             try
  194.             {
  195.                 EndPoint endpoint = new IPEndPoint(address, _port);
  196.                 using (MemoryStream stream = new MemoryStream())
  197.                 {
  198.                     using (BinaryWriter writer = new BinaryWriter(stream))
  199.                     {
  200.                         writer.Write("SAMP".ToCharArray());
  201.                         string[] SplitIP = address.ToString().Split('.');
  202.                         writer.Write(Convert.ToByte(SplitIP[0]));
  203.                         writer.Write(Convert.ToByte(SplitIP[1]));
  204.                         writer.Write(Convert.ToByte(SplitIP[2]));
  205.                         writer.Write(Convert.ToByte(SplitIP[3]));
  206.                         writer.Write((ushort)_port);
  207.                         writer.Write(opcode);
  208.                         if (opcode == 'p') writer.Write(sign.ToCharArray());
  209.                         timestamp[0] = DateTime.Now;
  210.                     }
  211.                     if (qSocket.SendTo(stream.ToArray(), endpoint) > 0) return true;
  212.                 }
  213.             }
  214.             catch
  215.             {
  216.                 return false;
  217.             }
  218.             return false;
  219.         }
  220.  
  221.         public int Recieve()
  222.         {
  223.             try
  224.             {
  225.                 _count = 0;
  226.                 EndPoint endpoint = new IPEndPoint(address, _port);
  227.                 byte[] rBuffer = new byte[500];
  228.                 qSocket.ReceiveFrom(rBuffer, ref endpoint);
  229.                 timestamp[1] = DateTime.Now;
  230.                 using (MemoryStream stream = new MemoryStream(rBuffer))
  231.                 {
  232.                     using (BinaryReader reader = new BinaryReader(stream))
  233.                     {
  234.                         if (stream.Length <= 10) return _count;
  235.                         reader.ReadBytes(10);
  236.                         switch (reader.ReadChar())
  237.                         {
  238.                             case 'i':
  239.                                 {
  240.                                     results = new string[6];
  241.                                     passworded = reader.ReadByte().ToString();
  242.                                     //results[_count++] = reader.ReadByte().ToString(); // either 0 or 1, depending whether if the password has been set.
  243.                                     players = reader.ReadInt16().ToString();
  244.                                     //results[_count++] = reader.ReadInt16().ToString(); // current amount of players online on the server
  245.                                     max_players = reader.ReadInt16().ToString();
  246.                                     //results[_count++] = reader.ReadInt16().ToString(); // maximum amount of players that can join the server
  247.                                     hostname = new string(reader.ReadChars(reader.ReadInt32()));
  248.                                     //results[_count++] = new string(reader.ReadChars(reader.ReadInt32())); // hostname
  249.                                     gamemode = new string(reader.ReadChars(reader.ReadInt32()));
  250.                                     //results[_count++] = new string(reader.ReadChars(reader.ReadInt32())); // gamemode
  251.                                     mapname = new string(reader.ReadChars(reader.ReadInt32()));
  252.                                     //results[_count++] = new string(reader.ReadChars(reader.ReadInt32())); // mapname
  253.                                     return _count;
  254.                                 }
  255.  
  256.                             case 'r':
  257.                                 {
  258.                                     int rulecount = reader.ReadInt16();
  259.                                     results = new string[rulecount * 2];
  260.                                     for (int i = 0; i < rulecount; i++)
  261.                                     {
  262.                                         results[_count++] = new string(reader.ReadChars(reader.ReadByte())); // rule name (key)
  263.                                         results[_count++] = new string(reader.ReadChars(reader.ReadByte())); // rule value (value)
  264.                                     }
  265.                                     return _count;
  266.                                 }
  267.  
  268.                             case 'c':
  269.                                 {
  270.                                     int playercount = reader.ReadInt16();
  271.                                     results = new string[playercount * 2];
  272.                                     for (int i = 0; i < playercount; i++)
  273.                                     {
  274.                                         results[_count++] = new string(reader.ReadChars(reader.ReadByte())); // nickname
  275.                                         results[_count++] = reader.ReadInt32().ToString(); // score
  276.                                     }
  277.                                     return _count;
  278.                                 }
  279.  
  280.                             case 'd':
  281.                                 {
  282.                                     int playercount = reader.ReadInt16();
  283.                                     results = new string[playercount * 4];
  284.                                     for (int i = 0; i < playercount; i++)
  285.                                     {
  286.                                         results[_count++] = reader.ReadByte().ToString(); //playerid
  287.                                         results[_count++] = new string(reader.ReadChars(reader.ReadByte())); //nick
  288.                                         results[_count++] = reader.ReadInt32().ToString(); //score
  289.                                         results[_count++] = reader.ReadInt32().ToString(); //ping
  290.                                     }
  291.                                     return _count;
  292.                                 }
  293.  
  294.                             case 'p':
  295.                                 {
  296.                                     results = new string[1];
  297.                                     results[_count++] = timestamp[1].Subtract(timestamp[0]).Milliseconds.ToString(); // time difference
  298.                                     ping = reader.ReadByte().ToString();
  299.                                     //results[_count++] = new string(reader.ReadChars(4)); // paket signature
  300.                                     return _count;
  301.                                 }
  302.  
  303.                             default: return _count;
  304.                         }
  305.                     }
  306.                 }
  307.             }
  308.             catch
  309.             {
  310.                 return _count;
  311.             }
  312.         }
  313.  
  314.         public string[] Store(int count)
  315.         {
  316.             string[] rString = new string[count];
  317.             for (int i = 0; i < count && i < _count; i++) rString[i] = results[i];
  318.             _count = 0;
  319.             return rString;
  320.         }
  321.  
  322.         public void Dispose()
  323.         {
  324.             try
  325.             {
  326.                 qSocket.Dispose();
  327.             }
  328.             catch
  329.             {
  330.             }
  331.         }
  332.     }
  333. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement