Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- namespace RconTool2
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- public string ipaddress;
- public string password;
- public byte[] rconbyte = { 0xFF, 0xFF, 0xFF, 0XFF, 0X72, 0X63, 0X6F, 0X6E }; //sends 4 oob bytes + rcon
- private void Form1_Load(object sender, EventArgs e)
- {
- string[] settings = File.ReadAllLines("rcon.ini");
- foreach (string setting in settings)
- {
- if (setting.StartsWith("Server="))
- ipaddress = setting.Substring(7);
- else if (setting.StartsWith("Password="))
- password = setting.Substring(9);
- }
- }
- private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == (char)Keys.Enter)
- {
- e.Handled = true;
- SendInput(txtInput.Text);
- if (txtInput.Text.StartsWith("map mp"))
- {
- SendInput("status");
- }
- txtInput.Text = "";
- }
- }
- public void SendInput(string input)
- {
- txtOutput.Text += "]" + input + Environment.NewLine;
- sendPacket(input);
- }
- public void sendPacket(string commandstring)
- {
- try
- {
- UdpClient udpClient = new UdpClient();
- int port = Int32.Parse(ipaddress.Split(':')[1]);
- string ip = ipaddress.Split(':')[0];
- try
- {
- udpClient.Connect(ip, port);
- var command = StrToByteArray(" " + password + " " + commandstring);
- var query = new byte[rconbyte.Length + command.Length];
- Array.Copy(rconbyte, 0, query, 0, rconbyte.Length);
- Array.Copy(command, 0, query, rconbyte.Length, command.Length);
- udpClient.Send(query, query.Length);
- IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
- udpClient.Client.ReceiveTimeout = 600;
- try
- {
- Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
- var returnData = Encoding.ASCII.GetString(receiveBytes);
- displayBytes(returnData);
- udpClient.Close();
- }
- catch (Exception e)
- {
- udpClient.Close();
- displayBytes(e.ToString());
- }
- }
- catch (Exception e)
- {
- udpClient.Close();
- displayBytes(e.ToString());
- }
- }
- catch
- {
- }
- }
- public void displayBytes(string data)
- {
- var strData = data.Substring(4).Split('\n');
- if (strData[0].StartsWith("print"))
- {
- for (int i = 1; i < strData.Length; i++)
- {
- txtOutput.Text += removeQuakeColorCodes(strData[i]) + Environment.NewLine;
- }
- }
- else
- {
- for (int i = 0; i < strData.Length; i++)
- {
- txtOutput.Text += strData[i] + Environment.NewLine;
- }
- }
- }
- public string removeQuakeColorCodes(string remove)
- {
- string filteredout = "";
- var array = remove.Split('^');
- filteredout += array[0];
- for (int i = 1; i < array.Length; i++)
- {
- if (array[i].StartsWith("7\""))
- filteredout += array[i].Substring(1);
- else
- filteredout += "^" + array[i];
- }
- return filteredout;
- }
- public static byte[] StrToByteArray(string str)
- {
- UTF8Encoding encoding = new UTF8Encoding();
- return encoding.GetBytes(str);
- }
- private void txtOutput_TextChanged(object sender, EventArgs e)
- {
- txtOutput.SelectionStart = txtOutput.Text.Length;
- txtOutput.ScrollToCaret();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment