Exemplo n.º 1
0
        public MessageDtoTests()
        {
            var pingRequest = new PingRequest();

            _messageDto = new MessageDto(pingRequest.ToProtocolMessage(PeerIdHelper.GetPeerId("Sender_Key")),
                                         PeerIdHelper.GetPeerId("Recipient_Key")
                                         );
        }
        /// <summary>
        ///     Signs a protocol message, or straight WriteAndFlush non-protocolMessages
        /// </summary>
        /// <param name="context"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        protected override Task WriteAsync0(IChannelHandlerContext context, IMessageDto <ProtocolMessage> message)
        {
            Logger.Verbose("Signing message {message}", message);
            var protocolMessageSigned = message.Content.Sign(_keySigner, _signingContext);
            var signedDto             = new SignedMessageDto(protocolMessageSigned, message.RecipientPeerIdentifier);

            return(context.WriteAsync(signedDto));
        }
Exemplo n.º 3
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="cmdDto"></param>
 /// <param name="responseNode"></param>
 /// <returns></returns>
 public static AnyMessage ToAnyCommand(this IMessageDto cmdDto, NodeDescriptor responseNode)
 {
     if (cmdDto == null)
     {
         throw new ArgumentNullException("cmdDto");
     }
     if (responseNode == null)
     {
         throw new ArgumentNullException("responseNode");
     }
     return AnyMessage.Create(HecpRequest.Create(responseNode.AcDomain, cmdDto), responseNode);
 }
        public ProtocolMessageSignHandlerTests()
        {
            _fakeContext = Substitute.For <IChannelHandlerContext>();
            _keySigner   = Substitute.For <FakeKeySigner>();
            _signature   = Substitute.For <ISignature>();

            _signature.SignatureBytes.Returns(ByteUtil.GenerateRandomByteArray(new FfiWrapper().SignatureLength));
            _signature.PublicKeyBytes.Returns(ByteUtil.GenerateRandomByteArray(new FfiWrapper().PublicKeyLength));

            _dto = new MessageDto(new PingRequest().ToProtocolMessage(PeerIdHelper.GetPeerId("sender")),
                                  PeerIdHelper.GetPeerId("recipient")
                                  );
        }
Exemplo n.º 5
0
        /// <inheritdoc />
        protected override Task Write0Async(IChannelHandlerContext context, IMessageDto <ProtocolMessage> message)
        {
            if (message.Content.TypeUrl.EndsWith(MessageTypes.Request.Name))
            {
                _messageCorrelationManager.AddPendingRequest(new CorrelatableMessage <ProtocolMessage>
                {
                    Recipient = message.RecipientPeerIdentifier,
                    Content   = message.Content,
                    SentAt    = DateTimeOffset.UtcNow
                });
            }

            return(context.WriteAsync(message));
        }
Exemplo n.º 6
0
 private HecpRequest(IAcDomain acDomain, IMessageDto cmdDto)
     : this(acDomain)
 {
     if (cmdDto == null)
     {
         throw new ArgumentNullException("cmdDto");
     }
     if (cmdDto.Body == null)
     {
         throw new GeneralException();
     }
     this._message = cmdDto;
     if (cmdDto.Body.InfoId == null)
     {
         _infoId = new DataItem[0];
     }
     if (cmdDto.Body.InfoValue == null)
     {
         _infoValue = new DataItem[0];
     }
     if (cmdDto.Body.InfoId != null)
     {
         _infoId = cmdDto.Body.InfoId.Where(a => a != null).Select(a => new DataItem(a.Key, a.Value)).ToArray();
     }
     if (cmdDto.Body.InfoValue != null)
     {
         _infoValue = cmdDto.Body.InfoValue.Where(a => a != null).Select(a => new DataItem(a.Key, a.Value)).ToArray();
     }
     this._queryList = cmdDto.Body.QueryList;
     this.Credential = new CredentialObject(cmdDto.Credential);
     this._verb      = new Verb(cmdDto.Verb);
     if (cmdDto.Body.Event == null)
     {
         return;
     }
     _eventSourceType   = cmdDto.Body.Event.SourceType;
     _eventSubject      = cmdDto.Body.Event.Subject;
     _eventStatus       = cmdDto.Body.Event.Status;
     _eventReasonPhrase = cmdDto.Body.Event.ReasonPhrase;
 }
