Esempio n. 1
0
    private void OnUsrResponse(MessageRouter router, Message message, object tag)
    {
        router.RemoveMessageEvent(message);
        OperationCompleteEvent op = (OperationCompleteEvent)tag;

        // see if its an XFR or a USR code.
        switch (message.Code)
        {
        case "XFR":                 // we need to connect to a notification server
        {
            // get ip and port of notification server from response string
            Regex.Regex regex = new Regex.Regex(
                @"NS (?<notifyServerIP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(?<notifyServerPort>\d{1,5})" +
                @"\s+\d+\s+" +
                @"(?<currentNotifyIP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(?<currentNotifyPort>\d{1,5})");
            Regex.Match match = regex.Match(message.Arguments);

            if (!match.Success)
            {
                op.Execute(new ConnectionCompleteArgs(ConnectionFailedReport.InvalidProtocol));
            }
            else
            {
                string notifyIP   = match.Groups["notifyServerIP"].Value;
                int    notifyPort = int.Parse(match.Groups["notifyServerPort"].Value);

                // connect to notification server
                if (this.notifyRouter != null)
                {
                    this.notifyRouter.Close();
                }

                try
                {
                    Proxy.IConnection con = control.CreateConnection();
                    con.Connect("", 0, notifyIP, notifyPort, Proxy.ConnectionType.Tcp);
                    this.notifyRouter = new MessageRouter(this, con,
                                                          new ResponseReceivedHandler(OnDefaultNotifyMessage),
                                                          new ResponseReceivedHandler(OnForcedDisconnect));
                }
                catch
                {
                    op.Execute(new ConnectionCompleteArgs(ConnectionFailedReport.ConnectionFailed));
                    return;
                }

                RegisterEvents();

                // go through authentication again, this time with the new notification server
                DoAuthentication(this.notifyRouter, (OperationCompleteEvent)tag);
            }
        }
        break;

        case "USR":                 // we need to do encrypt and send password
        {
            string challenge = message.Arguments.Substring(6);

            // we need to authenticate with passport server
            string url = this.settings.PassportURL;
            string ticket;

            if (url != string.Empty || url != null || url != "")
            {
                try
                {
                    ticket = Passport.Login(url, challenge, settings.Username, settings.Password);
                }
                catch
                {
                    // passport url is out of date
                    url    = Passport.GetLoginAddress(this.nexusUri);
                    ticket = Passport.Login(url, challenge, settings.Username, settings.Password);
                }
            }
            else
            {
                url    = Passport.GetLoginAddress(this.nexusUri);
                ticket = Passport.Login(url, challenge, settings.Username, settings.Password);
            }

            if (ticket == null || ticket == string.Empty || ticket == "")
            {
                // wrong username / password
                op.Execute(new ConnectionCompleteArgs(ConnectionFailedReport.AuthFailed));
                return;
            }

            // send ticket command
            router.SendMessage(Message.ConstructMessage("USR", "TWN S " + ticket), new ResponseReceivedHandler(OnPasswordSentResponse), tag);
        }
        break;
        }
    }