示例#1
0
        private IPEndPoint ConnectToDispathServer()
        {
            MessengerSocket socketP = new MessengerSocket();

            //socket.AutoFlush = false;

            #region Request

            //Request this Data!
            PackageRequest[] request = new PackageRequest[]
            {
                new VersionRequest(),
                new MachineInfoRequest(this.Account),
                new AuthenticationRequest(this.Account)
            };

            //Get responses from the server!
            Dictionary <int, PackageTransaction> responses = socketP.SendRangeAndBlock(request);

            #endregion

            #region Parse Responses if Any

            //Agora que temos as respostas vamos verificar se estão correctas!
            //Seja em quantidade, seja em 'qualidade'...

            if (responses.Count != 3)
            {
                throw new MessengerException("Unable to receive one or more responses from the server");
            }

            //Responses!
            VersionResponse     version = responses[request[0].TransactionID].Response as VersionResponse;
            MachineInfoResponse machine = responses[request[1].TransactionID].Response as MachineInfoResponse;
            DispatchServerRedirectionResponse redirection = responses[request[2].TransactionID].Response as DispatchServerRedirectionResponse;

            //Verificar se recebemos as respostas certas!
            version.NotNull <VersionResponse, MessengerProtocolException>("Unexpected response");
            machine.NotNull <MachineInfoResponse, MessengerProtocolException>("Unexpected response");
            redirection.NotNull <DispatchServerRedirectionResponse, MessengerProtocolException>("Unexpected response");

            //Verificar se é uma versão suportada do protocolo!
            version.Protocols.MustContainElement <string, MessengerProtocolException>("MSNP15", "Unsupported  Protocol");

            //Verificar se o nosso cliente ainda é aceitável!
            if ((request[1] as MachineInfoRequest).ClientVersion < machine.MinimumSafeVersion)
            {
                //Ignore! We are not a client! We are just the libraries!
                //throw new Exception("Unsupported  client");
            }

            #endregion

            return(redirection.Host);
        }
示例#2
0
        private void Authenticate(IPEndPoint host, int attemptsP)
        {
            this.socket = new MessengerSocket(host)
            {
                AutoFlush = false
            };

            //Agora que estamos ligados ao Notification Server vamos começar o processo de autenticação!!!

            #region Request

            //Request this Data!
            PackageRequest[] request = new PackageRequest[]
            {
                new VersionRequest(),
                new MachineInfoRequest(this.Account),
                new AuthenticationRequest(this.Account)
            };

            //Get responses from the server!
            Dictionary <int, PackageTransaction> responses = this.socket.SendRangeAndBlock(request);

            #endregion

            #region Parse Responses if Any

            //Agora que temos as respostas vamos verificar se estão correctas!
            //Seja em quantidade, seja em 'qualidade'...

            if (responses.Count != 3)
            {
                throw new MessengerException("Unable to receive one or more responses from the server");
            }

            //Responses!
            VersionResponse        version        = responses[request[0].TransactionID].Response as VersionResponse;
            MachineInfoResponse    machine        = responses[request[1].TransactionID].Response as MachineInfoResponse;
            AuthenticationResponse authentication = responses[request[2].TransactionID].Response as AuthenticationResponse;

            //Verificar se recebemos as respostas certas!
            version.NotNull <VersionResponse, MessengerProtocolException>("Unexpected response");
            machine.NotNull <MachineInfoResponse, MessengerProtocolException>("Unexpected response");

            if (authentication == null)
            {
                //Vamos ver se fomos redirecionados para outro servidor!
                DispatchServerRedirectionResponse redirection = responses[request[2].TransactionID].Response as DispatchServerRedirectionResponse;
                redirection.NotNull <DispatchServerRedirectionResponse, MessengerProtocolException>("Unexpected response");
                //Caso tenhamos sido redireccionados novamente!
                //Vamos tentar autenticação ai!
                if (attemptsP >= 0)
                {
                    this.Authenticate(redirection.Host, attemptsP - 1);
                }
                return;
            }

            //Verificar se é uma versão suportada do protocolo!
            version.Protocols.MustContainElement <string, MessengerProtocolException>("MSNP15", "Unsupported  Protocol");

            //Verificar se o nosso cliente ainda é aceitável!
            if ((request[1] as MachineInfoRequest).ClientVersion < machine.MinimumSafeVersion)
            {
                //Ignore! We are not a client! We are just the libraries!
                //throw new Exception("Unsupported  client");
            }

            #endregion

            //Como chegamos até aqui
            //Temos confiança que podemos prosseguir com a autenticação!

            #region Request Credentials

            //Vamos obter as nossas credenciais!
            //E neste caso vamos obter credencias para todos os serviços!
            PassportRequest pr = new PassportRequest(this.Account, this.Password, authentication.Policy);

            this.Credentials = pr.RequestCredentials();

            #endregion

            //Agora que temos as credenciais!
            //Vamos POR FIM tentar o 'passo final' de autenticação

            #region Validade Credentials

            const string authDomain = "messengerclear.live.com";
            if (!this.Credentials.ContainsKey(authDomain))
            {
                throw new MessengerAuthenticationException("Could not retrieve Credentials");
            }

            string securityToken = this.Credentials[authDomain].SecurityToken;
            string binarySecret  = this.Credentials[authDomain].BinarySecret;

            securityToken.NotEmpty <MessengerAuthenticationException>("Could not retrieve Credentials");
            binarySecret.NotEmpty <MessengerAuthenticationException>("Could not retrieve Credentials");

            string finalTicket = MessengerSocket.CalculateMBIResponse(binarySecret, authentication.Nonce);

            #endregion

            #region Final Request

            request = new PackageRequest[]
            {
                new AuthenticationRequest(true, string.Format("{0} {1}", securityToken, finalTicket))
            };

            //Get response from the server!
            responses = this.socket.SendRangeAndBlock(request);

            #endregion

            #region Final Parse Responses If Any

            //Agora que temos as respostas vamos verificar se estão correctas!
            //Seja em quantidade, seja em 'qualidade'...

            if (responses.Count != 1)
            {
                throw new MessengerException("Unable to receive response from the server");
            }

            //Responses!
            AuthenticationAchievedResponse authenticatedGranted = responses[request[0].TransactionID].Response as AuthenticationAchievedResponse;

            authenticatedGranted.NotNull <AuthenticationAchievedResponse, MessengerProtocolException>("Unexpected response");

            #endregion

            //Hacker lingo!
            //We are in!
        }