Exemplo n.º 1
0
        public override void OnPacketInbound(Packet pPacket)
        {
            if (pPacket.Length == 0)
                return;
            pPacket.Reset(0);

            AC_OnPacketInbound(pPacket);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Used as IAsyncResult parser for ContinueReading().
        /// </summary>
        /// <param name="pIAR">The result AsyncCallback makes</param>
        private void EndReading(IAsyncResult pIAR)
        {
            int amountReceived = 0;
            try
            {
                amountReceived = _socket.EndReceive(pIAR);
            }
            catch (Exception ex)
            {
                amountReceived = 0;
                Console.WriteLine(TypeName + " amountReceived == 0! " + ex.ToString());
            }
            if (amountReceived == 0)
            {
                 Console.WriteLine(TypeName + " amountReceived == 0! OnDisconnectInternal()");
                // We got a disWvsBeta.Common.Sessions here!
                OnDisconnectINTERNAL();
                return;
            }

            // Add amount of bytes received to _bufferpos so we know if we got everything.
            _bufferpos += amountReceived;

            try
            {

                // Check if we got all data. There is _no_ way we would have received more bytes than needed. Period.
                if (_bufferpos == _bufferlen)
                {
                    // It seems we have all data we need
                    // Now check if we got a header
                    if (_header)
                    {
                        if (!_encryption && _receivingFromServer)
                        {
                            // Unencrypted packets have a short header with plain length.
                            ushort length = (ushort)(_buffer[0] | _buffer[1] << 8);
                            StartReading(length);
                        }
                        else
                        {
                            int length = GetHeaderLength(_buffer);
                            StartReading(length);
                        }
                    }
                    else
                    {
                        Packet packet;
                        if (_encryption)
                        {
                            _buffer = Decrypt(_buffer);
                            packet = new Packet(_buffer);

                            DoAction((date) =>
                            {
                                try
                                {
                                    OnPacketInbound(packet);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("Handling Packet Error: {0}", ex.ToString());
                                }
                            });
                        }
                        else
                        {
                            _encryption = true; // First packet received or sent is unencrypted. All others are.
                            packet = new Packet(_buffer);

                            _mapleVersion = packet.ReadUShort();
                            _maplePatchLocation = _maplePatchLocation = packet.ReadString();
                            _encryptIV = packet.ReadBytes(4);
                            _decryptIV = packet.ReadBytes(4);
                            _mapleLocale = packet.ReadByte();
                            Console.WriteLine(TypeName + " MapleVersion: {0}; Patch Location: {1}; Locale: {2}", _mapleVersion, _maplePatchLocation, _mapleLocale);

                            if (_mapleLocale == 8 && _mapleVersion >= 118)
                            {
                                SwitchAESByVersion(_mapleVersion);
                            }

                            packet.Reset();
                            DoAction((date) =>
                            {
                                try
                                {
                                    OnHandshakeInbound(packet);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("Handling Packet Error: {0}", ex.ToString());
                                }
                            });
                        }

                        StartReading(4, true);
                    }
                }
                else
                {
                    ContinueReading();
                }

            }
            catch (SocketException socketException)
            {
                Console.WriteLine(TypeName + " Socket Exception while receiving data: {0}", socketException.Message);
                OnDisconnectINTERNAL();
            }
            catch (Exception ex)
            {
                Console.WriteLine(TypeName + " [ERROR] EndReading(): {0}", ex.ToString());
                OnDisconnectINTERNAL();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Used as IAsyncResult parser for ContinueReading().
        /// </summary>
        /// <param name="pIAR">The result AsyncCallback makes</param>
        private void EndReading(IAsyncResult pIAR)
        {
            int amountReceived = 0;

            try
            {
                amountReceived = _socket.EndReceive(pIAR);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("EXCEPT" + " : " + ex.ToString());
                amountReceived = 0;
            }
            if (amountReceived == 0)
            {
                OnDisconnectINTERNAL("No data received");
                return;
            }

            // Add amount of bytes received to _bufferpos so we know if we got everything.
            _bufferpos += amountReceived;

            try
            {
                // Check if we got all data. There is _no_ way we would have received more bytes than needed. Period.
                if (_bufferpos == _bufferlen)
                {
                    // It seems we have all data we need
                    // Now check if we got a header
                    if (_header)
                    {
                        if (!_encryption && _receivingFromServer)
                        {
                            // Unencrypted packets have a short header with plain length.
                            ushort length = (ushort)(_buffer[0] | _buffer[1] << 8);
                            StartReading(length);
                        }
                        else
                        {
                            int length = GetHeaderLength(_buffer, _bufferlen, _decryptIV, _mapleVersion, _receivingFromServer);
                            if (length == HEADER_ERROR_MORE_DATA)
                            {
                                _bufferlen += 4;
                                ContinueReading();
                            }
                            else
                            {
                                StartReading(length);
                            }
                        }
                    }
                    else
                    {
                        Packet packet;
                        if (_encryption)
                        {
                            // Small scope hack; this will be on the stack until the
                            // callback in DoAction leaves scope
                            byte[] tmpIV = new byte[4];
                            Array.Copy(_decryptIV, 0, tmpIV, 0, 4);

                            // Make a copy of the data because it will be transformed

                            var tempBuff = new byte[_bufferlen];
                            Array.Copy(_buffer, 0, tempBuff, 0, _bufferlen);
                            var data = Decrypt(tempBuff, _bufferlen, _decryptIV);

                            packet = new Packet(data, _bufferlen);

                            byte opcode = data[0];

                            DoAction((date) =>
                            {
                                if (Disconnected)
                                {
                                    return;
                                }
                                try
                                {
                                    Array.Copy(tmpIV, 0, previousDecryptIV, 0, 4);
                                    OnPacketInbound(packet);
                                }
                                catch (Exception ex)
                                {
                                    ////Console.WriteLine("Handling Packet Error: {0}", ex.ToString());
                                }
                            }, "Packet handling opcode: " + opcode);
                        }
                        else
                        {
                            _encryption = true; // First packet received or sent is unencrypted. All others are.
                            packet      = new Packet(_buffer, _bufferlen);

                            _mapleVersion       = packet.ReadUShort();
                            _maplePatchLocation = packet.ReadString();
                            _encryptIV          = packet.ReadBytes(4);
                            _decryptIV          = packet.ReadBytes(4);
                            _mapleLocale        = packet.ReadByte();

                            StartSendAndEncryptLoop();
                            initCipher();
                            packet.Reset();

                            DoAction((date) =>
                            {
                                try
                                {
                                    OnHandshakeInbound(packet);
                                }
                                catch (Exception ex)
                                {
                                    ////Console.WriteLine("Handling Packet Error: {0}", ex.ToString());
                                }
                            }, "Handshake handling");
                        }

                        StartReading(4, true);
                    }
                }
                else
                {
                    ContinueReading();
                }
            }
            catch (SocketException socketException)
            {
                Trace.WriteLine(TypeName + " Socket Exception while receiving data: " + socketException);
                OnDisconnectINTERNAL("EndRead socket exception");
            }
            catch (ObjectDisposedException)
            {
                OnDisconnectINTERNAL("EndRead object disposed");
            }
            catch (Exception ex)
            {
                Trace.WriteLine(TypeName + " [ERROR] EndReading(): " + ex);
                OnDisconnectINTERNAL("EndRead exception");
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Used as IAsyncResult parser for ContinueReading().
        /// </summary>
        /// <param name="pIAR">The result AsyncCallback makes</param>
        private void EndReading(IAsyncResult pIAR)
        {
            int amountReceived = 0;

            try
            {
                amountReceived = _socket.EndReceive(pIAR);
            }
            catch (Exception ex)
            {
                MWLR_Logging.Logger.WriteLine(TypeName + " : " + ex.ToString());
                amountReceived = 0;
            }
            if (amountReceived == 0)
            {
                // We got a disconnection here!
                OnDisconnectINTERNAL();
                return;
            }

            // Add amount of bytes received to _bufferpos so we know if we got everything.
            _bufferpos += amountReceived;

            try
            {
                // Check if we got all data. There is _no_ way we would have received more bytes than needed. Period.
                if (_bufferpos == _bufferlen)
                {
                    // It seems we have all data we need
                    // Now check if we got a header
                    if (_header)
                    {
                        if (!_encryption && _receivingFromServer)
                        {
                            // Unencrypted packets have a short header with plain length.
                            ushort length = (ushort)(_buffer[0] | _buffer[1] << 8);
                            StartReading(length);
                        }
                        else
                        {
                            int length = GetHeaderLength(_buffer);
                            StartReading(length);
                        }
                    }
                    else
                    {
                        Packet packet;
                        if (_encryption)
                        {
                            _buffer = Decrypt(_buffer, _mapleLocale == 1);
                            packet  = new Packet(_buffer);

                            DoAction((date) =>
                            {
                                try
                                {
                                    OnPacketInbound(packet);
                                }
                                catch (Exception ex)
                                {
                                    MWLR_Logging.Logger.WriteLine("Handling Packet Error: {0}", ex.ToString());
                                }
                            });
                        }
                        else
                        {
                            _encryption = true; // First packet received or sent is unencrypted. All others are.
                            packet      = new Packet(_buffer);

                            _mapleVersion       = packet.ReadUShort();
                            _maplePatchLocation = packet.ReadString();
                            _encryptIV          = packet.ReadBytes(4);
                            _decryptIV          = packet.ReadBytes(4);
                            _mapleLocale        = packet.ReadByte();

                            MWLR_Logging.Logger.WriteLine(TypeName + " MapleVersion: {0}; Patch Location: {1}; Locale: {2}", _mapleVersion, _maplePatchLocation, _mapleLocale);

                            if (_mapleLocale == 1)
                            {
                                int    test = int.Parse(_maplePatchLocation);
                                ushort t1   = (ushort)(test & 0x7FFF);
                                int    t2   = (test >> 15) & 1;
                                int    t3   = (test >> 16) & 0xFF;
                                MWLR_Logging.Logger.WriteLine("Logging KMS connection. Version {0} | {1} | {2}", t1, t2, t3);
                                _mapleVersion       = t1;
                                _maplePatchLocation = t3.ToString();
                            }
                            else if (_mapleLocale == 8 && _mapleVersion >= 118)
                            {
                                if (!MWLR_Logging.GMSKeys.ContainsKey(_mapleVersion))
                                {
                                    MWLR_Logging.GMSKeys.Initialize();
                                }

                                if (MWLR_Logging.GMSKeys.ContainsKey(_mapleVersion))
                                {
                                    SwitchAESByVersion(_mapleVersion);
                                }
                                else
                                {
                                    MWLR_Logging.Logger.WriteLine("There's no key for this version, yet.");
                                    MWLR_Logging.TwitterClient.Instance.SendMessage("HEY, @Diamondo25! Update da keyzz!!");

                                    Disconnect();
                                    System.Threading.Thread.Sleep(15 * 60 * 1000);
                                }
                            }

                            packet.Reset();
                            DoAction((date) =>
                            {
                                try
                                {
                                    OnHandshakeInbound(packet);
                                }
                                catch (Exception ex)
                                {
                                    MWLR_Logging.Logger.WriteLine("Handling Packet Error: {0}", ex.ToString());
                                }
                            });
                        }

                        StartReading(4, true);
                    }
                }
                else
                {
                    ContinueReading();
                }
            }
            catch (SocketException socketException)
            {
                MWLR_Logging.Logger.WriteLine(TypeName + " Socket Exception while receiving data: {0}", socketException.Message);
                OnDisconnectINTERNAL();
            }
            catch (Exception ex)
            {
                MWLR_Logging.Logger.WriteLine(TypeName + " [ERROR] EndReading(): {0}", ex.ToString());
                OnDisconnectINTERNAL();
            }
        }
Exemplo n.º 5
0
        public override void OnPacketInbound(Packet pPacket)
        {
            if (pPacket.Length == 0)
            {
                return;
            }
            StartLogging();
            try
            {
                byte header = pPacket.ReadByte();

                if (IsConnectedAsClient)
                {
                    if (header == (byte)ServerMessages.PING)
                    {
                        SendPong();
                    }
                }
                else
                {
                    if (header == (byte)ClientMessages.PONG)
                    {
                        gotPong = true;
                        PingMS  = (int)(MasterThread.CurrentTime - pingSentDateTime);
                    }
                    else if (header == (byte)ClientMessages.__CUSTOM_DC_ME__)
                    {
                        ScheduleDisconnect();
                        return;
                    }
                    else if (MEMORY_CRC_ENABLED)
                    {
                        // Check for expected CRC packet
                        if ((BitConverter.ToUInt16(previousDecryptIV, 0) % 31) == 0)
                        {
                            bool disconnect = true;
                            if (header == (byte)ClientMessages.CLIENT_HASH)
                            {
                                var mode = pPacket.ReadByte();
                                if (mode == 1)
                                {
                                    var clientCRC = pPacket.ReadUInt();
                                    if (ValidateCRC(clientCRC))
                                    {
                                        disconnect = false;
                                    }
                                    else
                                    {
                                        log.Error($"Disconnecting client because CRC didnt match {clientCRC}");
                                    }
                                }
                                else
                                {
                                    log.Error($"Disconnecting client because unexpected mode: {mode}");
                                }
                            }
                            else
                            {
                                log.Error(
                                    $"Disconnecting client because expected CLIENT_HASH packet, but got {header} instead");
                            }

                            if (disconnect)
                            {
                                Disconnect();
                                return;
                            }
                        }
                    }
                }

                pPacket.Reset(0);

                AC_OnPacketInbound(pPacket);
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            finally
            {
                EndLogging();
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Used as IAsyncResult parser for ContinueReading().
        /// </summary>
        /// <param name="pIAR">The result AsyncCallback makes</param>
        private void EndReading(IAsyncResult pIAR)
        {
            int amountReceived = 0;
            try
            {
                amountReceived = _socket.EndReceive(pIAR);
            }
            catch (Exception ex)
            {
                MWLR_Logging.Logger.WriteLine(TypeName + " : " + ex.ToString());
                amountReceived = 0;
            }
            if (amountReceived == 0)
            {
                // We got a disconnection here!
                OnDisconnectINTERNAL();
                return;
            }

            // Add amount of bytes received to _bufferpos so we know if we got everything.
            _bufferpos += amountReceived;

            try
            {

                // Check if we got all data. There is _no_ way we would have received more bytes than needed. Period.
                if (_bufferpos == _bufferlen)
                {
                    // It seems we have all data we need
                    // Now check if we got a header
                    if (_header)
                    {
                        if (!_encryption && _receivingFromServer)
                        {
                            // Unencrypted packets have a short header with plain length.
                            ushort length = (ushort)(_buffer[0] | _buffer[1] << 8);
                            StartReading(length);
                        }
                        else
                        {
                            int length = GetHeaderLength(_buffer);
                            StartReading(length);
                        }
                    }
                    else
                    {
                        Packet packet;
                        if (_encryption)
                        {
                            _buffer = Decrypt(_buffer, _mapleLocale == 1);
                            packet = new Packet(_buffer);

                            DoAction((date) =>
                            {
                                try
                                {
                                    OnPacketInbound(packet);
                                }
                                catch (Exception ex)
                                {
                                    MWLR_Logging.Logger.WriteLine("Handling Packet Error: {0}", ex.ToString());
                                }
                            });
                        }
                        else
                        {
                            _encryption = true; // First packet received or sent is unencrypted. All others are.
                            packet = new Packet(_buffer);

                            _mapleVersion = packet.ReadUShort();
                            _maplePatchLocation = packet.ReadString();
                            _encryptIV = packet.ReadBytes(4);
                            _decryptIV = packet.ReadBytes(4);
                            _mapleLocale = packet.ReadByte();

                            MWLR_Logging.Logger.WriteLine(TypeName + " MapleVersion: {0}; Patch Location: {1}; Locale: {2}", _mapleVersion, _maplePatchLocation, _mapleLocale);

                            if (_mapleLocale == 1)
                            {
                                int test = int.Parse(_maplePatchLocation);
                                ushort t1 = (ushort)(test & 0x7FFF);
                                int t2 = (test >> 15) & 1;
                                int t3 = (test >> 16) & 0xFF;
                                MWLR_Logging.Logger.WriteLine("Logging KMS connection. Version {0} | {1} | {2}", t1, t2, t3);
                                _mapleVersion = t1;
                                _maplePatchLocation = t3.ToString();
                            }
                            else if (_mapleLocale == 8 && _mapleVersion >= 118)
                            {
                                if (!MWLR_Logging.GMSKeys.ContainsKey(_mapleVersion))
                                    MWLR_Logging.GMSKeys.Initialize();

                                if (MWLR_Logging.GMSKeys.ContainsKey(_mapleVersion))
                                {
                                    SwitchAESByVersion(_mapleVersion);
                                }
                                else
                                {
                                    MWLR_Logging.Logger.WriteLine("There's no key for this version, yet.");
                                    MWLR_Logging.TwitterClient.Instance.SendMessage("HEY, @Diamondo25! Update da keyzz!!");

                                    Disconnect();
                                    System.Threading.Thread.Sleep(15 * 60 * 1000);
                                }
                            }

                            packet.Reset();
                            DoAction((date) =>
                            {
                                try
                                {
                                    OnHandshakeInbound(packet);
                                }
                                catch (Exception ex)
                                {
                                    MWLR_Logging.Logger.WriteLine("Handling Packet Error: {0}", ex.ToString());
                                }
                            });
                        }

                        StartReading(4, true);
                    }
                }
                else
                {
                    ContinueReading();
                }

            }
            catch (SocketException socketException)
            {
                MWLR_Logging.Logger.WriteLine(TypeName + " Socket Exception while receiving data: {0}", socketException.Message);
                OnDisconnectINTERNAL();
            }
            catch (Exception ex)
            {
                MWLR_Logging.Logger.WriteLine(TypeName + " [ERROR] EndReading(): {0}", ex.ToString());
                OnDisconnectINTERNAL();
            }
        }