Exemplo n.º 7
0
 private HecpRequest(IAcDomain acDomain, IMessageDto cmdDto)
     : this(acDomain)
 {
     if (cmdDto == null)
     {
         throw new ArgumentNullException("cmdDto");
     }
     if (cmdDto.Body == null)
     {
         throw new AnycmdException();
     }
     this._message = cmdDto;
     if (cmdDto.Body.InfoId == null)
     {
         _infoId = new DataItem[0];
     }
     if (cmdDto.Body.InfoValue == null)
     {
         _infoValue = new DataItem[0];
     }
     if (cmdDto.Body.InfoId != null)
     {
         _infoId = cmdDto.Body.InfoId.Where(a => a != null).Select(a => new DataItem(a.Key, a.Value)).ToArray();
     }
     if (cmdDto.Body.InfoValue != null)
     {
         _infoValue = cmdDto.Body.InfoValue.Where(a => a != null).Select(a => new DataItem(a.Key, a.Value)).ToArray();
     }
     this._queryList = cmdDto.Body.QueryList;
     this.Credential = new CredentialObject(cmdDto.Credential);
     this._verb = new Verb(cmdDto.Verb);
     if (cmdDto.Body.Event == null) return;
     _eventSourceType = cmdDto.Body.Event.SourceType;
     _eventSubject = cmdDto.Body.Event.Subject;
     _eventStatus = cmdDto.Body.Event.Status;
     _eventReasonPhrase = cmdDto.Body.Event.ReasonPhrase;
 }
Exemplo n.º 8
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="acDomain"></param>
 /// <param name="cmdDto"></param>
 /// <returns></returns>
 public static HecpRequest Create(IAcDomain acDomain, IMessageDto cmdDto)
 {
     return(cmdDto == null ? null : new HecpRequest(acDomain, cmdDto));
 }
 protected override void Encode(IChannelHandlerContext context,
                                IMessageDto <ProtocolMessage> message,
                                List <object> output)
 {
     output.Add(message.Content);
 }
Exemplo n.º 10
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="cmdDto"></param>
 /// <param name="toNode"></param>
 /// <returns></returns>
 public static IMessageDto RequestNode(this IMessageDto cmdDto, NodeDescriptor toNode)
 {
     if (cmdDto == null)
     {
         throw new ArgumentNullException("cmdDto");
     }
     if (toNode == null)
     {
         throw new ArgumentNullException("toNode");
     }
     if (cmdDto.Credential == null)
     {
         throw new GeneralException("非法状态的命令,没有设置证书");
     }
     if (string.IsNullOrEmpty(cmdDto.Credential.Password))
     {
         CredentialType credentialType;
         if (!cmdDto.Credential.CredentialType.TryParse(out credentialType))
         {
             throw new GeneralException("意外的证书类型" + cmdDto.Credential.CredentialType);
         }
         ClientType clientType;
         if (!cmdDto.Credential.ClientType.TryParse(out clientType))
         {
             throw new GeneralException("意外的客户端类型" + cmdDto.Credential.ClientType);
         }
         switch (clientType)
         {
             case ClientType.Undefined:
                 break;
             case ClientType.Node:
                 NodeDescriptor clientNode;
                 if (!toNode.AcDomain.NodeHost.Nodes.TryGetNodeByPublicKey(cmdDto.Credential.ClientId, out clientNode))
                 {
                     throw new GeneralException("意外的客户节点标识" + cmdDto.Credential.ClientId);
                 }
                 switch (credentialType)
                 {
                     case CredentialType.Undefined:
                         break;
                     case CredentialType.Token:
                         cmdDto.Credential.Password = TokenObject.Token(cmdDto.Credential.ClientId, cmdDto.Credential.Ticks, clientNode.Node.SecretKey);
                         break;
                     case CredentialType.Signature:
                         cmdDto.Credential.Password = Signature.Sign(cmdDto.ToOrignalString(cmdDto.Credential), clientNode.Node.SecretKey);
                         break;
                     case CredentialType.OAuth:
                         break;
                     default:
                         break;
                 }
                 break;
             case ClientType.App:
                 break;
             case ClientType.Monitor:
                 break;
             default:
                 break;
         }
     }
     return cmdDto.ToAnyCommand(toNode).Request();
 }
