Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- using HabboPlus.Util;
- namespace HabboPlus.Sockets
- {
- class SocketDataHandler
- {
- private Dictionary<uint, Socket> Connections = new Dictionary<uint,Socket>();
- public delegate void DataCallbackRouter(ref byte[] Data);
- private Socket uSocket;
- private uint uSocketID;
- byte[] data = new byte[250];
- private string IPAddress;
- private DataCallbackRouter nRouteReceivedDataCallback;
- private AsyncCallback mDataReceivedCallback;
- private HabboHexRC4 mRc4;
- private bool mEncryptionStarted;
- private byte[] mDataBuffer = null;
- private ClientMessageHandler mMessageHandler;
- public void Invoke(uint SockID, Socket Sock)
- {
- uSocket = Sock;
- uSocketID = SockID;
- IPAddress = Sock.RemoteEndPoint.ToString().Split(':')[0];
- Connections.Add(uSocketID, uSocket);
- DataCallbackRouter dataRouter = new DataCallbackRouter(HandleConnectionData);
- StartDelegates(dataRouter);
- uSocket.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(Recieve), uSocket);
- }
- public void HandleConnectionData(ref byte[] data)
- {
- // Gameclient protocol or policyrequest?
- if (data[0] != 64)
- {
- ServerMessage SecretKey = new ServerMessage(0);
- SendMessage(SecretKey);
- switch (SecretKey.Header)
- {
- case "CN": // CN * INIT_CRYPTO
- {
- SecretKey = new ServerMessage(227); // DU
- SecretKey.Append("IH");
- SendMessage(SecretKey);
- }
- break;
- case "CJ": // CN * INIT_CRYPTO
- {
- SecretKey = new ServerMessage(227); // DU
- SecretKey.Append("IH");
- SendMessage(SecretKey);
- SecretKey = new ServerMessage(8); // @H
- 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]");
- SendMessage(SecretKey);
- SecretKey = new ServerMessage(257); // DA
- SecretKey.AppendInt32(6); // RA
- SecretKey.AppendInt32(0); // H
- SecretKey.AppendInt32(1); // I
- SecretKey.AppendInt32(1); // I
- SecretKey.AppendInt32(1); // I
- SecretKey.AppendInt32(3); // K
- SecretKey.AppendInt32(0); // H
- SecretKey.AppendInt32(2); // J
- SecretKey.AppendInt32(1); // I
- SecretKey.AppendInt32(4); // PA
- SecretKey.AppendInt32(1); // I
- SecretKey.AppendInt32(5); // QA
- SecretKey.Append("dd-MM-yyyy");
- SendMessage(SecretKey);
- }
- break;
- }
- }
- else
- {
- int pos = 0;
- while (pos < data.Length)
- {
- try
- {
- // Total length of message (without this): 3 Base64 bytes
- int messageLength = Base64Encoding.DecodeInt32(new byte[] { data[pos++], data[pos++], data[pos++] });
- // ID of message: 2 Base64 bytes
- uint messageID = Base64Encoding.DecodeUInt32(new byte[] { data[pos++], data[pos++] });
- // Data of message: (messageLength - 2) bytes
- byte[] Content = new byte[messageLength - 2];
- for (int i = 0; i < Content.Length; i++)
- {
- Content[i] = data[pos++];
- }
- // Create message object
- ClientMessage message = new ClientMessage(messageID, Content);
- // Handle message object
- mMessageHandler.HandleClientMessage(message);
- }
- catch (IndexOutOfRangeException) // Bad formatting!
- {
- DropConnection();
- }
- catch (Exception ex)
- {
- Console.WriteLine("GameClient.HandleConnectionData", ex);
- }
- }
- }
- }
- private void DataReceived(IAsyncResult iAr)
- {
- // How many bytes has server received?
- int numReceivedBytes = 0;
- try
- {
- numReceivedBytes = uSocket.EndReceive(iAr);
- }
- catch (ObjectDisposedException)
- {
- DropConnection();
- return;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex);
- DropConnection();
- return;
- }
- if (numReceivedBytes > 0)
- {
- // Copy received data buffer
- byte[] dataToProcess = ByteUtil.ChompBytes(mDataBuffer, 0, numReceivedBytes);
- // Decipher received data?
- if (mEncryptionStarted)
- {
- dataToProcess = mRc4.Decipher(dataToProcess, numReceivedBytes);
- }
- // Route data to GameClient to parse and process messages
- RouteData(ref dataToProcess);
- }
- // Wait for new data
- uSocket.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(Recieve), uSocket);
- }
- private void RouteData(ref byte[] Data)
- {
- if (nRouteReceivedDataCallback != null)
- {
- nRouteReceivedDataCallback.Invoke(ref Data);
- }
- }
- private void DropConnection()
- {
- uSocket.Close();
- }
- public void StartDelegates(DataCallbackRouter router)
- {
- mDataBuffer = new byte[512];
- mDataReceivedCallback = new AsyncCallback(DataReceived);
- nRouteReceivedDataCallback = router;
- }
- private void Recieve(IAsyncResult iAr)
- {
- Socket Remote = (Socket)iAr.AsyncState;
- int recv = Remote.EndReceive(iAr);
- string receivedData = Encoding.ASCII.GetString(data, 0, recv);
- if (receivedData.Length > 0)
- {
- Console.WriteLine("<{1}> --> {0}", receivedData, IPAddress);
- }
- uSocket.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(Recieve), uSocket);
- }
- private void SendData(byte[] sData)
- {
- uSocket.BeginSend(sData, 0, sData.Length, SocketFlags.None, new AsyncCallback(SendData), uSocket);
- }
- public void SendMessage(ServerMessage Message)
- {
- SendData(Message.GetBytes());
- }
- public void SendMessage(string Message)
- {
- byte[] buff = Encoding.ASCII.GetBytes(Message);
- SendData(buff);
- }
- private static void SendData(IAsyncResult iar)
- {
- Socket server = (Socket)iar.AsyncState;
- int sent = server.EndSend(iar);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement