예제 #1
0
        private void OnMessageReceived(object sender, MessageReceivedEventArgs e)
        {
            if (e.Exception != null)
                Console.WriteLine("Exception occured: {0}", e.Exception.ToString());

            var handler = (MessageHandler)null;
            if (_server.MessageHandlers.TryGetValue(e.Message.ID, out handler))
                handler(_server, this, e.Message);
        }
예제 #2
0
        private void ClientReceived(object sender, MessageReceivedEventArgs e)
        {
            // C -> P -> S

            Console.WriteLine("[S < C] => ID:{0} Name:{1}", e.Message.ID, e.Message.GetType().Name);
            var message = e.Message;
            var messageBytes = (byte[])null;
            if (message is NewClientEncryptionMessage)
                messageBytes = e.MessageData;
            else if (message is LoginRequestMessage)
            {
                var lrMessage = e.Message as LoginRequestMessage;
                var rpkStr = Utils.BytesToString(lrMessage.PublicKey);
                var opkStr = Utils.BytesToString(ServerConnection.Crypto.KeyPair.PublicKey);

                Console.WriteLine("        => Decrypted LoginRequestMessage with pk {0}", rpkStr);
                _snonce = (byte[])lrMessage.Nonce.Clone();
                messageBytes = new byte[e.MessageData.Length];

                var body = e.MessageBody;
                ServerConnection.Crypto.Encrypt(ref body);

                Console.WriteLine("        => Encrypted LoginRequestMessage with our pk {0}", opkStr);
                Buffer.BlockCopy(e.MessageData, 0, messageBytes, 0, Message.HeaderSize); // header
                Buffer.BlockCopy(ServerConnection.Crypto.KeyPair.PublicKey, 0, messageBytes, Message.HeaderSize, CoCKeyPair.KeyLength); // gen public key
                Buffer.BlockCopy(body, 0, messageBytes, Message.HeaderSize + CoCKeyPair.KeyLength, body.Length); // body

                ServerConnection.Crypto.UpdateNonce(_snonce, UpdateNonceType.Blake);
                ServerConnection.Crypto.UpdateNonce(_snonce, UpdateNonceType.Encrypt); // set _snonce for crypto to use for later encryption
            }
            else
            {
                messageBytes = new byte[e.MessageData.Length];

                var body = e.MessageBody;
                ServerConnection.Crypto.Encrypt(ref body);

                Buffer.BlockCopy(e.MessageData, 0, messageBytes, 0, Message.HeaderSize); // header
                Buffer.BlockCopy(body, 0, messageBytes, Message.HeaderSize, body.Length); // body
            }

            File.WriteAllBytes("messages\\[C2S] " + DateTime.Now.ToString("hh-mm-ss.fff") + " " + e.Message.ID, messageBytes);
            ServerConnection.Connection.Send(messageBytes);
        }
예제 #3
0
        private void ServerReceived(object sender, MessageReceivedEventArgs e)
        {
            // C <- P <- S

            Console.WriteLine("[S > C] => ID:{0} Name:{1}", e.Message.ID, e.Message.GetType().Name);
            var message = e.Message;
            var messageBytes = (byte[])null;
            if (message is NewServerEncryptionMessage)
                messageBytes = e.MessageData;
            else if (message is LoginSuccessMessage)
            {
                var lsMessage = e.Message as LoginSuccessMessage;
                _rnonce = (byte[])lsMessage.Nonce.Clone();
                messageBytes = new byte[e.MessageData.Length];

                var body = e.MessageBody;
                ClientConnection.Crypto.Encrypt(ref body);

                Buffer.BlockCopy(e.MessageData, 0, messageBytes, 0, Message.HeaderSize); // header
                Buffer.BlockCopy(body, 0, messageBytes, Message.HeaderSize, body.Length); // body

                ClientConnection.Crypto.UpdateNonce(_rnonce, UpdateNonceType.Encrypt);
                ClientConnection.Crypto.UpdateSharedKey(lsMessage.PublicKey); // 'k'
            }
            else
            {
                messageBytes = new byte[e.MessageData.Length];

                var body = e.MessageBody;
                ClientConnection.Crypto.Encrypt(ref body);

                Buffer.BlockCopy(e.MessageData, 0, messageBytes, 0, Message.HeaderSize); // header
                Buffer.BlockCopy(body, 0, messageBytes, Message.HeaderSize, body.Length); // body
            }

            File.WriteAllBytes("messages\\[C2S] " + DateTime.Now.ToString("hh-mm-ss.fff") + " " + e.Message.ID, messageBytes);
            ClientConnection.Connection.Send(messageBytes);
        }
예제 #4
0
 /// <summary>
 /// Use this method to trigger the <see cref="MessageReceived"/> event.
 /// </summary>
 /// <param name="e">The arguments.</param>
 protected virtual void OnMessageReceived(MessageReceivedEventArgs e)
 {
     if (MessageReceived != null)
         MessageReceived(this, e);
 }