Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6. using System.Net;
  7. using System.Configuration;
  8. using MiddleMan.Server;
  9. using MiddleMan.GameStuff;
  10. using Starksoft.Net.Proxy;
  11.  
  12. namespace MiddleMan.Client
  13. {
  14.     class ClientCore
  15.     {        
  16.         protected Crypto client;
  17.         protected Player player;
  18.  
  19.         private bool UseProxy = true;
  20.         private const string ProxyString = "v4:12.238.228.7:1080";
  21.  
  22.         PData HeaderData;
  23.         PData PacketData;
  24.         byte[] TempData;        
  25.  
  26.         public ClientCore(Player player)
  27.         {            
  28.             this.player = player;
  29.  
  30.             string IP = ConfigurationManager.AppSettings["GMSIP"];
  31.             short Port = Convert.ToInt16(ConfigurationManager.AppSettings["LoginPort"]);
  32.             Connect(IP, Port);
  33.         }
  34.  
  35.         public void Connect(string IP, short Port)
  36.         {
  37.             if(client != null && client.Sock != null)
  38.                 client.Sock.Close();
  39.  
  40.             PacketData = null;
  41.             if (!UseProxy)
  42.             {
  43.                 string LanIP = ConfigurationManager.AppSettings["LanIP"];
  44.                 Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  45.                 sock.Bind(new IPEndPoint(IPAddress.Parse(LanIP), 0));
  46.                 sock.BeginConnect(new IPEndPoint(IPAddress.Parse(IP), Convert.ToInt16(Port)),
  47.                     new AsyncCallback(ConnectCallback), sock);
  48.             }
  49.             else
  50.             {
  51.                 Log.WriteLine("Connecting to Proxy Server!");
  52.                 try
  53.                 {
  54.                     string[] sett = ProxyString.Split(':');
  55.                     string version = sett[0];
  56.                     string ip = sett[1];
  57.                     int port = Convert.ToInt32(sett[2]);
  58.                     IProxyClient proxyClient = null;
  59.                     switch (version)
  60.                     {
  61.                         case "v4":
  62.                             proxyClient = new Socks4ProxyClient(ip, port);
  63.                             break;
  64.                         case "v5":
  65.                             proxyClient = new Socks5ProxyClient(ip, port);
  66.                             break;
  67.                         case "v4a":
  68.                             proxyClient = new Socks4aProxyClient(ip, port);
  69.                             break;
  70.                         default:
  71.                             proxyClient = new Socks5ProxyClient(ip, port);
  72.                             break;
  73.                     }
  74.                     proxyClient.CreateConnectionAsyncCompleted += new EventHandler<CreateConnectionAsyncCompletedEventArgs>(proxyClient_CreateConnectionAsyncCompleted);
  75.                     proxyClient.CreateConnectionAsync(IP, Port);
  76.                 }
  77.                 catch (Exception ex)
  78.                 {
  79.                     Log.WriteLine(ex.Message);
  80.                 }
  81.             }
  82.         }
  83.  
  84.         void proxyClient_CreateConnectionAsyncCompleted(object sender, CreateConnectionAsyncCompletedEventArgs e)
  85.         {
  86.             try
  87.             {
  88.                 if (e.Error != null)
  89.                 {
  90.                     Log.WriteLine("[PROXY] " + e.Error.Message);
  91.                     return;
  92.                 }
  93.                 Socket Sock = e.ProxyConnection.Client;
  94.                 BeginReceive(16, CallBackType.IV, Sock);
  95.             }
  96.             catch (Exception ex)
  97.             {
  98.                 ConnectionFailed(ex.ToString());
  99.             }
  100.         }
  101.  
  102.         public void Disconnect()
  103.         {
  104.             if (client != null && client.Sock != null)
  105.                 client.Sock.Close();
  106.         }
  107.  
  108.         /// <summary>
  109.         /// Sends a packet to the GMS server
  110.         /// </summary>
  111.         public void SendPacket(byte[] packet)
  112.         {
  113.             client.SendPacket(packet);
  114.         }
  115.  
  116.         protected virtual void ConnectionFailed(string Reason)
  117.         {
  118.             if (Game.Players.Contains(player) && player != null && player.connectState != ConnectState.LogToChSwap)
  119.             {
  120.                 if (player.PConfig.IsLogging())                
  121.                     player.PConfig.Log("");
  122.                 if (player.Name != "")
  123.                     Program.GUI.Functions.RemovePlayer(player.Name);
  124.                 Game.Players.Remove(player);
  125.                 player.Client.Disconnect();
  126.                 player.Server.Disconnect();
  127.                 player = null;
  128.                 Log.WriteLine("Connection interrupted (GMS server kick)");
  129.             }
  130.         }
  131.  
  132.         protected virtual void HandlePacket(BufferClass.Buffer packet)
  133.         {            
  134.         }
  135.  
  136.         void ConnectCallback(IAsyncResult ar)
  137.         {
  138.             try
  139.             {
  140.                 Socket Sock = (Socket)ar.AsyncState;
  141.                 Sock.EndConnect(ar);
  142.                 BeginReceive(16, CallBackType.IV, Sock);
  143.             }
  144.             catch (Exception e)
  145.             {
  146.                 ConnectionFailed(e.ToString());
  147.             }
  148.         }
  149.  
  150.         void OnRecvIV(IAsyncResult ar)
  151.         {
  152.             Socket Sock = (Socket)ar.AsyncState;
  153.             byte[] Data = GetRecievedData(ar);
  154.             if (Data.Length > 0)
  155.             {
  156.                 client = new Crypto(Data, Sock, true);//Init crypto
  157.                 if (player.connectState != ConnectState.Login && player.ConAuth != "")
  158.                     Packets.PMisc.ChannelConnect(player);
  159.  
  160.                 BeginReceive(4, CallBackType.Header, null);
  161.             }
  162.             else
  163.             {
  164.                 ConnectionFailed("Connection interrupted when receiving IV");
  165.             }
  166.         }
  167.  
  168.         void OnRecvHeader(IAsyncResult ar)
  169.         {
  170.             byte[] Data = GetRecievedData(ar);
  171.  
  172.             if (Data.Length > 0)
  173.             {                
  174.                 #region Short header
  175.                 if (Data.Length != 4)
  176.                 {
  177.                     if (HeaderData == null)
  178.                     {
  179.                         HeaderData = new PData(4);
  180.                         HeaderData.AddData(Data);
  181.                         BeginReceive(HeaderData.dataLength - HeaderData.dataIndex,
  182.                             CallBackType.Header, null);
  183.                     }
  184.                     else if (HeaderData.AddData(Data))
  185.                     {
  186.                         byte[] ActualData = HeaderData.packData;
  187.                         HeaderData = null;
  188.                         int PacketLength = client.GetPacketLength(ActualData);
  189.                         if (PacketLength == 0)
  190.                             ConnectionFailed("Malformed packet");
  191.                         else
  192.                         {
  193.                             PacketData = new PData(PacketLength);
  194.                             BeginReceive(PacketLength, CallBackType.Data, null);
  195.                         }
  196.                     }
  197.                     else
  198.                     {
  199.                         BeginReceive(HeaderData.dataLength - HeaderData.dataIndex,
  200.                             CallBackType.Header, null);
  201.                     }
  202.                 }
  203.                 #endregion
  204.                 else
  205.                 {
  206.                     int PacketLength = client.GetPacketLength(Data);
  207.                     if (PacketLength == 0)
  208.                         ConnectionFailed("Malformed packet");
  209.                     else
  210.                     {
  211.                         PacketData = new PData(PacketLength);
  212.                         BeginReceive(PacketLength, CallBackType.Data, null);
  213.                     }
  214.                 }
  215.             }
  216.             else
  217.             {
  218.                 ConnectionFailed("");
  219.             }
  220.         }
  221.  
  222.         void OnRecvData(IAsyncResult ar)
  223.         {
  224.             byte[] Data = GetRecievedData(ar);
  225.  
  226.             if (Data.Length > 0)
  227.             {
  228.                 if (PacketData.AddData(Data))//Check if all data is there
  229.                 {
  230.                     client.Decrypt(PacketData.packData);
  231.                     HandlePacket(new BufferClass.Buffer(PacketData.packData));
  232.  
  233.                     BeginReceive(4, CallBackType.Header, null);//Go get another packet
  234.                 }
  235.                 else//More data to get
  236.                 {
  237.                     BeginReceive(PacketData.dataLength - PacketData.dataIndex,
  238.                         CallBackType.Data, null);
  239.                 }
  240.             }
  241.             else
  242.             {
  243.                 ConnectionFailed("");
  244.             }
  245.         }
  246.  
  247.         void BeginReceive(int Length, CallBackType CBType, Socket sock)
  248.         {
  249.             if (sock == null)
  250.                 sock = client.Sock;
  251.  
  252.             if (!sock.Connected)
  253.             {
  254.                 return;
  255.             }
  256.  
  257.             switch (CBType)
  258.             {
  259.                 case CallBackType.IV:
  260.                     sock.BeginReceive(TempData = new byte[Length], 0, Length,
  261.                         SocketFlags.None, new AsyncCallback(OnRecvIV), sock);
  262.                     break;
  263.                 case CallBackType.Header:
  264.                     sock.BeginReceive(TempData = new byte[Length], 0, Length,
  265.                         SocketFlags.None, new AsyncCallback(OnRecvHeader), sock);
  266.                     break;
  267.                 case CallBackType.Data:
  268.                     sock.BeginReceive(TempData = new byte[Length], 0, Length,
  269.                         SocketFlags.None, new AsyncCallback(OnRecvData), sock);
  270.                     break;
  271.             }
  272.         }
  273.  
  274.         byte[] GetRecievedData(IAsyncResult ar)
  275.         {
  276.             Socket Sock = (Socket)(ar.AsyncState);
  277.             int nBytesRec = 0;
  278.             try { nBytesRec = Sock.EndReceive(ar); }
  279.             catch { }
  280.             byte[] byReturn = new byte[nBytesRec];
  281.             Array.Copy(TempData, byReturn, nBytesRec);
  282.  
  283.             return byReturn;
  284.         }
  285.     }
  286.  
  287.     enum CallBackType
  288.     {
  289.         IV,
  290.         Header,
  291.         Data
  292.     }
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement