Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Threading;
  9.  
  10. using HabboPlus.Util;
  11.  
  12. namespace HabboPlus.Sockets
  13. {
  14.     class SocketDataHandler
  15.     {
  16.         private Dictionary<uint, Socket> Connections = new Dictionary<uint,Socket>();
  17.         public delegate void DataCallbackRouter(ref byte[] Data);
  18.  
  19.         private Socket uSocket;
  20.         private uint uSocketID;
  21.         byte[] data = new byte[250];
  22.         private string IPAddress;
  23.         private DataCallbackRouter nRouteReceivedDataCallback;
  24.         private AsyncCallback mDataReceivedCallback;
  25.         private HabboHexRC4 mRc4;
  26.         private bool mEncryptionStarted;
  27.         private byte[] mDataBuffer = null;
  28.         private ClientMessageHandler mMessageHandler;
  29.  
  30.         public void Invoke(uint SockID, Socket Sock)
  31.         {
  32.             uSocket = Sock;
  33.             uSocketID = SockID;
  34.             IPAddress = Sock.RemoteEndPoint.ToString().Split(':')[0];
  35.             Connections.Add(uSocketID, uSocket);
  36.  
  37.             DataCallbackRouter dataRouter = new DataCallbackRouter(HandleConnectionData);
  38.             StartDelegates(dataRouter);
  39.  
  40.             uSocket.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(Recieve), uSocket);
  41.         }
  42.  
  43.         public void HandleConnectionData(ref byte[] data)
  44.         {
  45.             // Gameclient protocol or policyrequest?
  46.             if (data[0] != 64)
  47.             {
  48.                 ServerMessage SecretKey = new ServerMessage(0);
  49.                 SendMessage(SecretKey);
  50.                 switch (SecretKey.Header)
  51.                 {
  52.                     case "CN": // CN * INIT_CRYPTO
  53.                         {
  54.                             SecretKey = new ServerMessage(227); // DU
  55.                             SecretKey.Append("IH");
  56.                             SendMessage(SecretKey);
  57.                         }
  58.                         break;
  59.                     case "CJ": // CN * INIT_CRYPTO
  60.                         {
  61.                             SecretKey = new ServerMessage(227); // DU
  62.                             SecretKey.Append("IH");
  63.                             SendMessage(SecretKey);
  64.  
  65.                             SecretKey = new ServerMessage(8); // @H
  66.                             SecretKey.Append("[100,105,110,115,120,125,130,135,140,145,150,155,160,165,170,175,176,177,178,180,185,190,195,200,205,206,207,210,215,220,225,230,235,240,245,250,255,260,265,266,267,270,275,280,281,285,290,295,300,305,500,505,510,515,520,525,530,535,540,545,550,555,565,570,575,580,585,590,595,596,600,605,610,615,620,625,626,627,630,635,640,645,650,655,660,665,667,669,670,675,680,685,690,695,696,700,705,710,715,720,725,730,735,740]");
  67.                             SendMessage(SecretKey);
  68.  
  69.                             SecretKey = new ServerMessage(257); // DA
  70.                             SecretKey.AppendInt32(6); // RA
  71.                             SecretKey.AppendInt32(0); // H
  72.                             SecretKey.AppendInt32(1); // I
  73.                             SecretKey.AppendInt32(1); // I
  74.                             SecretKey.AppendInt32(1); // I
  75.                             SecretKey.AppendInt32(3); // K
  76.                             SecretKey.AppendInt32(0); // H
  77.                             SecretKey.AppendInt32(2); // J
  78.                             SecretKey.AppendInt32(1); // I
  79.                             SecretKey.AppendInt32(4); // PA
  80.                             SecretKey.AppendInt32(1); // I
  81.                             SecretKey.AppendInt32(5); // QA
  82.                             SecretKey.Append("dd-MM-yyyy");
  83.                             SendMessage(SecretKey);
  84.                         }
  85.                         break;
  86.                 }
  87.             }
  88.             else
  89.             {
  90.                 int pos = 0;
  91.                 while (pos < data.Length)
  92.                 {
  93.                     try
  94.                     {
  95.                         // Total length of message (without this): 3 Base64 bytes
  96.                         int messageLength = Base64Encoding.DecodeInt32(new byte[] { data[pos++], data[pos++], data[pos++] });
  97.  
  98.                         // ID of message: 2 Base64 bytes
  99.                         uint messageID = Base64Encoding.DecodeUInt32(new byte[] { data[pos++], data[pos++] });
  100.  
  101.                         // Data of message: (messageLength - 2) bytes
  102.                         byte[] Content = new byte[messageLength - 2];
  103.                         for (int i = 0; i < Content.Length; i++)
  104.                         {
  105.                             Content[i] = data[pos++];
  106.                         }
  107.  
  108.                         // Create message object
  109.                         ClientMessage message = new ClientMessage(messageID, Content);
  110.  
  111.                         // Handle message object
  112.                         mMessageHandler.HandleClientMessage(message);
  113.                     }
  114.                     catch (IndexOutOfRangeException) // Bad formatting!
  115.                     {
  116.                         DropConnection();
  117.                     }
  118.                     catch (Exception ex)
  119.                     {
  120.                         Console.WriteLine("GameClient.HandleConnectionData", ex);
  121.                     }
  122.                 }
  123.             }
  124.         }
  125.  
  126.         private void DataReceived(IAsyncResult iAr)
  127.         {
  128.  
  129.             // How many bytes has server received?
  130.             int numReceivedBytes = 0;
  131.             try
  132.             {
  133.                 numReceivedBytes = uSocket.EndReceive(iAr);
  134.             }
  135.             catch (ObjectDisposedException)
  136.             {
  137.                 DropConnection();
  138.                 return;
  139.             }
  140.             catch (Exception ex)
  141.             {
  142.                 Console.WriteLine(ex);
  143.                 DropConnection();
  144.                 return;
  145.             }
  146.  
  147.             if (numReceivedBytes > 0)
  148.             {
  149.                 // Copy received data buffer
  150.                 byte[] dataToProcess = ByteUtil.ChompBytes(mDataBuffer, 0, numReceivedBytes);
  151.  
  152.                 // Decipher received data?
  153.                 if (mEncryptionStarted)
  154.                 {
  155.                     dataToProcess = mRc4.Decipher(dataToProcess, numReceivedBytes);
  156.                 }
  157.  
  158.                 // Route data to GameClient to parse and process messages
  159.                 RouteData(ref dataToProcess);
  160.             }
  161.  
  162.             // Wait for new data
  163.             uSocket.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(Recieve), uSocket);
  164.         }
  165.  
  166.         private void RouteData(ref byte[] Data)
  167.         {
  168.             if (nRouteReceivedDataCallback != null)
  169.             {
  170.                 nRouteReceivedDataCallback.Invoke(ref Data);
  171.             }
  172.         }
  173.  
  174.         private void DropConnection()
  175.         {
  176.             uSocket.Close();
  177.         }
  178.  
  179.  
  180.         public void StartDelegates(DataCallbackRouter router)
  181.         {
  182.             mDataBuffer = new byte[512];
  183.             mDataReceivedCallback = new AsyncCallback(DataReceived);
  184.             nRouteReceivedDataCallback = router;
  185.         }
  186.  
  187.         private void Recieve(IAsyncResult iAr)
  188.         {
  189.             Socket Remote = (Socket)iAr.AsyncState;
  190.             int recv = Remote.EndReceive(iAr);
  191.             string receivedData = Encoding.ASCII.GetString(data, 0, recv);
  192.  
  193.             if (receivedData.Length > 0)
  194.             {
  195.                 Console.WriteLine("<{1}> --> {0}", receivedData, IPAddress);
  196.                
  197.             }
  198.  
  199.             uSocket.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(Recieve), uSocket);
  200.         }
  201.  
  202.         private void SendData(byte[] sData)
  203.         {
  204.             uSocket.BeginSend(sData, 0, sData.Length, SocketFlags.None, new AsyncCallback(SendData), uSocket);
  205.         }
  206.  
  207.         public void SendMessage(ServerMessage Message)
  208.         {
  209.             SendData(Message.GetBytes());
  210.         }
  211.  
  212.         public void SendMessage(string Message)
  213.         {
  214.             byte[] buff = Encoding.ASCII.GetBytes(Message);
  215.             SendData(buff);
  216.         }
  217.  
  218.         private static void SendData(IAsyncResult iar)
  219.         {
  220.             Socket server = (Socket)iar.AsyncState;
  221.             int sent = server.EndSend(iar);
  222.         }
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement