コード例 #1
0
ファイル: Server.cs プロジェクト: TheSleepyRaccoon/Sleepy-Net
        void ProcessMessage(ref Header header, Connection conn, byte[] payload, int len)
        {
            switch (header.Channel)
            {
            case MessageTypes.Ping:
                // Ping Test
                rawServer.Send(conn, payload.SubArray(0, len));
                conn.FTT = Ping.Desserialize(payload, len).LastKnownFTT;
                break;

            // ======== Encypted Message ===============
            case MessageTypes.RSARegistration:
                RSARegistration rsaMessage = RSARegistration.Desserialize(payload, len);
                RSARegistration rsaReply;
                switch (rsaMessage.step)
                {
                case RSARegistration.Step.InitalRequest:
                    rsaReply = new RSARegistration(RSARegistration.Step.ServerKey, RSAServerKeys.PublicBytes);
                    Send(conn, ref rsaReply);
                    break;

                case RSARegistration.Step.ClientResponse:
                    rsaMessage.Decrypt(RSAServerKeys);

                    byte[] clientRSAKey = rsaMessage.Data;
                    // RSAPublicKeys[conn] = clientRSAKey;
                    AESKeys[conn] = "Some Generated Key";         // TODO: Generate this AESKey

                    rsaReply = new RSARegistration(RSARegistration.Step.AESKey, System.Text.Encoding.Unicode.GetBytes(AESKeys[conn]));
                    rsaReply.Encrypt(new RSAEncryption.RSAKeys(_public: clientRSAKey));
                    Send(conn, ref rsaReply);
                    break;
                }
                break;
            // ========= End Encrpted Message ==================

            default:
                for (int i = 0; i < Handlers.Count; ++i)
                {
                    Handler h = Handlers[i];
                    if (h.channel == header.Channel)
                    {
                        if (header.IsAsync)
                        {
                            h.Call(conn, payload, len);
                        }
                        else
                        {
                            SyncMessagesToProcess.Enqueue(new ServerSyncMessageCall(h, payload, len, conn));
                        }
                    }
                }
                break;
            }
        }
コード例 #2
0
        // ================== Encyption ================

        void SetupEncryption()
        {
            if (!Connected)
            {
                return;
            }
#if SLEEPY_DEBUG
            RSAKeys = RSAEncryption.GenerateKeys(512);
#else
            RSAKeys = Sleepy.Security.RSAEncryption.GenerateKeys(2048);
#endif
            RSARegistration rsar = new RSARegistration(RSARegistration.Step.InitalRequest, new byte[0]);
            rawClient.Send(MessageUtil.Serialize(ref rsar)); // Kick off the Encrption Handshake
        }
コード例 #3
0
        void ProcessMessage(ref Header header, byte[] payload, int len)
        {
            switch (header.Channel)
            {
            case MessageTypes.MessagePartConfirmation:
                MessagePartConfirmation part = MessagePartConfirmation.Desserialize(payload, len);

                if (ActiveMessageParts.TryGetValue(part.MessageID, out ClientMessagePartsActive activeMessage))
                {
                    activeMessage.RecievedPart(part.PartNumber);
                }
                break;

            // ======== Encypted Message ===============
            case MessageTypes.RSARegistration:
                RSARegistration rsaMessage = RSARegistration.Desserialize(payload, len);
                RSARegistration rsaReply;
                switch (rsaMessage.step)
                {
                case RSARegistration.Step.ServerKey:
                    ServerPublicRSAKey = new RSAEncryption.RSAKeys(_public: rsaMessage.Data);

                    rsaReply = new RSARegistration(RSARegistration.Step.ClientResponse, RSAKeys.PublicBytes);
                    rsaReply.Encrypt(ServerPublicRSAKey);
                    Send(ref rsaReply);
                    break;

                case RSARegistration.Step.AESKey:
                    rsaMessage.Decrypt(RSAKeys);

                    AESKey = System.Text.Encoding.Unicode.GetString(rsaMessage.Data);
                    break;
                }
                break;
            // ========= End Encrpted Message ==================

            default:
                for (int i = 0; i < Handlers.Count; ++i)
                {
                    Handler h = Handlers[i];
                    if (h.channel == header.Channel)
                    {
                        if (header.IsAsync)
                        {
                            h.Call(payload, len);
                        }
                        else
                        {
                            SyncMessagesToProcess.Enqueue(new ClientSyncMessageCall(h, payload, len));
                        }
                    }
                }

                for (int i = DynamicMessagesInProgresses.Count - 1; i >= 0; --i)
                {
                    Handler dh = DynamicMessagesInProgresses[i];

                    if (dh.MessageID == header.ID)
                    {
                        if (header.IsAsync)
                        {
                            dh.Call(payload, len);
                        }
                        else
                        {
                            SyncMessagesToProcess.Enqueue(new ClientSyncMessageCall(dh, payload, len));
                        }
                        DynamicMessagesInProgresses.RemoveAt(i);
                    }
                }
                break;
            }
        }