예제 #1
0
 /// <summary>
 /// 传递一个消息
 /// </summary>
 /// <param name="arg">消息类</param>
 public virtual void OnMessageSend(DrawingFrameworkMessage arg)
 {
     if ((int)arg.Level >= (int)MessageLevel)
     {
         MessageListener?.Invoke(sender, arg);
     }
 }
예제 #2
0
 /// <summary>
 /// 传递一个消息
 /// </summary>
 /// <param name="msg">消息文本</param>
 /// <param name="level">消息等级</param>
 /// <param name="ex">可选参数,异常</param>
 public virtual void OnMessageSend(string msg, DrawingMessageLevel level, Exception ex = null)
 {
     if ((int)level >= (int)MessageLevel)
     {
         MessageListener?.Invoke(sender, new DrawingFrameworkMessage(msg, level, ex: ex));
     }
 }
예제 #3
0
        internal void OnObjectReciver <T>(MessageListener <T> a)
        {
            IMessageConsumer consumer = Session.CreateConsumer(Destination);

            consumer.Listener += msg =>
            {
                if (msg is ITextMessage txtMsg)
                {
                    try
                    {
                        var stt = new JsonSerializerSettings()
                        {
                            MissingMemberHandling = MissingMemberHandling.Error
                        };
                        var obj = JsonConvert.DeserializeObject <T>(txtMsg.Text, stt);
                        a.Invoke(obj);
                    }
                    catch (JsonSerializationException) { }
                }
            };
        }
예제 #4
0
 public void HandleMessage(IMessage message)
 {
     MessageListener.Invoke(message);
 }
예제 #5
0
        private void HandleMessageReceive(ChannelMessage message)
        {
            SimpleMessage simpleMessage = new SimpleMessage(message);

            if (ShowTransportPackets)
            {
                CConsole.Gray("{0}", JsonConvert.SerializeObject(simpleMessage, Formatting.Indented));
            }
            if (message.CipherText == null)
            {
                //  It's not a message packet
                if (message.AnnouncedKey != null && message.AcknowledgeKey == null)
                {
                    //  BOB
                    //  Initial announce from other side
                    if (PlaintextMode != message.PlaintextMode)
                    {
                        CConsole.Red("!!! Invalid Far-side configuration! Near-Side PlaintextMode = {0}, Far-Side PlaintextMode = {1}", PlaintextMode, message.PlaintextMode);
                        IsOpen = false;
                        MessageEvent.Set();
                        return;
                        //throw new Exception("PlaintextMode Mismatch");
                    }
                    Parameters = new DHParameters(
                        new BigInteger(message.Param_P, 16),
                        new BigInteger(message.Param_G, 16),
                        new BigInteger(message.Param_Q, 16),
                        message.Param_L);
                    RachetKeyPair = DHKeyPair.Generate(Parameters);
                    if (Verbose)
                    {
                        CConsole.Yellow("RootChainKey = {0}", H(RachetKeyPair.ComputeSharedSecret(message.AnnouncedKey)));
                    }
                    LastParterAnnouncedKey = message.AnnouncedKey;

                    IsOpen = true;
                    MessageEvent.Set();
                }
                else if (message.AnnouncedKey != null && message.AcknowledgeKey != null)
                {
                    //  ALICE
                    //  Initial acknowledge from other side
                    if (Verbose)
                    {
                        CConsole.DarkMagenta("Acknowledging = {0}", message.AnnouncedKey);
                    }
                    LastParterAnnouncedKey = message.AnnouncedKey;
                    if (message.AcknowledgeKey != RachetKeyPair.PublicKey)
                    {
                        CConsole.Red("!!! Invalid AcknowledgedKey !!!");
                        throw new Exception("Invalid AcknowledgedKey");
                    }
                    else
                    {
                        string secretA = RachetKeyPair.ComputeSharedSecret(message.AnnouncedKey);   //FIRST RootChainKey
                        if (PrefilledRootKey != null)
                        {
                            RootChainKey = PrefilledRootKey;
                            string outputA;
                            string outputB;
                            KDF(RootChainKey, RootChainKey, out outputA, out outputB);
                            RootChainKey = outputA;
                        }
                        else
                        {
                            RootChainKey = secretA;
                        }
                        if (Verbose)
                        {
                            CConsole.Yellow("RootChainKey = {0}", H(RootChainKey));
                        }
                        string outputA1;
                        string outputB1;
                        KDF(RootChainKey, RootChainKey, out outputA1, out outputB1);
                        RootChainKey      = outputA1;
                        ReceivingChainKey = outputB1;
                        if (Verbose)
                        {
                            CConsole.DarkYellow("RootChainKey = {0}", H(RootChainKey));
                        }
                        if (Verbose)
                        {
                            CConsole.Magenta("ReceivingChainKey = {0}", H(ReceivingChainKey));
                        }
                        string outputA2;
                        string outputB2;
                        RachetKeyPair = DHKeyPair.Generate(Parameters);
                        if (Verbose)
                        {
                            if (RootChainCount % 2 == 1)
                            {
                                CConsole.DarkMagenta("Announcing = {0}", H(RachetKeyPair.PublicKey));
                            }
                            else
                            {
                                CConsole.DarkCyan("Announcing = {0}", H(RachetKeyPair.PublicKey));
                            }
                        }
                        string secret = RachetKeyPair.ComputeSharedSecret(message.AnnouncedKey);
                        KDF(secret, RootChainKey, out outputA2, out outputB2);
                        RootChainKey    = outputA2;
                        SendingChainKey = outputB2;
                        if (Verbose)
                        {
                            CConsole.Yellow("RootChainKey = {0}", H(RootChainKey));
                        }
                    }
                    IsOpen = true;
                    MessageEvent.Set();
                }
            }
            else
            {
                //  It's a message packet! Do the decrypt
                string text;
                if (!Decrypt(message, out text))
                {
                    throw new Exception("Failed to Decrypt packet!");
                }
                OnMessage(text);
                MessageListener?.Invoke(text);
            }
        }