Guest User

Untitled

a guest
Jan 19th, 2019
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Threading;
  9.  
  10. namespace MFC
  11. {
  12.     class Program
  13.     {
  14.  
  15.         static String username, password, mpass, sip;
  16.         static int sport;
  17.         static Socket sckt;
  18.         static Thread rcvr = new Thread(receiver);
  19.         static String ver_key = "9907d5589092f109a97ff67b0ce1e3f9";
  20.         static int protocol_ver = 07;
  21.         static void Main(string[] args)
  22.         {
  23.             //Simple background foreground text title shit
  24.             Console.ForegroundColor = ConsoleColor.Red;
  25.             Console.BackgroundColor = ConsoleColor.DarkRed;
  26.             Console.Clear();
  27.             Console.Write("\t\t\t");
  28.             Console.BackgroundColor = ConsoleColor.Blue;
  29.             Console.WriteLine("/#########################\\");
  30.             Console.BackgroundColor = ConsoleColor.DarkRed;
  31.             Console.Write("\t\t\t");
  32.             Console.BackgroundColor = ConsoleColor.Blue;
  33.             Console.WriteLine("|##  Minecraft chatBot  ##|");
  34.             Console.BackgroundColor = ConsoleColor.DarkRed;
  35.             Console.Write("\t\t\t");
  36.             Console.BackgroundColor = ConsoleColor.Blue;
  37.             Console.WriteLine("\\#########################/");
  38.             Console.BackgroundColor = ConsoleColor.DarkRed;
  39.             //--
  40.  
  41.             Console.Write("\n\n\n");
  42.  
  43.             Console.Write("Please enter your username: ");
  44.             username = Console.ReadLine();
  45.             Console.Write("Please enter your password: ");
  46.             password = Console.ReadLine();
  47.             Console.Write("Please enter server ip: ");
  48.             sip = Console.ReadLine();
  49.             if (sip == "")
  50.                 sip = "192.168.178.11";
  51.             Console.Write("Please enter server port (press enter for default: 25565): ");
  52.             if (!int.TryParse(Console.ReadLine(), out sport))
  53.                 sport = 25565;
  54.            
  55.             //Information gathered lets move on to the socket etc.
  56.             sckt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  57.  
  58.             sckt.Connect(sip, sport);
  59.             Console.WriteLine("\n\nSocket Connected!");
  60.  
  61.             rcvr.Start();
  62.  
  63.             List<Byte> data = new List<Byte> { 00, 07 };
  64.             data.AddRange(CreateString(username));
  65.             data.AddRange(CreateString(ver_key));
  66.             data.Add(00);
  67.             sckt.Send(data.ToArray());
  68.  
  69.             Thread.Sleep(1500);
  70.             //say hello:
  71.             data = new List<Byte> { (byte)0x0d };
  72.             data.Add((byte)0x04);
  73.             data.AddRange(CreateString("Hello world, How are you? <3 Daisy"));
  74.             sckt.Send(data.ToArray());
  75.  
  76.             //Walk to side:
  77.             ////data = new List<Byte> { (byte)0x08 };
  78.             //data.Add(255);
  79.             //data.add()
  80.             //sckt.Send(data.ToArray());
  81.  
  82.             Console.Write("\n");
  83.             Console.ReadKey();
  84.  
  85.             Console.ReadKey();
  86.             sckt.Close();
  87.         }
  88.  
  89.         public static void receiver()
  90.         {
  91.             Byte[] buffer = new Byte[sckt.ReceiveBufferSize];
  92.             while (true)
  93.             {
  94.                 sckt.Receive(buffer);
  95.                 Console.WriteLine("RCVR> Received a packet: " + buffer[0]);
  96.                 switch (buffer[0])
  97.                 {
  98.                     case 00:
  99.                         if (buffer[1] == protocol_ver)
  100.                         {
  101.                             Console.WriteLine("RCVR> Got indetification from server: ");
  102.                             Console.WriteLine("RCVR> NAME: \"" + GetString(buffer, 2) + "\"");
  103.                             Console.WriteLine("RCVR> MOTD: \"" + GetString(buffer, 66) + "\"");
  104.  
  105.                         }
  106.                         break;
  107.  
  108.                     case 07:
  109.                         //if(GetString(buffer, 2) == username)
  110.                         //{
  111.                             Console.WriteLine(buffer[3 + 64] + " ; " + buffer[4 + 64] + " ; " + buffer[5 + 64]);
  112.                         //}
  113.                         break;
  114.  
  115.                     case 08:
  116.                         Console.WriteLine(buffer[2 + 64] + " ; " + buffer[3 + 64] + " ; " + buffer[4 + 64]);
  117.                         break;
  118.  
  119.                     case 09:
  120.                         Console.WriteLine(buffer[2 + 64] + " ; " + buffer[3 + 64] + " ; " + buffer[4 + 64]);
  121.                         break;
  122.  
  123.                     case (byte)0x0a:
  124.                         Console.WriteLine(buffer[2 + 64] + " ; " + buffer[3 + 64] + " ; " + buffer[4 + 64]);
  125.                         break;
  126.  
  127.                 }
  128.  
  129.                 Thread.Sleep(1);
  130.             }
  131.         }
  132.  
  133.         //thanks to sircmpwn right string encoding
  134.         //###############################################
  135.         public static byte[] CreateString(string text)
  136.         {
  137.             //return System.Text.ASCIIEncoding.ASCII.GetBytes(text);
  138.             return Encoding.ASCII.GetBytes(text.PadRight(64, '\x00'));
  139.         }
  140.         public static String GetString(byte[] buffer, int index)
  141.         {
  142.             List<Byte> data = buffer.ToList();
  143.             data.RemoveRange(index + 64, buffer.Length- (index+64));
  144.             data.RemoveRange(0, index);
  145.             int curCut = 63;
  146.  
  147.             while (data[curCut] == 20)
  148.                 curCut -= 1;
  149.  
  150.             data.RemoveRange(curCut, 64-curCut);
  151.             return Encoding.ASCII.GetString(data.ToArray());
  152.         }
  153.         //##############################################
  154.     }
  155. }
Add Comment
Please, Sign In to add comment