Exemplo n.º 1
0
        private static void AcceptConnection(IAsyncResult ar)
        {
            Socket _listen = (Socket)ar.AsyncState;

            try
            {
                Socket client = _listen.EndAccept(ar);
                if (client != null)
                {
                    int ClientID = client.GetHashCode();
                    if (!ClientFunctions.ClientIdExist(ClientID))
                    {
                        //Console.WriteLine("[+] Client is Connected: " + client.LocalEndPoint.ToString());
                        ClientManager MyClient = new ClientManager(ClientID, client);


                        init.Clients.Add(MyClient);
                        //Console.WriteLine("[+] Client added to list!");

                        if (MyClient != null)
                        {
                            int ClientIndex = ClientFunctions.GetClientIndex(ClientID);
                            if (ClientIndex != -1)
                            {
                                //Console.WriteLine(" [+] Client listening packets ...");
                                init.Clients[ClientIndex]._socket.BeginReceive(init.Clients[ClientIndex].buffer, 0, ClientManager.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), init.Clients[ClientIndex]);
                            }
                        }
                        else
                        {
                            Console.WriteLine("[-] Client in list does not exist!");
                        }
                    }
                    else
                    {
                        // report error
                        // cloned client id hash code
                        Console.WriteLine("[-] Already client id exists: " + ClientID);
                    }
                }
            }
            catch
            {
                // add to log Accept Connection error
                return;
            }
            finally
            {
                _listen.BeginAccept(new AsyncCallback(AcceptConnection), _listen);
            }
        }
Exemplo n.º 2
0
        public static void SendResponseSelectChar(ClientManager MyClient, byte hero_order)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                int len = 0;
                using (BinaryWriter bw = new BinaryWriter(stream, Encoding.UTF8))
                {
                    bw.Write((byte)0x06); // packet type
                    bw.Write((byte)0x00); // response type


                    IPAddress ip       = IPAddress.Parse("127.0.0.1");
                    byte[]    ip_bytes = ip.GetAddressBytes();

                    bw.Write(ip_bytes);     // ZoneServer IP

                    bw.Write((short)20165); // ZoneServer port
                    bw.Write((byte)0x6b);   // ZoneServer public cripto

                    Random rd    = new Random();
                    int    Token = rd.Next(0xFFFFFF);
                    bw.Write((Int32)Token);

                    // Send token to ZoneServer
                    int id_idx = MyClient.data.id_idx;



                    // verify this values

                    // send to zs
                    ZS_Bind.SendToken(id_idx, Token, hero_order);
                    len = (int)bw.BaseStream.Length;
                }
                byte[] buffer = stream.GetBuffer();
                Array.Resize(ref buffer, len);
                MakePacketAndSend(MyClient, buffer);

                ClientFunctions.DisconnectClientFromID(MyClient.id);
            }
        }
Exemplo n.º 3
0
        public static void ParsePacket(ClientManager MyClient, byte[] data)
        {
            if (MyClient == null)
            {
                return;
            }
            if (MyClient._socket == null)
            {
                return;
            }
            if (!MyClient._socket.Connected)
            {
                return;
            }

            byte[] PacketDecrypted = PacketFunctions.GetPacketDataDecrypted(data);

            using (MemoryStream ms = new MemoryStream(PacketDecrypted))
            {
                using (BinaryReader br = new BinaryReader(ms))
                {
                    int id_idx = br.ReadInt32();
                    if (!ClientFunctions.SearchUserIDInSession(id_idx))
                    {
                        Console.WriteLine("Client not found in list");
                        return;
                    }


                    br.ReadByte();
                    byte PacketType = br.ReadByte();

                    if (MyClient.data.Authenticated)
                    {
                        switch (PacketType)
                        {
                        case 2:     // load characters data
                            //Console.WriteLine("Receive load character!");
                            SendData.SendResponseLoadCharacters(MyClient);
                            SendData.SendResponseGMSInfo(MyClient);
                            return;

                            break;

                        case 3:     // send create character prev data
                            //Console.WriteLine("Receive get create char data!");
                            SendData.SendResponseCreateCharData(MyClient);
                            return;

                            break;

                        case 4:     // delete character
                            //Console.WriteLine("Receive delete hero request!");
                            ProcessDeleteHeroReceive(MyClient, PacketDecrypted);
                            return;

                            break;

                        case 5:     // create hero
                            //Console.WriteLine("Receive create hero!");
                            ProcessCreateHeroReceive(MyClient, PacketDecrypted);
                            return;

                            break;

                        case 6:     // select hero
                            Console.WriteLine("[HeroSelect] Request hero select.");
                            ProcessHeroSelected(MyClient, PacketDecrypted);
                            break;

                        case 10:     // guild mark list
                            break;

                        case 32:     // change heroname
                            break;
                        }
                    }

                    float exe_version = br.ReadSingle();
                    if (exe_version != MyInfo.EXE_VERSION)
                    {
                        //Console.WriteLine("Client use exe_version old version");
                        // disconnect by using client old_version
                        return;
                    }

                    int    id_idx2        = br.ReadInt32();
                    int    LGS_TOKEN      = br.ReadInt32();
                    byte[] username_bytes = br.ReadBytes(32);
                    string username       = PacketFunctions.ExtractStringFromBytes(username_bytes);

                    if (!ClientFunctions.TokenIsValidInSession(id_idx2, LGS_TOKEN, username))
                    {
                        //Console.WriteLine("Incorrect token!");
                        // disconnect by using a incorrect token
                        return;
                    }

                    MyClient.data.id_idx        = id_idx2;  // copy id_idx valid from packet
                    MyClient.data.username      = username; // copy username valid from packet
                    MyClient.data.Authenticated = true;     // confirm client authenticated aprove
                    SendData.SendAprovedAuthentication(MyClient);
                    SendData.SendAprovedSession(MyClient);
                    //Console.WriteLine("[W] User auth aproved! " + username + ":" + id_idx2);
                }
            }
        }
Exemplo n.º 4
0
        private static void ReceiveCallback(IAsyncResult ar)
        {
            byte[]        Data;
            ClientManager MyClient = (ClientManager)ar.AsyncState;
            Socket        client   = MyClient._socket;


            if (client == null)
            {
                return;
            }
            if (!client.Connected)
            {
                return;
            }
            int BufferSize = getPendingByteCount(client);

            if (BufferSize > 1000)
            {
                return;
            }

            try
            {
                int BytesReceive = client.EndReceive(ar);
                if (BytesReceive > 0)
                {
                    Data = new byte[BytesReceive];
                    Array.Copy(MyClient.buffer, Data, BytesReceive);
                    ReceiveData.ParsePacket(MyClient, Data);
                    MyClient.buffer = new byte[ClientManager.BufferSize];

                    // Process received data
                }
                else
                {
                    ClientFunctions.DisconnectClientFromID(MyClient.id);
                    ClientFunctions.RemoveClientFromInstance(MyClient);
                }
                client.BeginReceive(MyClient.buffer, 0, ClientManager.BufferSize, SocketFlags.None, new AsyncCallback(ReceiveCallback), MyClient);
            }
            catch (SocketException e)
            {
                if (e.ErrorCode == 10054)
                {
                    ClientFunctions.DisconnectClientFromID(MyClient.id);
                    ClientFunctions.RemoveClientFromInstance(MyClient);
                    //Console.WriteLine("Client disconnected!");
                    return;
                }
            }
            catch
            {
                ClientFunctions.DisconnectClientFromID(MyClient.id);
                ClientFunctions.RemoveClientFromInstance(MyClient);
                //Console.WriteLine("Client disconnected!");
                // add to log receive error
                // remove client connected if is a error: remove from clients list
                //
                return;
            }
        }