示例#1
0
        private bool GetAuthKey()
        {
            try
            {
                StringBuilder sb      = new StringBuilder();
                string        payload = "user="******",password="******"payload=" + payload;
                if (useGarena)
                {
                    payload = reToken(garenaToken);
                    query   = "payload=8393%20" + payload;
                }

                WebRequest con = WebRequest.Create(loginQueue + "login-queue/rest/queue/authenticate");
                con.Method = "POST";

                Stream outputStream = con.GetRequestStream();
                outputStream.Write(Encoding.ASCII.GetBytes(query), 0, Encoding.ASCII.GetByteCount(query));

                WebResponse webresponse = con.GetResponse();
                Stream      inputStream = webresponse.GetResponseStream();

                int c;
                while ((c = inputStream.ReadByte()) != -1)
                {
                    sb.Append((char)c);
                }

                TypedObject result = serializer.Deserialize <TypedObject>(sb.ToString());
                outputStream.Close();
                inputStream.Close();
                con.Abort();

                if (!result.ContainsKey("token"))
                {
                    int    node  = (int)result.GetInt("node");
                    string champ = result.GetString("champ");
                    int    rate  = (int)result.GetInt("rate");
                    int    delay = (int)result.GetInt("delay");

                    int id  = 0;
                    int cur = 0;

                    object[] tickers = result.GetArray("tickers");
                    foreach (object o in tickers)
                    {
                        Dictionary <string, object> to = (Dictionary <string, object>)o;

                        int tnode = (int)to["node"];
                        if (tnode != node)
                        {
                            continue;
                        }

                        id  = (int)to["id"];
                        cur = (int)to["current"];
                        break;
                    }

                    while (id - cur > rate)
                    {
                        sb.Clear();
                        if (OnLoginQueueUpdate != null)
                        {
                            OnLoginQueueUpdate(this, id - cur);
                        }
                        con         = WebRequest.Create(loginQueue + "login-queue/rest/queue/ticker/" + champ);
                        con.Method  = "GET";
                        webresponse = con.GetResponse();
                        inputStream = webresponse.GetResponseStream();

                        int d;
                        while ((d = inputStream.ReadByte()) != -1)
                        {
                            sb.Append((char)d);
                        }

                        result = serializer.Deserialize <TypedObject>(sb.ToString());

                        inputStream.Close();
                        con.Abort();

                        if (result == null)
                        {
                            continue;
                        }

                        cur = HexToInt(result.GetString(node.ToString()));
                    }

                    while (sb.ToString() == null || !result.ContainsKey("token"))
                    {
                        sb.Clear();
                        if (id - cur < 0)
                        {
                            if (OnLoginQueueUpdate != null)
                            {
                                OnLoginQueueUpdate(this, 0);
                            }
                            else if (OnLoginQueueUpdate != null)
                            {
                                OnLoginQueueUpdate(this, id - cur);
                            }
                        }

                        con        = WebRequest.Create(loginQueue + "login-queue/rest/queue/authToken/" + user.ToLower());
                        con.Method = "GET";
                        try
                        {
                            using (WebResponse nWebresponse = con.GetResponse())
                            {
                                using (Stream nInputStream = nWebresponse.GetResponseStream())
                                {
                                    int f;
                                    while ((f = nInputStream.ReadByte()) != -1)
                                    {
                                        sb.Append((char)f);
                                    }

                                    result = serializer.Deserialize <TypedObject>(sb.ToString());
                                }
                            }
                        }
                        catch (WebException e)
                        {
                            if (e.Status == WebExceptionStatus.ProtocolError && e.Response != null)
                            {
                                var response = (HttpWebResponse)e.Response;
                                if (response.StatusCode == HttpStatusCode.NotFound)
                                {
                                    Thread.Sleep(50);
                                    continue;
                                }
                                else
                                {
                                    Debug.WriteLine(e.Message);
                                }
                            }
                        }
                        inputStream.Close();
                        con.Abort();
                    }
                }
                if (OnLoginQueueUpdate != null)
                {
                    OnLoginQueueUpdate(this, 0);
                }
                authToken = result.GetString("token");
                if (useGarena)
                {
                    userID = result.GetString("token");
                }

                return(true);
            }
            catch (Exception e)
            {
                if (e.Message == "The remote name could not be resolved: '" + loginQueue + "'")
                {
                    Error("Please make sure you are connected the internet!", ErrorType.AuthKey);
                    Disconnect();
                }
                else if (e.Message == "The remote server returned an error: (403) Forbidden.")
                {
                    Error("Your username or password is incorrect!", ErrorType.Password);
                    Disconnect();
                }
                else if (e.Message == "The given key was not present in the dictionary.")
                {
                    Error("The given key was not present in the dictionary. Client version is wrong maybe?", ErrorType.AuthKey);
                    Disconnect();
                }
                else
                {
                    Error("Unable to get Auth Key \n" + e, ErrorType.AuthKey);
                    Disconnect();
                }

                return(false);
            }
        }
        private bool GetAuthKey()
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                string payload = "user="******",password="******"payload=" + payload;

                if (useGarena)
                    payload = garenaToken;

                WebRequest con = WebRequest.Create(loginQueue + "login-queue/rest/queue/authenticate");
                con.Method = "POST";

                Stream outputStream = con.GetRequestStream();
                outputStream.Write(Encoding.ASCII.GetBytes(query), 0, Encoding.ASCII.GetByteCount(query));

                WebResponse webresponse = con.GetResponse();
                Stream inputStream = webresponse.GetResponseStream();

                int c;
                while ((c = inputStream.ReadByte()) != -1)
                    sb.Append((char)c);

                TypedObject result = serializer.Deserialize<TypedObject>(sb.ToString());
                outputStream.Close();
                inputStream.Close();
                con.Abort();

                if (!result.ContainsKey("token"))
                {
                    int node = (int)result.GetInt("node");
                    string champ = result.GetString("champ");
                    int rate = (int)result.GetInt("rate");
                    int delay = (int)result.GetInt("delay");

                    int id = 0;
                    int cur = 0;

                    object[] tickers = result.GetArray("tickers");
                    foreach (object o in tickers)
                    {
                        Dictionary<string, object> to = (Dictionary<string, object>)o;

                        int tnode = (int)to["node"];
                        if (tnode != node)
                            continue;

                        id = (int)to["id"];
                        cur = (int)to["current"];
                        break;
                    }

                    while (id - cur > rate)
                    {
                        sb.Clear();
                        if (OnLoginQueueUpdate != null)
                            OnLoginQueueUpdate(this, id - cur);

                        Thread.Sleep(delay);
                        con = WebRequest.Create(loginQueue + "login-queue/rest/queue/ticker/" + champ);
                        con.Method = "GET";
                        webresponse = con.GetResponse();
                        inputStream = webresponse.GetResponseStream();

                        int d;
                        while ((d = inputStream.ReadByte()) != -1)
                            sb.Append((char)d);

                        result = serializer.Deserialize<TypedObject>(sb.ToString());

                        inputStream.Close();
                        con.Abort();

                        if (result == null)
                            continue;

                        cur = HexToInt(result.GetString(node.ToString()));
                    }

                    while (sb.ToString() == null || !result.ContainsKey("token"))
                    {
                        try
                        {
                            sb.Clear();

                            if (id - cur < 0)
                                if (OnLoginQueueUpdate != null)
                                    OnLoginQueueUpdate(this, 0);
                                else if (OnLoginQueueUpdate != null)
                                    OnLoginQueueUpdate(this, id - cur);

                            Thread.Sleep(delay / 10);
                            con = WebRequest.Create(loginQueue + "login-queue/rest/queue/authToken/" + user.ToLower());
                            con.Method = "GET";
                            webresponse = con.GetResponse();
                            inputStream = webresponse.GetResponseStream();

                            int f;
                            while ((f = inputStream.ReadByte()) != -1)
                                sb.Append((char)f);

                            result = serializer.Deserialize<TypedObject>(sb.ToString());

                            inputStream.Close();
                            con.Abort();
                        }
                        catch
                        {
                        }
                    }
                }
                if (OnLoginQueueUpdate != null)
                    OnLoginQueueUpdate(this, 0);
                authToken = result.GetString("token");

                return true;
            }
            catch (Exception e)
            {
                if (e.Message == "The remote name could not be resolved: '" + loginQueue + "'")
                {
                    Error("Please make sure you are connected the internet!", ErrorType.AuthKey);
                    Disconnect();
                }
                else if (e.Message == "The remote server returned an error: (403) Forbidden.")
                {
                    Error("Your username or password is incorrect!", ErrorType.Password);
                    Disconnect();
                }
                else
                {
                    Error("Unable to get Auth Key", ErrorType.AuthKey);
                    Disconnect();
                }

                return false;
            }
        }