Exemple #1
0
        public void Login(WiFiTicket ticket)    // HACK to async class
        {
            var content = new FormUrlEncodedContent(new Dictionary <string, string> {
                { "myusername", ticket.username },
                { "mypassword", ticket.password }
            });

            try {
                _handler.CookieContainer.Add(new Uri(BASE_URL), _session_cookie);
                var r = _client.PostAsync(LOGIN_URL, content).Result;

                var result = r.Content.ReadAsStringAsync().Result;

                //Error handlings
                var unknown_error = true;   // HACK
                if (result.Contains("Can%20not%20read%20data%20from%20Cookie"))
                {
                    // TODO throw exception
                    Console.WriteLine("Can not read data from Cookie");     // HACK
                    unknown_error = false;
                }
                if (r.RequestMessage.RequestUri.AbsoluteUri.Contains("Invalid%20username%20or%20password"))
                {
                    unknown_error = false;
                    throw new LoginFailedException("Invalid username or password");
                }
                if (r.RequestMessage.RequestUri.AbsoluteUri.Contains("Your%20have%20run%20out%20of%20your%20qouta"))
                {
                    unknown_error = false;
                    throw new LoginFailedException("Your have run out of your qouta");
                }
                if (r.RequestMessage.RequestUri.AbsoluteUri.Contains("The%20account%20is%20expired"))
                {
                    unknown_error = false;
                    throw new LoginFailedException("The account is expired");
                }
                if (r.RequestMessage.RequestUri.AbsoluteUri.Contains("loginpages/popup1.shtml"))
                {
                    //when success page, reset flag
                    unknown_error = false;
                }

                // TODO "Deny login because you have loggedin the system via the other machine."

                if (unknown_error)
                {
                    throw new LoginFailedException(r.RequestMessage.RequestUri.AbsoluteUri + result);
                }

                // save the URL for later uses.
                _usage_check_url = BASE_URL + "loginpages" + result.SubStringByToken("/loginpages", "\"");
            } catch (LoginFailedException) {
                throw;
            } catch (Exception e) {
                Console.WriteLine(e.InnerException.ToString());
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                System.Console.WriteLine(HELP_MSG);
                Environment.Exit(1);
            }
            var hNZauth = new HospitalityNZauth();

            if (hNZauth.CheckConnected())
            {
                switch (args[0])
                {
                case "login":
                    if (args.Length != 3)
                    {
                        Console.WriteLine("Please specify username and password.\n\nExample: login 1234@h passw0rd");
                        Environment.Exit(1);
                    }

                    // Check Logged in or not
                    if (hNZauth.CheckLoggedIn())
                    {
                        Console.WriteLine("You are already logged in.");
                        hNZauth.LoadState(hNZauth.TEMPDIR_PATH + STATE_FILENAME);
                        PrintUsage(hNZauth.CheckUsage());
                    }
                    else
                    {
                        // Try login
                        var ticket = new WiFiTicket(args[1], args[2]);
                        try {
                            hNZauth.Login(ticket);
                        } catch (LoginFailedException e) {
                            Console.WriteLine(e.Message);
                        }

                        hNZauth.SaveState(hNZauth.TEMPDIR_PATH + STATE_FILENAME);
                        PrintUsage(hNZauth.CheckUsage());
                    }
                    break;

                case "logout":
                    if (hNZauth.CheckLoggedIn())
                    {
                        // Try logout
                        hNZauth.Logout();
                        Console.WriteLine("Logged out.");
                    }
                    else
                    {
                        Console.WriteLine("You are not logged in.");
                    }
                    break;

                case "stat":
                case "status":
                    if (hNZauth.CheckLoggedIn())
                    {
                        hNZauth.LoadState(hNZauth.TEMPDIR_PATH + STATE_FILENAME);
                        PrintUsage(hNZauth.CheckUsage());
                    }
                    else
                    {
                        Console.WriteLine("You are not logged in.");
                    }
                    break;

                default:
                    Console.WriteLine(args[0] + ": No such sub-command");
                    break;
                }
            }
            else
            {
                Console.WriteLine("Not connected valid access point. Please check connection.");
            }
        }