Deathmax

Untitled

Sep 19th, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using System.Threading;
  13.  
  14. namespace RconTool2
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         public string ipaddress;
  24.         public string password;
  25.         public byte[] rconbyte = { 0xFF, 0xFF, 0xFF, 0XFF, 0X72, 0X63, 0X6F, 0X6E }; //sends 4 oob bytes + rcon
  26.  
  27.         private void Form1_Load(object sender, EventArgs e)
  28.         {
  29.             string[] settings = File.ReadAllLines("rcon.ini");
  30.             foreach (string setting in settings)
  31.             {
  32.                 if (setting.StartsWith("Server="))
  33.                     ipaddress = setting.Substring(7);
  34.                 else if (setting.StartsWith("Password="))
  35.                     password = setting.Substring(9);
  36.             }
  37.         }
  38.  
  39.         private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
  40.         {
  41.             if (e.KeyChar == (char)Keys.Enter)
  42.             {
  43.                 e.Handled = true;
  44.                 SendInput(txtInput.Text);
  45.                 if (txtInput.Text.StartsWith("map mp"))
  46.                 {
  47.                     SendInput("status");
  48.                 }
  49.                 txtInput.Text = "";
  50.             }
  51.         }
  52.  
  53.         public void SendInput(string input)
  54.         {
  55.             txtOutput.Text += "]" + input + Environment.NewLine;
  56.             sendPacket(input);
  57.         }
  58.  
  59.         public void sendPacket(string commandstring)
  60.         {
  61.             try
  62.             {
  63.                 UdpClient udpClient = new UdpClient();
  64.                 int port = Int32.Parse(ipaddress.Split(':')[1]);
  65.                 string ip = ipaddress.Split(':')[0];
  66.                 try
  67.                 {
  68.                     udpClient.Connect(ip, port);
  69.                     var command = StrToByteArray(" " + password + " " + commandstring);
  70.                     var query = new byte[rconbyte.Length + command.Length];
  71.                     Array.Copy(rconbyte, 0, query, 0, rconbyte.Length);
  72.                     Array.Copy(command, 0, query, rconbyte.Length, command.Length);
  73.                     udpClient.Send(query, query.Length);
  74.                     IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
  75.                     udpClient.Client.ReceiveTimeout = 600;
  76.                     try
  77.                     {
  78.                         Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
  79.                         var returnData = Encoding.ASCII.GetString(receiveBytes);
  80.                         displayBytes(returnData);
  81.                         udpClient.Close();
  82.                     }
  83.                     catch (Exception e)
  84.                     {
  85.                         udpClient.Close();
  86.                         displayBytes(e.ToString());
  87.                     }
  88.                 }
  89.                 catch (Exception e)
  90.                 {
  91.                     udpClient.Close();
  92.                     displayBytes(e.ToString());
  93.                 }
  94.             }
  95.             catch
  96.             {
  97.             }
  98.         }
  99.  
  100.         public void displayBytes(string data)
  101.         {
  102.             var strData = data.Substring(4).Split('\n');
  103.             if (strData[0].StartsWith("print"))
  104.             {
  105.                 for (int i = 1; i < strData.Length; i++)
  106.                 {
  107.                     txtOutput.Text += removeQuakeColorCodes(strData[i]) + Environment.NewLine;
  108.                 }
  109.             }
  110.             else
  111.             {
  112.                 for (int i = 0; i < strData.Length; i++)
  113.                 {
  114.                     txtOutput.Text += strData[i] + Environment.NewLine;
  115.                 }
  116.             }
  117.         }
  118.  
  119.         public string removeQuakeColorCodes(string remove)
  120.         {
  121.             string filteredout = "";
  122.             var array = remove.Split('^');
  123.             filteredout += array[0];
  124.             for (int i = 1; i < array.Length; i++)
  125.             {
  126.                 if (array[i].StartsWith("7\""))
  127.                     filteredout += array[i].Substring(1);
  128.                 else
  129.                     filteredout += "^" + array[i];
  130.             }
  131.             return filteredout;
  132.         }
  133.  
  134.         public static byte[] StrToByteArray(string str)
  135.         {
  136.             UTF8Encoding encoding = new UTF8Encoding();
  137.             return encoding.GetBytes(str);
  138.         }
  139.  
  140.         private void txtOutput_TextChanged(object sender, EventArgs e)
  141.         {
  142.             txtOutput.SelectionStart = txtOutput.Text.Length;
  143.             txtOutput.ScrollToCaret();
  144.         }
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment