コード例 #1
0
        public static void HandleOpCodes(Decode decode)
        {
            try
            {
                Systems sys = (Systems)decode.Packet;
                sys.PacketInformation = decode;
                PacketReader reader = new PacketReader(sys.PacketInformation.buffer);
                LogDebug.Show("Opcode: {0}", decode.opcode);
                OpCodes opc = (OpCodes)decode.opcode;
                if (opc == OpCodes._MSG_LOGIN)
                {
                    {
                        string usernameShift = reader.String(32);
                        string passwordMd5   = reader.String(32);
                        reader.Skip(4);
                        string clientMac = reader.String(32);
                        reader.Skip(32);
                        uint        unk3     = reader.UInt32();
                        StringShift shift    = new StringShift();
                        string      username = shift.Parser(usernameShift);
                        LogDebug.Show("username: {0}", username);
                        LogDebug.Show("password_md5: {0}", passwordMd5);
                        LogDebug.Show("MAC: {0}", clientMac);
                        LogDebug.Show("unk3: {0}", unk3);
                        int res = UserLogin(username, passwordMd5, clientMac);
                        switch (res)
                        {
                        case (int)AuthenticationStatus.OK:
                        {
                            sys.client.SendC(ServerList());
                            break;
                        }

                        case (int)AuthenticationStatus.BANNED:
                        {
                            sys.client.SendC(UserFail(0xF0, Reason.BANNED));
                            break;
                        }

                        default:
                        {
                            sys.client.SendC(UserFail(0xF0, Reason.AUTH_FAILED));
                            break;
                        }
                        }
                    }
                }
                else
                {
                    LogConsole.Show("Default Opcode: {0:X} - {1}", decode.opcode, opc);
                    LogDebug.HexDump(sys.PacketInformation.buffer, 16, true, false);
                }
            }
            catch (Exception)
            {
                // ignored
            }
        }
コード例 #2
0
            public void ReceiveData(IAsyncResult ar)
            {
                Socket wSocket = (Socket)ar.AsyncState;

                try
                {
                    if (wSocket.Connected)
                    {
                        int recvSize = wSocket.EndReceive(ar);  // get the count of received bytes

                        bool checkData = true;
                        if (recvSize > 0)
                        {
                            if ((recvSize + bufCount) > MAX_BUFFER)  // that may be a try to force buffer overflow, we don't allow that ;)
                            {
                                checkData = false;
                                LocalDisconnect(wSocket);
                            }
                            else
                            {                                                            // we have something in input buffer and it is not beyond our limits
                                Buffer.BlockCopy(tmpbuf, 0, buffer, bufCount, recvSize); // copy the new data to our buffer
                                bufCount += recvSize;                                    // increase our buffer-counter
                            }
                        }
                        else
                        {   // 0 bytes received, this should be a disconnect
                            checkData = false;
                            LocalDisconnect(wSocket);
                        }

                        while (checkData) // repeat while we have data
                        {
                            checkData = false;
                            if (bufCount >= 4) // a minimum of 4 byte is required for us
                            {
                                byte[] newtmp = new byte[bufCount];
                                Buffer.BlockCopy(buffer, 0, newtmp, 0, bufCount);
                                LogDebug.HexDump(newtmp, 16, true, true);
                                Decode de     = new Decode(buffer); // only get the size first.
                                Decode tmp_de = new Decode(tmpbuf);
                                LogConsole.Show("TMP BUFFER OPCODE: {0}", tmp_de.opcode);
                                LogConsole.Show("bufCount: {0} dataSize: {1}", bufCount, de.dataSize);
                                if (bufCount >= (de.dataSize - 2))                                      // It's a complete packet. Call the handler.
                                {
                                    de = new Decode(wSocket, de.tempbuff, this, Packets);               // build up the Decode structure for next step
                                    OnReceiveData(de);                                                  // call the handling routine
                                    bufCount -= (de.dataSize);                                          // decrease buffer-counter
                                    if (bufCount >= 0)                                                  // was the buffer greater than what the packet needs? then it may be the next packet.
                                    {
                                        Buffer.BlockCopy(buffer, 2 + de.dataSize, buffer, 0, bufCount); // move the rest to buffer start
                                        checkData = true;                                               // loop for next packet
                                    }
                                }
                                else
                                {
                                    byte[] _tempddd = new byte[bufCount];
                                    EncDec c        = new EncDec();
                                    byte[] dddxx    = c.Crypt(buffer);
                                    Buffer.BlockCopy(dddxx, 0, _tempddd, 0, de.dataSize);
                                    LogConsole.Show("bufCount: {0} dataSize: {1}", bufCount, de.dataSize);
                                    LogDebug.HexDump(_tempddd, 16, true, true);
                                }
                                de = null;
                            }
                        }
                        // start the next async read
                        if (wSocket.Connected)
                        {
                            wSocket.BeginReceive(tmpbuf, 0, tmpbuf.Length, SocketFlags.None, new AsyncCallback(ReceiveData), wSocket);
                        }
                    }
                    else
                    {
                        LocalDisconnect(wSocket);
                    }
                }
                catch (SocketException)  // explicit handling of SocketException
                {
                    LocalDisconnect(wSocket);
                }
                catch (Exception ex) // other exceptions
                {
                    LogConsole.Show("Exception Occurred! {0}", ex.Message);
                    LocalDisconnect(wSocket);
                }
            }