예제 #1
0
        public void OnReceivedRequest(INodeEndpointProtocolRequest request)
        {
            string requestMessage = request.RequestMessage();

            if (requestMessage.StartsWith("[REQUEST]"))
            {
                Guid   guid;
                string method;
                string message;
                if (ProtocolEnabledHelper.SplitRequest(requestMessage, out guid, out method, out message))
                {
                    try
                    {
                        XElement body    = XElement.Parse(message);
                        Request  wrapper = new Request(this.tracerBroadcaster, this.endpoint, request, guid, method, body);
                        this.tracerBroadcaster.OnReceivedRequest(wrapper);
                        this.endpoint.QueueRequest(wrapper);
                    }
                    catch (Exception exception)
                    {
                        throw new NodeEndpointMessageException("Cannot understand the request format.", exception);
                    }
                }
            }
        }
            public override bool Pass(INodeEndpointProtocolRequest request)
            {
                lock (this.stepLock)
                {
                    switch (this.currentStep)
                    {
                    case Step.WaitingForAuthData:
                    {
                        this.currentStep = Step.WaitingForAuthResult;
                        byte[] authData          = request.Message;
                        byte[] encryptedAuthData = Md5AuthDataAndPassword(authData, this.passwordMD5);
                        this.client.Send(encryptedAuthData);
                    }
                        return(false);

                    case Step.WaitingForAuthResult:
                    {
                        this.currentStep = Step.WaitingForAesKey;
                        if (request.RequestMessage() != "PASS")
                        {
                            this.client.Disconnect();
                        }
                        else
                        {
                            byte[] publicKey, privateKey;
                            Crypting.RsaGenerateKey(out publicKey, out privateKey);

                            this.client.Send(publicKey);
                            this.privateKey = privateKey;
                        }
                    }
                        return(false);

                    case Step.WaitingForAesKey:
                    {
                        this.currentStep = Step.Done;

                        byte[] encryptedMessage = request.Message;
                        byte[] message          = Crypting.RsaDecrypt(encryptedMessage, this.privateKey);
                        this.privateKey = null;

                        int keyLength = StreamProtocol <Stream> .ReadLeadBytes(message);

                        byte[] key = new byte[keyLength];
                        byte[] iv  = new byte[message.Length - sizeof(int) - keyLength];
                        Array.Copy(message, sizeof(int), key, 0, key.Length);
                        Array.Copy(message, sizeof(int) + keyLength, iv, 0, iv.Length);
                        SetKey(key, iv);
                        this.connectionEvent.Set();
                    }
                        return(false);
                    }
                }
                return(true);
            }
예제 #3
0
 public void OnReceivedRequest(INodeEndpointProtocolRequest request)
 {
     if (!this.server.Connected)
     {
         if (this.client != null)
         {
             this.client.Disconnect();
         }
     }
     else
     {
         if (this.client == null)
         {
             string message = request.RequestMessage();
             if (message.StartsWith("[CONNECTENDPOINT]"))
             {
                 string endpointName = message.Substring(17);
                 INodeEndpointProtocolClient protocolClient = this.protocolFactory.CreateClient();
                 if (protocolClient.Connect("localhost/" + TcpShareProtocolFactory.GetInternalPipeAddress(port), endpointName, NodeEndpointProtocolFactoryExtension.DefaultTimeout))
                 {
                     this.client = protocolClient;
                     this.client.AddListener(new ResponseListener(this.server));
                     this.client.BeginListen();
                     request.Respond("[CONNECTED]");
                     return;
                 }
                 else
                 {
                     request.Respond("[CANNOTFINDENDPOINT]");
                 }
             }
             else
             {
                 request.Respond("[ENDPOINTNOTCONNECTED]");
             }
             this.server.Disconnect();
         }
         else if (this.client.Connected)
         {
             this.client.Send(request.Message);
         }
         else
         {
             this.server.Disconnect();
         }
     }
 }
            public void OnReceivedRequest(INodeEndpointProtocolRequest request)
            {
                const int streamHeaderLength = 54;

                if (request.Message.Length >= streamHeaderLength)
                {
                    byte[] header = new byte[streamHeaderLength];
                    Array.Copy(request.Message, header, streamHeaderLength);
                    string headerString = header.NodeServiceDecode();
                    if (headerString.StartsWith("[RESPONSESTREAM"))
                    {
                        byte[] stream = new byte[request.Message.Length - streamHeaderLength];
                        Array.Copy(request.Message, streamHeaderLength, stream, 0, stream.Length);
                        this.clientProvider.Receive(headerString, stream);
                    }
                }

                string requestMessage = request.RequestMessage();

                if (requestMessage.StartsWith("[RESPONSE]"))
                {
                    this.clientProvider.Receive(requestMessage);
                }
            }
            public override bool Pass(INodeEndpointProtocolRequest request)
            {
                lock (this.stepLock)
                {
                    switch (this.currentStep)
                    {
                    case Step.WaitingForUserName:
                    {
                        this.currentStep = Step.WaitingForEncryptedAuthData;
                        string userName    = request.RequestMessage();
                        byte[] passwordMD5 = this.authenticationProvider.GetUserNamePasswordMD5(userName);
                        byte[] authData    = new byte[128];
                        new Random().NextBytes(authData);

                        if (passwordMD5 != null)
                        {
                            this.expectedMixedAuthDataAndPassword = Md5AuthDataAndPassword(authData, passwordMD5);
                        }
                        request.Respond(authData);
                    }
                        return(false);

                    case Step.WaitingForEncryptedAuthData:
                    {
                        if (this.expectedMixedAuthDataAndPassword == null)
                        {
                            request.Respond("FAIL");
                        }
                        else
                        {
                            this.currentStep = Step.WaitingForPublicKey;
                            byte[] expected = this.expectedMixedAuthDataAndPassword;
                            byte[] actual   = request.Message;
                            this.expectedMixedAuthDataAndPassword = null;

                            bool fail = false;
                            if (expected.Length != actual.Length)
                            {
                                fail = true;
                            }
                            else
                            {
                                for (int i = 0; i < expected.Length; i++)
                                {
                                    if (expected[i] != actual[i])
                                    {
                                        fail = true;
                                        break;
                                    }
                                }
                            }

                            request.Respond(fail ? "FAIL" : "PASS");
                        }
                    }
                        return(false);

                    case Step.WaitingForPublicKey:
                    {
                        this.currentStep = Step.Done;
                        byte[] publicKey = request.Message;
                        byte[] key, iv;
                        Crypting.AesGenerateKey(out key, out iv);

                        byte[] message = new byte[sizeof(int) + key.Length + iv.Length];
                        StreamProtocol <Stream> .WriteLeadBytes(key.Length, message);

                        Array.Copy(key, 0, message, sizeof(int), key.Length);
                        Array.Copy(iv, 0, message, sizeof(int) + key.Length, iv.Length);

                        byte[] encryptedMessage = Crypting.RsaEncrypt(message, publicKey);
                        request.Respond(encryptedMessage);

                        SetKey(key, iv);
                    }
                        return(false);
                    }
                }
                return(true);
            }