Ejemplo n.º 1
0
        public void TestMessagePutHeader()
        {
            Message message = new Message();

            message.PutLong(long.MinValue);

            Assert.AreEqual(8, message.ReadableBytes);
            Assert.AreEqual(message.ReaderIndex + 8, message.WriterIndex);

            var oldReaderIndex = message.ReaderIndex;
            var oldWriterIndex = message.WriterIndex;

            message.PutHeader((ushort)message.ReadableBytes);

            Assert.AreEqual(10, message.ReadableBytes);
            Assert.AreEqual(oldWriterIndex, message.WriterIndex);
            Assert.AreEqual(oldReaderIndex - 2, message.ReaderIndex);

            oldReaderIndex = message.ReaderIndex;
            oldWriterIndex = message.WriterIndex;

            message.PutHeader((uint)message.ReadableBytes);

            Assert.AreEqual(14, message.ReadableBytes);
            Assert.AreEqual(oldWriterIndex, message.WriterIndex);
            Assert.AreEqual(oldReaderIndex - 4, message.ReaderIndex);
        }
Ejemplo n.º 2
0
        public override void OnReceiveFirstMessage(Message message)
        {
            message.GetUShort(); //OS
            var version = message.GetUShort();

            Rsa.Decrypt(message);

            uint[] key = new uint[4];
            key[0] = message.GetUInt();
            key[1] = message.GetUInt();
            key[2] = message.GetUInt();
            key[3] = message.GetUInt();

            XteaKey = key;
            HasXteaEncryption = true;

            var isGm = message.GetBool();
            var accountName = message.GetString();
            var playerName = message.GetString();
            var password = message.GetString();

            message.ReaderIndex += 6;

            if (version != Constants.ClientVersionNumber)
            {
                Disconnect(0x0A, "This server requires client version " + Constants.ClientVersion + ".");
                return;
            }

            Disconnect(0x14, "Under construction.");
        }
