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.Sockets;
- using System.Net;
- using System.Configuration;
- using MiddleMan.Server;
- using MiddleMan.GameStuff;
- using Starksoft.Net.Proxy;
- namespace MiddleMan.Client
- {
- class ClientCore
- {
- protected Crypto client;
- protected Player player;
- private bool UseProxy = true;
- private const string ProxyString = "v4:12.238.228.7:1080";
- PData HeaderData;
- PData PacketData;
- byte[] TempData;
- public ClientCore(Player player)
- {
- this.player = player;
- string IP = ConfigurationManager.AppSettings["GMSIP"];
- short Port = Convert.ToInt16(ConfigurationManager.AppSettings["LoginPort"]);
- Connect(IP, Port);
- }
- public void Connect(string IP, short Port)
- {
- if(client != null && client.Sock != null)
- client.Sock.Close();
- PacketData = null;
- if (!UseProxy)
- {
- string LanIP = ConfigurationManager.AppSettings["LanIP"];
- Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- sock.Bind(new IPEndPoint(IPAddress.Parse(LanIP), 0));
- sock.BeginConnect(new IPEndPoint(IPAddress.Parse(IP), Convert.ToInt16(Port)),
- new AsyncCallback(ConnectCallback), sock);
- }
- else
- {
- Log.WriteLine("Connecting to Proxy Server!");
- try
- {
- string[] sett = ProxyString.Split(':');
- string version = sett[0];
- string ip = sett[1];
- int port = Convert.ToInt32(sett[2]);
- IProxyClient proxyClient = null;
- switch (version)
- {
- case "v4":
- proxyClient = new Socks4ProxyClient(ip, port);
- break;
- case "v5":
- proxyClient = new Socks5ProxyClient(ip, port);
- break;
- case "v4a":
- proxyClient = new Socks4aProxyClient(ip, port);
- break;
- default:
- proxyClient = new Socks5ProxyClient(ip, port);
- break;
- }
- proxyClient.CreateConnectionAsyncCompleted += new EventHandler<CreateConnectionAsyncCompletedEventArgs>(proxyClient_CreateConnectionAsyncCompleted);
- proxyClient.CreateConnectionAsync(IP, Port);
- }
- catch (Exception ex)
- {
- Log.WriteLine(ex.Message);
- }
- }
- }
- void proxyClient_CreateConnectionAsyncCompleted(object sender, CreateConnectionAsyncCompletedEventArgs e)
- {
- try
- {
- if (e.Error != null)
- {
- Log.WriteLine("[PROXY] " + e.Error.Message);
- return;
- }
- Socket Sock = e.ProxyConnection.Client;
- BeginReceive(16, CallBackType.IV, Sock);
- }
- catch (Exception ex)
- {
- ConnectionFailed(ex.ToString());
- }
- }
- public void Disconnect()
- {
- if (client != null && client.Sock != null)
- client.Sock.Close();
- }
- /// <summary>
- /// Sends a packet to the GMS server
- /// </summary>
- public void SendPacket(byte[] packet)
- {
- client.SendPacket(packet);
- }
- protected virtual void ConnectionFailed(string Reason)
- {
- if (Game.Players.Contains(player) && player != null && player.connectState != ConnectState.LogToChSwap)
- {
- if (player.PConfig.IsLogging())
- player.PConfig.Log("");
- if (player.Name != "")
- Program.GUI.Functions.RemovePlayer(player.Name);
- Game.Players.Remove(player);
- player.Client.Disconnect();
- player.Server.Disconnect();
- player = null;
- Log.WriteLine("Connection interrupted (GMS server kick)");
- }
- }
- protected virtual void HandlePacket(BufferClass.Buffer packet)
- {
- }
- void ConnectCallback(IAsyncResult ar)
- {
- try
- {
- Socket Sock = (Socket)ar.AsyncState;
- Sock.EndConnect(ar);
- BeginReceive(16, CallBackType.IV, Sock);
- }
- catch (Exception e)
- {
- ConnectionFailed(e.ToString());
- }
- }
- void OnRecvIV(IAsyncResult ar)
- {
- Socket Sock = (Socket)ar.AsyncState;
- byte[] Data = GetRecievedData(ar);
- if (Data.Length > 0)
- {
- client = new Crypto(Data, Sock, true);//Init crypto
- if (player.connectState != ConnectState.Login && player.ConAuth != "")
- Packets.PMisc.ChannelConnect(player);
- BeginReceive(4, CallBackType.Header, null);
- }
- else
- {
- ConnectionFailed("Connection interrupted when receiving IV");
- }
- }
- void OnRecvHeader(IAsyncResult ar)
- {
- byte[] Data = GetRecievedData(ar);
- if (Data.Length > 0)
- {
- #region Short header
- if (Data.Length != 4)
- {
- if (HeaderData == null)
- {
- HeaderData = new PData(4);
- HeaderData.AddData(Data);
- BeginReceive(HeaderData.dataLength - HeaderData.dataIndex,
- CallBackType.Header, null);
- }
- else if (HeaderData.AddData(Data))
- {
- byte[] ActualData = HeaderData.packData;
- HeaderData = null;
- int PacketLength = client.GetPacketLength(ActualData);
- if (PacketLength == 0)
- ConnectionFailed("Malformed packet");
- else
- {
- PacketData = new PData(PacketLength);
- BeginReceive(PacketLength, CallBackType.Data, null);
- }
- }
- else
- {
- BeginReceive(HeaderData.dataLength - HeaderData.dataIndex,
- CallBackType.Header, null);
- }
- }
- #endregion
- else
- {
- int PacketLength = client.GetPacketLength(Data);
- if (PacketLength == 0)
- ConnectionFailed("Malformed packet");
- else
- {
- PacketData = new PData(PacketLength);
- BeginReceive(PacketLength, CallBackType.Data, null);
- }
- }
- }
- else
- {
- ConnectionFailed("");
- }
- }
- void OnRecvData(IAsyncResult ar)
- {
- byte[] Data = GetRecievedData(ar);
- if (Data.Length > 0)
- {
- if (PacketData.AddData(Data))//Check if all data is there
- {
- client.Decrypt(PacketData.packData);
- HandlePacket(new BufferClass.Buffer(PacketData.packData));
- BeginReceive(4, CallBackType.Header, null);//Go get another packet
- }
- else//More data to get
- {
- BeginReceive(PacketData.dataLength - PacketData.dataIndex,
- CallBackType.Data, null);
- }
- }
- else
- {
- ConnectionFailed("");
- }
- }
- void BeginReceive(int Length, CallBackType CBType, Socket sock)
- {
- if (sock == null)
- sock = client.Sock;
- if (!sock.Connected)
- {
- return;
- }
- switch (CBType)
- {
- case CallBackType.IV:
- sock.BeginReceive(TempData = new byte[Length], 0, Length,
- SocketFlags.None, new AsyncCallback(OnRecvIV), sock);
- break;
- case CallBackType.Header:
- sock.BeginReceive(TempData = new byte[Length], 0, Length,
- SocketFlags.None, new AsyncCallback(OnRecvHeader), sock);
- break;
- case CallBackType.Data:
- sock.BeginReceive(TempData = new byte[Length], 0, Length,
- SocketFlags.None, new AsyncCallback(OnRecvData), sock);
- break;
- }
- }
- byte[] GetRecievedData(IAsyncResult ar)
- {
- Socket Sock = (Socket)(ar.AsyncState);
- int nBytesRec = 0;
- try { nBytesRec = Sock.EndReceive(ar); }
- catch { }
- byte[] byReturn = new byte[nBytesRec];
- Array.Copy(TempData, byReturn, nBytesRec);
- return byReturn;
- }
- }
- enum CallBackType
- {
- IV,
- Header,
- Data
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement