예제 #1
0
            public void OnReceivedRequest(INodeEndpointProtocolRequest request)
            {
                ITranslatorProtocolHandler handler = this.protocolBase.Handler;
                bool pass = false;

                try
                {
                    pass = handler.Pass(request);
                }
                catch (Exception)
                {
                    this.protocolBase.Disconnect();
                }
                if (pass)
                {
                    Request translatedRequest = new Request(request, handler);
                    lock (this.protocolBase.Listeners)
                    {
                        foreach (var listener in this.protocolBase.Listeners)
                        {
                            listener.OnReceivedRequest(translatedRequest);
                        }
                    }
                }
            }
예제 #2
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);
                    }
                }
            }
        }
예제 #3
0
 public Request(TracerBroadcaster tracerBroadcaster, INodeEndpoint endpoint, INodeEndpointProtocolRequest request, Guid guid, string method, XElement body)
 {
     this.tracerBroadcaster = tracerBroadcaster;
     this.endpoint          = endpoint;
     this.request           = request;
     this.guid   = guid;
     this.method = method;
     this.body   = body;
 }
            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);
            }
예제 #5
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 abstract bool Pass(INodeEndpointProtocolRequest request);
            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);
            }
예제 #9
0
 public void OnReceivedRequest(INodeEndpointProtocolRequest request)
 {
     this.server.Send(request.Message);
 }
 public static void Respond(this INodeEndpointProtocolRequest request, string message)
 {
     request.Respond(message.NodeServiceEncode());
 }
 public static string RequestMessage(this INodeEndpointProtocolRequest request)
 {
     return(request.Message.NodeServiceDecode());
 }
예제 #12
0
 public Request(INodeEndpointProtocolRequest request, ITranslatorProtocolHandler handler)
 {
     this.handler = handler;
     this.request = request;
     this.message = this.handler.Decode(this.request.Message);
 }
예제 #13
0
 public bool Pass(INodeEndpointProtocolRequest request)
 {
     return(true);
 }