Ejemplo n.º 3
0
        public static unsafe void Encrypt(Message message, uint[] key)
        {
            var pad = message.ReadableBytes % 8;

            if (pad > 0)
                message.WriterIndex += (8 - pad);

            var msgSize = message.ReadableBytes;

            fixed (byte* bufferPtr = message.Buffer) {
                var words = (uint*)(bufferPtr + message.ReaderIndex);

                for (var pos = 0; pos < msgSize / 4; pos += 2) {
                    uint sum = 0;
                    uint count = 32;

                    while (count-- > 0) {
                        words [pos] += (words [pos + 1] << 4 ^ words [pos + 1] >> 5) + words [pos + 1] ^ sum
                            + key [sum & 3];
                        sum += Constants.XteaDelta;
                        words [pos + 1] += (words [pos] << 4 ^ words [pos] >> 5) + words [pos] ^ sum
                            + key [sum >> 11 & 3];
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public virtual void OnReceiveMessage(Message message)
        {
            if (_hasChecksum && message.GetUInt() != Adler.Generate(message))
                throw new Exception("Invalid message checksum.");

            if (_hasXteaEncryption)
            {
                Xtea.Decrypt(message, _xteaKey);
                var length = message.GetUShort();
                message.WriterIndex = message.ReaderIndex + length;
            }
        }
Ejemplo n.º 5
0
        public void Put(Message message)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            lock (_messages)
            {
                if (_messages.Count < _maxSize)
                    _messages.Push(message);
                else
                    Dispose(message);
            }
        }
Ejemplo n.º 6
0
        public void TestMessagePutGet()
        {
            Message message = new Message();
            Assert.AreEqual(0, message.ReadableBytes);

            message.PutByte(byte.MaxValue);
            message.PutByte(byte.MinValue);

            message.PutUShort(ushort.MaxValue);
            message.PutUShort(ushort.MinValue);
            message.PutShort(short.MaxValue);
            message.PutShort(short.MinValue);

            message.PutUInt(uint.MaxValue);
            message.PutUInt(uint.MinValue);
            message.PutInt(int.MaxValue);
            message.PutInt(int.MinValue);

            message.PutULong(ulong.MaxValue);
            message.PutULong(ulong.MinValue);
            message.PutLong(long.MaxValue);
            message.PutLong(long.MinValue);

            message.PutString(string.Empty);
            message.PutString("123");

            Assert.AreEqual(65, message.ReadableBytes);

            Assert.AreEqual(byte.MaxValue, message.GetByte());
            Assert.AreEqual(byte.MinValue, message.GetByte());

            Assert.AreEqual(ushort.MaxValue, message.GetUShort());
            Assert.AreEqual(ushort.MinValue, message.GetUShort());
            Assert.AreEqual(short.MaxValue, message.GetShort());
            Assert.AreEqual(short.MinValue, message.GetShort());

            Assert.AreEqual(uint.MaxValue, message.GetUInt());
            Assert.AreEqual(uint.MinValue, message.GetUInt());
            Assert.AreEqual(int.MaxValue, message.GetInt());
            Assert.AreEqual(int.MinValue, message.GetInt());

            Assert.AreEqual(ulong.MaxValue, message.GetULong());
            Assert.AreEqual(ulong.MinValue, message.GetULong());
            Assert.AreEqual(long.MaxValue, message.GetLong());
            Assert.AreEqual(long.MinValue, message.GetLong());

            Assert.AreEqual(string.Empty, message.GetString());
            Assert.AreEqual("123", message.GetString());

            Assert.AreEqual(0, message.ReadableBytes);
        }
Ejemplo n.º 7
0
        public void TestAdlerGenerate()
        {
            Message message = new Message();
            message.PutULong(ulong.MaxValue);

            var checksum = Adler.Generate(message);

            Xtea.Encrypt(message, _key);

            Assert.AreNotEqual(checksum, Adler.Generate(message));

            Xtea.Decrypt(message, _key);

            Assert.AreEqual(checksum, Adler.Generate(message));
        }
Ejemplo n.º 8
0
        public static uint Generate(Message message)
        {
            if (message.ReadableBytes <= 0)
                throw new InvalidOperationException("Invalid message length.");

            var end = message.WriterIndex;
            var start = message.ReaderIndex;
            var buffer = message.Buffer;

            var unSum1 = Constants.AdlerChecksumStart & 0xFFFF;
            var unSum2 = (Constants.AdlerChecksumStart >> 16) & 0xFFFF;

            for (var i = start; i < end; i++)
            {
                unSum1 = (unSum1 + buffer[i]) % Constants.AdlerChecksumBase;
                unSum2 = (unSum1 + unSum2) % Constants.AdlerChecksumBase;
            }

            return (unSum2 << 16) + unSum1;
        }
Ejemplo n.º 9
0
        public void TestXteaEncryptDecrypt()
        {
            Message message = new Message();

            message.PutUInt(uint.MaxValue);
            message.PutShort(short.MinValue);

            Xtea.Encrypt(message, _key);

            Assert.AreEqual(8, message.ReadableBytes);

            message.MarkReaderIndex();

            Assert.AreNotEqual(uint.MaxValue, message.GetUInt());
            Assert.AreNotEqual(short.MinValue, message.GetShort());

            message.ResetReaderIndex();

            Xtea.Decrypt(message, _key);

            Assert.AreEqual(uint.MaxValue, message.GetUInt());
            Assert.AreEqual(short.MinValue, message.GetShort());
        }
Ejemplo n.º 10
0
        public Connection(Server server, ServicePort servicePort, Socket socket)
        {
            if (server == null)
                throw new ArgumentNullException("server");
            if(servicePort == null)
                throw new ArgumentNullException("servicePort");
            if (socket == null)
                throw new ArgumentNullException("socket");
            if (!socket.Connected)
                throw new ArgumentException("The socket is not connected.");

            _server = server;
            _servicePort = servicePort;
            _socket = socket;

            _message = new Message(true);
            _sendLock = new object();

            _sendEventArgs = new SocketAsyncEventArgs();
            _sendEventArgs.Completed += SendCallback;

            _remoteAddress = ((IPEndPoint)_socket.RemoteEndPoint).Address.ToString();
            _ipAddress = IPConverter.ToUInt32(_remoteAddress);
        }
Ejemplo n.º 11
0
        public static unsafe void Decrypt(Message message, uint[] key)
        {
            var msgSize = message.ReadableBytes;

            if (msgSize % 8 > 0)
                throw new Exception("Invalid Xtea encrypted message size.");

            fixed (byte* bufferPtr = message.Buffer) {
                var words = (uint*)(bufferPtr + message.ReaderIndex);

                for (var pos = 0; pos < msgSize / 4; pos += 2) {
                    var count = 32;
                    var sum = 0xC6EF3720;

                    while (count-- > 0) {
                        words [pos + 1] -= (words [pos] << 4 ^ words [pos] >> 5) + words [pos] ^ sum
                            + key [sum >> 11 & 3];
                        sum -= Constants.XteaDelta;
                        words [pos] -= (words [pos + 1] << 4 ^ words [pos + 1] >> 5) + words [pos + 1] ^ sum
                            + key [sum & 3];
                    }
                }
            }
        }
Ejemplo n.º 12
0
 public virtual void OnReceiveFirstMessage(Message message)
 {
 }
Ejemplo n.º 13
0
        protected Message GetOutputBuffer()
        {
            if (_outputBuffer != null && _outputBuffer.ReadableBytes < Constants.MessageDefaultSize - 4096)
                return _outputBuffer;
            else if (_connection != null)
            {
                _outputBuffer = _server.OutputMessagePool.Get(_connection, true);
                return _outputBuffer;
            }

            return null;
        }
Ejemplo n.º 14
0
        public override void OnReceiveFirstMessage(Message message)
        {
            message.GetUShort();
            var version = message.GetUShort();
            message.ReaderIndex += 12;

            Rsa.Decrypt(message);

            uint[] key = new uint[4];
            key[0] = message.GetUInt();
            key[1] = message.GetUInt();
            key[2] = message.GetUInt();
            key[3] = message.GetUInt();

            XteaKey = key;
            HasXteaEncryption = true;

            if (version != Constants.ClientVersionNumber)
            {
                Disconnect(0x0A, "This server requires client version " + Constants.ClientVersion + ".");
                return;
            }

            var login = message.GetString();
            var password = message.GetString();

            if (login.Length < 5)
            {
                Disconnect(0x0A, "Invalid Account Name.");
                return;
            }

            var account = Server.Instance.AccountRepository.Load(login);

            if (account == null)
            {
                Disconnect(0x0A, "Account name or password is not correct.");
                return;
            }

            var output = Server.OutputMessagePool.Get(Connection, false);

            if (output != null)
            {
                var serverIp = Server.ServiceManager.IpAddresses.First().Key;
                foreach (var keyPair in Server.ServiceManager.IpAddresses)
                {
                    if ((keyPair.Key & keyPair.Value) == (Connection.IpAddress & keyPair.Value))
                    {
                        serverIp = keyPair.Key;
                        break;
                    }
                }

                output.PutByte(0x64);
                output.PutByte((byte)account.Characters.Count);

                foreach (var character in account.Characters)
                {
                    output.PutString(character);
                    output.PutString(Server.ConfigManager.WorldName);
                    output.PutUInt(serverIp);
                    output.PutUShort((ushort)Server.ConfigManager.GamePort);
                }

                output.PutUShort(account.PremiumDaysLeft);

                Connection.Write(output);
            }

            Connection.Close();
        }
Ejemplo n.º 15
0
 protected virtual void OnSendMessage(Message message)
 {
     try
     {
         if (_protocol != null)
             _protocol.OnSendMessage(message);
     }
     catch (Exception e)
     {
         OnExceptionCaught(e);
     }
 }
Ejemplo n.º 16
0
        public void Write(Message message)
        {
            OnSendMessage(message);

            lock (_sendLock)
            {
                _pendingSend++;

                if (_sending)
                {
                    _server.OutputMessagePool.PutSend(message);
                    return;
                }

                if (_sendTimeout > 0)
                    _sendTimeoutScheduleId = _server.Scheduler.Add(_sendTimeout, OnMessageSendTimeout);

                _sendEventArgs.SetBuffer(message.Buffer, message.ReaderIndex, message.ReadableBytes);
                _sendEventArgs.UserToken = message;
                _sending = true;

                _socket.SendAsync(_sendEventArgs);
            }
        }
Ejemplo n.º 17
0
 private void Dispose(Message value)
 {
     if (value != null)
         _count--;
 }
Ejemplo n.º 18
0
        public void TestMessageReadOnly()
        {
            Message message = new Message(true);

            Assert.IsTrue(message.IsReadyOnly);
            message.PutByte(byte.MaxValue);
        }
Ejemplo n.º 19
0
        public virtual void OnSendMessage(Message message)
        {
            message.PutHeader((ushort)message.ReadableBytes);

            if (_hasXteaEncryption && _xteaKey != null)
                Xtea.Encrypt(message, _xteaKey);
            if (_hasChecksum)
                message.PutHeader(Adler.Generate(message));

            if (_hasXteaEncryption || _hasChecksum)
                message.PutHeader((ushort)message.ReadableBytes);
        }
Ejemplo n.º 20
0
        public void PutSend(Message message)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            lock (_autoSendMessages)
                _autoSendMessages.Add(message);
        }
Ejemplo n.º 21
0
 //public static void Encrypt(Message message)
 //{
 //    var encryptedBytes = EncryptRsaEngine.ProcessBlock(message.Buffer, message.ReaderIndex, 128);
 //    Array.Copy(encryptedBytes, 0, message.Buffer, offset, length);
 //}
 public static void Decrypt(Message message)
 {
     var decryptedBytes = DecryptRsaEngine.ProcessBlock(message.Buffer, message.ReaderIndex, 128);
     Array.Copy(decryptedBytes, 0, message.Buffer, message.ReaderIndex, decryptedBytes.Length);
 }
Ejemplo n.º 22
0
        private Message Configure(Message message, Connection connection, bool autoSend)
        {
            if (autoSend)
            {
                lock (_autoSendMessages)
                    _autoSendMessages.Add(message);
            }

            message.Clear();
            message.Connection = connection;
            message.FrameTime = _frameTime;

            return message;
        }