Advertisement
Guest User

PB Program.cs

a guest
May 9th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.93 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Threading;
  8.  
  9.  
  10.  
  11. class TcpEchoServer
  12. {
  13.     static void Main(string[] args)
  14.     {
  15.         Thread authServer = new Thread(startAuthServer);
  16.         Thread gameServer = new Thread(startGameServer);
  17.  
  18.         authServer.Start();
  19.         gameServer.Start();
  20.     }
  21.  
  22.     public static void startGameServer()
  23.     {
  24.         try
  25.         {
  26.            // while (true)
  27.            // {
  28.                 int recv;
  29.                 byte[] data;
  30.                 IPEndPoint ipep = new IPEndPoint(IPAddress.Any,
  31.                                        29890);
  32.  
  33.                 Socket newsock = new
  34.                     Socket(AddressFamily.InterNetwork,
  35.                                 SocketType.Stream, ProtocolType.Tcp);
  36.  
  37.                 newsock.Bind(ipep);
  38.                 newsock.Listen(10);
  39.                 Console.WriteLine("[Game] Waiting for a client...");
  40.                 Socket client = newsock.Accept();
  41.                 IPEndPoint clientep =
  42.                              (IPEndPoint)client.RemoteEndPoint;
  43.                 Console.WriteLine("[Game] Connected with {0} at port {1}",
  44.                                 clientep.Address, clientep.Port);
  45.                 client.Send(new byte[] { 0, 0, 0, 0 });
  46.                 //client.Send(new byte[] { 0x9f, 0x00, 0x02, 0x02, 0x00, 0x85, 0xa8, 0x07, 0x00, 0x00, 0x59, 0x28, 0x02, 0x0b, 0x00, 0x09, 0x00, 0x85, 0x00, 0x80, 0x00, 0x83, 0x69, 0xcc, 0x95, 0xfb, 0x66, 0xfa, 0xe4, 0xb8, 0x58, 0xfc, 0x4b, 0x31, 0xaf, 0xc2, 0xd6, 0xb6, 0x1c, 0x8c, 0x92, 0x39, 0x10, 0x53, 0x70, 0x14, 0xf2, 0x39, 0x18, 0x1f, 0x95, 0x49, 0xd0, 0x56, 0x3d, 0x03, 0x03, 0xfb, 0x4a, 0xea, 0x20, 0xc2, 0xed, 0xd1, 0x96, 0xef, 0xba, 0x96, 0xa7, 0x25, 0xc0, 0x65, 0xe7, 0x93, 0x3d, 0x0d, 0x82, 0xf8, 0x99, 0xea, 0x09, 0x30, 0xa8, 0xbc, 0x97, 0xe4, 0xe6, 0x58, 0xa7, 0x49, 0x28, 0xa5, 0xcd, 0x32, 0x62, 0x32, 0xad, 0xc0, 0xe5, 0x90, 0x27, 0xae, 0x2c, 0x7e, 0x55, 0xb9, 0xcf, 0x9b, 0xa3, 0x9a, 0x00, 0x3e, 0x37, 0x95, 0x2f, 0x2b, 0x34, 0x04, 0xf7, 0x93, 0xeb, 0x74, 0x86, 0xc7, 0xee, 0x22, 0x17, 0xb6, 0x9d, 0xbd, 0x0a, 0x1a, 0x2f, 0xed, 0x4d, 0xc4, 0x75, 0xdc, 0x0b, 0xf3, 0x88, 0x9a, 0x2d, 0xfb, 0xc8, 0x5c, 0xf4, 0xd1, 0x9e, 0x01, 0x00, 0x11, 0x0a, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04 }, SocketFlags.None);
  47.  
  48.  
  49.                 /*Stream s = new MemoryStream();
  50.                 byte[] d = new byte[]{
  51.                     1, 0, 0, 0,
  52.                     1, 0, 0, 127,
  53.                     0xC2, 0x74,
  54.                     1, 0,
  55.                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  56.                     0,
  57.  
  58.                     2,
  59.  
  60.                     0, 0, 0, 0,
  61.                     1, 0, 0, 127,
  62.                     0, 0,
  63.                     5,
  64.                     0, 0,
  65.                     0, 0, 0, 0,
  66.                
  67.                     1, 0, 0, 0,
  68.                     1, 0, 0, 127,
  69.                     0xA1, 0x26,
  70.                     1, 0, 0, 0,
  71.                     0xFF, 0x00, 0x00, 0x00,
  72.                     0x00, 0x00};
  73.  
  74.                 byte[] packetsz = new byte[0];
  75.                 //packetsz.Concat(BitConverter.GetBytes((short)d.Length).Reverse()).Concat(BitConverter.GetBytes((short)0x801).Reverse()).Concat(d);
  76.                 packetsz = packetsz.Concat(BitConverter.GetBytes((short)0)).Concat(BitConverter.GetBytes((short)0x801)).ToArray();
  77.                 client.Send(packetsz, SocketFlags.None);*/
  78.  
  79.  
  80.                 while (true)
  81.                 {
  82.                     data = new byte[client.Available];
  83.                     recv = client.Receive(data);
  84.                     if (recv == 0)
  85.                         break;
  86.  
  87.                     Console.WriteLine("[Game] Received:" + nicePacketString(data));
  88.                     Packet packet = new Packet(data);
  89.                     packet.Parse();
  90.  
  91.                     //client.Send(data, recv, SocketFlags.None);
  92.                 }
  93.                // Console.WriteLine("[Game] Disconnected from {0}",
  94.                              //     clientep.Address);
  95.                 //client.Close();
  96.                 //newsock.Close();
  97.           //  }
  98.         }
  99.         catch (Exception e)
  100.         {
  101.             Console.WriteLine(e.StackTrace);
  102.         }
  103.     }
  104.  
  105.     public static void startAuthServer()
  106.     {
  107.         try
  108.         {
  109.             //while (true)
  110.             //{
  111.  
  112.                 int recv;
  113.                 byte[] data;
  114.                 IPEndPoint ipep = new IPEndPoint(IPAddress.Any,
  115.                                        39190);
  116.  
  117.                 Socket newsock = new
  118.                     Socket(AddressFamily.InterNetwork,
  119.                                 SocketType.Stream, ProtocolType.Tcp);
  120.  
  121.                 newsock.Bind(ipep);
  122.                 newsock.Listen(10);
  123.                 Console.WriteLine("[Auth] Waiting for a client...");
  124.                 Socket client = newsock.Accept();
  125.                 IPEndPoint clientep =
  126.                              (IPEndPoint)client.RemoteEndPoint;
  127.                 Console.WriteLine("[Auth] Connected with {0} at port {1}",
  128.                                 clientep.Address, clientep.Port);
  129.  
  130.                 client.Send(new byte[] { 0, 0, 0, 0 });
  131.                 client.Send(new byte[] { 0x9f, 0x00, 0x02, 0x02, 0x00, 0x85, 0xa8, 0x07, 0x00, 0x00, 0x59, 0x28, 0x02, 0x0b, 0x00, 0x09, 0x00, 0x85, 0x00, 0x80, 0x00, 0x83, 0x69, 0xcc, 0x95, 0xfb, 0x66, 0xfa, 0xe4, 0xb8, 0x58, 0xfc, 0x4b, 0x31, 0xaf, 0xc2, 0xd6, 0xb6, 0x1c, 0x8c, 0x92, 0x39, 0x10, 0x53, 0x70, 0x14, 0xf2, 0x39, 0x18, 0x1f, 0x95, 0x49, 0xd0, 0x56, 0x3d, 0x03, 0x03, 0xfb, 0x4a, 0xea, 0x20, 0xc2, 0xed, 0xd1, 0x96, 0xef, 0xba, 0x96, 0xa7, 0x25, 0xc0, 0x65, 0xe7, 0x93, 0x3d, 0x0d, 0x82, 0xf8, 0x99, 0xea, 0x09, 0x30, 0xa8, 0xbc, 0x97, 0xe4, 0xe6, 0x58, 0xa7, 0x49, 0x28, 0xa5, 0xcd, 0x32, 0x62, 0x32, 0xad, 0xc0, 0xe5, 0x90, 0x27, 0xae, 0x2c, 0x7e, 0x55, 0xb9, 0xcf, 0x9b, 0xa3, 0x9a, 0x00, 0x3e, 0x37, 0x95, 0x2f, 0x2b, 0x34, 0x04, 0xf7, 0x93, 0xeb, 0x74, 0x86, 0xc7, 0xee, 0x22, 0x17, 0xb6, 0x9d, 0xbd, 0x0a, 0x1a, 0x2f, 0xed, 0x4d, 0xc4, 0x75, 0xdc, 0x0b, 0xf3, 0x88, 0x9a, 0x2d, 0xfb, 0xc8, 0x5c, 0xf4, 0xd1, 0x9e, 0x01, 0x00, 0x11, 0x0a, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04 }, SocketFlags.None);
  132.                 client.Send(new byte[] { 0x0a, 0x00, 0x03, 0x01, 0x00, 0x00, 0x18, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, SocketFlags.None); //Wrong password Packet
  133.  
  134.                 byte[] d = new byte[]{
  135.                     1, 0, 0, 0,
  136.                     1, 0, 0, 127,
  137.                     0x74, 0xC2,
  138.                     1, 0,
  139.                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  140.                     0,
  141.  
  142.                     2,
  143.  
  144.                     0, 0, 0, 0,
  145.                     1, 0, 0, 127,
  146.                     0, 0,
  147.                     5,
  148.                     0, 0,
  149.                     0, 0, 0, 0,
  150.                
  151.                     1, 0, 0, 0,
  152.                     1, 0, 0, 127,
  153.                     0x26, 0xA1,
  154.                     1, 0, 0, 0,
  155.                     0xFF, 0x00, 0x00, 0x00,
  156.                     0x00, 0x00};
  157.  
  158.                 byte[] packetsz = new byte[0];
  159.                 packetsz = packetsz.Concat(BitConverter.GetBytes((short)d.Length)).Concat(BitConverter.GetBytes((short)0x801)).Concat(d).ToArray();
  160.                 //packetsz = packetsz.Concat(BitConverter.GetBytes((short)0)).Concat(BitConverter.GetBytes((short)0x801)).ToArray();
  161.                 Console.WriteLine("Sending: " + nicePacketString(packetsz));
  162.                 client.Send(packetsz, SocketFlags.None);
  163.  
  164.  
  165.                 while (true)
  166.                 {
  167.                     data = new byte[1024];
  168.                     recv = client.Receive(data);
  169.                     if (recv == 0)
  170.                         break;
  171.  
  172.                     //Console.WriteLine("[Auth] Received:" + nicePacketString(data));
  173.                     Packet packet = new Packet(data);
  174.                     packet.Parse();
  175.  
  176.                     //client.Send(data, recv, SocketFlags.None);
  177.                 }
  178.                 //Console.WriteLine("[Auth] Disconnected from {0}",
  179.                                  // clientep.Address);
  180.                 //client.Close();
  181.                 //newsock.Close();
  182.            // }
  183.         }
  184.         catch(Exception e)
  185.         {
  186.             Console.WriteLine(e.StackTrace);
  187.         }
  188.     }
  189.  
  190.     public static string nicePacketString(byte[] bytes)
  191.     {
  192.         return BitConverter.ToString(bytes).Replace("-", " ");
  193.     }
  194.  
  195.     public static void writePacketToFile(byte[] data)
  196.     {
  197.         string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
  198.  
  199.         StringBuilder sb = new StringBuilder();
  200.  
  201.         sb.AppendLine();
  202.         foreach(byte b in data)
  203.         {
  204.             sb.Append((char)b);
  205.         }
  206.         sb.AppendLine();
  207.         using (StreamWriter outfile = new StreamWriter(mydocpath + @"\UserInputFile.txt", true))
  208.         {
  209.             outfile.Write(sb.ToString());
  210.         }
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement