コード例 #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
ファイル: Mixer.cs プロジェクト: r3db/Messenger
        public Mixer(IPEndPoint endpoint, string account, int sessionID, int timeout)
        {
            endpoint.NotNull();
            account.NotEmpty();

            this.socket = new MessengerSocket(endpoint, timeout);

            this.Account   = account;
            this.SessionID = sessionID;
            this.Timeout   = timeout;
            this.socket.Reader.MessageReceived += MessageReceived;
        }
コード例 #3
0
 public void Authenticate(IPEndPoint host)
 {
     try
     {
         this.Authenticate(host, this.Attempts);
     }
     catch
     {
         this.socket      = null;
         this.Credentials = null;
         throw;
     }
     this.socket.Reader.MessageReceived += this.EventParser;
 }
コード例 #4
0
 public void Authenticate()
 {
     try
     {
         this.Authenticate(this.Attempts);
         this.Authenticated = true;
     }
     catch
     {
         this.socket        = null;
         this.Credentials   = null;
         this.Authenticated = false;
         throw;
     }
 }
コード例 #5
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!
        }