Exemplo n.º 11
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="acDomain"></param>
 /// <param name="cmdDto"></param>
 /// <returns></returns>
 public static HecpRequest Create(IAcDomain acDomain, IMessageDto cmdDto)
 {
     return cmdDto == null ? null : new HecpRequest(acDomain, cmdDto);
 }
Exemplo n.º 12
0
 public virtual void SendMessage <T>(IMessageDto <T> message) where T : IMessage <T>
 {
     Channel.WriteAsync(message).ConfigureAwait(false);
 }
Exemplo n.º 13
0
        /// <summary>
        /// 将给定的命令消息转化为原始签名字符串
        /// </summary>
        /// <param name="message">命令消息</param>
        /// <param name="credential"></param>
        /// <returns></returns>
        public static string ToOrignalString(this IMessageDto message, ICredentialData credential)
        {
            if (message == null)
            {
                return(string.Empty);
            }
            if (credential == null)
            {
                return(string.Empty);
            }
            var sb = new StringBuilder();

            // 证书
            sb.Append("CredentialType=").Append(credential.CredentialType);
            sb.Append("&SignatureMethod=").Append(credential.SignatureMethod);
            sb.Append("&ClientId=").Append(credential.ClientId);
            sb.Append("&ClientType=").Append(credential.ClientType);
            sb.Append("&UserName="******"&UserType=").Append(credential.UserType);
            sb.Append("&Ticks=").Append(credential.Ticks);
            // 命令
            sb.Append("&Version=").Append(message.Version);
            sb.Append("&MessageId=").Append(message.MessageId);
            sb.Append("&MessageType=").Append(message.MessageType);
            sb.Append("&Verb=").Append(message.Verb);
            sb.Append("&Ontology=").Append(message.Ontology);
            if (message.Body != null)
            {
                sb.Append("&InfoId=");
                if (message.Body.InfoId != null)
                {
                    foreach (var item in message.Body.InfoId)
                    {
                        if (item != null)
                        {
                            sb.Append("&").Append("InfoId_").Append(item.Key).Append("=").Append(item.Value);
                        }
                    }
                }
                sb.Append("&InfoValue=");
                if (message.Body.InfoValue != null)
                {
                    foreach (var item in message.Body.InfoValue)
                    {
                        if (item != null)
                        {
                            sb.Append("&").Append("InfoValue_").Append(item.Key).Append("=").Append(item.Value);
                        }
                    }
                }
                if (message.Body.QueryList != null)
                {
                    string resultItem = string.Empty;
                    int    l          = resultItem.Length;
                    foreach (var item in message.Body.QueryList)
                    {
                        if (resultItem.Length != l)
                        {
                            resultItem += ",";
                        }
                        resultItem += item;
                    }
                }
                if (message.Body.Event != null)
                {
                    sb.Append("&Event_SourceType=").Append(message.Body.Event.SourceType);
                    sb.Append("&Event_Subject=").Append(message.Body.Event.Subject);
                    sb.Append("&Event_Status=").Append(message.Body.Event.Status);
                    sb.Append("&Event_ReasonPhrase=").Append(message.Body.Event.ReasonPhrase);
                }
            }
            sb.Append("&TimeStamp=").Append(message.TimeStamp);
            sb.Append("&IsDumb=").Append(message.IsDumb);

            // 忽略大小写
            return(sb.ToString().ToLower());
        }